{-# language StandaloneKindSignatures #-}
module Rel8.Type.JSONBEncoded ( JSONBEncoded(..) ) where
import Data.Aeson ( FromJSON, ToJSON, parseJSON, toJSON )
import Data.Aeson.Types ( parseEither )
import Data.Bifunctor ( first )
import Data.Kind ( Type )
import Prelude
import qualified Hasql.Decoders as Hasql
import Rel8.Type ( DBType(..) )
import Rel8.Type.Information ( TypeInformation(..) )
import Data.Text ( pack )
type JSONBEncoded :: Type -> Type
newtype JSONBEncoded a = JSONBEncoded { forall a. JSONBEncoded a -> a
fromJSONBEncoded :: a }
instance (FromJSON a, ToJSON a) => DBType (JSONBEncoded a) where
typeInformation :: TypeInformation (JSONBEncoded a)
typeInformation = TypeInformation
{ encode :: JSONBEncoded a -> PrimExpr
encode = forall a. TypeInformation a -> a -> PrimExpr
encode forall a. DBType a => TypeInformation a
typeInformation forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToJSON a => a -> Value
toJSON forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. JSONBEncoded a -> a
fromJSONBEncoded
, decode :: Value (JSONBEncoded a)
decode = forall a b. (a -> Either Text b) -> Value a -> Value b
Hasql.refine (forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first String -> Text
pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> JSONBEncoded a
JSONBEncoded forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> Parser b) -> a -> Either String b
parseEither forall a. FromJSON a => Value -> Parser a
parseJSON) Value Value
Hasql.jsonb
, typeName :: String
typeName = String
"jsonb"
}