about summary refs log tree commit diff
path: root/src/Types.hs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-07-24T21·46+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-07-24T21·48+0100
commit1d47e94bbe26479ffaaafecd27cdb83d072bfe01 (patch)
treef21dbed4627fc3c3c3ae6aa83d097fecd7da21ff /src/Types.hs
parent660b8d43e5272e2b71b6092b4c879a82c4d861a8 (diff)
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.
Diffstat (limited to 'src/Types.hs')
-rw-r--r--src/Types.hs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/Types.hs b/src/Types.hs
new file mode 100644
index 000000000000..3a410dc4b525
--- /dev/null
+++ b/src/Types.hs
@@ -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
+           ]