on da76712 help sign in
rel8/src/Rel8/Type/JSONBEncoded.hs
35 lines 928 B history raw
1{-# language StandaloneKindSignatures #-}
3module Rel8.Type.JSONBEncoded ( JSONBEncoded(..) ) where
5-- aeson
6import Data.Aeson ( FromJSON, ToJSON, parseJSON, toJSON )
7import Data.Aeson.Types ( parseEither )
9-- base
10import Data.Bifunctor ( first )
11import Data.Kind ( Type )
12import Prelude
14-- hasql
15import qualified Hasql.Decoders as Hasql
17-- rel8
18import Rel8.Type ( DBType(..) )
19import Rel8.Type.Information ( TypeInformation(..) )
21-- text
22import Data.Text ( pack )
25-- | Like 'Rel8.JSONEncoded', but works for @jsonb@ columns.
26type JSONBEncoded :: Type -> Type
27newtype JSONBEncoded a = JSONBEncoded { fromJSONBEncoded :: a }
30instance (FromJSON a, ToJSON a) => DBType (JSONBEncoded a) where
31 typeInformation = TypeInformation
32 { encode = encode typeInformation . toJSON . fromJSONBEncoded
33 , decode = Hasql.refine (first pack . fmap JSONBEncoded . parseEither parseJSON) Hasql.jsonb
34 , typeName = "jsonb"
35 }