[英]Yesod new data type definition and mapping
In Yesod, I want to define a new data type: 在Yesod中,我想定义一个新的数据类型:
data Status = Read | Reviewed | Learned
I'm using the Scaffold example. 我正在使用脚手架示例。 So in the best practice where should I declare the above data?
因此,在最佳实践中,应在哪里声明上述数据? In the
Foundation.hs
or Application.hs
or elsewhere? 在
Foundation.hs
或Application.hs
还是其他地方?
I will then create a database table with one of the column as this Status
type. 然后,我将使用列之一作为此
Status
类型创建一个数据库表。 How is this mapped to my Postgresql backend? 这如何映射到我的Postgresql后端? Which sql data type should correspond to this
Status
type? 哪种SQL数据类型应与此
Status
类型相对应?
So in the best practice where should I declare the above data?
因此,在最佳实践中,应在哪里声明上述数据? In the Foundation.hs or Application.hs or elsewhere?
在Foundation.hs或Application.hs还是其他地方?
I define it neither of the places. 我没有定义这两个地方。 I usually create a new module for it and define the type there.
我通常会为其创建一个新模块并在其中定义类型。 But ultimately it boils down to personal taste.
但最终归结为个人品味。 I don't recommend it doing it in
Foundation.hs
because that's a module where your master application type and it's instances for various Yesod related typeclasses reside on. 我不建议在
Foundation.hs
这样做,因为这是您的主应用程序类型所在的模块,并且是各种与Yesod相关的类型类的实例所在。 Similary I would not add it in Application.hs
because that's a module where your application's setting and the Wai Application
related functions reside on. 类似地,我不会在
Application.hs
添加它,因为这是您的应用程序设置以及Wai Application
相关功能所在的模块。 But that's just my taste. 但这只是我的口味。 :-)
:-)
I will then create a database table with one of the column as this Status type.
然后,我将使用列之一作为此Status类型创建一个数据库表。 How is this mapped to my Postgresql backend?
这如何映射到我的Postgresql后端? Which sql data type should correspond to this Status type?
哪种SQL数据类型应与此状态类型相对应?
You can use Status
algebric type to be define as it is. 您可以使用
Status
代数类型按原样定义。 An example: 一个例子:
#!/usr/bin/env stack
{- stack
--resolver lts-6.19
--install-ghc
runghc
--package persistent
--package aeson
--package persistent-postgresql
--package text
--package persistent-template
--package time
--package mtl
-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleInstances#-}
{-# LANGUAGE TypeFamilies #-}
import Database.Persist
import Database.Persist.Postgresql
import Database.Persist.TH
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Logger (runStderrLoggingT)
import Data.Time
import Data.Text
import Data.Aeson
import ModelSum
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
User
name Text
age Int
status Status
deriving Show
|]
connStr = "host=localhost dbname=test user=postgres password=postgres port=5432"
main :: IO ()
main = mockMigration migrateAll
And the ModelSum
file: 和
ModelSum
文件:
{-# LANGUAGE TemplateHaskell #-}
module ModelSum where
import Database.Persist.TH
data Status
= Read
| Reviewed
| Learned
deriving (Show, Eq, Read)
derivePersistField "Status"
On executing it, you get: 在执行它时,您将获得:
$ ./script.hs
CREATe TABLE "user"("id" SERIAL8 PRIMARY KEY UNIQUE,"name" VARCHAR NOT NULL,"age" INT8 NOT NULL,"status" VARCHAR NOT NULL)
You can see that the status
column is created as varchar
. 您可以看到
status
列创建为varchar
。 Internally it performs the conversion using the Show
and Read
instances. 在内部,它使用
Show
和Read
实例执行转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.