Skip to content

Instantly share code, notes, and snippets.

@jlavelle
Created November 28, 2022 00:02
Show Gist options
  • Save jlavelle/5f8446305e45e252126aaa61bfaa62b7 to your computer and use it in GitHub Desktop.
Save jlavelle/5f8446305e45e252126aaa61bfaa62b7 to your computer and use it in GitHub Desktop.
Orville JSONB SqlType
module Jsonb where
import qualified Orville.PostgreSQL as O
import qualified Orville.PostgreSQL.Internal.Expr as Expr
import qualified Orville.PostgreSQL.Internal.PgTextFormatValue as PgTextFormatValue
import qualified Orville.PostgreSQL.Internal.RawSql as RawSql
import qualified Orville.PostgreSQL.Internal.SqlValue as SqlValue
import qualified Database.PostgreSQL.LibPQ as LibPQ
import Data.ByteString (ByteString)
jsonbField :: String -> O.FieldDefinition O.NotNull ByteString
jsonbField = O.fieldOfType jsonb
jsonb :: O.SqlType ByteString
jsonb =
O.SqlType
{ O.sqlTypeExpr = jsonbDataType
, O.sqlTypeReferenceExpr = Just jsonbDataType
, O.sqlTypeOid = LibPQ.Oid 3802
, O.sqlTypeMaximumLength = Nothing
, O.sqlTypeToSql = SqlValue.fromRawBytes
, O.sqlTypeFromSql =
maybe
(Left "Failed to read JSONB")
(Right . PgTextFormatValue.toByteString)
. SqlValue.toPgValue
, O.sqlTypeDontDropImplicitDefaultDuringMigrate = False
}
jsonbDataType :: Expr.DataType
jsonbDataType = RawSql.unsafeFromRawSql $ RawSql.fromString "JSONB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment