我目前正在从事的一个项目广泛使用了persistent。我不想使用persistent的准引用语法来指定模型,而是使用json。现在,我使用一个脚本来生成persistent期望使用simple-templates的准引用。这在工作流中增加了一个相当尴尬的步骤。使用template-haskell可以避免这种情况吗?
这是当前由脚本生成的:
-- File : ProjSpecific.Models
share [mkPersist sqlSettings, mkMigrate "migrateAll"]
[persistLowerCase|
Person
name String
age Int Maybe
deriving Show
BlogPost
title String
authorId PersonId
deriving Show
|]理想情况下,我希望这样做:
-- File : ProjSpecific.Config
import Data.Aeson.QQ
import Data.Aeson (Value)
models :: Value
models = [aesonQQ| {some json encoding of above models} |]和
-- File : ProjSpecific.Models
complie time logic to generate the persistent models对于如何做到这一点,有什么想法吗?或者有更好的方法来实现我想要做的事情呢?
发布于 2014-07-25 11:11:12
是的,应该是相对无痛的。您实际上希望使用来自persistLowerCase的persistLowerCase字段,这将为您提供一个String -> Q Exp类型的函数。使用预处理器将JSON转换为预期的语法,然后将其传递给函数。
https://stackoverflow.com/questions/24953088
复制相似问题