Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createTables function #115

Open
vrom911 opened this issue Mar 22, 2019 · 1 comment
Open

createTables function #115

vrom911 opened this issue Mar 22, 2019 · 1 comment

Comments

@vrom911
Copy link

vrom911 commented Mar 22, 2019

At the moment you need to write the "set-up" function by hands. Though, it mostly duplicates the type definition and translates types into the corresponding functions.
What would be great is to have the function createTables (or any other name) which would do this for you:

createTables :: forall schema . Definition '[] schema

Example

Let's say we have this schema:

type Schema =
    '[ "test_users" ::: 'Table
        ( '[ "pk_users" ::: 'PrimaryKey '["id"] ]
      :=> '[ "id"   :::   'Def :=> 'NotNull 'PGint4
           , "name" ::: 'NoDef :=> 'NotNull 'PGtext
           ]
        )
    ]

to setup it now I would need to do the following:

setup :: Definition '[] Schema
setup = createTable #test_users
    ( serial `as` #id :* (text & notNullable) `as` #name )
    ( primaryKey #id `as` #pk_users )

If the createTable function would be implemented instead of that you could do just:

setup :: Definition '[] Schema
setup = createTables @Schema

This approach

This is not an uncommon approach. You can see as the example of the servant-client library. client function generates a set of client functions for an API by a given Proxy api:

@echatav
Copy link
Contributor

echatav commented Mar 22, 2019

It is a bit redundant and it would be cool to auto-generate CREATE statements. However, it's not entirely redundant because while the type-level DSL says if a column has a DEFAULT, it doesn't say what that DEFAULT is so at the very least you'd need to specify that at the term-level. Similarly for CHECK constraints. With that in mind, for anyone who wants to try their hand at writing functions that auto-generate CREATE statements, it would be a worthwhile endeavor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants