persistent-template-1.3.1: Type-safe, non-relational, multi-backend persistence.

Safe HaskellNone




This module provides utilities for creating backends. Regular users do not need to use this module.


Parse entity defs

persistWith :: PersistSettings -> QuasiQuoterSource

Converts a quasi-quoted syntax into a list of entity definitions, to be used as input to the template haskell generation code (mkPersist).

persistFileWith :: PersistSettings -> FilePath -> Q ExpSource

Same as persistWith, but uses an external file instead of a quasiquotation.

Turn EntityDefs into types

mkPersist :: MkPersistSettings -> [EntityDef SqlType] -> Q [Dec]Source

Create data types and appropriate PersistEntity instances for the given EntityDefs. Works well with the persist quasi-quoter.

data MkPersistSettings Source

Settings to be passed to the mkPersist function.

mpsBackend :: MkPersistSettings -> TypeSource

Which database backend we're using.

When generating data types, each type is given a generic version- which works with any backend- and a type synonym for the commonly used backend. This is where you specify that commonly used backend.

mpsGeneric :: MkPersistSettings -> BoolSource

Create generic types that can be used with multiple backends. Good for reusable code, but makes error messages harder to understand. Default: True.

mpsPrefixFields :: MkPersistSettings -> BoolSource

Prefix field names with the model name. Default: True.

mpsEntityJSON :: MkPersistSettings -> Maybe EntityJSONSource

Generate ToJSON/FromJSON instances for each model types. If it's Nothing, no instances will be generated. Default:

  Just EntityJSON
      { entityToJSON = 'keyValueEntityToJSON
      , entityFromJSON = 'keyValueEntityFromJSON

mpsGenerateLenses :: MkPersistSettings -> BoolSource

Instead of generating normal field accessors, generator lens-style accessors.

Default: False

Since 1.3.1

entityToJSON :: EntityJSON -> NameSource

Name of the toJSON implementation for Entity a.

entityFromJSON :: EntityJSON -> NameSource

Name of the fromJSON implementation for Entity a.



:: Type

Value for mpsBackend

-> MkPersistSettings 

Create an MkPersistSettings with default values.

sqlSettings :: MkPersistSettingsSource

Use the SqlPersist backend.

sqlOnlySettings :: MkPersistSettingsSource

Same as sqlSettings, but set mpsGeneric to False.

Since 1.1.1

Various other TH functions

mkMigrate :: Lift' a => String -> [EntityDef a] -> Q [Dec]Source

Creates a single function to perform all migrations for the entities defined here. One thing to be aware of is dependencies: if you have entities with foreign references, make sure to place those definitions after the entities they reference.

mkSave :: String -> [EntityDef SqlType] -> Q [Dec]Source

Save the EntityDefs passed in under the given name.

mkDeleteCascade :: MkPersistSettings -> [EntityDef a] -> Q [Dec]Source

Generate a DeleteCascade instance for the given EntityDefs.

share :: [[EntityDef a] -> Q [Dec]] -> [EntityDef a] -> Q [Dec]Source

Apply the given list of functions to the same EntityDefs.

This function is useful for cases such as:

>>> share [mkSave "myDefs", mkPersist sqlSettings] [persistLowerCase|...|]

derivePersistField :: String -> Q [Dec]Source

Automatically creates a valid PersistField instance for any datatype that has valid Show and Read instances. Can be very convenient for Enum types.

persistFieldFromEntity :: MkPersistSettings -> EntityDef a -> Q [Dec]Source

produce code similar to the following:

instance PersistEntity e => PersistField e where toPersistValue = PersistMap $ zip columNames (map toPersistValue . toPersistFields) fromPersistValue (PersistMap o) = let columns = HM.fromList x in fromPersistValues $ map (name -> case HM.lookup name o of Just v -> case fromPersistValue v of Left e -> error e Right r -> r Nothing -> error $ Missing field: mappend unpack name) columnNames fromPersistValue x = Left $ Expected PersistMap, received: ++ show x sqlType _ = SqlString


lensPTH :: (s -> a) -> (s -> b -> t) -> Lens s t a bSource