此時GHCJS無法編譯postgresql-simple
包(請參閱[1])。我想用persistent
包生成數據庫模型。我想知道是否可以使用GHC自己編譯模型,並重新使用由GHCJS源代碼中的模板Haskell生成的代碼?GHCJS可以重用由模板生成的代碼Haskell
我對我的問題已經有了解決方法,但問題仍然是相關的。我將把它打開幾天,如果沒有人會回答如何使用模板Haskell生成的代碼,我會關閉它。我在底部粘貼了結果代碼。
UPDATE:
thomie建議我-dth-dec-file
標誌,它可以寫成模型文件語言編譯,例如{-# OPTIONS_GHC -dth-dec-file #-}
。然後在運行stack build
命令後,在.stack-work/dist/<arch>/<cabal-version>/build/src
文件夾下有一個文件Model.th.hs
。這個文件看起來像有效的Haskell,但是由於解析錯誤,GHC拒絕它(見底部的代碼)。但是,我發現了一種使用GHCJS編譯模型的方法。我在小集團文件添加條件,從依賴除去postgresql-simple
:通過模板哈斯克爾產生
-- project.cabal
library
-- ...
if impl(ghcjs)
build-depends: persistent
, persistent-template
else
build-depends: persistent
, persistent-postgresql
, persistent-template
, postgresql-simple
代碼(測試此代碼我複製項目源文件夾此文件,並添加在頂層模塊聲明)
-- src/Model.hs:(16,1)-(17,54): Splicing declarations
instance Database.Persist.Class.PersistField.PersistField Manufacturer where
Database.Persist.Class.PersistField.toPersistValue
= \ ent_a9ov
-> (Database.Persist.Types.Base.PersistMap
GHC.Base.$
(GHC.List.zip
(GHC.Base.map Data.Text.pack ["name"])
((GHC.Base.map Database.Persist.Class.PersistField.toPersistValue)
GHC.Base.$
(Database.Persist.Class.PersistEntity.toPersistFields ent_a9ov))))
Database.Persist.Class.PersistField.fromPersistValue
= ((\ x_a9ow
-> let columns_a9ox = Data.HashMap.Strict.fromList x_a9ow
in
(Database.Persist.Class.PersistEntity.fromPersistValues
GHC.Base.$
((GHC.Base.map
(\ name_a9oy
-> case
Data.HashMap.Base.lookup (Data.Text.pack name_a9oy) columns_a9ox
of {
GHC.Base.Just v_a9oz -> v_a9oz
GHC.Base.Nothing -> Database.Persist.Types.Base.PersistNull }))
GHC.Base.$ ["name"])))
Control.Monad.<=<
Database.Persist.Class.PersistField.getPersistMap)
instance Database.Persist.Sql.Class.PersistFieldSql Manufacturer where
Database.Persist.Sql.Class.sqlType _
= Database.Persist.Types.Base.SqlString
data Manufacturer
= Manufacturer {manufacturerName :: !Text}
deriving (Show, Read, Typeable)
type ManufacturerId =
Database.Persist.Class.PersistEntity.Key Manufacturer
instance Database.Persist.Class.PersistEntity.PersistEntity Manufacturer where
type Database.Persist.Class.PersistEntity.PersistEntityBackend Manufacturer = Database.Persist.Sql.Types.SqlBackend
data Database.Persist.Class.PersistEntity.Unique Manufacturer
= UniqueManufacturer Text
newtype Database.Persist.Class.PersistEntity.Key Manufacturer
= ManufacturerKey {unManufacturerKey :: Database.Persist.Class.PersistStore.BackendKey Database.Persist.Sql.Types.SqlBackend}
deriving (GHC.Show.Show,
GHC.Read.Read,
GHC.Classes.Eq,
GHC.Classes.Ord,
Web.PathPieces.PathPiece,
Web.HttpApiData.Internal.ToHttpApiData,
Web.HttpApiData.Internal.FromHttpApiData,
Database.Persist.Class.PersistField.PersistField,
Database.Persist.Sql.Class.PersistFieldSql,
Data.Aeson.Types.Class.ToJSON,
Data.Aeson.Types.Class.FromJSON)
data Database.Persist.Class.PersistEntity.EntityField Manufacturer typ
= typ ~ Database.Persist.Class.PersistEntity.Key Manufacturer =>
ManufacturerId |
typ ~ Text => ManufacturerName
Database.Persist.Class.PersistEntity.keyToValues
= ((GHC.Types.: [])
GHC.Base..
(Database.Persist.Class.PersistField.toPersistValue
GHC.Base.. unManufacturerKey))
Database.Persist.Class.PersistEntity.keyFromValues
= ((GHC.Base.fmap ManufacturerKey)
GHC.Base..
(Database.Persist.Class.PersistField.fromPersistValue
GHC.Base.. Database.Persist.TH.headNote))
Database.Persist.Class.PersistEntity.entityDef _
= Database.Persist.Types.Base.EntityDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "Manufacturer"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "manufacturer"))
(Database.Persist.Types.Base.FieldDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "Id"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "id"))
(Database.Persist.Types.Base.FTTypeCon
GHC.Base.Nothing (Database.Persist.TH.packPTH "ManufacturerId"))
Database.Persist.Types.Base.SqlInt64
[]
GHC.Types.True
(Database.Persist.Types.Base.ForeignRef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "Manufacturer"))
(Database.Persist.Types.Base.FTTypeCon
(GHC.Base.Just (Database.Persist.TH.packPTH "Data.Int"))
(Database.Persist.TH.packPTH "Int64"))))
[Database.Persist.TH.packPTH "json"]
[Database.Persist.Types.Base.FieldDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "name"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "name"))
(Database.Persist.Types.Base.FTTypeCon
GHC.Base.Nothing (Database.Persist.TH.packPTH "Text"))
Database.Persist.Types.Base.SqlString
[]
GHC.Types.True
Database.Persist.Types.Base.NoReference]
[Database.Persist.Types.Base.UniqueDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "UniqueManufacturer"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "unique_manufacturer"))
[(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "name"),
Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "name"))]
[]]
[]
[Database.Persist.TH.packPTH "Show",
Database.Persist.TH.packPTH "Read",
Database.Persist.TH.packPTH "Typeable"]
(Data.Map.Base.fromList [])
GHC.Types.False
Database.Persist.Class.PersistEntity.toPersistFields
(Manufacturer x_a9oA)
= [Database.Persist.Class.PersistField.SomePersistField x_a9oA]
Database.Persist.Class.PersistEntity.fromPersistValues [x1_a9oC]
= Manufacturer
Data.Functor.<$>
((Database.Persist.TH.mapLeft
(Database.Persist.TH.fieldError
(Database.Persist.TH.packPTH "name")))
GHC.Base.. Database.Persist.Class.PersistField.fromPersistValue)
x1_a9oC
Database.Persist.Class.PersistEntity.fromPersistValues x_a9oB
= (Data.Either.Left
GHC.Base.$
(GHC.Base.mappend
(Database.Persist.TH.packPTH
"Manufacturer: fromPersistValues failed on: ")
(Data.Text.pack GHC.Base.$ (GHC.Show.show x_a9oB))))
Database.Persist.Class.PersistEntity.persistUniqueToFieldNames
(UniqueManufacturer {})
= [(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "name"),
Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "name"))]
Database.Persist.Class.PersistEntity.persistUniqueToValues
(UniqueManufacturer x_a9oD)
= [Database.Persist.Class.PersistField.toPersistValue x_a9oD]
Database.Persist.Class.PersistEntity.persistUniqueKeys
(Manufacturer _name_a9oE)
= [UniqueManufacturer _name_a9oE]
Database.Persist.Class.PersistEntity.persistFieldDef ManufacturerId
= Database.Persist.Types.Base.FieldDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "Id"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "id"))
(Database.Persist.Types.Base.FTTypeCon
GHC.Base.Nothing (Database.Persist.TH.packPTH "ManufacturerId"))
Database.Persist.Types.Base.SqlInt64
[]
GHC.Types.True
(Database.Persist.Types.Base.ForeignRef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "Manufacturer"))
(Database.Persist.Types.Base.FTTypeCon
(GHC.Base.Just (Database.Persist.TH.packPTH "Data.Int"))
(Database.Persist.TH.packPTH "Int64")))
Database.Persist.Class.PersistEntity.persistFieldDef
ManufacturerName
= Database.Persist.Types.Base.FieldDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "name"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "name"))
(Database.Persist.Types.Base.FTTypeCon
GHC.Base.Nothing (Database.Persist.TH.packPTH "Text"))
Database.Persist.Types.Base.SqlString
[]
GHC.Types.True
Database.Persist.Types.Base.NoReference
Database.Persist.Class.PersistEntity.persistIdField
= ManufacturerId
Database.Persist.Class.PersistEntity.fieldLens ManufacturerId
= Database.Persist.TH.lensPTH
Database.Persist.Class.PersistEntity.entityKey
(\ (Database.Persist.Class.PersistEntity.Entity _ value_a9oF)
key_a9oG
-> Database.Persist.Class.PersistEntity.Entity key_a9oG value_a9oF)
Database.Persist.Class.PersistEntity.fieldLens ManufacturerName
= Database.Persist.TH.lensPTH
(manufacturerName
GHC.Base.. Database.Persist.Class.PersistEntity.entityVal)
(\ (Database.Persist.Class.PersistEntity.Entity key_a9oH
value_a9oI)
x_a9oJ
-> Database.Persist.Class.PersistEntity.Entity
key_a9oH (value_a9oI {manufacturerName = x_a9oJ}))
instance Database.Persist.Class.PersistStore.ToBackendKey Database.Persist.Sql.Types.SqlBackend Manufacturer where
Database.Persist.Class.PersistStore.toBackendKey
= unManufacturerKey
Database.Persist.Class.PersistStore.fromBackendKey
= ManufacturerKey
instance Data.Aeson.Types.Class.ToJSON Manufacturer where
Data.Aeson.Types.Class.toJSON (Manufacturer name_a9oL)
= Data.Aeson.Types.Internal.object
[((Data.Text.pack "name") Data.Aeson.Types.Instances..= name_a9oL)]
instance Data.Aeson.Types.Class.FromJSON Manufacturer where
Data.Aeson.Types.Class.parseJSON
(Data.Aeson.Types.Internal.Object obj_a9oK)
= ((GHC.Base.pure Manufacturer)
GHC.Base.<*>
(obj_a9oK Data.Aeson.Types.Instances..: (Data.Text.pack "name")))
Data.Aeson.Types.Class.parseJSON _ = GHC.Base.mzero
instance Data.Aeson.Types.Class.ToJSON (Database.Persist.Class.PersistEntity.Entity Manufacturer) where
Data.Aeson.Types.Class.toJSON
= Database.Persist.Class.PersistEntity.entityIdToJSON
instance Data.Aeson.Types.Class.FromJSON (Database.Persist.Class.PersistEntity.Entity Manufacturer) where
Data.Aeson.Types.Class.parseJSON
= Database.Persist.Class.PersistEntity.entityIdFromJSON
migrateAll :: Database.Persist.Sql.Types.Migration
migrateAll
= do { let defs_a9oM
= [Database.Persist.Types.Base.EntityDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "Manufacturer"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "manufacturer"))
(Database.Persist.Types.Base.FieldDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "Id"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "id"))
(Database.Persist.Types.Base.FTTypeCon
GHC.Base.Nothing (Database.Persist.TH.packPTH "ManufacturerId"))
Database.Persist.Types.Base.SqlInt64
[]
GHC.Types.True
(Database.Persist.Types.Base.ForeignRef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "Manufacturer"))
(Database.Persist.Types.Base.FTTypeCon
(GHC.Base.Just (Database.Persist.TH.packPTH "Data.Int"))
(Database.Persist.TH.packPTH "Int64"))))
[Database.Persist.TH.packPTH "json"]
[Database.Persist.Types.Base.FieldDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "name"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "name"))
(Database.Persist.Types.Base.FTTypeCon
GHC.Base.Nothing (Database.Persist.TH.packPTH "Text"))
Database.Persist.Types.Base.SqlString
[]
GHC.Types.True
Database.Persist.Types.Base.NoReference]
[Database.Persist.Types.Base.UniqueDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "UniqueManufacturer"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "unique_manufacturer"))
[(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "name"),
Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "name"))]
[]]
[]
[Database.Persist.TH.packPTH "Show",
Database.Persist.TH.packPTH "Read",
Database.Persist.TH.packPTH "Typeable"]
(Data.Map.Base.fromList [])
GHC.Types.False];
Database.Persist.Sql.Migration.migrate
defs_a9oM
(Database.Persist.Types.Base.EntityDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "Manufacturer"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "manufacturer"))
(Database.Persist.Types.Base.FieldDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "Id"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "id"))
(Database.Persist.Types.Base.FTTypeCon
GHC.Base.Nothing (Database.Persist.TH.packPTH "ManufacturerId"))
Database.Persist.Types.Base.SqlInt64
[]
GHC.Types.True
(Database.Persist.Types.Base.ForeignRef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "Manufacturer"))
(Database.Persist.Types.Base.FTTypeCon
(GHC.Base.Just (Database.Persist.TH.packPTH "Data.Int"))
(Database.Persist.TH.packPTH "Int64"))))
[Database.Persist.TH.packPTH "json"]
[Database.Persist.Types.Base.FieldDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "name"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "name"))
(Database.Persist.Types.Base.FTTypeCon
GHC.Base.Nothing (Database.Persist.TH.packPTH "Text"))
Database.Persist.Types.Base.SqlString
[]
GHC.Types.True
Database.Persist.Types.Base.NoReference]
[Database.Persist.Types.Base.UniqueDef
(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "UniqueManufacturer"))
(Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "unique_manufacturer"))
[(Database.Persist.Types.Base.HaskellName
(Database.Persist.TH.packPTH "name"),
Database.Persist.Types.Base.DBName
(Database.Persist.TH.packPTH "name"))]
[]]
[]
[Database.Persist.TH.packPTH "Show",
Database.Persist.TH.packPTH "Read",
Database.Persist.TH.packPTH "Typeable"]
(Data.Map.Base.fromList [])
GHC.Types.False) }
錯誤消息報告解析錯誤的->
上線開始GHC.Base.Nothing ->
(\ name_a9oy
-> case
Data.HashMap.Base.lookup (Data.Text.pack name_a9oy) columns_a9ox
of {
GHC.Base.Just v_a9oz -> v_a9oz
GHC.Base.Nothing -> Database.Persist.Types.Base.PersistNull }))
Hi @Martin Vik!你的意思是建立模塊與整個庫分開嗎? –
是的,如果我避免了客戶端代碼的'postgresql-simple'依賴性,你的方法就可以工作。 –