squeal icon indicating copy to clipboard operation
squeal copied to clipboard

createTables function

Open vrom911 opened this issue 5 years ago • 1 comments

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:

  • http://hackage.haskell.org/package/servant-client-0.16/docs/Servant-Client.html#v:client

vrom911 avatar Mar 22 '19 05:03 vrom911

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.

echatav avatar Mar 22 '19 13:03 echatav