Skip to content

createTables function #115

Description

@vrom911

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:

Activity

  1. echatav commented on Mar 22, 2019

    @echatav
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions