@effect-app/infra 4.0.0-beta.9 → 4.0.0-beta.91

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.
Files changed (176) hide show
  1. package/CHANGELOG.md +596 -0
  2. package/dist/CUPS.d.ts +3 -3
  3. package/dist/CUPS.d.ts.map +1 -1
  4. package/dist/CUPS.js +3 -3
  5. package/dist/Emailer/Sendgrid.js +1 -1
  6. package/dist/Emailer/service.d.ts +3 -3
  7. package/dist/Emailer/service.d.ts.map +1 -1
  8. package/dist/Emailer/service.js +3 -3
  9. package/dist/MainFiberSet.d.ts +2 -2
  10. package/dist/MainFiberSet.d.ts.map +1 -1
  11. package/dist/MainFiberSet.js +3 -3
  12. package/dist/Model/Repository/internal/internal.d.ts +3 -3
  13. package/dist/Model/Repository/internal/internal.d.ts.map +1 -1
  14. package/dist/Model/Repository/internal/internal.js +11 -7
  15. package/dist/Model/Repository/makeRepo.d.ts +2 -2
  16. package/dist/Model/Repository/makeRepo.d.ts.map +1 -1
  17. package/dist/Model/Repository/makeRepo.js +1 -1
  18. package/dist/Model/Repository/validation.d.ts +7 -6
  19. package/dist/Model/Repository/validation.d.ts.map +1 -1
  20. package/dist/Model/query/dsl.d.ts +9 -9
  21. package/dist/Operations.d.ts +2 -2
  22. package/dist/Operations.d.ts.map +1 -1
  23. package/dist/Operations.js +3 -3
  24. package/dist/OperationsRepo.d.ts +2 -2
  25. package/dist/OperationsRepo.d.ts.map +1 -1
  26. package/dist/OperationsRepo.js +3 -3
  27. package/dist/QueueMaker/SQLQueue.d.ts +3 -5
  28. package/dist/QueueMaker/SQLQueue.d.ts.map +1 -1
  29. package/dist/QueueMaker/SQLQueue.js +9 -7
  30. package/dist/QueueMaker/errors.d.ts +1 -1
  31. package/dist/QueueMaker/errors.d.ts.map +1 -1
  32. package/dist/QueueMaker/memQueue.d.ts.map +1 -1
  33. package/dist/QueueMaker/memQueue.js +10 -9
  34. package/dist/QueueMaker/sbqueue.d.ts.map +1 -1
  35. package/dist/QueueMaker/sbqueue.js +11 -9
  36. package/dist/RequestContext.d.ts +41 -21
  37. package/dist/RequestContext.d.ts.map +1 -1
  38. package/dist/RequestContext.js +5 -5
  39. package/dist/RequestFiberSet.d.ts +2 -2
  40. package/dist/RequestFiberSet.d.ts.map +1 -1
  41. package/dist/RequestFiberSet.js +5 -5
  42. package/dist/Store/ContextMapContainer.d.ts +14 -3
  43. package/dist/Store/ContextMapContainer.d.ts.map +1 -1
  44. package/dist/Store/ContextMapContainer.js +64 -3
  45. package/dist/Store/Cosmos.d.ts.map +1 -1
  46. package/dist/Store/Cosmos.js +91 -56
  47. package/dist/Store/Disk.d.ts.map +1 -1
  48. package/dist/Store/Disk.js +3 -4
  49. package/dist/Store/Memory.d.ts +2 -2
  50. package/dist/Store/Memory.d.ts.map +1 -1
  51. package/dist/Store/Memory.js +4 -4
  52. package/dist/Store/SQL/Pg.d.ts +4 -0
  53. package/dist/Store/SQL/Pg.d.ts.map +1 -0
  54. package/dist/Store/SQL/Pg.js +186 -0
  55. package/dist/Store/SQL/query.d.ts +36 -0
  56. package/dist/Store/SQL/query.d.ts.map +1 -0
  57. package/dist/Store/SQL/query.js +385 -0
  58. package/dist/Store/SQL.d.ts +11 -0
  59. package/dist/Store/SQL.d.ts.map +1 -0
  60. package/dist/Store/SQL.js +212 -0
  61. package/dist/Store/index.d.ts +1 -1
  62. package/dist/Store/index.d.ts.map +1 -1
  63. package/dist/Store/index.js +11 -1
  64. package/dist/Store/service.d.ts +8 -5
  65. package/dist/Store/service.d.ts.map +1 -1
  66. package/dist/Store/service.js +14 -6
  67. package/dist/adapters/SQL/Model.d.ts +2 -5
  68. package/dist/adapters/SQL/Model.d.ts.map +1 -1
  69. package/dist/adapters/SQL/Model.js +21 -13
  70. package/dist/adapters/ServiceBus.d.ts +6 -6
  71. package/dist/adapters/ServiceBus.d.ts.map +1 -1
  72. package/dist/adapters/ServiceBus.js +9 -9
  73. package/dist/adapters/cosmos-client.d.ts +2 -2
  74. package/dist/adapters/cosmos-client.d.ts.map +1 -1
  75. package/dist/adapters/cosmos-client.js +3 -3
  76. package/dist/adapters/logger.d.ts.map +1 -1
  77. package/dist/adapters/memQueue.d.ts +2 -2
  78. package/dist/adapters/memQueue.d.ts.map +1 -1
  79. package/dist/adapters/memQueue.js +3 -3
  80. package/dist/adapters/mongo-client.d.ts +2 -2
  81. package/dist/adapters/mongo-client.d.ts.map +1 -1
  82. package/dist/adapters/mongo-client.js +3 -3
  83. package/dist/adapters/redis-client.d.ts +3 -3
  84. package/dist/adapters/redis-client.d.ts.map +1 -1
  85. package/dist/adapters/redis-client.js +3 -3
  86. package/dist/api/ContextProvider.d.ts +6 -6
  87. package/dist/api/ContextProvider.d.ts.map +1 -1
  88. package/dist/api/ContextProvider.js +6 -6
  89. package/dist/api/internal/auth.d.ts +1 -1
  90. package/dist/api/internal/events.d.ts +2 -2
  91. package/dist/api/internal/events.d.ts.map +1 -1
  92. package/dist/api/internal/events.js +7 -5
  93. package/dist/api/layerUtils.d.ts +5 -5
  94. package/dist/api/layerUtils.d.ts.map +1 -1
  95. package/dist/api/layerUtils.js +5 -5
  96. package/dist/api/routing/middleware/RouterMiddleware.d.ts +3 -3
  97. package/dist/api/routing/middleware/RouterMiddleware.d.ts.map +1 -1
  98. package/dist/api/routing/middleware/middleware.d.ts +35 -1
  99. package/dist/api/routing/middleware/middleware.d.ts.map +1 -1
  100. package/dist/api/routing/middleware/middleware.js +39 -1
  101. package/dist/api/routing/schema/jwt.d.ts +1 -1
  102. package/dist/api/routing/schema/jwt.d.ts.map +1 -1
  103. package/dist/api/routing/schema/jwt.js +1 -1
  104. package/dist/api/routing.d.ts +1 -5
  105. package/dist/api/routing.d.ts.map +1 -1
  106. package/dist/api/routing.js +3 -2
  107. package/dist/api/setupRequest.d.ts +6 -3
  108. package/dist/api/setupRequest.d.ts.map +1 -1
  109. package/dist/api/setupRequest.js +11 -6
  110. package/dist/errorReporter.d.ts +1 -1
  111. package/dist/errorReporter.d.ts.map +1 -1
  112. package/dist/errorReporter.js +1 -1
  113. package/dist/fileUtil.js +1 -1
  114. package/dist/logger.d.ts.map +1 -1
  115. package/dist/rateLimit.js +1 -1
  116. package/examples/query.ts +29 -25
  117. package/package.json +32 -18
  118. package/src/CUPS.ts +2 -2
  119. package/src/Emailer/Sendgrid.ts +1 -1
  120. package/src/Emailer/service.ts +2 -2
  121. package/src/MainFiberSet.ts +2 -2
  122. package/src/Model/Repository/internal/internal.ts +11 -8
  123. package/src/Model/Repository/makeRepo.ts +2 -2
  124. package/src/Operations.ts +2 -2
  125. package/src/OperationsRepo.ts +2 -2
  126. package/src/QueueMaker/SQLQueue.ts +10 -10
  127. package/src/QueueMaker/memQueue.ts +41 -42
  128. package/src/QueueMaker/sbqueue.ts +65 -62
  129. package/src/RequestContext.ts +4 -4
  130. package/src/RequestFiberSet.ts +4 -4
  131. package/src/Store/ContextMapContainer.ts +98 -2
  132. package/src/Store/Cosmos.ts +273 -207
  133. package/src/Store/Disk.ts +2 -3
  134. package/src/Store/Memory.ts +4 -6
  135. package/src/Store/SQL/Pg.ts +328 -0
  136. package/src/Store/SQL/query.ts +430 -0
  137. package/src/Store/SQL.ts +357 -0
  138. package/src/Store/index.ts +10 -0
  139. package/src/Store/service.ts +16 -7
  140. package/src/adapters/SQL/Model.ts +76 -71
  141. package/src/adapters/ServiceBus.ts +8 -8
  142. package/src/adapters/cosmos-client.ts +2 -2
  143. package/src/adapters/memQueue.ts +2 -2
  144. package/src/adapters/mongo-client.ts +2 -2
  145. package/src/adapters/redis-client.ts +2 -2
  146. package/src/api/ContextProvider.ts +11 -11
  147. package/src/api/internal/events.ts +7 -6
  148. package/src/api/layerUtils.ts +8 -8
  149. package/src/api/routing/middleware/RouterMiddleware.ts +4 -4
  150. package/src/api/routing/middleware/middleware.ts +43 -0
  151. package/src/api/routing/schema/jwt.ts +2 -3
  152. package/src/api/routing.ts +7 -6
  153. package/src/api/setupRequest.ts +27 -7
  154. package/src/errorReporter.ts +1 -1
  155. package/src/fileUtil.ts +1 -1
  156. package/src/rateLimit.ts +2 -2
  157. package/test/contextProvider.test.ts +5 -5
  158. package/test/controller.test.ts +12 -9
  159. package/test/dist/contextProvider.test.d.ts.map +1 -1
  160. package/test/dist/controller.test.d.ts.map +1 -1
  161. package/test/dist/fixtures.d.ts +19 -9
  162. package/test/dist/fixtures.d.ts.map +1 -1
  163. package/test/dist/fixtures.js +11 -9
  164. package/test/dist/query.test.d.ts.map +1 -1
  165. package/test/dist/rawQuery.test.d.ts.map +1 -1
  166. package/test/dist/requires.test.d.ts.map +1 -1
  167. package/test/dist/rpc-multi-middleware.test.d.ts.map +1 -1
  168. package/test/dist/sql-store.test.d.ts.map +1 -0
  169. package/test/fixtures.ts +10 -8
  170. package/test/query.test.ts +160 -14
  171. package/test/rawQuery.test.ts +19 -17
  172. package/test/requires.test.ts +6 -5
  173. package/test/rpc-multi-middleware.test.ts +73 -4
  174. package/test/sql-store.test.ts +776 -0
  175. package/test/validateSample.test.ts +1 -1
  176. package/tsconfig.json +0 -1
@@ -0,0 +1,776 @@
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 { parseRow } from "../src/Store/SQL.js"
6
+ import { buildWhereSQLQuery, pgDialect, sqliteDialect } from "../src/Store/SQL/query.js"
7
+ import { makeETag } from "../src/Store/utils.js"
8
+
9
+ const query = (db: Sqlite.Database, sql: string, params: unknown[] = []) =>
10
+ db.prepare(sql).all(...params as any[]) as any[]
11
+
12
+ // --- Query builder unit tests ---
13
+
14
+ describe("SQL query builder (SQLite dialect)", () => {
15
+ it("where eq string", () => {
16
+ const result = buildWhereSQLQuery(
17
+ sqliteDialect,
18
+ "id",
19
+ [{ t: "where", path: "name", op: "eq", value: "John" }],
20
+ "users",
21
+ {}
22
+ )
23
+ expect(result.sql).toContain("json_extract(data, '$.name') = ?")
24
+ expect(result.params).toContain("John")
25
+ })
26
+
27
+ it("where eq number", () => {
28
+ const result = buildWhereSQLQuery(
29
+ sqliteDialect,
30
+ "id",
31
+ [{ t: "where", path: "age", op: "eq", value: 25 as any }],
32
+ "users",
33
+ {}
34
+ )
35
+ expect(result.sql).toContain("json_extract(data, '$.age') = ?")
36
+ expect(result.params).toContain(25)
37
+ })
38
+
39
+ it("where gt", () => {
40
+ const result = buildWhereSQLQuery(
41
+ sqliteDialect,
42
+ "id",
43
+ [{ t: "where", path: "age", op: "gt", value: 18 as any }],
44
+ "users",
45
+ {}
46
+ )
47
+ expect(result.sql).toContain("json_extract(data, '$.age') > ?")
48
+ expect(result.params).toContain(18)
49
+ })
50
+
51
+ it("where or", () => {
52
+ const result = buildWhereSQLQuery(
53
+ sqliteDialect,
54
+ "id",
55
+ [
56
+ { t: "where", path: "name", op: "eq", value: "Alice" },
57
+ { t: "or", path: "name", op: "eq", value: "Bob" }
58
+ ],
59
+ "users",
60
+ {}
61
+ )
62
+ expect(result.sql).toContain("= ?")
63
+ expect(result.sql).toContain("OR")
64
+ expect(result.params).toEqual(expect.arrayContaining(["Alice", "Bob"]))
65
+ })
66
+
67
+ it("where and", () => {
68
+ const result = buildWhereSQLQuery(
69
+ sqliteDialect,
70
+ "id",
71
+ [
72
+ { t: "where", path: "name", op: "eq", value: "Alice" },
73
+ { t: "and", path: "age", op: "gt", value: 18 as any }
74
+ ],
75
+ "users",
76
+ {}
77
+ )
78
+ expect(result.sql).toContain("AND")
79
+ expect(result.params).toEqual(expect.arrayContaining(["Alice", 18]))
80
+ })
81
+
82
+ it("where in", () => {
83
+ const result = buildWhereSQLQuery(
84
+ sqliteDialect,
85
+ "id",
86
+ [{ t: "where", path: "id", op: "in", value: ["a", "b", "c"] as any }],
87
+ "users",
88
+ {}
89
+ )
90
+ expect(result.sql).toContain("id IN (?, ?, ?)")
91
+ expect(result.params).toEqual(expect.arrayContaining(["a", "b", "c"]))
92
+ })
93
+
94
+ it("where null", () => {
95
+ const result = buildWhereSQLQuery(
96
+ sqliteDialect,
97
+ "id",
98
+ [{ t: "where", path: "status", op: "eq", value: null as any }],
99
+ "users",
100
+ {}
101
+ )
102
+ expect(result.sql).toContain("IS NULL")
103
+ })
104
+
105
+ it("where neq null", () => {
106
+ const result = buildWhereSQLQuery(
107
+ sqliteDialect,
108
+ "id",
109
+ [{ t: "where", path: "status", op: "neq", value: null as any }],
110
+ "users",
111
+ {}
112
+ )
113
+ expect(result.sql).toContain("IS NOT NULL")
114
+ })
115
+
116
+ it("where contains", () => {
117
+ const result = buildWhereSQLQuery(
118
+ sqliteDialect,
119
+ "id",
120
+ [{ t: "where", path: "name", op: "contains", value: "oh" }],
121
+ "users",
122
+ {}
123
+ )
124
+ expect(result.sql).toContain("LIKE")
125
+ expect(result.sql).toContain("LOWER")
126
+ expect(result.params).toContain("%oh%")
127
+ })
128
+
129
+ it("where startsWith", () => {
130
+ const result = buildWhereSQLQuery(
131
+ sqliteDialect,
132
+ "id",
133
+ [{ t: "where", path: "name", op: "startsWith", value: "Jo" }],
134
+ "users",
135
+ {}
136
+ )
137
+ expect(result.sql).toContain("LIKE")
138
+ expect(result.params).toContain("Jo%")
139
+ })
140
+
141
+ it("where endsWith", () => {
142
+ const result = buildWhereSQLQuery(
143
+ sqliteDialect,
144
+ "id",
145
+ [{ t: "where", path: "name", op: "endsWith", value: "hn" }],
146
+ "users",
147
+ {}
148
+ )
149
+ expect(result.sql).toContain("LIKE")
150
+ expect(result.params).toContain("%hn")
151
+ })
152
+
153
+ it("where includes (array contains)", () => {
154
+ const result = buildWhereSQLQuery(
155
+ sqliteDialect,
156
+ "id",
157
+ [{ t: "where", path: "tags", op: "includes", value: "admin" }],
158
+ "users",
159
+ {}
160
+ )
161
+ expect(result.sql).toContain("json_each")
162
+ expect(result.sql).toContain("value = ?")
163
+ expect(result.params).toContain("admin")
164
+ })
165
+
166
+ it("where includes-any (array contains any)", () => {
167
+ const result = buildWhereSQLQuery(
168
+ sqliteDialect,
169
+ "id",
170
+ [{ t: "where", path: "tags", op: "includes-any", value: ["admin", "user"] as any }],
171
+ "users",
172
+ {}
173
+ )
174
+ expect(result.sql).toContain("json_each")
175
+ expect(result.sql).toContain("IN")
176
+ })
177
+
178
+ it("nested scopes", () => {
179
+ const result = buildWhereSQLQuery(
180
+ sqliteDialect,
181
+ "id",
182
+ [
183
+ { t: "where", path: "a", op: "eq", value: "1" },
184
+ {
185
+ t: "or-scope",
186
+ result: [
187
+ { t: "where", path: "b", op: "eq", value: "2" },
188
+ { t: "and", path: "c", op: "eq", value: "3" }
189
+ ],
190
+ relation: "some" as const
191
+ }
192
+ ],
193
+ "test",
194
+ {}
195
+ )
196
+ expect(result.sql).toContain("OR (")
197
+ expect(result.sql).toContain("AND")
198
+ expect(result.params).toEqual(expect.arrayContaining(["1", "2", "3"]))
199
+ })
200
+
201
+ it("id key maps to id column", () => {
202
+ const result = buildWhereSQLQuery(
203
+ sqliteDialect,
204
+ "myId",
205
+ [{ t: "where", path: "myId", op: "eq", value: "123" }],
206
+ "users",
207
+ {}
208
+ )
209
+ expect(result.sql).toContain("id = ?")
210
+ expect(result.sql).not.toContain("json_extract")
211
+ expect(result.params).toContain("123")
212
+ })
213
+
214
+ it("order + limit + skip", () => {
215
+ const result = buildWhereSQLQuery(
216
+ sqliteDialect,
217
+ "id",
218
+ [],
219
+ "users",
220
+ {},
221
+ undefined,
222
+ [{ key: "name", direction: "ASC" }] as any,
223
+ 5,
224
+ 10
225
+ )
226
+ expect(result.sql).toContain("ORDER BY")
227
+ expect(result.sql).toContain("ASC")
228
+ expect(result.sql).toContain("LIMIT")
229
+ expect(result.sql).toContain("OFFSET")
230
+ })
231
+ })
232
+
233
+ describe("SQL query builder (PostgreSQL dialect)", () => {
234
+ it("where eq string uses ->> operator", () => {
235
+ const result = buildWhereSQLQuery(
236
+ pgDialect,
237
+ "id",
238
+ [{ t: "where", path: "name", op: "eq", value: "John" }],
239
+ "users",
240
+ {}
241
+ )
242
+ expect(result.sql).toContain("data->>'name'")
243
+ expect(result.sql).toContain("$1")
244
+ expect(result.params).toContain("John")
245
+ })
246
+
247
+ it("where contains uses ILIKE", () => {
248
+ const result = buildWhereSQLQuery(
249
+ pgDialect,
250
+ "id",
251
+ [{ t: "where", path: "name", op: "contains", value: "oh" }],
252
+ "users",
253
+ {}
254
+ )
255
+ expect(result.sql).toContain("ILIKE")
256
+ expect(result.params).toContain("%oh%")
257
+ })
258
+
259
+ it("where in uses $N placeholders", () => {
260
+ const result = buildWhereSQLQuery(
261
+ pgDialect,
262
+ "id",
263
+ [{ t: "where", path: "status", op: "in", value: ["active", "pending"] as any }],
264
+ "users",
265
+ {}
266
+ )
267
+ expect(result.sql).toContain("$1")
268
+ expect(result.sql).toContain("$2")
269
+ expect(result.params).toEqual(expect.arrayContaining(["active", "pending"]))
270
+ })
271
+
272
+ it("where includes uses @> jsonb operator", () => {
273
+ const result = buildWhereSQLQuery(
274
+ pgDialect,
275
+ "id",
276
+ [{ t: "where", path: "tags", op: "includes", value: "admin" }],
277
+ "users",
278
+ {}
279
+ )
280
+ expect(result.sql).toContain("@>")
281
+ expect(result.sql).toContain("jsonb")
282
+ })
283
+
284
+ it("nested path uses chained -> operators", () => {
285
+ const result = buildWhereSQLQuery(
286
+ pgDialect,
287
+ "id",
288
+ [{ t: "where", path: "address.city", op: "eq", value: "NYC" }],
289
+ "users",
290
+ {}
291
+ )
292
+ expect(result.sql).toContain("data->'address'->>'city'")
293
+ })
294
+ })
295
+
296
+ // --- Integration tests with in-memory SQLite (direct, no Effect SQL client) ---
297
+
298
+ describe("SQL Store (SQLite integration)", () => {
299
+ const withDb = (fn: (db: Sqlite.Database) => void) => {
300
+ const db = new BetterSqlite(":memory:")
301
+ db.pragma("journal_mode = WAL")
302
+ try {
303
+ fn(db)
304
+ } finally {
305
+ db.close()
306
+ }
307
+ }
308
+
309
+ it("creates table and seeds data", () =>
310
+ withDb((db) => {
311
+ db.exec(
312
+ `CREATE TABLE IF NOT EXISTS "test_items" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
313
+ )
314
+ db
315
+ .prepare(`INSERT INTO "test_items" (id, _etag, data) VALUES (?, ?, ?)`)
316
+ .run("1", "etag1", JSON.stringify({ name: "Alice", age: 30 }))
317
+
318
+ const rows = db.prepare(`SELECT * FROM "test_items"`).all()
319
+ expect(rows.length).toBe(1)
320
+ expect((rows[0] as any).id).toBe("1")
321
+ }))
322
+
323
+ it("data column should not contain _etag or id", () =>
324
+ withDb((db) => {
325
+ db.exec(
326
+ `CREATE TABLE IF NOT EXISTS "test_clean" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
327
+ )
328
+ // Simulate what toRow now produces: data without id or _etag
329
+ const data = { name: "Alice", age: 30, tags: ["admin"] }
330
+ db
331
+ .prepare(`INSERT INTO "test_clean" (id, _etag, data) VALUES (?, ?, ?)`)
332
+ .run("1", "etag1", JSON.stringify(data))
333
+
334
+ const row = db.prepare(`SELECT * FROM "test_clean" WHERE id = ?`).get("1") as any
335
+ const parsed = JSON.parse(row.data) as any
336
+ expect(parsed).not.toHaveProperty("id")
337
+ expect(parsed).not.toHaveProperty("_etag")
338
+ expect(parsed.name).toBe("Alice")
339
+ expect(parsed.age).toBe(30)
340
+ expect(parsed.tags).toEqual(["admin"])
341
+ // id and _etag come from their own columns
342
+ expect(row.id).toBe("1")
343
+ expect(row._etag).toBe("etag1")
344
+ }))
345
+
346
+ it("backward compat: rows with id/_etag in data still work with queries", () =>
347
+ withDb((db) => {
348
+ db.exec(
349
+ `CREATE TABLE IF NOT EXISTS "test_compat" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
350
+ )
351
+ // Old format: id and _etag inside data
352
+ db
353
+ .prepare(`INSERT INTO "test_compat" (id, _etag, data) VALUES (?, ?, ?)`)
354
+ .run("1", "etag1", JSON.stringify({ id: "1", _etag: "old_etag", name: "Alice", age: 30 }))
355
+ // New format: id and _etag stripped from data
356
+ db
357
+ .prepare(`INSERT INTO "test_compat" (id, _etag, data) VALUES (?, ?, ?)`)
358
+ .run("2", "etag2", JSON.stringify({ name: "Bob", age: 25 }))
359
+
360
+ // Both should be queryable by name
361
+ const q1 = buildWhereSQLQuery(
362
+ sqliteDialect,
363
+ "id",
364
+ [{ t: "where", path: "name", op: "eq", value: "Alice" }],
365
+ "test_compat",
366
+ {}
367
+ )
368
+ const r1 = query(db, q1.sql, q1.params)
369
+ expect(r1.length).toBe(1)
370
+ expect((r1[0] as any).id).toBe("1")
371
+
372
+ const q2 = buildWhereSQLQuery(
373
+ sqliteDialect,
374
+ "id",
375
+ [{ t: "where", path: "name", op: "eq", value: "Bob" }],
376
+ "test_compat",
377
+ {}
378
+ )
379
+ const r2 = query(db, q2.sql, q2.params)
380
+ expect(r2.length).toBe(1)
381
+ expect((r2[0] as any).id).toBe("2")
382
+
383
+ // Both queryable by id column
384
+ const q3 = buildWhereSQLQuery(
385
+ sqliteDialect,
386
+ "id",
387
+ [{ t: "where", path: "id", op: "in", value: ["1", "2"] as any }],
388
+ "test_compat",
389
+ {}
390
+ )
391
+ expect(query(db, q3.sql, q3.params).length).toBe(2)
392
+ }))
393
+
394
+ it("queries work when data does not contain id", () =>
395
+ withDb((db) => {
396
+ db.exec(
397
+ `CREATE TABLE IF NOT EXISTS "test_noid" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
398
+ )
399
+ const people = [
400
+ { name: "Alice", age: 30 },
401
+ { name: "Bob", age: 25 },
402
+ { name: "Charlie", age: 35 }
403
+ ]
404
+ const insert = db.prepare(
405
+ `INSERT INTO "test_noid" (id, _etag, data) VALUES (?, ?, ?)`
406
+ )
407
+ people.forEach((p, i) => insert.run(String(i + 1), `etag_${i + 1}`, JSON.stringify(p)))
408
+
409
+ // Filter by field in data
410
+ const q1 = buildWhereSQLQuery(
411
+ sqliteDialect,
412
+ "id",
413
+ [{ t: "where", path: "age", op: "gt", value: 28 as any }],
414
+ "test_noid",
415
+ {}
416
+ )
417
+ expect(query(db, q1.sql, q1.params).length).toBe(2) // Alice(30), Charlie(35)
418
+
419
+ // Filter by id column
420
+ const q2 = buildWhereSQLQuery(
421
+ sqliteDialect,
422
+ "id",
423
+ [{ t: "where", path: "id", op: "eq", value: "2" }],
424
+ "test_noid",
425
+ {}
426
+ )
427
+ const r2 = query(db, q2.sql, q2.params)
428
+ expect(r2.length).toBe(1)
429
+ expect((r2[0] as any).id).toBe("2")
430
+ expect((JSON.parse((r2[0] as any).data) as any).name).toBe("Bob")
431
+
432
+ // Order + limit still works
433
+ const q3 = buildWhereSQLQuery(
434
+ sqliteDialect,
435
+ "id",
436
+ [],
437
+ "test_noid",
438
+ {},
439
+ undefined,
440
+ [{ key: "age", direction: "ASC" }] as any,
441
+ undefined,
442
+ 2
443
+ )
444
+ const r3 = query(db, q3.sql, q3.params)
445
+ expect(r3.length).toBe(2)
446
+ expect((JSON.parse((r3[0] as any).data) as any).name).toBe("Bob") // youngest first
447
+ }))
448
+
449
+ it("query builder generates valid SQL for SQLite", () =>
450
+ withDb((db) => {
451
+ db.exec(
452
+ `CREATE TABLE IF NOT EXISTS "test_people" (id TEXT PRIMARY KEY, _etag TEXT, data JSON NOT NULL)`
453
+ )
454
+
455
+ const people = [
456
+ { id: "1", name: "Alice", age: 30, tags: ["admin", "user"] },
457
+ { id: "2", name: "Bob", age: 25, tags: ["user"] },
458
+ { id: "3", name: "Charlie", age: 35, tags: ["admin"] },
459
+ { id: "4", name: "Diana", age: 28, tags: ["user", "editor"] }
460
+ ]
461
+
462
+ const insert = db.prepare(
463
+ `INSERT INTO "test_people" (id, _etag, data) VALUES (?, ?, ?)`
464
+ )
465
+ for (const p of people) {
466
+ const { id, ...data } = p
467
+ insert.run(id, `etag_${id}`, JSON.stringify(data))
468
+ }
469
+
470
+ // Test eq
471
+ const q1 = buildWhereSQLQuery(
472
+ sqliteDialect,
473
+ "id",
474
+ [{ t: "where", path: "name", op: "eq", value: "Alice" }],
475
+ "test_people",
476
+ {}
477
+ )
478
+ expect(query(db, q1.sql, q1.params).length).toBe(1)
479
+ expect((JSON.parse((query(db, q1.sql, q1.params)[0] as any).data) as any).name).toBe("Alice")
480
+
481
+ // Test gt
482
+ const q2 = buildWhereSQLQuery(
483
+ sqliteDialect,
484
+ "id",
485
+ [{ t: "where", path: "age", op: "gt", value: 28 as any }],
486
+ "test_people",
487
+ {}
488
+ )
489
+ expect(query(db, q2.sql, q2.params).length).toBe(2)
490
+
491
+ // Test OR
492
+ const q3 = buildWhereSQLQuery(
493
+ sqliteDialect,
494
+ "id",
495
+ [
496
+ { t: "where", path: "name", op: "eq", value: "Alice" },
497
+ { t: "or", path: "name", op: "eq", value: "Bob" }
498
+ ],
499
+ "test_people",
500
+ {}
501
+ )
502
+ expect(query(db, q3.sql, q3.params).length).toBe(2)
503
+
504
+ // Test AND
505
+ const q4 = buildWhereSQLQuery(
506
+ sqliteDialect,
507
+ "id",
508
+ [
509
+ { t: "where", path: "name", op: "eq", value: "Alice" },
510
+ { t: "and", path: "age", op: "gt", value: 25 as any }
511
+ ],
512
+ "test_people",
513
+ {}
514
+ )
515
+ const r4 = query(db, q4.sql, q4.params)
516
+ expect(r4.length).toBe(1)
517
+ expect((JSON.parse((r4[0] as any).data) as any).name).toBe("Alice")
518
+
519
+ // Test IN
520
+ const q5 = buildWhereSQLQuery(
521
+ sqliteDialect,
522
+ "id",
523
+ [{ t: "where", path: "id", op: "in", value: ["1", "3"] as any }],
524
+ "test_people",
525
+ {}
526
+ )
527
+ expect(query(db, q5.sql, q5.params).length).toBe(2)
528
+
529
+ // Test contains (string)
530
+ const q6 = buildWhereSQLQuery(
531
+ sqliteDialect,
532
+ "id",
533
+ [{ t: "where", path: "name", op: "contains", value: "li" }],
534
+ "test_people",
535
+ {}
536
+ )
537
+ expect(query(db, q6.sql, q6.params).length).toBe(2) // Alice, Charlie
538
+
539
+ // Test startsWith
540
+ const q7 = buildWhereSQLQuery(
541
+ sqliteDialect,
542
+ "id",
543
+ [{ t: "where", path: "name", op: "startsWith", value: "Al" }],
544
+ "test_people",
545
+ {}
546
+ )
547
+ const r7 = query(db, q7.sql, q7.params)
548
+ expect(r7.length).toBe(1)
549
+ expect((JSON.parse((r7[0] as any).data) as any).name).toBe("Alice")
550
+
551
+ // Test includes (array)
552
+ const q8 = buildWhereSQLQuery(
553
+ sqliteDialect,
554
+ "id",
555
+ [{ t: "where", path: "tags", op: "includes", value: "admin" }],
556
+ "test_people",
557
+ {}
558
+ )
559
+ expect(query(db, q8.sql, q8.params).length).toBe(2) // Alice, Charlie
560
+
561
+ // Test nested scope: where name = Alice OR (age > 30 AND name contains 'ar')
562
+ const q9 = buildWhereSQLQuery(
563
+ sqliteDialect,
564
+ "id",
565
+ [
566
+ { t: "where", path: "name", op: "eq", value: "Alice" },
567
+ {
568
+ t: "or-scope",
569
+ result: [
570
+ { t: "where", path: "age", op: "gt", value: 30 as any },
571
+ { t: "and", path: "name", op: "contains", value: "ar" }
572
+ ],
573
+ relation: "some"
574
+ }
575
+ ],
576
+ "test_people",
577
+ {}
578
+ )
579
+ expect(query(db, q9.sql, q9.params).length).toBe(2) // Alice + Charlie
580
+
581
+ // Test order + limit
582
+ const q10 = buildWhereSQLQuery(
583
+ sqliteDialect,
584
+ "id",
585
+ [],
586
+ "test_people",
587
+ {},
588
+ undefined,
589
+ [{ key: "age", direction: "DESC" }] as any,
590
+ undefined,
591
+ 2
592
+ )
593
+ const r10 = query(db, q10.sql, q10.params)
594
+ expect(r10.length).toBe(2)
595
+ expect((JSON.parse((r10[0] as any).data) as any).name).toBe("Charlie") // oldest first
596
+ }))
597
+
598
+ it("namespace param is in correct position for SQLite positional placeholders", () =>
599
+ withDb((db) => {
600
+ db.exec(
601
+ `CREATE TABLE IF NOT EXISTS "test_ns" (id TEXT NOT NULL, _namespace TEXT NOT NULL DEFAULT 'primary', _etag TEXT, data JSON NOT NULL, PRIMARY KEY (id, _namespace))`
602
+ )
603
+ const insert = db.prepare(
604
+ `INSERT INTO "test_ns" (id, _namespace, _etag, data) VALUES (?, ?, ?, ?)`
605
+ )
606
+ insert.run("1", "primary", "e1", JSON.stringify({ name: "Alice", role: "admin" }))
607
+ insert.run("2", "primary", "e2", JSON.stringify({ name: "Bob", role: "user" }))
608
+ insert.run("3", "other", "e3", JSON.stringify({ name: "Charlie", role: "admin" }))
609
+
610
+ // Build a filter query: role != 'deleted'
611
+ const q = buildWhereSQLQuery(
612
+ sqliteDialect,
613
+ "id",
614
+ [{ t: "where", path: "role", op: "neq", value: "deleted" }],
615
+ "test_ns",
616
+ {}
617
+ )
618
+
619
+ // Simulate what SQL.ts does: prepend _namespace = ? and put ns FIRST in params
620
+ const hasWhere = q.sql.includes("WHERE")
621
+ const nsSql = hasWhere
622
+ ? q.sql.replace("WHERE", `WHERE _namespace = ? AND`)
623
+ : q.sql.replace(`FROM "test_ns"`, `FROM "test_ns" WHERE _namespace = ?`)
624
+ const params = ["primary", ...q.params]
625
+
626
+ const results = query(db, nsSql, params)
627
+ // Should only get Alice and Bob (primary namespace), not Charlie (other namespace)
628
+ expect(results.length).toBe(2)
629
+ const names = results.map((r) => (JSON.parse((r as any).data) as any).name).sort()
630
+ expect(names).toEqual(["Alice", "Bob"])
631
+ }))
632
+ })
633
+
634
+ // --- toRow stripping and parseRow reconstruction tests ---
635
+
636
+ describe("toRow strips _etag and id from data", () => {
637
+ // Replicate the toRow logic from SQL.ts to test in isolation
638
+ const toRow = <IdKey extends PropertyKey>(e: any, idKey: IdKey) => {
639
+ const newE = makeETag(e)
640
+ const id = newE[idKey] as string
641
+ const { _etag, [idKey]: _id, ...rest } = newE as any
642
+ const data = JSON.stringify(rest)
643
+ return { id, _etag: newE._etag!, data, item: newE }
644
+ }
645
+
646
+ it("data JSON does not contain _etag", () => {
647
+ const row = toRow({ id: "1", _etag: undefined, name: "Alice", age: 30 }, "id")
648
+ const parsed = JSON.parse(row.data) as any
649
+ expect(parsed).not.toHaveProperty("_etag")
650
+ expect(parsed.name).toBe("Alice")
651
+ expect(parsed.age).toBe(30)
652
+ })
653
+
654
+ it("data JSON does not contain id field", () => {
655
+ const row = toRow({ id: "1", _etag: undefined, name: "Alice" }, "id")
656
+ const parsed = JSON.parse(row.data) as any
657
+ expect(parsed).not.toHaveProperty("id")
658
+ expect(parsed.name).toBe("Alice")
659
+ })
660
+
661
+ it("data JSON does not contain custom idKey field", () => {
662
+ const row = toRow({ myId: "abc", _etag: undefined, name: "Bob" }, "myId")
663
+ const parsed = JSON.parse(row.data) as any
664
+ expect(parsed).not.toHaveProperty("myId")
665
+ expect(parsed.name).toBe("Bob")
666
+ expect(row.id).toBe("abc")
667
+ })
668
+
669
+ it("id and _etag are returned as separate fields", () => {
670
+ const row = toRow({ id: "1", _etag: undefined, name: "Alice" }, "id")
671
+ expect(row.id).toBe("1")
672
+ expect(typeof row._etag).toBe("string")
673
+ expect(row._etag.length).toBeGreaterThan(0)
674
+ })
675
+
676
+ it("item still contains all fields including _etag and id", () => {
677
+ const row = toRow({ id: "1", _etag: undefined, name: "Alice" }, "id")
678
+ expect(row.item.id).toBe("1")
679
+ expect(row.item._etag).toBe(row._etag)
680
+ expect(row.item.name).toBe("Alice")
681
+ })
682
+
683
+ it("preserves nested objects in data", () => {
684
+ const row = toRow({ id: "1", _etag: undefined, address: { city: "NYC", zip: "10001" } }, "id")
685
+ const parsed = JSON.parse(row.data) as any
686
+ expect(parsed.address).toEqual({ city: "NYC", zip: "10001" })
687
+ expect(parsed).not.toHaveProperty("id")
688
+ expect(parsed).not.toHaveProperty("_etag")
689
+ })
690
+ })
691
+
692
+ describe("parseRow reconstructs full object from row", () => {
693
+ it("re-injects id from row column using idKey", () => {
694
+ const result: any = parseRow(
695
+ { id: "42", _etag: "etag1", data: JSON.stringify({ name: "Alice", age: 30 }) },
696
+ "id",
697
+ {}
698
+ )
699
+ expect(result.id).toBe("42")
700
+ expect(result.name).toBe("Alice")
701
+ expect(result.age).toBe(30)
702
+ expect(result._etag).toBe("etag1")
703
+ })
704
+
705
+ it("re-injects custom idKey from row column", () => {
706
+ const result: any = parseRow(
707
+ { id: "abc", _etag: "etag2", data: JSON.stringify({ name: "Bob" }) },
708
+ "myId",
709
+ {}
710
+ )
711
+ expect(result.myId).toBe("abc")
712
+ expect(result.name).toBe("Bob")
713
+ expect(result._etag).toBe("etag2")
714
+ })
715
+
716
+ it("uses _etag from row column, not from data", () => {
717
+ const result: any = parseRow(
718
+ { id: "1", _etag: "column_etag", data: JSON.stringify({ _etag: "stale_data_etag", name: "Alice" }) },
719
+ "id",
720
+ {}
721
+ )
722
+ expect(result._etag).toBe("column_etag")
723
+ })
724
+
725
+ it("uses id from row column, not from data", () => {
726
+ const result: any = parseRow(
727
+ { id: "correct_id", _etag: "e1", data: JSON.stringify({ id: "wrong_id", name: "Alice" }) },
728
+ "id",
729
+ {}
730
+ )
731
+ expect(result.id).toBe("correct_id")
732
+ })
733
+
734
+ it("applies defaultValues for missing fields", () => {
735
+ const result: any = parseRow(
736
+ { id: "1", _etag: "e1", data: JSON.stringify({ name: "Alice" }) },
737
+ "id",
738
+ { status: "active", role: "user" }
739
+ )
740
+ expect(result.name).toBe("Alice")
741
+ expect(result.status).toBe("active")
742
+ expect(result.role).toBe("user")
743
+ })
744
+
745
+ it("data fields override defaultValues", () => {
746
+ const result: any = parseRow(
747
+ { id: "1", _etag: "e1", data: JSON.stringify({ name: "Alice", status: "inactive" }) },
748
+ "id",
749
+ { status: "active" }
750
+ )
751
+ expect(result.status).toBe("inactive")
752
+ })
753
+
754
+ it("handles null _etag from row", () => {
755
+ const result: any = parseRow(
756
+ { id: "1", _etag: null, data: JSON.stringify({ name: "Alice" }) },
757
+ "id",
758
+ {}
759
+ )
760
+ expect(result._etag).toBeUndefined()
761
+ })
762
+
763
+ it("round-trip: toRow then parseRow reconstructs the original", () => {
764
+ const original = { id: "1", _etag: undefined as string | undefined, name: "Alice", age: 30, tags: ["admin"] }
765
+ const newE = makeETag(original)
766
+ const { _etag, id: _id, ...rest } = newE as any
767
+ const row = { id: newE.id, _etag: newE._etag!, data: JSON.stringify(rest) }
768
+
769
+ const reconstructed: any = parseRow(row, "id", {})
770
+ expect(reconstructed.id).toBe("1")
771
+ expect(reconstructed.name).toBe("Alice")
772
+ expect(reconstructed.age).toBe(30)
773
+ expect(reconstructed.tags).toEqual(["admin"])
774
+ expect(reconstructed._etag).toBe(newE._etag)
775
+ })
776
+ })