@effect-app/infra 4.0.0-beta.81 → 4.0.0-beta.83

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,553 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import type Sqlite from "better-sqlite3"
3
+ import BetterSqlite from "better-sqlite3"
4
+ import { describe, expect, it } from "vitest"
5
+ import { buildWhereSQLQuery, pgDialect, sqliteDialect } from "../src/Store/SQL/query.js"
6
+
7
+ const query = (db: Sqlite.Database, sql: string, params: unknown[] = []) =>
8
+ db.prepare(sql).all(...params as any[]) as any[]
9
+
10
+ // --- Query builder unit tests ---
11
+
12
+ describe("SQL query builder (SQLite dialect)", () => {
13
+ it("where eq string", () => {
14
+ const result = buildWhereSQLQuery(
15
+ sqliteDialect,
16
+ "id",
17
+ [{ t: "where", path: "name", op: "eq", value: "John" }],
18
+ "users",
19
+ {}
20
+ )
21
+ expect(result.sql).toContain("json_extract(data, '$.name') = ?")
22
+ expect(result.params).toContain("John")
23
+ })
24
+
25
+ it("where eq number", () => {
26
+ const result = buildWhereSQLQuery(
27
+ sqliteDialect,
28
+ "id",
29
+ [{ t: "where", path: "age", op: "eq", value: 25 as any }],
30
+ "users",
31
+ {}
32
+ )
33
+ expect(result.sql).toContain("json_extract(data, '$.age') = ?")
34
+ expect(result.params).toContain(25)
35
+ })
36
+
37
+ it("where gt", () => {
38
+ const result = buildWhereSQLQuery(
39
+ sqliteDialect,
40
+ "id",
41
+ [{ t: "where", path: "age", op: "gt", value: 18 as any }],
42
+ "users",
43
+ {}
44
+ )
45
+ expect(result.sql).toContain("json_extract(data, '$.age') > ?")
46
+ expect(result.params).toContain(18)
47
+ })
48
+
49
+ it("where or", () => {
50
+ const result = buildWhereSQLQuery(
51
+ sqliteDialect,
52
+ "id",
53
+ [
54
+ { t: "where", path: "name", op: "eq", value: "Alice" },
55
+ { t: "or", path: "name", op: "eq", value: "Bob" }
56
+ ],
57
+ "users",
58
+ {}
59
+ )
60
+ expect(result.sql).toContain("= ?")
61
+ expect(result.sql).toContain("OR")
62
+ expect(result.params).toEqual(expect.arrayContaining(["Alice", "Bob"]))
63
+ })
64
+
65
+ it("where and", () => {
66
+ const result = buildWhereSQLQuery(
67
+ sqliteDialect,
68
+ "id",
69
+ [
70
+ { t: "where", path: "name", op: "eq", value: "Alice" },
71
+ { t: "and", path: "age", op: "gt", value: 18 as any }
72
+ ],
73
+ "users",
74
+ {}
75
+ )
76
+ expect(result.sql).toContain("AND")
77
+ expect(result.params).toEqual(expect.arrayContaining(["Alice", 18]))
78
+ })
79
+
80
+ it("where in", () => {
81
+ const result = buildWhereSQLQuery(
82
+ sqliteDialect,
83
+ "id",
84
+ [{ t: "where", path: "id", op: "in", value: ["a", "b", "c"] as any }],
85
+ "users",
86
+ {}
87
+ )
88
+ expect(result.sql).toContain("id IN (?, ?, ?)")
89
+ expect(result.params).toEqual(expect.arrayContaining(["a", "b", "c"]))
90
+ })
91
+
92
+ it("where null", () => {
93
+ const result = buildWhereSQLQuery(
94
+ sqliteDialect,
95
+ "id",
96
+ [{ t: "where", path: "status", op: "eq", value: null as any }],
97
+ "users",
98
+ {}
99
+ )
100
+ expect(result.sql).toContain("IS NULL")
101
+ })
102
+
103
+ it("where neq null", () => {
104
+ const result = buildWhereSQLQuery(
105
+ sqliteDialect,
106
+ "id",
107
+ [{ t: "where", path: "status", op: "neq", value: null as any }],
108
+ "users",
109
+ {}
110
+ )
111
+ expect(result.sql).toContain("IS NOT NULL")
112
+ })
113
+
114
+ it("where contains", () => {
115
+ const result = buildWhereSQLQuery(
116
+ sqliteDialect,
117
+ "id",
118
+ [{ t: "where", path: "name", op: "contains", value: "oh" }],
119
+ "users",
120
+ {}
121
+ )
122
+ expect(result.sql).toContain("LIKE")
123
+ expect(result.sql).toContain("LOWER")
124
+ expect(result.params).toContain("%oh%")
125
+ })
126
+
127
+ it("where startsWith", () => {
128
+ const result = buildWhereSQLQuery(
129
+ sqliteDialect,
130
+ "id",
131
+ [{ t: "where", path: "name", op: "startsWith", value: "Jo" }],
132
+ "users",
133
+ {}
134
+ )
135
+ expect(result.sql).toContain("LIKE")
136
+ expect(result.params).toContain("Jo%")
137
+ })
138
+
139
+ it("where endsWith", () => {
140
+ const result = buildWhereSQLQuery(
141
+ sqliteDialect,
142
+ "id",
143
+ [{ t: "where", path: "name", op: "endsWith", value: "hn" }],
144
+ "users",
145
+ {}
146
+ )
147
+ expect(result.sql).toContain("LIKE")
148
+ expect(result.params).toContain("%hn")
149
+ })
150
+
151
+ it("where includes (array contains)", () => {
152
+ const result = buildWhereSQLQuery(
153
+ sqliteDialect,
154
+ "id",
155
+ [{ t: "where", path: "tags", op: "includes", value: "admin" }],
156
+ "users",
157
+ {}
158
+ )
159
+ expect(result.sql).toContain("json_each")
160
+ expect(result.sql).toContain("value = ?")
161
+ expect(result.params).toContain("admin")
162
+ })
163
+
164
+ it("where includes-any (array contains any)", () => {
165
+ const result = buildWhereSQLQuery(
166
+ sqliteDialect,
167
+ "id",
168
+ [{ t: "where", path: "tags", op: "includes-any", value: ["admin", "user"] as any }],
169
+ "users",
170
+ {}
171
+ )
172
+ expect(result.sql).toContain("json_each")
173
+ expect(result.sql).toContain("IN")
174
+ })
175
+
176
+ it("nested scopes", () => {
177
+ const result = buildWhereSQLQuery(
178
+ sqliteDialect,
179
+ "id",
180
+ [
181
+ { t: "where", path: "a", op: "eq", value: "1" },
182
+ {
183
+ t: "or-scope",
184
+ result: [
185
+ { t: "where", path: "b", op: "eq", value: "2" },
186
+ { t: "and", path: "c", op: "eq", value: "3" }
187
+ ],
188
+ relation: "some" as const
189
+ }
190
+ ],
191
+ "test",
192
+ {}
193
+ )
194
+ expect(result.sql).toContain("OR (")
195
+ expect(result.sql).toContain("AND")
196
+ expect(result.params).toEqual(expect.arrayContaining(["1", "2", "3"]))
197
+ })
198
+
199
+ it("id key maps to id column", () => {
200
+ const result = buildWhereSQLQuery(
201
+ sqliteDialect,
202
+ "myId",
203
+ [{ t: "where", path: "myId", op: "eq", value: "123" }],
204
+ "users",
205
+ {}
206
+ )
207
+ expect(result.sql).toContain("id = ?")
208
+ expect(result.sql).not.toContain("json_extract")
209
+ expect(result.params).toContain("123")
210
+ })
211
+
212
+ it("order + limit + skip", () => {
213
+ const result = buildWhereSQLQuery(
214
+ sqliteDialect,
215
+ "id",
216
+ [],
217
+ "users",
218
+ {},
219
+ undefined,
220
+ [{ key: "name", direction: "ASC" }] as any,
221
+ 5,
222
+ 10
223
+ )
224
+ expect(result.sql).toContain("ORDER BY")
225
+ expect(result.sql).toContain("ASC")
226
+ expect(result.sql).toContain("LIMIT")
227
+ expect(result.sql).toContain("OFFSET")
228
+ })
229
+ })
230
+
231
+ describe("SQL query builder (PostgreSQL dialect)", () => {
232
+ it("where eq string uses ->> operator", () => {
233
+ const result = buildWhereSQLQuery(
234
+ pgDialect,
235
+ "id",
236
+ [{ t: "where", path: "name", op: "eq", value: "John" }],
237
+ "users",
238
+ {}
239
+ )
240
+ expect(result.sql).toContain("data->>'name'")
241
+ expect(result.sql).toContain("$1")
242
+ expect(result.params).toContain("John")
243
+ })
244
+
245
+ it("where contains uses ILIKE", () => {
246
+ const result = buildWhereSQLQuery(
247
+ pgDialect,
248
+ "id",
249
+ [{ t: "where", path: "name", op: "contains", value: "oh" }],
250
+ "users",
251
+ {}
252
+ )
253
+ expect(result.sql).toContain("ILIKE")
254
+ expect(result.params).toContain("%oh%")
255
+ })
256
+
257
+ it("where in uses $N placeholders", () => {
258
+ const result = buildWhereSQLQuery(
259
+ pgDialect,
260
+ "id",
261
+ [{ t: "where", path: "status", op: "in", value: ["active", "pending"] as any }],
262
+ "users",
263
+ {}
264
+ )
265
+ expect(result.sql).toContain("$1")
266
+ expect(result.sql).toContain("$2")
267
+ expect(result.params).toEqual(expect.arrayContaining(["active", "pending"]))
268
+ })
269
+
270
+ it("where includes uses @> jsonb operator", () => {
271
+ const result = buildWhereSQLQuery(
272
+ pgDialect,
273
+ "id",
274
+ [{ t: "where", path: "tags", op: "includes", value: "admin" }],
275
+ "users",
276
+ {}
277
+ )
278
+ expect(result.sql).toContain("@>")
279
+ expect(result.sql).toContain("jsonb")
280
+ })
281
+
282
+ it("nested path uses chained -> operators", () => {
283
+ const result = buildWhereSQLQuery(
284
+ pgDialect,
285
+ "id",
286
+ [{ t: "where", path: "address.city", op: "eq", value: "NYC" }],
287
+ "users",
288
+ {}
289
+ )
290
+ expect(result.sql).toContain("data->'address'->>'city'")
291
+ })
292
+ })
293
+
294
+ // --- Integration tests with in-memory SQLite (direct, no Effect SQL client) ---
295
+
296
+ describe("SQL Store (SQLite integration)", () => {
297
+ const withDb = (fn: (db: Sqlite.Database) => void) => {
298
+ const db = new BetterSqlite(":memory:")
299
+ db.pragma("journal_mode = WAL")
300
+ try {
301
+ fn(db)
302
+ } finally {
303
+ db.close()
304
+ }
305
+ }
306
+
307
+ it("creates table and seeds data", () =>
308
+ withDb((db) => {
309
+ db.exec(
310
+ `CREATE TABLE IF NOT EXISTS "test_items" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
311
+ )
312
+ db.prepare(`INSERT INTO "test_items" (id, _etag, data) VALUES (?, ?, ?)`)
313
+ .run("1", "etag1", JSON.stringify({ name: "Alice", age: 30 }))
314
+
315
+ const rows = db.prepare(`SELECT * FROM "test_items"`).all()
316
+ expect(rows.length).toBe(1)
317
+ expect((rows[0] as any).id).toBe("1")
318
+ }))
319
+
320
+ it("data column should not contain _etag or id", () =>
321
+ withDb((db) => {
322
+ db.exec(
323
+ `CREATE TABLE IF NOT EXISTS "test_clean" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
324
+ )
325
+ // Simulate what toRow now produces: data without id or _etag
326
+ const data = { name: "Alice", age: 30, tags: ["admin"] }
327
+ db.prepare(`INSERT INTO "test_clean" (id, _etag, data) VALUES (?, ?, ?)`)
328
+ .run("1", "etag1", JSON.stringify(data))
329
+
330
+ const row = db.prepare(`SELECT * FROM "test_clean" WHERE id = ?`).get("1") as any
331
+ const parsed = JSON.parse(row.data) as any
332
+ expect(parsed).not.toHaveProperty("id")
333
+ expect(parsed).not.toHaveProperty("_etag")
334
+ expect(parsed.name).toBe("Alice")
335
+ expect(parsed.age).toBe(30)
336
+ expect(parsed.tags).toEqual(["admin"])
337
+ // id and _etag come from their own columns
338
+ expect(row.id).toBe("1")
339
+ expect(row._etag).toBe("etag1")
340
+ }))
341
+
342
+ it("backward compat: rows with id/_etag in data still work with queries", () =>
343
+ withDb((db) => {
344
+ db.exec(
345
+ `CREATE TABLE IF NOT EXISTS "test_compat" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
346
+ )
347
+ // Old format: id and _etag inside data
348
+ db.prepare(`INSERT INTO "test_compat" (id, _etag, data) VALUES (?, ?, ?)`)
349
+ .run("1", "etag1", JSON.stringify({ id: "1", _etag: "old_etag", name: "Alice", age: 30 }))
350
+ // New format: id and _etag stripped from data
351
+ db.prepare(`INSERT INTO "test_compat" (id, _etag, data) VALUES (?, ?, ?)`)
352
+ .run("2", "etag2", JSON.stringify({ name: "Bob", age: 25 }))
353
+
354
+ // Both should be queryable by name
355
+ const q1 = buildWhereSQLQuery(
356
+ sqliteDialect, "id",
357
+ [{ t: "where", path: "name", op: "eq", value: "Alice" }],
358
+ "test_compat", {}
359
+ )
360
+ const r1 = query(db, q1.sql, q1.params)
361
+ expect(r1.length).toBe(1)
362
+ expect((r1[0] as any).id).toBe("1")
363
+
364
+ const q2 = buildWhereSQLQuery(
365
+ sqliteDialect, "id",
366
+ [{ t: "where", path: "name", op: "eq", value: "Bob" }],
367
+ "test_compat", {}
368
+ )
369
+ const r2 = query(db, q2.sql, q2.params)
370
+ expect(r2.length).toBe(1)
371
+ expect((r2[0] as any).id).toBe("2")
372
+
373
+ // Both queryable by id column
374
+ const q3 = buildWhereSQLQuery(
375
+ sqliteDialect, "id",
376
+ [{ t: "where", path: "id", op: "in", value: ["1", "2"] as any }],
377
+ "test_compat", {}
378
+ )
379
+ expect(query(db, q3.sql, q3.params).length).toBe(2)
380
+ }))
381
+
382
+ it("queries work when data does not contain id", () =>
383
+ withDb((db) => {
384
+ db.exec(
385
+ `CREATE TABLE IF NOT EXISTS "test_noid" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
386
+ )
387
+ const people = [
388
+ { name: "Alice", age: 30 },
389
+ { name: "Bob", age: 25 },
390
+ { name: "Charlie", age: 35 }
391
+ ]
392
+ const insert = db.prepare(
393
+ `INSERT INTO "test_noid" (id, _etag, data) VALUES (?, ?, ?)`
394
+ )
395
+ people.forEach((p, i) => insert.run(String(i + 1), `etag_${i + 1}`, JSON.stringify(p)))
396
+
397
+ // Filter by field in data
398
+ const q1 = buildWhereSQLQuery(
399
+ sqliteDialect, "id",
400
+ [{ t: "where", path: "age", op: "gt", value: 28 as any }],
401
+ "test_noid", {}
402
+ )
403
+ expect(query(db, q1.sql, q1.params).length).toBe(2) // Alice(30), Charlie(35)
404
+
405
+ // Filter by id column
406
+ const q2 = buildWhereSQLQuery(
407
+ sqliteDialect, "id",
408
+ [{ t: "where", path: "id", op: "eq", value: "2" }],
409
+ "test_noid", {}
410
+ )
411
+ const r2 = query(db, q2.sql, q2.params)
412
+ expect(r2.length).toBe(1)
413
+ expect((r2[0] as any).id).toBe("2")
414
+ expect((JSON.parse((r2[0] as any).data) as any).name).toBe("Bob")
415
+
416
+ // Order + limit still works
417
+ const q3 = buildWhereSQLQuery(
418
+ sqliteDialect, "id", [], "test_noid", {},
419
+ undefined,
420
+ [{ key: "age", direction: "ASC" }] as any,
421
+ undefined, 2
422
+ )
423
+ const r3 = query(db, q3.sql, q3.params)
424
+ expect(r3.length).toBe(2)
425
+ expect((JSON.parse((r3[0] as any).data) as any).name).toBe("Bob") // youngest first
426
+ }))
427
+
428
+ it("query builder generates valid SQL for SQLite", () =>
429
+ withDb((db) => {
430
+ db.exec(
431
+ `CREATE TABLE IF NOT EXISTS "test_people" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
432
+ )
433
+
434
+ const people = [
435
+ { id: "1", name: "Alice", age: 30, tags: ["admin", "user"] },
436
+ { id: "2", name: "Bob", age: 25, tags: ["user"] },
437
+ { id: "3", name: "Charlie", age: 35, tags: ["admin"] },
438
+ { id: "4", name: "Diana", age: 28, tags: ["user", "editor"] }
439
+ ]
440
+
441
+ const insert = db.prepare(
442
+ `INSERT INTO "test_people" (id, _etag, data) VALUES (?, ?, ?)`
443
+ )
444
+ for (const p of people) {
445
+ const { id, ...data } = p
446
+ insert.run(id, `etag_${id}`, JSON.stringify(data))
447
+ }
448
+
449
+ // Test eq
450
+ const q1 = buildWhereSQLQuery(
451
+ sqliteDialect, "id",
452
+ [{ t: "where", path: "name", op: "eq", value: "Alice" }],
453
+ "test_people", {}
454
+ )
455
+ expect(query(db, q1.sql, q1.params).length).toBe(1)
456
+ expect((JSON.parse((query(db, q1.sql, q1.params)[0] as any).data) as any).name).toBe("Alice")
457
+
458
+ // Test gt
459
+ const q2 = buildWhereSQLQuery(
460
+ sqliteDialect, "id",
461
+ [{ t: "where", path: "age", op: "gt", value: 28 as any }],
462
+ "test_people", {}
463
+ )
464
+ expect(query(db, q2.sql, q2.params).length).toBe(2)
465
+
466
+ // Test OR
467
+ const q3 = buildWhereSQLQuery(
468
+ sqliteDialect, "id",
469
+ [
470
+ { t: "where", path: "name", op: "eq", value: "Alice" },
471
+ { t: "or", path: "name", op: "eq", value: "Bob" }
472
+ ],
473
+ "test_people", {}
474
+ )
475
+ expect(query(db, q3.sql, q3.params).length).toBe(2)
476
+
477
+ // Test AND
478
+ const q4 = buildWhereSQLQuery(
479
+ sqliteDialect, "id",
480
+ [
481
+ { t: "where", path: "name", op: "eq", value: "Alice" },
482
+ { t: "and", path: "age", op: "gt", value: 25 as any }
483
+ ],
484
+ "test_people", {}
485
+ )
486
+ const r4 = query(db, q4.sql, q4.params)
487
+ expect(r4.length).toBe(1)
488
+ expect((JSON.parse((r4[0] as any).data) as any).name).toBe("Alice")
489
+
490
+ // Test IN
491
+ const q5 = buildWhereSQLQuery(
492
+ sqliteDialect, "id",
493
+ [{ t: "where", path: "id", op: "in", value: ["1", "3"] as any }],
494
+ "test_people", {}
495
+ )
496
+ expect(query(db, q5.sql, q5.params).length).toBe(2)
497
+
498
+ // Test contains (string)
499
+ const q6 = buildWhereSQLQuery(
500
+ sqliteDialect, "id",
501
+ [{ t: "where", path: "name", op: "contains", value: "li" }],
502
+ "test_people", {}
503
+ )
504
+ expect(query(db, q6.sql, q6.params).length).toBe(2) // Alice, Charlie
505
+
506
+ // Test startsWith
507
+ const q7 = buildWhereSQLQuery(
508
+ sqliteDialect, "id",
509
+ [{ t: "where", path: "name", op: "startsWith", value: "Al" }],
510
+ "test_people", {}
511
+ )
512
+ const r7 = query(db, q7.sql, q7.params)
513
+ expect(r7.length).toBe(1)
514
+ expect((JSON.parse((r7[0] as any).data) as any).name).toBe("Alice")
515
+
516
+ // Test includes (array)
517
+ const q8 = buildWhereSQLQuery(
518
+ sqliteDialect, "id",
519
+ [{ t: "where", path: "tags", op: "includes", value: "admin" }],
520
+ "test_people", {}
521
+ )
522
+ expect(query(db, q8.sql, q8.params).length).toBe(2) // Alice, Charlie
523
+
524
+ // Test nested scope: where name = Alice OR (age > 30 AND name contains 'ar')
525
+ const q9 = buildWhereSQLQuery(
526
+ sqliteDialect, "id",
527
+ [
528
+ { t: "where", path: "name", op: "eq", value: "Alice" },
529
+ {
530
+ t: "or-scope",
531
+ result: [
532
+ { t: "where", path: "age", op: "gt", value: 30 as any },
533
+ { t: "and", path: "name", op: "contains", value: "ar" }
534
+ ],
535
+ relation: "some"
536
+ }
537
+ ],
538
+ "test_people", {}
539
+ )
540
+ expect(query(db, q9.sql, q9.params).length).toBe(2) // Alice + Charlie
541
+
542
+ // Test order + limit
543
+ const q10 = buildWhereSQLQuery(
544
+ sqliteDialect, "id", [], "test_people", {},
545
+ undefined,
546
+ [{ key: "age", direction: "DESC" }] as any,
547
+ undefined, 2
548
+ )
549
+ const r10 = query(db, q10.sql, q10.params)
550
+ expect(r10.length).toBe(2)
551
+ expect((JSON.parse((r10[0] as any).data) as any).name).toBe("Charlie") // oldest first
552
+ }))
553
+ })