squeal
squeal copied to clipboard
createTables function
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
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.