20. 抽象度の高いDBアクセス
class PersistStore b m where
insert :: PersistEntity val => val → b m (Key b val)
insertKey :: PersistEntity val => Key b val → val → b m ()
repsert :: PersistEntity val => Key b val → val → b m ()
replace :: PersistEntity val => Key b val → val → b m ()
delete :: PersistEntity val => Key b val → b m ()
get :: PersistEntity val => Key b val → b m (Maybe val)
• DBバックエンドの型クラス。
• 函数の意味を型が雄弁に物語っている
21. 抽象度の高いDBアクセス
• データベースに格納出来る値のクラス
• 関連型の機能を使っている
• 型クラスに付随する函数のようなもの
• 識別子やバックエンド、フィールドを表現する型など
class PersistEntity val where
data EntityField val :: * → *
type PersistEntityBackend val :: ((* → *) → * → *)
data Unique val :: ((* → *) → * → *) → *
persistFieldDef :: EntityField val typ → FieldDef
entityDef :: val → EntityDef
toPersistFields :: val → [SomePersistField]
fromPersistValues :: [PersistValue] → Either T.Text val
22. 全部記述するの?
User
ident UserName
password Text
salt Text Maybe
UniqueUser ident
Email
email Text
user UserId Maybe
verkey Text Maybe
UniqueEmail email
23. 全部記述するの?
• コンパイル時に自動生成!
User
ident UserName
password Text
salt Text Maybe
UniqueUser ident
Email
email Text
user UserId Maybe
verkey Text Maybe
UniqueEmail email
24. 全部記述するの?
• コンパイル時に自動生成!
• Template Haskell / Quasi Quote の威力
User
ident UserName
password Text
salt Text Maybe
UniqueUser ident
Email
email Text
user UserId Maybe
verkey Text Maybe
UniqueEmail email
25. 全部記述するの?
• コンパイル時に自動生成!
• Template Haskell / Quasi Quote の威力
• Haskell のコンパイルタイムプログラムの手法・リーダマクロ
User
ident UserName
password Text
salt Text Maybe
UniqueUser ident
Email
email Text
user UserId Maybe
verkey Text Maybe
UniqueEmail email
26. 全部記述するの?
• コンパイル時に自動生成!
• Template Haskell / Quasi Quote の威力
• Haskell のコンパイルタイムプログラムの手法・リーダマクロ
User
ident UserName
password Text
salt Text Maybe
UniqueUser ident
Email
email Text
user UserId Maybe
verkey Text Maybe
UniqueEmail email
• 一意識別子やクェリの種類(Eq, Lt, Gt)なども指定
27. 全部記述するの?
• コンパイル時に自動生成!
• Template Haskell / Quasi Quote の威力
• Haskell のコンパイルタイムプログラムの手法・リーダマクロ
User
ident UserName
password Text
salt Text Maybe
UniqueUser ident
Email
email Text
user UserId Maybe
verkey Text Maybe
UniqueEmail email
• 一意識別子やクェリの種類(Eq, Lt, Gt)なども指定
• これがおよそ300行程度(!!)に展開される
28. 型付きURL
• 関連型・Template Haskell の利用例。
• リンク切れや不正リンクを型レベルで防止!
/static StaticR Static getStatic
/auth AuthR Auth getAuth
/favicon.ico FaviconR GET
/robots.txt RobotsR GET
/repo/#String/tree/*ObjPiece TreeR GET
/repo/#String/blob/*ObjPiece BlobR GET
/repo/#String/commits/*ObjPiece CommitsR GET
• Method も指定可能(REST APIが作りやすい)
• リンク用の関連型も自動生成
• あとは各routeに対する処理函数を書くだけ
• 静的ファイルへのリンク函数も生成してくれる