Integrate Persistent with Servant
Query my SQLite database from within my Servant handlers. Nothing I've written is domain-specific to the business logic yet -- I'm just making sure everything integrates.
This commit is contained in:
parent
660b8d43e5
commit
1d47e94bbe
6 changed files with 117 additions and 34 deletions
35
src/Types.hs
Normal file
35
src/Types.hs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
{-# LANGUAGE EmptyDataDecls #-}
|
||||
{-# LANGUAGE FlexibleContexts #-}
|
||||
{-# LANGUAGE GADTs #-}
|
||||
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
{-# LANGUAGE TemplateHaskell #-}
|
||||
{-# LANGUAGE TypeFamilies #-}
|
||||
--------------------------------------------------------------------------------
|
||||
module Types where
|
||||
--------------------------------------------------------------------------------
|
||||
import Data.Aeson
|
||||
import Data.Text
|
||||
import Database.Persist.TH
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
|
||||
User
|
||||
name Text
|
||||
age Int
|
||||
UniqueName name
|
||||
deriving Eq Read Show
|
||||
|]
|
||||
|
||||
instance FromJSON User where
|
||||
parseJSON = withObject "User" $ \ v ->
|
||||
User <$> v .: "name"
|
||||
<*> v .: "age"
|
||||
|
||||
instance ToJSON User where
|
||||
toJSON (User name age) =
|
||||
object [ "name" .= name
|
||||
, "age" .= age
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue