一个示例显示它是如何完成的:
#!/usr/bin/env stack
{- stack
--resolver lts-9.0
--install-ghc
runghc
--package persistent
--package persistent-sqlite
--package persistent-template
-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
import Control.Monad.IO.Class (liftIO)
import Database.Persist
import Database.Persist.Sqlite
import Database.Persist.TH
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
name String
age Int Maybe
deriving Show
BlogPost
title String
authorId PersonId
deriving Show
|]
main :: IO ()
main = let efields = entityFields $ entityDef (undefined :: Maybe Person)
in print efields
演示:
$ stack efield.hs
[FieldDef {fieldHaskell = HaskellName {unHaskellName = "name"}, fieldDB = DBName {unDBName = "name"}, fieldType = FTTypeCon Nothing "String", fieldSqlType = SqlString, fieldAttrs = [], fieldStrict = True, fieldReference = NoReference},FieldDef {fieldHaskell = HaskellName {unHaskellName = "age"}, fieldDB = DBName {unDBName = "age"}, fieldType = FTTypeCon Nothing "Int", fieldSqlType = SqlInt64, fieldAttrs = ["Maybe"], fieldStrict = True, fieldReference = NoReference}]