Safe Haskell | None |
---|---|
Language | Haskell98 |
A library for creating datatypes and Groundhog mappings from a database schema. The mappings match the database structure
so if you run migration for the generated mappings, no changes to schema should be suggested.
The generated Haskell identifiers may sometimes conflict with each other and with Haskell keywords. If that happens, adjust ReverseNamingStyle
.
Synopsis
- collectTables :: (PersistBackend m, SchemaAnalyzer (Conn m)) => (QualifiedName -> Bool) -> Maybe String -> m (Map QualifiedName TableInfo)
- data ReverseNamingStyle = ReverseNamingStyle {
- mkEntityName :: QualifiedName -> String
- mkConstructorName :: QualifiedName -> String
- mkFieldName :: QualifiedName -> String -> String
- mkKeyFieldName :: QualifiedName -> Reference -> String
- mkChooseReferencedUnique :: QualifiedName -> [UniqueDefInfo] -> UniqueDefInfo
- mkUniqueKeyPhantomName :: QualifiedName -> UniqueDefInfo -> String
- mkUniqueName :: QualifiedName -> Int -> UniqueDefInfo -> String
- defaultReverseNamingStyle :: ReverseNamingStyle
- followReferencedTables :: (PersistBackend m, SchemaAnalyzer (Conn m)) => (QualifiedName -> Bool) -> Map QualifiedName TableInfo -> m (Map QualifiedName TableInfo)
- data DataCodegenConfig = DataCodegenConfig {
- generateUniqueKeysPhantoms :: Bool
- mkType :: Column -> Type
- defaultDataCodegenConfig :: DataCodegenConfig
- generateData :: DataCodegenConfig -> ReverseNamingStyle -> Map QualifiedName TableInfo -> Map QualifiedName (Dec, [Dec])
- showData :: Dec -> String
- defaultMkType :: Column -> Type
- sqliteMkType :: Column -> Type
- generateMapping :: (PersistBackend m, SchemaAnalyzer (Conn m)) => ReverseNamingStyle -> Map QualifiedName TableInfo -> m (Map QualifiedName PSEntityDef)
- minimizeMapping :: NamingStyle -> Dec -> PSEntityDef -> PSEntityDef
- showMappings :: [PSEntityDef] -> ByteString
Mapping essentials
:: (PersistBackend m, SchemaAnalyzer (Conn m)) | |
=> (QualifiedName -> Bool) | Decides if we follow the reference to a table. It can be used to prevent mapping of the referenced audit or system tables |
-> Maybe String | Schema name |
-> m (Map QualifiedName TableInfo) |
Returns tables from a passed schema and tables which they reference.
If you call collectTables several times with different filtering functions,
it is better to call followReferencedTables
afterwards manually to ensure that no dependencies are missing
let filterRefs (schema, tableName) = schema /= "audit" publicTables <- collectTables filterRefs (Just "public") websiteTables <- collectTables filterRefs (Just "website") let allTables = publicTables <> websiteTables
data ReverseNamingStyle Source #
It supplies the names for the haskell datatypes
ReverseNamingStyle | |
|
followReferencedTables Source #
:: (PersistBackend m, SchemaAnalyzer (Conn m)) | |
=> (QualifiedName -> Bool) | Decides if we follow reference to this table. It can be used to prevent mapping of the referenced audit or system tables |
-> Map QualifiedName TableInfo | |
-> m (Map QualifiedName TableInfo) |
It looks for the references to the tables not contained in the passed map.
If there are such references and the reference filter function returns True, the corresponding TableInfo is fetched and included into the map.
The references for the newly added tables are processed in the same way. This function can be useful if your set of tables is created not by collectTables
.
Creating Haskell datatypes
data DataCodegenConfig Source #
Confuguration datatype generation
DataCodegenConfig | |
|
:: DataCodegenConfig | |
-> ReverseNamingStyle | |
-> Map QualifiedName TableInfo | Tables for which the mappings will be generated |
-> Map QualifiedName (Dec, [Dec]) |
Returns declarations for the mapped datatype and auxiliary declarations like unique key phantom datatypes
showData :: Dec -> String Source #
It pretty-prints Template Haskell declaration into compilable Haskell code
defaultMkType :: Column -> Type Source #
sqliteMkType :: Column -> Type Source #
It uses Sqlite type affinity to find the corresponding Haskell type
Creating mapping settings
generateMapping :: (PersistBackend m, SchemaAnalyzer (Conn m)) => ReverseNamingStyle -> Map QualifiedName TableInfo -> m (Map QualifiedName PSEntityDef) Source #
minimizeMapping :: NamingStyle -> Dec -> PSEntityDef -> PSEntityDef Source #
The mappings created by generateMapping
contain a lot of setttings. This function makes the settings more compact by eliminating settings
which are default for the passed NamingStyle
.
showMappings :: [PSEntityDef] -> ByteString Source #
It pretty-prints the mapping settings as JSON. Package groundhog-th accepts JSON and YAML which is a more human-readable superset of JSON. You can use a third-party tool to convert JSON to YAML.