postgresql-query-2.3.0: Sql interpolating quasiquote plus some kind of primitive ORM using it

Safe HaskellNone
LanguageHaskell2010

Database.PostgreSQL.Query.TH.Entity

Synopsis

Documentation

data EntityOptions Source

Options for deriving Entity

Constructors

EntityOptions 

Fields

eoTableName :: String -> String

Type name to table name converter

eoColumnNames :: String -> String

Record field to column name converter

eoDeriveClasses :: [Name]

Typeclasses to derive for Id

eoIdType :: Name

Base type for Id

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