From 856b6483f715404b2b395074cf4ec3608ccebc56 Mon Sep 17 00:00:00 2001 From: Marc Scholten Date: Tue, 8 Sep 2020 08:25:53 +0200 Subject: [PATCH] Added jsonb type --- IHP/IDE/SchemaDesigner/Compiler.hs | 1 + IHP/IDE/SchemaDesigner/Parser.hs | 5 +++++ IHP/IDE/SchemaDesigner/Types.hs | 1 + IHP/ModelSupport.hs | 4 ++++ IHP/SchemaCompiler.hs | 2 ++ 5 files changed, 13 insertions(+) diff --git a/IHP/IDE/SchemaDesigner/Compiler.hs b/IHP/IDE/SchemaDesigner/Compiler.hs index 9d52058dd..b223f1536 100644 --- a/IHP/IDE/SchemaDesigner/Compiler.hs +++ b/IHP/IDE/SchemaDesigner/Compiler.hs @@ -98,6 +98,7 @@ compilePostgresType (PVaryingN limit) = "CHARACTER VARYING(" <> show limit <> ") compilePostgresType (PCharacterN length) = "CHARACTER(" <> show length <> ")" compilePostgresType PSerial = "SERIAL" compilePostgresType PBigserial = "BIGSERIAL" +compilePostgresType PJSONB = "JSONB" compilePostgresType (PCustomType theType) = theType compileIdentifier :: _ -> Text diff --git a/IHP/IDE/SchemaDesigner/Parser.hs b/IHP/IDE/SchemaDesigner/Parser.hs index af6fc502b..c88af69d5 100644 --- a/IHP/IDE/SchemaDesigner/Parser.hs +++ b/IHP/IDE/SchemaDesigner/Parser.hs @@ -200,6 +200,7 @@ sqlType = choice , varchar , serial , bigserial + , jsonb , customType ] where @@ -310,6 +311,10 @@ sqlType = choice try (symbol' "BIGSERIAL") pure PBigserial + jsonb = do + try (symbol' "JSONB") + pure PJSONB + customType = do theType <- try (takeWhile1P (Just "Custom type") (\c -> isAlphaNum c || c == '_')) pure (PCustomType theType) diff --git a/IHP/IDE/SchemaDesigner/Types.hs b/IHP/IDE/SchemaDesigner/Types.hs index 57ac1bc2c..bddcdee77 100644 --- a/IHP/IDE/SchemaDesigner/Types.hs +++ b/IHP/IDE/SchemaDesigner/Types.hs @@ -89,5 +89,6 @@ data PostgresType | PCharacterN Int | PSerial | PBigserial + | PJSONB | PCustomType Text deriving (Eq, Show) \ No newline at end of file diff --git a/IHP/ModelSupport.hs b/IHP/ModelSupport.hs index 84e66addc..1bc3856b6 100644 --- a/IHP/ModelSupport.hs +++ b/IHP/ModelSupport.hs @@ -32,6 +32,7 @@ import Control.Applicative (Const) import qualified GHC.Types as Type import qualified Data.Text as Text import Data.Aeson (ToJSON (..)) +import qualified Data.Aeson as Aeson -- | Provides the db connection and some IHP-specific db configuration data ModelContext = ModelContext @@ -455,3 +456,6 @@ data RecordNotFoundException deriving (Show) instance Exception RecordNotFoundException + +instance Default Aeson.Value where + def = Aeson.Null \ No newline at end of file diff --git a/IHP/SchemaCompiler.hs b/IHP/SchemaCompiler.hs index 96135cc3d..4f2c4713c 100644 --- a/IHP/SchemaCompiler.hs +++ b/IHP/SchemaCompiler.hs @@ -54,6 +54,7 @@ atomicType :: PostgresType -> Text atomicType = \case PInt -> "Int" PBigInt -> "Integer" + PJSONB -> "Data.Aeson.Value" PText -> "Text" PBoolean -> "Bool" PTimestampWithTimezone -> "UTCTime" @@ -134,6 +135,7 @@ compileTypes options schema@(Schema statements) = <> "import Data.Data\n" <> "import qualified Data.String.Conversions\n" <> "import qualified Data.Text.Encoding\n" + <> "import qualified Data.Aeson\n" <> "import Database.PostgreSQL.Simple.Types (Query (Query), Binary ( .. ))\n" compileStatementPreview :: [Statement] -> Statement -> Text