@@ -3,14 +3,19 @@ module Test.Postgres.Schema where
33import Prelude
44
55import Data.Array as Array
6−import Data.DateTime (DateTime)
7−import Data.Maybe (Maybe(..))
6+import Data.Date (canonicalDate)
7+import Data.DateTime (DateTime(..))
8+import Data.Enum (toEnum)
9+import Data.Maybe (Maybe(..), fromJust)
10+import Data.Time (Time(..))
11+import Partial.Unsafe (unsafePartial)
812import Data.String (contains, Pattern(..))
913import Data.Tuple.Nested (type (/\))
1014import Effect.Aff (Aff)
1115import Effect.Class (liftEffect)
12−import Foreign (Foreign)
16+import Foreign (Foreign, unsafeToForeign)
1317import JS.BigInt (BigInt)
18+import JS.BigInt as BigInt
1419import Prim.Boolean (True)
1520import Test.Spec (Spec, around, describe, it)
1621import Test.Spec.Assertions (shouldEqual, shouldSatisfy)
@@ -103,7 +108,18 @@ typedTitleLike = from eventsTable # selectAll # where_ @"title LIKE $title"
103108
104109typedArrayWhere
105110 :: Q "events" _ _ (id :: Array Int) _
106−typedArrayWhere = from eventsTable # selectAll # where_ @"id IN $id"
111+typedArrayWhere = from eventsTable # selectAll # where_ @"id = ANY($id)"
112+
113+testDateTime :: DateTime
114+testDateTime = unsafePartial do
115+ let year = fromJust (toEnum 2025)
116+ let month = fromJust (toEnum 1)
117+ let day = fromJust (toEnum 15)
118+ let hour = fromJust (toEnum 12)
119+ let minute = fromJust (toEnum 0)
120+ let second = fromJust (toEnum 0)
121+ let ms = fromJust (toEnum 0)
122+ DateTime (canonicalDate year month day) (Time hour minute second ms)
107123
108124-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
109125-- Phase 6: Type-safe UPDATE
@@ -447,6 +463,47 @@ integrationSpec conn = do
447463 # runQueryOne conn { name: "Nobody" }
448464 result `shouldEqual` Nothing
449465
466+ describe "Extended types execution" do
467+ it "inserts and selects Jsonb, Array, DateTime, BigInt" do
468+ _ <- from eventsTable
469+ # insert
470+ { title: "Launch"
471+ , metadata: Jsonb (unsafeToForeign { key: "value", nested: { a: 1 } })
472+ , tags: [ "release", "v1" ]
473+ , created_at: testDateTime
474+ , view_count: BigInt.fromInt 42
475+ }
476+ # runExecute conn {}
477+ rows <- from eventsTable
478+ # select @"title, tags, view_count"
479+ # where_ @"title = $title"
480+ # runQuery conn { title: "Launch" }
481+ Array.length rows `shouldEqual` 1
482+ (map _.title rows) `shouldEqual` [ "Launch" ]
483+
484+ it "inserts with returningAll" do
485+ rows <- from eventsTable
486+ # insert { title: "Minimal", metadata: Jsonb (unsafeToForeign {}), tags: ([] :: Array String), created_at: testDateTime, view_count: BigInt.fromInt 0 }
487+ # returning @"id, title"
488+ # runQuery conn {}
489+ Array.length rows `shouldEqual` 1
490+ (map _.title rows) `shouldEqual` [ "Minimal" ]
491+
492+ it "WHERE with JSONB @> operator" do
493+ rows <- from eventsTable
494+ # select @"title"
495+ # where_ @"metadata @> $metadata"
496+ # runQuery conn { metadata: Jsonb (unsafeToForeign { key: "value" }) }
497+ Array.length rows `shouldEqual` 1
498+ (map _.title rows) `shouldEqual` [ "Launch" ]
499+
500+ it "WHERE with = ANY() array param" do
501+ rows <- from usersTable
502+ # select @"name"
503+ # where_ @"id = ANY($id)"
504+ # runQuery conn { id: [ 1, 2 ] }
505+ Array.length rows `shouldSatisfy` (_ >= 1)
506+
450507 it "complex where with multiple params" do
451508 rows <- from usersTable
452509 # selectAll