groundhog-0.0.1: Type-safe, relational, multi-backend persistence.

Database.Groundhog.TH

Description

This module provides functions to generate the auxiliary structures for the user data type

Synopsis

Documentation

deriveEntity :: Name -> Maybe (State THEntityDef ()) -> Q [Dec]Source

Creates the auxiliary structures for a user datatype, which are required by Groundhog to manipulate it.

It creates GADT Fields data instance for referring to the fields in expressions and phantom types for data constructors. For record constructors the Field name is the regular field name with first letter capitalized and postpended "Field". If the field is an ordinary constructor, its name is constructor name and postponed field name. The constructor phantom datatypes have the same name as constructors with "Constructor" postpended.

The generation can be adjusted using the optional modifier function. Example:

 data SomeData a = Normal Int | Record { bar :: Maybe String, asc :: a}
 deriveEntity ''SomeData $ Just $ do
   setDbEntityName "SomeTableName"
   setConstructor 'Normal $ do
     setPhantomName "NormalConstructor" -- the same as default

It will generate these new datatypes and required instances.

 data NormalConstructor
 data RecordConstructor
 instance PersistEntity where
   data Fields (SomeData a) where
     Normal0Field :: Fields NormalConstructor Int
     BarField :: Fields RecordConstructor (Maybe String)
     AscField :: Fields RecordConstructor a
 ...

setDbEntityName :: String -> State THEntityDef ()Source

Set name of the table in the datatype

setConstructor :: Name -> State THConstructorDef () -> State THEntityDef ()Source

Modify constructor

setPhantomName :: String -> State THConstructorDef ()Source

Set name used to parametrise fields

setDbConstrName :: String -> State THConstructorDef ()Source

Set name of the constructor specific table

setConstraints :: [Constraint] -> State THConstructorDef ()Source

Set constraints of the constructor. The names should be database names of the fields

setField :: String -> State FieldDef () -> State THConstructorDef ()Source

Modify field. Field name is a regular field name in record constructor. Otherwise, it is lower-case constructor name with field number.

setDbFieldName :: String -> State FieldDef ()Source

Set name of the field column in a database

setExprFieldName :: String -> State FieldDef ()Source

Set name of field constructor used in expressions