@@ -6,11 +6,10 @@ import Control.Monad.Error.Class (throwError)
66import Data.Array as Array
77import Data.Maybe (Maybe(..), isNothing)
88import Effect.Aff (Aff)
9−import Effect.Class (liftEffect)
10−import Effect.Exception (error)
9+import Effect.Class (class MonadEffect, liftEffect)
10+import Effect.Exception as Exception
1111import Foreign (MultipleErrors)
1212import Test.Spec (Spec, describe, it)
13−import Test.Spec.Assertions (shouldEqual)
1413import Type.Function (type (#))
1514import Type.Proxy (Proxy(..))
1615import Yoga.Om as Om
@@ -32,624 +31,400 @@ import Yoga.Postgres.ClientOm
3231 , gt, gte, ilike, inArray, join, like_, lt, lte
3332 , maxOf, minOf, minOfWhere, ne, not_, or_
3433 , selectFrom, selectFromWhere, selectOneFrom, selectOneFromWhere
35− , sumOf, sumOfWhere, updateWhere, upsert, upsertMany, withTransaction
34+ , sumOf, sumOfWhere, transactionally, updateWhere, upsert, upsertMany
3635 )
37−import Yoga.Postgres.Schema (Table, PrimaryKey, AutoIncrement, Unique, Default, DefaultExpr, ForeignKey, References, PGUUID)
38−
39−-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
40−-- Test schemas
41−-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
42−
43−type UsersTable = Table "test_om_users"
44− ( id :: PrimaryKey (AutoIncrement Int)
45− , name :: String
46− , email :: Unique String
47− , active :: Default "true" Boolean
48− , role :: String
49− )
50−
51−usersTable :: Proxy UsersTable
52−usersTable = Proxy
53−
54−type SettingsTable = Table "test_om_settings"
55− ( key :: PrimaryKey String
56− , value :: String
57− )
58−
59−settingsTable :: Proxy SettingsTable
60−settingsTable = Proxy
61−
62−type UuidTable = Table "test_om_uuid_things"
63− ( id :: PGUUID # PrimaryKey # DefaultExpr "gen_random_uuid()"
64− , label :: String
65− )
66−
67−uuidTable :: Proxy UuidTable
68−uuidTable = Proxy
69−
70−type PostsTable = Table "test_om_posts"
71− ( id :: PrimaryKey (AutoIncrement Int)
72− , title :: String
73− , body :: String
74− , user_id :: ForeignKey "test_om_users" References "id" Int
75− )
76−
77−postsTable :: Proxy PostsTable
78−postsTable = Proxy
79−
80−type CommentsTable = Table "test_om_comments"
81− ( id :: PrimaryKey (AutoIncrement Int)
82− , text :: String
83− , user_id :: ForeignKey "test_om_users" References "id" Int
84− )
85−
86−commentsTable :: Proxy CommentsTable
87−commentsTable = Proxy
88−
89−-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
90−-- Helpers
91−-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
92−
93−testConnection :: Aff PG.Connection
94−testConnection =
95− PG.postgres
96− { host: PG.PostgresHost "localhost"
97− , port: PG.PostgresPort 5433
98− , database: PG.PostgresDatabase "test_playground"
99− , username: PG.PostgresUsername "postgres"
100− , password: PG.PostgresPassword "postgres"
101− } # liftEffect
102−
103−run
104− :: forall a
105− . Om.Om { postgresConnection :: PG.Connection } (parseError :: MultipleErrors) a
106− -> PG.Connection
107− -> Aff a
108−run om conn = Om.runOm { postgresConnection: conn }
109− { exception: \e -> throwError e
110− , parseError: \errs -> throwError (error ("Parse error: " <> show errs))
111− }
112− om
113−
114−runNoErr
115− :: forall a
116− . Om.Om { postgresConnection :: PG.Connection } () a
117− -> PG.Connection
118− -> Aff a
119−runNoErr om conn = Om.runOm { postgresConnection: conn }
120− { exception: \e -> throwError e }
121− om
122−
123−withUsersTable :: (PG.Connection -> Aff Unit) -> Aff Unit
124−withUsersTable test = do
125− conn <- testConnection
126− _ <- PG.executeSimple createDDL conn
127− test conn
128− _ <- PG.executeSimple dropDDL conn
129− PG.end conn
130− where
131− dropDDL = PG.SQL "DROP TABLE IF EXISTS test_om_users CASCADE"
132− createDDL = PG.SQL
133− """DROP TABLE IF EXISTS test_om_users CASCADE;
134− CREATE TABLE test_om_users (
135− id SERIAL PRIMARY KEY,
136− name TEXT NOT NULL,
137− email TEXT UNIQUE NOT NULL,
138− active BOOLEAN DEFAULT true NOT NULL,
139− role TEXT NOT NULL
140− )"""
141−
142−withSettingsTable :: (PG.Connection -> Aff Unit) -> Aff Unit
143−withSettingsTable test = do
144− conn <- testConnection
145− _ <- PG.executeSimple createDDL conn
146− test conn
147− _ <- PG.executeSimple dropDDL conn
148− PG.end conn
149− where
150− dropDDL = PG.SQL "DROP TABLE IF EXISTS test_om_settings CASCADE"
151− createDDL = PG.SQL
152− """DROP TABLE IF EXISTS test_om_settings CASCADE;
153− CREATE TABLE test_om_settings (
154− key TEXT PRIMARY KEY,
155− value TEXT NOT NULL
156− )"""
157−
158−withUuidTable :: (PG.Connection -> Aff Unit) -> Aff Unit
159−withUuidTable test = do
160− conn <- testConnection
161− _ <- PG.executeSimple createDDL conn
162− test conn
163− _ <- PG.executeSimple dropDDL conn
164− PG.end conn
165− where
166− dropDDL = PG.SQL "DROP TABLE IF EXISTS test_om_uuid_things CASCADE"
167− createDDL = PG.SQL
168− """DROP TABLE IF EXISTS test_om_uuid_things CASCADE;
169− CREATE TABLE test_om_uuid_things (
170− id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
171− label TEXT NOT NULL
172− )"""
173−
174−withPostsTable :: (PG.Connection -> Aff Unit) -> Aff Unit
175−withPostsTable test = do
176− conn <- testConnection
177− _ <- PG.executeSimple createDDL conn
178− test conn
179− _ <- PG.executeSimple (PG.SQL "DROP TABLE IF EXISTS test_om_posts CASCADE") conn
180− _ <- PG.executeSimple (PG.SQL "DROP TABLE IF EXISTS test_om_users CASCADE") conn
181− PG.end conn
182− where
183− createDDL = PG.SQL
184− """DROP TABLE IF EXISTS test_om_posts CASCADE;
185− DROP TABLE IF EXISTS test_om_users CASCADE;
186− CREATE TABLE test_om_users (
187− id SERIAL PRIMARY KEY,
188− name TEXT NOT NULL,
189− email TEXT UNIQUE NOT NULL,
190− active BOOLEAN DEFAULT true NOT NULL,
191− role TEXT NOT NULL
192− );
193− CREATE TABLE test_om_posts (
194− id SERIAL PRIMARY KEY,
195− title TEXT NOT NULL,
196− body TEXT NOT NULL,
197− user_id INTEGER NOT NULL REFERENCES test_om_users(id)
198− )"""
199−
200−withPostsAndCommentsTable :: (PG.Connection -> Aff Unit) -> Aff Unit
201−withPostsAndCommentsTable test = do
202− conn <- testConnection
203− _ <- PG.executeSimple createDDL conn
204− test conn
205− _ <- PG.executeSimple (PG.SQL "DROP TABLE IF EXISTS test_om_comments CASCADE") conn
206− _ <- PG.executeSimple (PG.SQL "DROP TABLE IF EXISTS test_om_posts CASCADE") conn
207− _ <- PG.executeSimple (PG.SQL "DROP TABLE IF EXISTS test_om_users CASCADE") conn
208− PG.end conn
209− where
210− createDDL = PG.SQL
211− """DROP TABLE IF EXISTS test_om_comments CASCADE;
212− DROP TABLE IF EXISTS test_om_posts CASCADE;
213− DROP TABLE IF EXISTS test_om_users CASCADE;
214− CREATE TABLE test_om_users (
215− id SERIAL PRIMARY KEY,
216− name TEXT NOT NULL,
217− email TEXT UNIQUE NOT NULL,
218− active BOOLEAN DEFAULT true NOT NULL,
219− role TEXT NOT NULL
220− );
221− CREATE TABLE test_om_posts (
222− id SERIAL PRIMARY KEY,
223− title TEXT NOT NULL,
224− body TEXT NOT NULL,
225− user_id INTEGER NOT NULL REFERENCES test_om_users(id)
226− );
227− CREATE TABLE test_om_comments (
228− id SERIAL PRIMARY KEY,
229− text TEXT NOT NULL,
230− user_id INTEGER NOT NULL REFERENCES test_om_users(id)
231− )"""
232−
233−shouldBeJust :: forall a. Maybe a -> Aff a
234−shouldBeJust (Just a) = pure a
235−shouldBeJust Nothing = throwError $ error "Expected Just but got Nothing"
236−
237−-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
238−-- Spec
239−-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
36+import Yoga.Postgres.Schema (Table, PrimaryKey, AutoIncrement, Unique, Default, ForeignKey, References, PGUUID)
24037
24138spec :: Spec Unit
24239spec = describe "ClientOm" do
24340
24441 describe "Creating records" do
245− it "inserts a record and returns it with generated fields" $ withUsersTable \conn -> do
246− created <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
42+ it "inserts a record and returns it with generated fields" $ withUsersTable do
43+ created <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
24744 alice <- shouldBeJust created
24845 alice.name `shouldEqual` "Alice"
24946 (alice.id > 0) `shouldEqual` true
25047
251− it "lets the database fill in defaults" $ withUsersTable \conn -> do
252− created <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
48+ it "lets the database fill in defaults" $ withUsersTable do
49+ created <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
25350 alice <- shouldBeJust created
25451 alice.active `shouldEqual` true
25552
256− it "inserts many records at once" $ withUsersTable \conn -> do
53+ it "inserts many records at once" $ withUsersTable do
25754 let noUsers = [] :: Array { name :: String, email :: String, role :: String }
258− empty <- conn # run do usersTable # createMany noUsers
55+ empty <- usersTable # createMany noUsers
25956 Array.length empty `shouldEqual` 0
260− users <- conn # run do
261− usersTable # createMany
262− [ { name: "Alice", email: "a@t.com", role: "admin" }
263− , { name: "Bob", email: "b@t.com", role: "user" }
264− , { name: "Carol", email: "c@t.com", role: "user" }
265− ]
57+ users <- usersTable # createMany
58+ [ { name: "Alice", email: "a@t.com", role: "admin" }
59+ , { name: "Bob", email: "b@t.com", role: "user" }
60+ , { name: "Carol", email: "c@t.com", role: "user" }
61+ ]
26662 Array.length users `shouldEqual` 3
267− n <- conn # runNoErr do usersTable # count
63+ n <- usersTable # count
26864 n `shouldEqual` 3
26965
270− it "skips insert when a conflict would occur" $ withSettingsTable \conn -> do
271− inserted <- conn # run do settingsTable # createIfNotExists { key: "theme", value: "dark" }
66+ it "skips insert when a conflict would occur" $ withSettingsTable do
67+ inserted <- settingsTable # createIfNotExists { key: "theme", value: "dark" }
27268 r1 <- shouldBeJust inserted
27369 r1.value `shouldEqual` "dark"
274− skipped <- conn # run do settingsTable # createIfNotExists { key: "theme", value: "light" }
70+ skipped <- settingsTable # createIfNotExists { key: "theme", value: "light" }
27571 isNothing skipped `shouldEqual` true
276− found <- conn # run do settingsTable # findOneBy @"key" "theme"
72+ found <- settingsTable # findOneBy @"key" "theme"
27773 existing <- shouldBeJust found
27874 existing.value `shouldEqual` "dark"
27975
280− it "upserts: inserts or updates on conflict" $ withSettingsTable \conn -> do
281− inserted <- conn # run do settingsTable # upsert { key: "theme", value: "dark" }
76+ it "upserts: inserts or updates on conflict" $ withSettingsTable do
77+ inserted <- settingsTable # upsert { key: "theme", value: "dark" }
28278 r1 <- shouldBeJust inserted
28379 r1.value `shouldEqual` "dark"
284− updated <- conn # run do settingsTable # upsert { key: "theme", value: "light" }
80+ updated <- settingsTable # upsert { key: "theme", value: "light" }
28581 r2 <- shouldBeJust updated
28682 r2.value `shouldEqual` "light"
287− n <- conn # runNoErr do settingsTable # count
83+ n <- settingsTable # count
28884 n `shouldEqual` 1
28985
290− it "bulk upserts a batch" $ withSettingsTable \conn -> do
291− _ <- conn # run do
292− settingsTable # upsertMany
293− [ { key: "theme", value: "dark" }
294− , { key: "lang", value: "en" }
295− ]
296− n1 <- conn # runNoErr do settingsTable # count
86+ it "bulk upserts a batch" $ withSettingsTable do
87+ _ <- settingsTable # upsertMany
88+ [ { key: "theme", value: "dark" }
89+ , { key: "lang", value: "en" }
90+ ]
91+ n1 <- settingsTable # count
29792 n1 `shouldEqual` 2
298− updated <- conn # run do
299− settingsTable # upsertMany
300− [ { key: "theme", value: "light" }
301− , { key: "lang", value: "de" }
302− , { key: "tz", value: "UTC" }
303− ]
93+ updated <- settingsTable # upsertMany
94+ [ { key: "theme", value: "light" }
95+ , { key: "lang", value: "de" }
96+ , { key: "tz", value: "UTC" }
97+ ]
30498 Array.length updated `shouldEqual` 3
305− n2 <- conn # runNoErr do settingsTable # count
99+ n2 <- settingsTable # count
306100 n2 `shouldEqual` 3
307− found <- conn # run do settingsTable # findOneBy @"key" "theme"
101+ found <- settingsTable # findOneBy @"key" "theme"
308102 setting <- shouldBeJust found
309103 setting.value `shouldEqual` "light"
310104
311105 describe "Finding records" do
312− it "finds a single record by primary key" $ withUsersTable \conn -> do
313− created <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
106+ it "finds a single record by primary key" $ withUsersTable do
107+ created <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
314108 alice <- shouldBeJust created
315− found <- conn # run do usersTable # findById alice.id
109+ found <- usersTable # findById alice.id
316110 r <- shouldBeJust found
317111 r.name `shouldEqual` "Alice"
318112
319− it "finds a single record by any unique column" $ withUsersTable \conn -> do
320− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
321− found <- conn # run do usersTable # findOneBy @"email" "a@t.com"
113+ it "finds a single record by any unique column" $ withUsersTable do
114+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
115+ found <- usersTable # findOneBy @"email" "a@t.com"
322116 alice <- shouldBeJust found
323117 alice.name `shouldEqual` "Alice"
324118
325− it "finds all records matching a column value" $ withUsersTable \conn -> do
326− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
327− _ <- conn # run do usersTable # create { name: "Bob", email: "b@t.com", role: "admin" }
328− _ <- conn # run do usersTable # create { name: "Carol", email: "c@t.com", role: "user" }
329− admins <- conn # run do usersTable # findBy @"role" "admin"
119+ it "finds all records matching a column value" $ withUsersTable do
120+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
121+ _ <- usersTable # create { name: "Bob", email: "b@t.com", role: "admin" }
122+ _ <- usersTable # create { name: "Carol", email: "c@t.com", role: "user" }
123+ admins <- usersTable # findBy @"role" "admin"
330124 Array.length admins `shouldEqual` 2
331125
332− it "finds all records matching multiple conditions" $ withUsersTable \conn -> do
333− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
334− _ <- conn # run do usersTable # create { name: "Bob", email: "b@t.com", role: "admin" }
335− _ <- conn # run do usersTable # create { name: "Carol", email: "c@t.com", role: "user" }
336− alices <- conn # run do usersTable # findAllWhere { name: "Alice", role: "admin" }
126+ it "finds all records matching multiple conditions" $ withUsersTable do
127+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
128+ _ <- usersTable # create { name: "Bob", email: "b@t.com", role: "admin" }
129+ _ <- usersTable # create { name: "Carol", email: "c@t.com", role: "user" }
130+ alices <- usersTable # findAllWhere { name: "Alice", role: "admin" }
337131 Array.length alices `shouldEqual` 1
338132 alice <- shouldBeJust (Array.head alices)
339133 alice.email `shouldEqual` "a@t.com"
340134
341− it "returns Nothing when no record matches" $ withUsersTable \conn -> do
342− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
343− missing <- conn # run do usersTable # findOneBy @"email" "nobody@t.com"
135+ it "returns Nothing when no record matches" $ withUsersTable do
136+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
137+ missing <- usersTable # findOneBy @"email" "nobody@t.com"
344138 isNothing missing `shouldEqual` true
345− alsoMissing <- conn # run do usersTable # findOneWhere { name: "Alice", role: "user" }
139+ alsoMissing <- usersTable # findOneWhere { name: "Alice", role: "user" }
346140 isNothing alsoMissing `shouldEqual` true
347141
348− it "finds records by a list of primary keys" $ withUsersTable \conn -> do
349− created1 <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
142+ it "finds records by a list of primary keys" $ withUsersTable do
143+ created1 <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
350144 u1 <- shouldBeJust created1
351− created2 <- conn # run do usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
145+ created2 <- usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
352146 u2 <- shouldBeJust created2
353− _ <- conn # run do usersTable # create { name: "Carol", email: "c@t.com", role: "user" }
354− found <- conn # run do usersTable # findByIdIn [ u1.id, u2.id ]
147+ _ <- usersTable # create { name: "Carol", email: "c@t.com", role: "user" }
148+ found <- usersTable # findByIdIn [ u1.id, u2.id ]
355149 Array.length found `shouldEqual` 2
356− empty <- conn # run do usersTable # findByIdIn ([] :: Array Int)
150+ empty <- usersTable # findByIdIn ([] :: Array Int)
357151 Array.length empty `shouldEqual` 0
358152
359− it "returns all records in the table" $ withUsersTable \conn -> do
360− _ <- conn # run do
361− usersTable # createMany
362− [ { name: "Alice", email: "a@t.com", role: "admin" }
363− , { name: "Bob", email: "b@t.com", role: "user" }
364− ]
365− all <- conn # run do usersTable # findAll
153+ it "returns all records in the table" $ withUsersTable do
154+ _ <- usersTable # createMany
155+ [ { name: "Alice", email: "a@t.com", role: "admin" }
156+ , { name: "Bob", email: "b@t.com", role: "user" }
157+ ]
158+ all <- usersTable # findAll
366159 Array.length all `shouldEqual` 2
367160
368161 describe "Filtering with operators" do
369− it "compares with gt, gte, lt, lte" $ withUsersTable \conn -> do
370− _ <- conn # run do
371− usersTable # createMany
372− [ { name: "Alice", email: "a@t.com", role: "admin" }
373− , { name: "Bob", email: "b@t.com", role: "user" }
374− , { name: "Carol", email: "c@t.com", role: "user" }
375− ]
376− all <- conn # run do usersTable # findAll
162+ it "compares with gt, gte, lt, lte" $ withUsersTable do
163+ _ <- usersTable # createMany
164+ [ { name: "Alice", email: "a@t.com", role: "admin" }
165+ , { name: "Bob", email: "b@t.com", role: "user" }
166+ , { name: "Carol", email: "c@t.com", role: "user" }
167+ ]
168+ all <- usersTable # findAll
377169 let
378170 id1 = case Array.head all of
379171 Just u -> u.id
380172 Nothing -> 0
381− gtRows <- conn # run do usersTable # findAllWhere { id: gt id1 }
173+ gtRows <- usersTable # findAllWhere { id: gt id1 }
382174 Array.length gtRows `shouldEqual` 2
383− gteRows <- conn # run do usersTable # findAllWhere { id: gte id1 }
175+ gteRows <- usersTable # findAllWhere { id: gte id1 }
384176 Array.length gteRows `shouldEqual` 3
385− ltRows <- conn # run do usersTable # findAllWhere { id: lt (id1 + 3) }
177+ ltRows <- usersTable # findAllWhere { id: lt (id1 + 3) }
386178 Array.length ltRows `shouldEqual` 3
387− lteRows <- conn # run do usersTable # findAllWhere { id: lte id1 }
179+ lteRows <- usersTable # findAllWhere { id: lte id1 }
388180 Array.length lteRows `shouldEqual` 1
389181
390− it "excludes with ne" $ withUsersTable \conn -> do
391− _ <- conn # run do
392− usersTable # createMany
393− [ { name: "Alice", email: "a@t.com", role: "admin" }
394− , { name: "Bob", email: "b@t.com", role: "user" }
395− , { name: "Carol", email: "c@t.com", role: "user" }
396− ]
397− rows <- conn # run do usersTable # findAllWhere { role: ne "admin" }
182+ it "excludes with ne" $ withUsersTable do
183+ _ <- usersTable # createMany
184+ [ { name: "Alice", email: "a@t.com", role: "admin" }
185+ , { name: "Bob", email: "b@t.com", role: "user" }
186+ , { name: "Carol", email: "c@t.com", role: "user" }
187+ ]
188+ rows <- usersTable # findAllWhere { role: ne "admin" }
398189 Array.length rows `shouldEqual` 2
399190
400− it "pattern-matches with like_ and ilike" $ withUsersTable \conn -> do
401− _ <- conn # run do
402− usersTable # createMany
403− [ { name: "Alice", email: "a@t.com", role: "admin" }
404− , { name: "Bob", email: "b@t.com", role: "user" }
405− , { name: "Carol", email: "c@t.com", role: "user" }
406− ]
407− likeRows <- conn # run do usersTable # findAllWhere { name: like_ "A%" }
191+ it "pattern-matches with like_ and ilike" $ withUsersTable do
192+ _ <- usersTable # createMany
193+ [ { name: "Alice", email: "a@t.com", role: "admin" }
194+ , { name: "Bob", email: "b@t.com", role: "user" }
195+ , { name: "Carol", email: "c@t.com", role: "user" }
196+ ]
197+ likeRows <- usersTable # findAllWhere { name: like_ "A%" }
408198 Array.length likeRows `shouldEqual` 1
409− ilikeRows <- conn # run do usersTable # findAllWhere { name: ilike "a%" }
199+ ilikeRows <- usersTable # findAllWhere { name: ilike "a%" }
410200 Array.length ilikeRows `shouldEqual` 1
411201
412− it "checks membership with inArray" $ withUsersTable \conn -> do
413− _ <- conn # run do
414− usersTable # createMany
415− [ { name: "Alice", email: "a@t.com", role: "admin" }
416− , { name: "Bob", email: "b@t.com", role: "user" }
417− , { name: "Carol", email: "c@t.com", role: "mod" }
418− ]
419− rows <- conn # run do usersTable # findAllWhere { role: inArray [ "admin", "mod" ] }
202+ it "checks membership with inArray" $ withUsersTable do
203+ _ <- usersTable # createMany
204+ [ { name: "Alice", email: "a@t.com", role: "admin" }
205+ , { name: "Bob", email: "b@t.com", role: "user" }
206+ , { name: "Carol", email: "c@t.com", role: "mod" }
207+ ]
208+ rows <- usersTable # findAllWhere { role: inArray [ "admin", "mod" ] }
420209 Array.length rows `shouldEqual` 2
421210
422− it "combines conditions with or_" $ withUsersTable \conn -> do
423− _ <- conn # run do
424− usersTable # createMany
425− [ { name: "Alice", email: "a@t.com", role: "admin" }
426− , { name: "Bob", email: "b@t.com", role: "user" }
427− , { name: "Carol", email: "c@t.com", role: "mod" }
428− ]
429− rows <- conn # run do usersTable # findAllWhere { role: or_ (eq_ "admin") (eq_ "mod") }
211+ it "combines conditions with or_" $ withUsersTable do
212+ _ <- usersTable # createMany
213+ [ { name: "Alice", email: "a@t.com", role: "admin" }
214+ , { name: "Bob", email: "b@t.com", role: "user" }
215+ , { name: "Carol", email: "c@t.com", role: "mod" }
216+ ]
217+ rows <- usersTable # findAllWhere { role: or_ (eq_ "admin") (eq_ "mod") }
430218 Array.length rows `shouldEqual` 2
431219
432− it "negates conditions with not_" $ withUsersTable \conn -> do
433− _ <- conn # run do
434− usersTable # createMany
435− [ { name: "Alice", email: "a@t.com", role: "admin" }
436− , { name: "Bob", email: "b@t.com", role: "user" }
437− , { name: "Carol", email: "c@t.com", role: "user" }
438− ]
439− rows <- conn # run do usersTable # findAllWhere { role: not_ (eq_ "admin") }
220+ it "negates conditions with not_" $ withUsersTable do
221+ _ <- usersTable # createMany
222+ [ { name: "Alice", email: "a@t.com", role: "admin" }
223+ , { name: "Bob", email: "b@t.com", role: "user" }
224+ , { name: "Carol", email: "c@t.com", role: "user" }
225+ ]
226+ rows <- usersTable # findAllWhere { role: not_ (eq_ "admin") }
440227 Array.length rows `shouldEqual` 2
441228
442229 describe "Updating and deleting" do
443− it "updates rows matching a condition" $ withUsersTable \conn -> do
444− _ <- conn # run do
445− usersTable # createMany
446− [ { name: "Alice", email: "a@t.com", role: "admin" }
447− , { name: "Bob", email: "b@t.com", role: "user" }
448− , { name: "Carol", email: "c@t.com", role: "user" }
449− ]
450− updated <- conn # run do usersTable # updateWhere { role: "user" } { role: "member" }
230+ it "updates rows matching a condition" $ withUsersTable do
231+ _ <- usersTable # createMany
232+ [ { name: "Alice", email: "a@t.com", role: "admin" }
233+ , { name: "Bob", email: "b@t.com", role: "user" }
234+ , { name: "Carol", email: "c@t.com", role: "user" }
235+ ]
236+ updated <- usersTable # updateWhere { role: "user" } { role: "member" }
451237 Array.length updated `shouldEqual` 2
452− members <- conn # run do usersTable # findBy @"role" "member"
238+ members <- usersTable # findBy @"role" "member"
453239 Array.length members `shouldEqual` 2
454240
455− it "deletes rows matching a condition" $ withUsersTable \conn -> do
456− _ <- conn # run do
457− usersTable # createMany
458− [ { name: "Alice", email: "a@t.com", role: "admin" }
459− , { name: "Bob", email: "b@t.com", role: "user" }
460− , { name: "Carol", email: "c@t.com", role: "user" }
461− ]
462− deleted <- conn # runNoErr do usersTable # deleteWhere { role: "user" }
241+ it "deletes rows matching a condition" $ withUsersTable do
242+ _ <- usersTable # createMany
243+ [ { name: "Alice", email: "a@t.com", role: "admin" }
244+ , { name: "Bob", email: "b@t.com", role: "user" }
245+ , { name: "Carol", email: "c@t.com", role: "user" }
246+ ]
247+ deleted <- usersTable # deleteWhere { role: "user" }
463248 deleted `shouldEqual` 2
464− remaining <- conn # runNoErr do usersTable # count
249+ remaining <- usersTable # count
465250 remaining `shouldEqual` 1
466251
467− it "deletes and returns the deleted rows" $ withUsersTable \conn -> do
468− _ <- conn # run do
469− usersTable # createMany
470− [ { name: "Alice", email: "a@t.com", role: "admin" }
471− , { name: "Bob", email: "b@t.com", role: "user" }
472− , { name: "Carol", email: "c@t.com", role: "user" }
473− ]
474− deleted <- conn # run do usersTable # deleteWhereReturning { role: "user" }
252+ it "deletes and returns the deleted rows" $ withUsersTable do
253+ _ <- usersTable # createMany
254+ [ { name: "Alice", email: "a@t.com", role: "admin" }
255+ , { name: "Bob", email: "b@t.com", role: "user" }
256+ , { name: "Carol", email: "c@t.com", role: "user" }
257+ ]
258+ deleted <- usersTable # deleteWhereReturning { role: "user" }
475259 Array.length deleted `shouldEqual` 2
476260 Array.sort (map _.name deleted) `shouldEqual` [ "Bob", "Carol" ]
477− remaining <- conn # runNoErr do usersTable # count
261+ remaining <- usersTable # count
478262 remaining `shouldEqual` 1
479263
480− it "deletes everything" $ withUsersTable \conn -> do
481− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
482− _ <- conn # run do usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
483− deleted <- conn # runNoErr do usersTable # deleteAll
264+ it "deletes everything" $ withUsersTable do
265+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
266+ _ <- usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
267+ deleted <- usersTable # deleteAll
484268 deleted `shouldEqual` 2
485− n <- conn # runNoErr do usersTable # count
269+ n <- usersTable # count
486270 n `shouldEqual` 0
487271
488272 describe "Ordering and pagination" do
489− it "orders results by a column" $ withUsersTable \conn -> do
490− _ <- conn # run do
491− usersTable # createMany
492− [ { name: "Carol", email: "c@t.com", role: "user" }
493− , { name: "Alice", email: "a@t.com", role: "admin" }
494− , { name: "Bob", email: "b@t.com", role: "user" }
495− ]
496− asc <- conn # run do usersTable # findOrdered @"name" Asc
273+ it "orders results by a column" $ withUsersTable do
274+ _ <- usersTable # createMany
275+ [ { name: "Carol", email: "c@t.com", role: "user" }
276+ , { name: "Alice", email: "a@t.com", role: "admin" }
277+ , { name: "Bob", email: "b@t.com", role: "user" }
278+ ]
279+ asc <- usersTable # findOrdered @"name" Asc
497280 (map _.name asc) `shouldEqual` [ "Alice", "Bob", "Carol" ]
498− desc <- conn # run do usersTable # findOrdered @"name" Desc
281+ desc <- usersTable # findOrdered @"name" Desc
499282 (map _.name desc) `shouldEqual` [ "Carol", "Bob", "Alice" ]
500283
501− it "filters, orders, and limits in one call" $ withUsersTable \conn -> do
502− _ <- conn # run do
503− usersTable # createMany
504− [ { name: "Alice", email: "a@t.com", role: "user" }
505− , { name: "Bob", email: "b@t.com", role: "user" }
506− , { name: "Carol", email: "c@t.com", role: "user" }
507− , { name: "Dave", email: "d@t.com", role: "user" }
508− ]
509− page1 <- conn # run do usersTable # findWhereLimited @"name" { role: "user" } Asc { limit: 2, offset: 0 }
284+ it "filters, orders, and limits in one call" $ withUsersTable do
285+ _ <- usersTable # createMany
286+ [ { name: "Alice", email: "a@t.com", role: "user" }
287+ , { name: "Bob", email: "b@t.com", role: "user" }
288+ , { name: "Carol", email: "c@t.com", role: "user" }
289+ , { name: "Dave", email: "d@t.com", role: "user" }
290+ ]
291+ page1 <- usersTable # findWhereLimited @"name" { role: "user" } Asc { limit: 2, offset: 0 }
510292 (map _.name page1) `shouldEqual` [ "Alice", "Bob" ]
511− page2 <- conn # run do usersTable # findWhereLimited @"name" { role: "user" } Asc { limit: 2, offset: 2 }
293+ page2 <- usersTable # findWhereLimited @"name" { role: "user" } Asc { limit: 2, offset: 2 }
512294 (map _.name page2) `shouldEqual` [ "Carol", "Dave" ]
513295
514− it "paginates forward with cursors" $ withUsersTable \conn -> do
515− _ <- conn # run do
516− usersTable # createMany
517− [ { name: "Alice", email: "a@t.com", role: "admin" }
518− , { name: "Bob", email: "b@t.com", role: "user" }
519− , { name: "Carol", email: "c@t.com", role: "user" }
520− , { name: "Dave", email: "d@t.com", role: "user" }
521− , { name: "Eve", email: "e@t.com", role: "admin" }
522− ]
523− page1 <- conn # run do usersTable # findFirst @"id" 2
296+ it "paginates forward with cursors" $ withUsersTable do
297+ _ <- usersTable # createMany
298+ [ { name: "Alice", email: "a@t.com", role: "admin" }
299+ , { name: "Bob", email: "b@t.com", role: "user" }
300+ , { name: "Carol", email: "c@t.com", role: "user" }
301+ , { name: "Dave", email: "d@t.com", role: "user" }
302+ , { name: "Eve", email: "e@t.com", role: "admin" }
303+ ]
304+ page1 <- usersTable # findFirst @"id" 2
524305 Array.length page1.items `shouldEqual` 2
525306 page1.hasMore `shouldEqual` true
526307 let
527308 lastId = case Array.last page1.items of
528309 Just u -> u.id
529310 Nothing -> 0
530− page2 <- conn # run do usersTable # findAfter @"id" lastId 2
311+ page2 <- usersTable # findAfter @"id" lastId 2
531312 Array.length page2.items `shouldEqual` 2
532313 page2.hasMore `shouldEqual` true
533314 let
534315 lastId2 = case Array.last page2.items of
535316 Just u -> u.id
536317 Nothing -> 0
537− page3 <- conn # run do usersTable # findAfter @"id" lastId2 2
318+ page3 <- usersTable # findAfter @"id" lastId2 2
538319 Array.length page3.items `shouldEqual` 1
539320 page3.hasMore `shouldEqual` false
540321
541− it "paginates backward with cursors" $ withUsersTable \conn -> do
542− _ <- conn # run do
543− usersTable # createMany
544− [ { name: "Alice", email: "a@t.com", role: "admin" }
545− , { name: "Bob", email: "b@t.com", role: "user" }
546− , { name: "Carol", email: "c@t.com", role: "user" }
547− , { name: "Dave", email: "d@t.com", role: "user" }
548− , { name: "Eve", email: "e@t.com", role: "admin" }
549− ]
550− all <- conn # run do usersTable # findAll
322+ it "paginates backward with cursors" $ withUsersTable do
323+ _ <- usersTable # createMany
324+ [ { name: "Alice", email: "a@t.com", role: "admin" }
325+ , { name: "Bob", email: "b@t.com", role: "user" }
326+ , { name: "Carol", email: "c@t.com", role: "user" }
327+ , { name: "Dave", email: "d@t.com", role: "user" }
328+ , { name: "Eve", email: "e@t.com", role: "admin" }
329+ ]
330+ all <- usersTable # findAll
551331 let
552332 lastUser = case Array.last all of
553333 Just u -> u
554334 Nothing -> { id: 0, name: "", email: "", active: true, role: "" }
555− page <- conn # run do usersTable # findBefore @"id" lastUser.id 2
335+ page <- usersTable # findBefore @"id" lastUser.id 2
556336 Array.length page.items `shouldEqual` 2
557337 page.hasMore `shouldEqual` true
558338
559− it "paginates with a filter applied" $ withUsersTable \conn -> do
560− _ <- conn # run do
561− usersTable # createMany
562− [ { name: "Alice", email: "a@t.com", role: "admin" }
563− , { name: "Bob", email: "b@t.com", role: "user" }
564− , { name: "Carol", email: "c@t.com", role: "user" }
565− , { name: "Dave", email: "d@t.com", role: "user" }
566− , { name: "Eve", email: "e@t.com", role: "admin" }
567− ]
568− page1 <- conn # run do usersTable # findFirstWhere @"id" { role: "user" } 2
339+ it "paginates with a filter applied" $ withUsersTable do
340+ _ <- usersTable # createMany
341+ [ { name: "Alice", email: "a@t.com", role: "admin" }
342+ , { name: "Bob", email: "b@t.com", role: "user" }
343+ , { name: "Carol", email: "c@t.com", role: "user" }
344+ , { name: "Dave", email: "d@t.com", role: "user" }
345+ , { name: "Eve", email: "e@t.com", role: "admin" }
346+ ]
347+ page1 <- usersTable # findFirstWhere @"id" { role: "user" } 2
569348 Array.length page1.items `shouldEqual` 2
570349 page1.hasMore `shouldEqual` true
571350 let
572351 lastId = case Array.last page1.items of
573352 Just u -> u.id
574353 Nothing -> 0
575− page2 <- conn # run do usersTable # findAfterWhere @"id" { role: "user" } lastId 2
354+ page2 <- usersTable # findAfterWhere @"id" { role: "user" } lastId 2
576355 Array.length page2.items `shouldEqual` 1
577356 page2.hasMore `shouldEqual` false
578357
579358 describe "Aggregations and checks" do
580− it "counts rows, optionally with a filter" $ withUsersTable \conn -> do
581− n0 <- conn # runNoErr do usersTable # count
359+ it "counts rows, optionally with a filter" $ withUsersTable do
360+ n0 <- usersTable # count
582361 n0 `shouldEqual` 0
583− _ <- conn # run do
584− usersTable # createMany
585− [ { name: "Alice", email: "a@t.com", role: "admin" }
586− , { name: "Bob", email: "b@t.com", role: "user" }
587− , { name: "Carol", email: "c@t.com", role: "admin" }
588− ]
589− n3 <- conn # runNoErr do usersTable # count
362+ _ <- usersTable # createMany
363+ [ { name: "Alice", email: "a@t.com", role: "admin" }
364+ , { name: "Bob", email: "b@t.com", role: "user" }
365+ , { name: "Carol", email: "c@t.com", role: "admin" }
366+ ]
367+ n3 <- usersTable # count
590368 n3 `shouldEqual` 3
591− nAdmins <- conn # runNoErr do usersTable # countWhere { role: "admin" }
369+ nAdmins <- usersTable # countWhere { role: "admin" }
592370 nAdmins `shouldEqual` 2
593371
594− it "checks whether a record exists" $ withUsersTable \conn -> do
595− created <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
372+ it "checks whether a record exists" $ withUsersTable do
373+ created <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
596374 alice <- shouldBeJust created
597− yes <- conn # runNoErr do usersTable # exists alice.id
375+ yes <- usersTable # exists alice.id
598376 yes `shouldEqual` true
599− no <- conn # runNoErr do usersTable # exists 99999
377+ no <- usersTable # exists 99999
600378 no `shouldEqual` false
601− yesWhere <- conn # runNoErr do usersTable # existsWhere { name: "Alice", role: "admin" }
379+ yesWhere <- usersTable # existsWhere { name: "Alice", role: "admin" }
602380 yesWhere `shouldEqual` true
603− noWhere <- conn # runNoErr do usersTable # existsWhere { name: "Alice", role: "user" }
381+ noWhere <- usersTable # existsWhere { name: "Alice", role: "user" }
604382 noWhere `shouldEqual` false
605383
606− it "sums and averages a numeric column" $ withUsersTable \conn -> do
607− _ <- conn # run do
608− usersTable # createMany
609− [ { name: "Alice", email: "a@t.com", role: "admin" }
610− , { name: "Bob", email: "b@t.com", role: "user" }
611− ]
612− total <- conn # runNoErr do usersTable # sumOf @"id"
384+ it "sums and averages a numeric column" $ withUsersTable do
385+ _ <- usersTable # createMany
386+ [ { name: "Alice", email: "a@t.com", role: "admin" }
387+ , { name: "Bob", email: "b@t.com", role: "user" }
388+ ]
389+ total <- usersTable # sumOf @"id"
613390 (total > 0.0) `shouldEqual` true
614− emptySum <- conn # runNoErr do usersTable # sumOfWhere @"id" { role: "nobody" }
391+ emptySum <- usersTable # sumOfWhere @"id" { role: "nobody" }
615392 emptySum `shouldEqual` 0.0
616− avg <- conn # runNoErr do usersTable # avgOf @"id"
393+ avg <- usersTable # avgOf @"id"
617394 isNothing avg `shouldEqual` false
618− emptyAvg <- conn # runNoErr do usersTable # avgOfWhere @"id" { role: "nobody" }
395+ emptyAvg <- usersTable # avgOfWhere @"id" { role: "nobody" }
619396 isNothing emptyAvg `shouldEqual` true
620397
621− it "finds the min and max of a column" $ withUsersTable \conn -> do
622− _ <- conn # run do
623− usersTable # createMany
624− [ { name: "Alice", email: "a@t.com", role: "admin" }
625− , { name: "Bob", email: "b@t.com", role: "user" }
626− , { name: "Carol", email: "c@t.com", role: "user" }
627− ]
628− minName <- conn # run do usersTable # minOf @"name"
398+ it "finds the min and max of a column" $ withUsersTable do
399+ _ <- usersTable # createMany
400+ [ { name: "Alice", email: "a@t.com", role: "admin" }
401+ , { name: "Bob", email: "b@t.com", role: "user" }
402+ , { name: "Carol", email: "c@t.com", role: "user" }
403+ ]
404+ minName <- usersTable # minOf @"name"
629405 minName `shouldEqual` (Just "Alice")
630− maxName <- conn # run do usersTable # maxOf @"name"
406+ maxName <- usersTable # maxOf @"name"
631407 maxName `shouldEqual` (Just "Carol")
632− noMin <- conn # run do usersTable # minOfWhere @"name" { role: "nobody" }
408+ noMin <- usersTable # minOfWhere @"name" { role: "nobody" }
633409 isNothing noMin `shouldEqual` true
634410
635− it "lists distinct values of a column" $ withUsersTable \conn -> do
636− _ <- conn # run do
637− usersTable # createMany
638− [ { name: "Alice", email: "a@t.com", role: "admin" }
639− , { name: "Bob", email: "b@t.com", role: "user" }
640− , { name: "Carol", email: "c@t.com", role: "user" }
641− ]
642− roles <- conn # run do usersTable # findDistinct @"role"
411+ it "lists distinct values of a column" $ withUsersTable do
412+ _ <- usersTable # createMany
413+ [ { name: "Alice", email: "a@t.com", role: "admin" }
414+ , { name: "Bob", email: "b@t.com", role: "user" }
415+ , { name: "Carol", email: "c@t.com", role: "user" }
416+ ]
417+ roles <- usersTable # findDistinct @"role"
643418 Array.sort roles `shouldEqual` [ "admin", "user" ]
644− activeRoles <- conn # run do usersTable # findDistinctWhere @"role" { active: true }
419+ activeRoles <- usersTable # findDistinctWhere @"role" { active: true }
645420 Array.sort activeRoles `shouldEqual` [ "admin", "user" ]
646421
647422 describe "Selecting specific columns" do
648− it "fetches only the columns you need" $ withUsersTable \conn -> do
649− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
650− _ <- conn # run do usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
651− _ <- conn # run do usersTable # create { name: "Carol", email: "c@t.com", role: "user" }
652− rows <- conn # run do usersTable # selectFrom @"name, email"
423+ it "fetches only the columns you need" $ withUsersTable do
424+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
425+ _ <- usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
426+ _ <- usersTable # create { name: "Carol", email: "c@t.com", role: "user" }
427+ rows <- usersTable # selectFrom @"name, email"
653428 Array.length rows `shouldEqual` 3
654429 let
655430 first = case Array.head rows of
@@ -657,31 +432,31 @@ spec = describe "ClientOm" do
657432 Nothing -> { name: "", email: "" }
658433 first.name `shouldEqual` "Alice"
659434 first.email `shouldEqual` "a@t.com"
660− filtered <- conn # run do usersTable # selectFromWhere @"name" { role: "user" }
435+ filtered <- usersTable # selectFromWhere @"name" { role: "user" }
661436 Array.length filtered `shouldEqual` 2
662437 Array.sort (map _.name filtered) `shouldEqual` [ "Bob", "Carol" ]
663438
664− it "fetches one row with specific columns and a filter" $ withUsersTable \conn -> do
665− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
666− _ <- conn # run do usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
667− found <- conn # run do usersTable # selectOneFrom @"name, role"
439+ it "fetches one row with specific columns and a filter" $ withUsersTable do
440+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
441+ _ <- usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
442+ found <- usersTable # selectOneFrom @"name, role"
668443 r <- shouldBeJust found
669444 r.name `shouldEqual` "Alice"
670445 r.role `shouldEqual` "admin"
671− byName <- conn # run do usersTable # selectOneFromWhere @"email" { name: "Bob" }
446+ byName <- usersTable # selectOneFromWhere @"email" { name: "Bob" }
672447 r2 <- shouldBeJust byName
673448 r2.email `shouldEqual` "b@t.com"
674− missing <- conn # run do usersTable # selectOneFromWhere @"email" { name: "Nobody" }
449+ missing <- usersTable # selectOneFromWhere @"email" { name: "Nobody" }
675450 isNothing missing `shouldEqual` true
676451
677452 describe "Relationships" do
678− it "joins a child table to its parent (flat)" $ withPostsTable \conn -> do
679− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
680− _ <- conn # run do usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
681− _ <- conn # run do postsTable # create { title: "First Post", body: "Hello", user_id: 1 }
682− _ <- conn # run do postsTable # create { title: "Second Post", body: "World", user_id: 1 }
683− _ <- conn # run do postsTable # create { title: "Bob's Post", body: "Hi", user_id: 2 }
684− rows <- conn # run do findWith postsTable usersTable
453+ it "joins a child table to its parent (flat)" $ withPostsTable do
454+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
455+ _ <- usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
456+ _ <- postsTable # create { title: "First Post", body: "Hello", user_id: 1 }
457+ _ <- postsTable # create { title: "Second Post", body: "World", user_id: 1 }
458+ _ <- postsTable # create { title: "Bob's Post", body: "Hi", user_id: 2 }
459+ rows <- findWith postsTable usersTable
685460 Array.length rows `shouldEqual` 3
686461 let
687462 r = case Array.head rows of
@@ -690,20 +465,20 @@ spec = describe "ClientOm" do
690465 r.test_om_posts.title `shouldEqual` "First Post"
691466 r.test_om_users.name `shouldEqual` "Alice"
692467
693− it "finds a joined record by primary key" $ withPostsTable \conn -> do
694− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
695− _ <- conn # run do postsTable # create { title: "My Post", body: "Content", user_id: 1 }
696− found <- conn # run do findByIdWith 1 postsTable usersTable
468+ it "finds a joined record by primary key" $ withPostsTable do
469+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
470+ _ <- postsTable # create { title: "My Post", body: "Content", user_id: 1 }
471+ found <- findByIdWith 1 postsTable usersTable
697472 r <- shouldBeJust found
698473 r.test_om_posts.title `shouldEqual` "My Post"
699474 r.test_om_users.name `shouldEqual` "Alice"
700475
701− it "joins with a filter" $ withPostsTable \conn -> do
702− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
703− _ <- conn # run do usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
704− _ <- conn # run do postsTable # create { title: "Post A", body: "A", user_id: 1 }
705− _ <- conn # run do postsTable # create { title: "Post B", body: "B", user_id: 2 }
706− rows <- conn # run do findWithWhere { user_id: 1 } postsTable usersTable
476+ it "joins with a filter" $ withPostsTable do
477+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
478+ _ <- usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
479+ _ <- postsTable # create { title: "Post A", body: "A", user_id: 1 }
480+ _ <- postsTable # create { title: "Post B", body: "B", user_id: 2 }
481+ rows <- findWithWhere { user_id: 1 } postsTable usersTable
707482 Array.length rows `shouldEqual` 1
708483 let
709484 r = case Array.head rows of
@@ -711,12 +486,12 @@ spec = describe "ClientOm" do
711486 Nothing -> { test_om_posts: { id: 0, title: "", body: "", user_id: 0 }, test_om_users: { id: 0, name: "", email: "", active: false, role: "" } }
712487 r.test_om_posts.title `shouldEqual` "Post A"
713488
714− it "nests children under their parent" $ withPostsTable \conn -> do
715− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
716− _ <- conn # run do usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
717− _ <- conn # run do postsTable # create { title: "Post 1", body: "A", user_id: 1 }
718− _ <- conn # run do postsTable # create { title: "Post 2", body: "B", user_id: 1 }
719− rows <- conn # run do findIncluding @"posts" usersTable postsTable
489+ it "nests children under their parent" $ withPostsTable do
490+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
491+ _ <- usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
492+ _ <- postsTable # create { title: "Post 1", body: "A", user_id: 1 }
493+ _ <- postsTable # create { title: "Post 2", body: "B", user_id: 1 }
494+ rows <- findIncluding @"posts" usersTable postsTable
720495 Array.length rows `shouldEqual` 2
721496 let
722497 alice = case Array.head rows of
@@ -725,22 +500,22 @@ spec = describe "ClientOm" do
725500 alice.name `shouldEqual` "Alice"
726501 Array.length alice.posts `shouldEqual` 2
727502
728− it "nests children for a single parent by id" $ withPostsTable \conn -> do
729− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
730− _ <- conn # run do postsTable # create { title: "Post 1", body: "A", user_id: 1 }
731− _ <- conn # run do postsTable # create { title: "Post 2", body: "B", user_id: 1 }
732− found <- conn # run do findByIdIncluding @"posts" 1 usersTable postsTable
503+ it "nests children for a single parent by id" $ withPostsTable do
504+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
505+ _ <- postsTable # create { title: "Post 1", body: "A", user_id: 1 }
506+ _ <- postsTable # create { title: "Post 2", body: "B", user_id: 1 }
507+ found <- findByIdIncluding @"posts" 1 usersTable postsTable
733508 r <- shouldBeJust found
734509 r.name `shouldEqual` "Alice"
735510 Array.length r.posts `shouldEqual` 2
736511
737− it "uses the join combinator to compose tables" $ withPostsTable \conn -> do
738− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
739− _ <- conn # run do usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
740− _ <- conn # run do postsTable # create { title: "Post 1", body: "A", user_id: 1 }
741− _ <- conn # run do postsTable # create { title: "Post 2", body: "B", user_id: 1 }
742− _ <- conn # run do postsTable # create { title: "Post 3", body: "C", user_id: 2 }
743− rows <- conn # run do (usersTable # join postsTable) # findAll
512+ it "uses the join combinator to compose tables" $ withPostsTable do
513+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
514+ _ <- usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
515+ _ <- postsTable # create { title: "Post 1", body: "A", user_id: 1 }
516+ _ <- postsTable # create { title: "Post 2", body: "B", user_id: 1 }
517+ _ <- postsTable # create { title: "Post 3", body: "C", user_id: 2 }
518+ rows <- (usersTable # join postsTable) # findAll
744519 Array.length rows `shouldEqual` 2
745520 let
746521 alice = case Array.head rows of
@@ -749,12 +524,12 @@ spec = describe "ClientOm" do
749524 alice.name `shouldEqual` "Alice"
750525 Array.length alice.test_om_posts `shouldEqual` 2
751526
752− it "joins multiple child tables at once" $ withPostsAndCommentsTable \conn -> do
753− _ <- conn # run do usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
754− _ <- conn # run do postsTable # create { title: "Post 1", body: "A", user_id: 1 }
755− _ <- conn # run do postsTable # create { title: "Post 2", body: "B", user_id: 1 }
756− _ <- conn # run do commentsTable # create { text: "Nice!", user_id: 1 }
757− rows <- conn # run do (usersTable # join postsTable # join commentsTable) # findAll
527+ it "joins multiple child tables at once" $ withPostsAndCommentsTable do
528+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
529+ _ <- postsTable # create { title: "Post 1", body: "A", user_id: 1 }
530+ _ <- postsTable # create { title: "Post 2", body: "B", user_id: 1 }
531+ _ <- commentsTable # create { text: "Nice!", user_id: 1 }
532+ rows <- (usersTable # join postsTable # join commentsTable) # findAll
758533 Array.length rows `shouldEqual` 1
759534 let
760535 r = case Array.head rows of
@@ -765,27 +540,239 @@ spec = describe "ClientOm" do
765540 Array.length r.test_om_comments `shouldEqual` 1
766541
767542 describe "Transactions" do
768− it "commits multiple inserts atomically" $ withUsersTable \conn -> do
769− conn # run do
770− withTransaction do
771− _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
772− _ <- usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
773− pure unit
774− n <- conn # runNoErr do usersTable # count
543+ it "commits multiple inserts atomically" $ withUsersTable do
544+ transactionally do
545+ _ <- usersTable # create { name: "Alice", email: "a@t.com", role: "admin" }
546+ _ <- usersTable # create { name: "Bob", email: "b@t.com", role: "user" }
547+ pure unit
548+ n <- usersTable # count
775549 n `shouldEqual` 2
776550
777551 describe "UUIDs and newtypes" do
778− it "round-trips a UUID primary key through create/find/exists" $ withUuidTable \conn -> do
779− created <- conn # run do uuidTable # create { label: "first" }
552+ it "round-trips a UUID primary key through create/find/exists" $ withUuidTable do
553+ created <- uuidTable # create { label: "first" }
780554 row <- shouldBeJust created
781555 row.label `shouldEqual` "first"
782− found <- conn # run do uuidTable # findById row.id
556+ found <- uuidTable # findById row.id
783557 r2 <- shouldBeJust found
784558 r2.label `shouldEqual` "first"
785− byCol <- conn # run do uuidTable # findBy @"label" "first"
559+ byCol <- uuidTable # findBy @"label" "first"
786560 Array.length byCol `shouldEqual` 1
787− byId <- conn # run do uuidTable # findOneBy @"id" row.id
561+ byId <- uuidTable # findOneBy @"id" row.id
788562 r3 <- shouldBeJust byId
789563 r3.label `shouldEqual` "first"
790− yes <- conn # runNoErr do uuidTable # exists row.id
564+ yes <- uuidTable # exists row.id
791565 yes `shouldEqual` true
566+
567+-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
568+-- Test schemas
569+-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
570+
571+type UsersTable = Table "test_om_users"
572+ ( id :: PrimaryKey (AutoIncrement Int)
573+ , name :: String
574+ , email :: Unique String
575+ , active :: Default "true" Boolean
576+ , role :: String
577+ )
578+
579+usersTable :: Proxy UsersTable
580+usersTable = Proxy
581+
582+type SettingsTable = Table "test_om_settings"
583+ ( key :: PrimaryKey String
584+ , value :: String
585+ )
586+
587+settingsTable :: Proxy SettingsTable
588+settingsTable = Proxy
589+
590+type UuidTable = Table "test_om_uuid_things"
591+ ( id :: PGUUID # PrimaryKey # Default "gen_random_uuid()"
592+ , label :: String
593+ )
594+
595+uuidTable :: Proxy UuidTable
596+uuidTable = Proxy
597+
598+type PostsTable = Table "test_om_posts"
599+ ( id :: PrimaryKey (AutoIncrement Int)
600+ , title :: String
601+ , body :: String
602+ , user_id :: ForeignKey "test_om_users" References "id" Int
603+ )
604+
605+postsTable :: Proxy PostsTable
606+postsTable = Proxy
607+
608+type CommentsTable = Table "test_om_comments"
609+ ( id :: PrimaryKey (AutoIncrement Int)
610+ , text :: String
611+ , user_id :: ForeignKey "test_om_users" References "id" Int
612+ )
613+
614+commentsTable :: Proxy CommentsTable
615+commentsTable = Proxy
616+
617+-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
618+-- Helpers
619+-- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
620+
621+type TestOm = Om.Om { postgresConnection :: PG.Connection } (parseError :: MultipleErrors) Unit
622+
623+shouldEqual :: forall m t. MonadEffect m => Show t => Eq t => t -> t -> m Unit
624+shouldEqual v1 v2 = when (v1 /= v2) $ liftEffect $ Exception.throwException $ Exception.error $ show v1 <> " ≠ " <> show v2
625+
626+shouldBeJust :: forall m a. MonadEffect m => Maybe a -> m a
627+shouldBeJust (Just a) = pure a
628+shouldBeJust Nothing = liftEffect $ Exception.throwException $ Exception.error "Expected Just but got Nothing"
629+
630+testConnection :: Aff PG.Connection
631+testConnection =
632+ PG.postgres
633+ { host: PG.PostgresHost "localhost"
634+ , port: PG.PostgresPort 5433
635+ , database: PG.PostgresDatabase "test_playground"
636+ , username: PG.PostgresUsername "postgres"
637+ , password: PG.PostgresPassword "postgres"
638+ } # liftEffect
639+
640+runOm :: TestOm -> Aff Unit
641+runOm om = do
642+ conn <- testConnection
643+ Om.runOm { postgresConnection: conn }
644+ { exception: \e -> throwError e
645+ , parseError: \errs -> throwError (Exception.error ("Parse error: " <> show errs))
646+ }
647+ om
648+ PG.end conn
649+
650+withUsersTable :: TestOm -> Aff Unit
651+withUsersTable test = do
652+ conn <- testConnection
653+ _ <- PG.executeSimple createDDL conn
654+ Om.runOm { postgresConnection: conn }
655+ { exception: \e -> throwError e
656+ , parseError: \errs -> throwError (Exception.error ("Parse error: " <> show errs))
657+ }
658+ test
659+ _ <- PG.executeSimple dropDDL conn
660+ PG.end conn
661+ where
662+ dropDDL = PG.SQL "DROP TABLE IF EXISTS test_om_users CASCADE"
663+ createDDL = PG.SQL
664+ """DROP TABLE IF EXISTS test_om_users CASCADE;
665+ CREATE TABLE test_om_users (
666+ id SERIAL PRIMARY KEY,
667+ name TEXT NOT NULL,
668+ email TEXT UNIQUE NOT NULL,
669+ active BOOLEAN DEFAULT true NOT NULL,
670+ role TEXT NOT NULL
671+ )"""
672+
673+withSettingsTable :: TestOm -> Aff Unit
674+withSettingsTable test = do
675+ conn <- testConnection
676+ _ <- PG.executeSimple createDDL conn
677+ Om.runOm { postgresConnection: conn }
678+ { exception: \e -> throwError e
679+ , parseError: \errs -> throwError (Exception.error ("Parse error: " <> show errs))
680+ }
681+ test
682+ _ <- PG.executeSimple dropDDL conn
683+ PG.end conn
684+ where
685+ dropDDL = PG.SQL "DROP TABLE IF EXISTS test_om_settings CASCADE"
686+ createDDL = PG.SQL
687+ """DROP TABLE IF EXISTS test_om_settings CASCADE;
688+ CREATE TABLE test_om_settings (
689+ key TEXT PRIMARY KEY,
690+ value TEXT NOT NULL
691+ )"""
692+
693+withUuidTable :: TestOm -> Aff Unit
694+withUuidTable test = do
695+ conn <- testConnection
696+ _ <- PG.executeSimple createDDL conn
697+ Om.runOm { postgresConnection: conn }
698+ { exception: \e -> throwError e
699+ , parseError: \errs -> throwError (Exception.error ("Parse error: " <> show errs))
700+ }
701+ test
702+ _ <- PG.executeSimple dropDDL conn
703+ PG.end conn
704+ where
705+ dropDDL = PG.SQL "DROP TABLE IF EXISTS test_om_uuid_things CASCADE"
706+ createDDL = PG.SQL
707+ """DROP TABLE IF EXISTS test_om_uuid_things CASCADE;
708+ CREATE TABLE test_om_uuid_things (
709+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
710+ label TEXT NOT NULL
711+ )"""
712+
713+withPostsTable :: TestOm -> Aff Unit
714+withPostsTable test = do
715+ conn <- testConnection
716+ _ <- PG.executeSimple createDDL conn
717+ Om.runOm { postgresConnection: conn }
718+ { exception: \e -> throwError e
719+ , parseError: \errs -> throwError (Exception.error ("Parse error: " <> show errs))
720+ }
721+ test
722+ _ <- PG.executeSimple (PG.SQL "DROP TABLE IF EXISTS test_om_posts CASCADE") conn
723+ _ <- PG.executeSimple (PG.SQL "DROP TABLE IF EXISTS test_om_users CASCADE") conn
724+ PG.end conn
725+ where
726+ createDDL = PG.SQL
727+ """DROP TABLE IF EXISTS test_om_posts CASCADE;
728+ DROP TABLE IF EXISTS test_om_users CASCADE;
729+ CREATE TABLE test_om_users (
730+ id SERIAL PRIMARY KEY,
731+ name TEXT NOT NULL,
732+ email TEXT UNIQUE NOT NULL,
733+ active BOOLEAN DEFAULT true NOT NULL,
734+ role TEXT NOT NULL
735+ );
736+ CREATE TABLE test_om_posts (
737+ id SERIAL PRIMARY KEY,
738+ title TEXT NOT NULL,
739+ body TEXT NOT NULL,
740+ user_id INTEGER NOT NULL REFERENCES test_om_users(id)
741+ )"""
742+
743+withPostsAndCommentsTable :: TestOm -> Aff Unit
744+withPostsAndCommentsTable test = do
745+ conn <- testConnection
746+ _ <- PG.executeSimple createDDL conn
747+ Om.runOm { postgresConnection: conn }
748+ { exception: \e -> throwError e
749+ , parseError: \errs -> throwError (Exception.error ("Parse error: " <> show errs))
750+ }
751+ test
752+ _ <- PG.executeSimple (PG.SQL "DROP TABLE IF EXISTS test_om_comments CASCADE") conn
753+ _ <- PG.executeSimple (PG.SQL "DROP TABLE IF EXISTS test_om_posts CASCADE") conn
754+ _ <- PG.executeSimple (PG.SQL "DROP TABLE IF EXISTS test_om_users CASCADE") conn
755+ PG.end conn
756+ where
757+ createDDL = PG.SQL
758+ """DROP TABLE IF EXISTS test_om_comments CASCADE;
759+ DROP TABLE IF EXISTS test_om_posts CASCADE;
760+ DROP TABLE IF EXISTS test_om_users CASCADE;
761+ CREATE TABLE test_om_users (
762+ id SERIAL PRIMARY KEY,
763+ name TEXT NOT NULL,
764+ email TEXT UNIQUE NOT NULL,
765+ active BOOLEAN DEFAULT true NOT NULL,
766+ role TEXT NOT NULL
767+ );
768+ CREATE TABLE test_om_posts (
769+ id SERIAL PRIMARY KEY,
770+ title TEXT NOT NULL,
771+ body TEXT NOT NULL,
772+ user_id INTEGER NOT NULL REFERENCES test_om_users(id)
773+ );
774+ CREATE TABLE test_om_comments (
775+ id SERIAL PRIMARY KEY,
776+ text TEXT NOT NULL,
777+ user_id INTEGER NOT NULL REFERENCES test_om_users(id)
778+ )"""