| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Database.PostgreSQL.Query.TH.Entity
- data EntityOptions = EntityOptions {
- eoTableName :: String -> String
- eoColumnNames :: String -> String
- eoDeriveClasses :: [Name]
- eoIdType :: Name
- deriveEntity :: EntityOptions -> Name -> Q [Dec]
Documentation
data EntityOptions Source #
Options for deriving Entity
Constructors
| EntityOptions | |
Fields
| |
Instances
deriveEntity :: EntityOptions -> Name -> Q [Dec] Source #
Derives instance for Entity using type name and field names. Also
generates type synonim for ID. E.g. code like this:
data Agent = Agent
{ aName :: !Text
, aAttributes :: !HStoreMap
, aLongWeirdName :: !Int
} deriving (Ord, Eq, Show)
$(deriveEntity
def { eoIdType = ''Id
, eoTableName = toUnderscore
, eoColumnNames = toUnderscore . drop 1
, eoDeriveClasses =
[''Show, ''Read, ''Ord, ''Eq
, ''FromField, ''ToField, ''PathPiece]
}
''Agent )
Will generate code like this:
instance Database.PostgreSQL.Query.Entity Agent where
newtype EntityId Agent
= AgentId {getAgentId :: Id}
deriving (Show, Read, Ord, Eq, FromField, ToField, PathPiece)
tableName _ = "agent"
fieldNames _ = ["name", "attributes", "long_weird_name"]
type AgentId = EntityId Agent
So, you dont need to write it by hands any more.
NOTE: toUnderscore is from package inflections here