on 62f323a help sign in
rel8/src/Rel8/Type/JSONBEncoded.hs
31 lines 825 B history raw
1module Rel8.Type.JSONBEncoded ( JSONBEncoded(..) ) where
3-- aeson
4import Data.Aeson ( FromJSON, ToJSON, parseJSON, toJSON )
5import Data.Aeson.Types ( parseEither )
7-- base
8import Data.Bifunctor ( first )
9import Prelude
11-- hasql
12import qualified Hasql.Decoders as Hasql
14-- rel8
15import Rel8.Type ( DBType(..) )
16import Rel8.Type.Information ( TypeInformation(..) )
18-- text
19import Data.Text ( pack )
22-- | Like 'Rel8.JSONEncoded', but works for @jsonb@ columns.
23newtype JSONBEncoded a = JSONBEncoded { fromJSONBEncoded :: a }
26instance (FromJSON a, ToJSON a) => DBType (JSONBEncoded a) where
27 typeInformation = TypeInformation
28 { encode = encode typeInformation . toJSON . fromJSONBEncoded
29 , decode = Hasql.refine (first pack . fmap JSONBEncoded . parseEither parseJSON) Hasql.jsonb
30 , typeName = "jsonb"
31 }