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:
William Carroll 2020-07-24 22:46:54 +01:00
parent 660b8d43e5
commit 1d47e94bbe
6 changed files with 117 additions and 34 deletions

35
src/Types.hs Normal file
View 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
]