@fragno-dev/db 0.1.13 → 0.1.14
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.
- package/.turbo/turbo-build.log +48 -41
- package/CHANGELOG.md +6 -0
- package/dist/adapters/adapters.d.ts +13 -1
- package/dist/adapters/adapters.d.ts.map +1 -1
- package/dist/adapters/adapters.js.map +1 -1
- package/dist/adapters/drizzle/drizzle-adapter.d.ts +2 -0
- package/dist/adapters/drizzle/drizzle-adapter.d.ts.map +1 -1
- package/dist/adapters/drizzle/drizzle-adapter.js +6 -1
- package/dist/adapters/drizzle/drizzle-adapter.js.map +1 -1
- package/dist/adapters/drizzle/drizzle-query.js +6 -4
- package/dist/adapters/drizzle/drizzle-query.js.map +1 -1
- package/dist/adapters/drizzle/drizzle-uow-compiler.d.ts +0 -1
- package/dist/adapters/drizzle/drizzle-uow-compiler.d.ts.map +1 -1
- package/dist/adapters/drizzle/drizzle-uow-compiler.js +49 -36
- package/dist/adapters/drizzle/drizzle-uow-compiler.js.map +1 -1
- package/dist/adapters/drizzle/drizzle-uow-decoder.js +1 -1
- package/dist/adapters/drizzle/drizzle-uow-decoder.js.map +1 -1
- package/dist/adapters/drizzle/shared.d.ts +14 -1
- package/dist/adapters/drizzle/shared.d.ts.map +1 -0
- package/dist/adapters/kysely/kysely-adapter.d.ts +2 -0
- package/dist/adapters/kysely/kysely-adapter.d.ts.map +1 -1
- package/dist/adapters/kysely/kysely-adapter.js +7 -2
- package/dist/adapters/kysely/kysely-adapter.js.map +1 -1
- package/dist/adapters/kysely/kysely-query.js +5 -3
- package/dist/adapters/kysely/kysely-query.js.map +1 -1
- package/dist/adapters/kysely/kysely-shared.d.ts +11 -0
- package/dist/adapters/kysely/kysely-shared.d.ts.map +1 -0
- package/dist/adapters/kysely/kysely-uow-compiler.js +38 -9
- package/dist/adapters/kysely/kysely-uow-compiler.js.map +1 -1
- package/dist/bind-services.d.ts +7 -0
- package/dist/bind-services.d.ts.map +1 -0
- package/dist/bind-services.js +14 -0
- package/dist/bind-services.js.map +1 -0
- package/dist/fragment.d.ts +131 -12
- package/dist/fragment.d.ts.map +1 -1
- package/dist/fragment.js +107 -8
- package/dist/fragment.js.map +1 -1
- package/dist/mod.d.ts +4 -2
- package/dist/mod.d.ts.map +1 -1
- package/dist/mod.js +3 -2
- package/dist/mod.js.map +1 -1
- package/dist/query/query.d.ts +2 -2
- package/dist/query/query.d.ts.map +1 -1
- package/dist/query/unit-of-work.d.ts +100 -15
- package/dist/query/unit-of-work.d.ts.map +1 -1
- package/dist/query/unit-of-work.js +214 -7
- package/dist/query/unit-of-work.js.map +1 -1
- package/package.json +3 -3
- package/src/adapters/adapters.ts +14 -0
- package/src/adapters/drizzle/drizzle-adapter-pglite.test.ts +6 -1
- package/src/adapters/drizzle/drizzle-adapter-sqlite.test.ts +133 -5
- package/src/adapters/drizzle/drizzle-adapter.ts +16 -1
- package/src/adapters/drizzle/drizzle-query.ts +26 -15
- package/src/adapters/drizzle/drizzle-uow-compiler.test.ts +57 -57
- package/src/adapters/drizzle/drizzle-uow-compiler.ts +79 -39
- package/src/adapters/drizzle/drizzle-uow-decoder.ts +2 -5
- package/src/adapters/kysely/kysely-adapter-pglite.test.ts +2 -2
- package/src/adapters/kysely/kysely-adapter.ts +16 -1
- package/src/adapters/kysely/kysely-query.ts +26 -15
- package/src/adapters/kysely/kysely-uow-compiler.test.ts +43 -43
- package/src/adapters/kysely/kysely-uow-compiler.ts +50 -14
- package/src/adapters/kysely/kysely-uow-joins.test.ts +30 -30
- package/src/bind-services.test.ts +214 -0
- package/src/bind-services.ts +37 -0
- package/src/db-fragment.test.ts +800 -0
- package/src/fragment.ts +557 -28
- package/src/mod.ts +19 -0
- package/src/query/query.ts +2 -2
- package/src/query/unit-of-work-multi-schema.test.ts +64 -0
- package/src/query/unit-of-work-types.test.ts +13 -0
- package/src/query/unit-of-work.test.ts +5 -9
- package/src/query/unit-of-work.ts +511 -62
- package/src/uow-context-integration.test.ts +102 -0
- package/src/uow-context.test.ts +182 -0
- package/src/fragment.test.ts +0 -341
|
@@ -114,12 +114,12 @@ describe("kysely-uow-compiler", () => {
|
|
|
114
114
|
|
|
115
115
|
// Helper to create UnitOfWork for testing
|
|
116
116
|
function createTestUOW(name?: string) {
|
|
117
|
-
const mockCompiler = createKyselyUOWCompiler(
|
|
117
|
+
const mockCompiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
118
118
|
const mockExecutor = {
|
|
119
119
|
executeRetrievalPhase: async () => [],
|
|
120
120
|
executeMutationPhase: async () => ({ success: true, createdInternalIds: [] }),
|
|
121
121
|
};
|
|
122
|
-
const mockDecoder: UOWDecoder
|
|
122
|
+
const mockDecoder: UOWDecoder = (rawResults, operations) => {
|
|
123
123
|
if (rawResults.length !== operations.length) {
|
|
124
124
|
throw new Error("rawResults and ops must have the same length");
|
|
125
125
|
}
|
|
@@ -136,7 +136,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
136
136
|
b.whereIndex("idx_email", (eb) => eb("email", "=", "test@example.com")),
|
|
137
137
|
);
|
|
138
138
|
|
|
139
|
-
const compiler = createKyselyUOWCompiler(
|
|
139
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
140
140
|
const compiled = uow.compile(compiler);
|
|
141
141
|
|
|
142
142
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -152,7 +152,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
152
152
|
b.whereIndex("idx_name", (eb) => eb("name", "=", "Alice")).select(["id", "name"]),
|
|
153
153
|
);
|
|
154
154
|
|
|
155
|
-
const compiler = createKyselyUOWCompiler(
|
|
155
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
156
156
|
const compiled = uow.compile(compiler);
|
|
157
157
|
|
|
158
158
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -166,7 +166,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
166
166
|
const uow = createTestUOW();
|
|
167
167
|
uow.find("users", (b) => b.whereIndex("primary").pageSize(10));
|
|
168
168
|
|
|
169
|
-
const compiler = createKyselyUOWCompiler(
|
|
169
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
170
170
|
const compiled = uow.compile(compiler);
|
|
171
171
|
|
|
172
172
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -180,7 +180,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
180
180
|
const uow = createTestUOW();
|
|
181
181
|
uow.find("users", (b) => b.whereIndex("primary").orderByIndex("primary", "desc"));
|
|
182
182
|
|
|
183
|
-
const compiler = createKyselyUOWCompiler(
|
|
183
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
184
184
|
const compiled = uow.compile(compiler);
|
|
185
185
|
|
|
186
186
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -193,7 +193,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
193
193
|
const uow = createTestUOW();
|
|
194
194
|
uow.find("users", (b) => b.whereIndex("idx_name").orderByIndex("idx_name", "desc"));
|
|
195
195
|
|
|
196
|
-
const compiler = createKyselyUOWCompiler(
|
|
196
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
197
197
|
const compiled = uow.compile(compiler);
|
|
198
198
|
|
|
199
199
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -209,7 +209,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
209
209
|
);
|
|
210
210
|
uow.find("posts", (b) => b.whereIndex("idx_title", (eb) => eb("title", "contains", "test")));
|
|
211
211
|
|
|
212
|
-
const compiler = createKyselyUOWCompiler(
|
|
212
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
213
213
|
const compiled = uow.compile(compiler);
|
|
214
214
|
|
|
215
215
|
expect(compiled.retrievalBatch).toHaveLength(2);
|
|
@@ -228,7 +228,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
228
228
|
return b;
|
|
229
229
|
});
|
|
230
230
|
|
|
231
|
-
const compiler = createKyselyUOWCompiler(
|
|
231
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
232
232
|
const compiled = uow.compile(compiler);
|
|
233
233
|
|
|
234
234
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -241,7 +241,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
241
241
|
const uow = createTestUOW();
|
|
242
242
|
uow.find("users", (b) => b.whereIndex("idx_age", (eb) => eb("age", ">", 25)).selectCount());
|
|
243
243
|
|
|
244
|
-
const compiler = createKyselyUOWCompiler(
|
|
244
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
245
245
|
const compiled = uow.compile(compiler);
|
|
246
246
|
|
|
247
247
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -263,7 +263,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
263
263
|
b.whereIndex("idx_name").orderByIndex("idx_name", "asc").after(cursor).pageSize(10),
|
|
264
264
|
);
|
|
265
265
|
|
|
266
|
-
const compiler = createKyselyUOWCompiler(
|
|
266
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
267
267
|
const compiled = uow.compile(compiler);
|
|
268
268
|
|
|
269
269
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -285,7 +285,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
285
285
|
b.whereIndex("idx_name").orderByIndex("idx_name", "desc").before(cursor).pageSize(10),
|
|
286
286
|
);
|
|
287
287
|
|
|
288
|
-
const compiler = createKyselyUOWCompiler(
|
|
288
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
289
289
|
const compiled = uow.compile(compiler);
|
|
290
290
|
|
|
291
291
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -311,7 +311,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
311
311
|
.pageSize(5),
|
|
312
312
|
);
|
|
313
313
|
|
|
314
|
-
const compiler = createKyselyUOWCompiler(
|
|
314
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
315
315
|
const compiled = uow.compile(compiler);
|
|
316
316
|
|
|
317
317
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -331,7 +331,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
331
331
|
age: 30,
|
|
332
332
|
});
|
|
333
333
|
|
|
334
|
-
const compiler = createKyselyUOWCompiler(
|
|
334
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
335
335
|
const compiled = uow.compile(compiler);
|
|
336
336
|
const [batch] = compiled.mutationBatch;
|
|
337
337
|
assert(batch);
|
|
@@ -357,7 +357,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
357
357
|
}),
|
|
358
358
|
);
|
|
359
359
|
|
|
360
|
-
const compiler = createKyselyUOWCompiler(
|
|
360
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
361
361
|
const compiled = uow.compile(compiler);
|
|
362
362
|
const [batch] = compiled.mutationBatch;
|
|
363
363
|
assert(batch);
|
|
@@ -373,7 +373,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
373
373
|
const userId = FragnoId.fromExternal("user123", 5);
|
|
374
374
|
uow.update("users", userId, (b) => b.set({ age: 18 }).check());
|
|
375
375
|
|
|
376
|
-
const compiler = createKyselyUOWCompiler(
|
|
376
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
377
377
|
const compiled = uow.compile(compiler);
|
|
378
378
|
const [batch] = compiled.mutationBatch;
|
|
379
379
|
assert(batch);
|
|
@@ -389,7 +389,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
389
389
|
const userId = FragnoId.fromExternal("user123", 0);
|
|
390
390
|
uow.delete("users", userId);
|
|
391
391
|
|
|
392
|
-
const compiler = createKyselyUOWCompiler(
|
|
392
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
393
393
|
const compiled = uow.compile(compiler);
|
|
394
394
|
const [batch] = compiled.mutationBatch;
|
|
395
395
|
assert(batch);
|
|
@@ -405,7 +405,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
405
405
|
const userId = FragnoId.fromExternal("user123", 3);
|
|
406
406
|
uow.delete("users", userId, (b) => b.check());
|
|
407
407
|
|
|
408
|
-
const compiler = createKyselyUOWCompiler(
|
|
408
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
409
409
|
const compiled = uow.compile(compiler);
|
|
410
410
|
const [batch] = compiled.mutationBatch;
|
|
411
411
|
assert(batch);
|
|
@@ -425,7 +425,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
425
425
|
}),
|
|
426
426
|
);
|
|
427
427
|
|
|
428
|
-
const compiler = createKyselyUOWCompiler(
|
|
428
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
429
429
|
const compiled = uow.compile(compiler);
|
|
430
430
|
const [batch] = compiled.mutationBatch;
|
|
431
431
|
assert(batch);
|
|
@@ -440,7 +440,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
440
440
|
const uow = createTestUOW();
|
|
441
441
|
uow.delete("users", "user123");
|
|
442
442
|
|
|
443
|
-
const compiler = createKyselyUOWCompiler(
|
|
443
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
444
444
|
const compiled = uow.compile(compiler);
|
|
445
445
|
const [batch] = compiled.mutationBatch;
|
|
446
446
|
assert(batch);
|
|
@@ -480,7 +480,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
480
480
|
const userId = FragnoId.fromExternal("user456", 0);
|
|
481
481
|
uow.delete("posts", userId);
|
|
482
482
|
|
|
483
|
-
const compiler = createKyselyUOWCompiler(
|
|
483
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
484
484
|
const compiled = uow.compile(compiler);
|
|
485
485
|
const [createBatch, updateBatch, deleteBatch] = compiled.mutationBatch;
|
|
486
486
|
|
|
@@ -519,7 +519,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
519
519
|
const userId = FragnoId.fromExternal("user123", 3);
|
|
520
520
|
uow.update("users", userId, (b) => b.set({ age: 31 }).check());
|
|
521
521
|
|
|
522
|
-
const compiler = createKyselyUOWCompiler(
|
|
522
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
523
523
|
const compiled = uow.compile(compiler);
|
|
524
524
|
|
|
525
525
|
expect(compiled.name).toBe("update-user-balance");
|
|
@@ -551,7 +551,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
551
551
|
),
|
|
552
552
|
);
|
|
553
553
|
|
|
554
|
-
const compiler = createKyselyUOWCompiler(
|
|
554
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
555
555
|
const compiled = uow.compile(compiler);
|
|
556
556
|
|
|
557
557
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -565,7 +565,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
565
565
|
const uow = createTestUOW();
|
|
566
566
|
uow.find("users", (b) => b.whereIndex("primary", () => false));
|
|
567
567
|
|
|
568
|
-
const compiler = createKyselyUOWCompiler(
|
|
568
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
569
569
|
const compiled = uow.compile(compiler);
|
|
570
570
|
|
|
571
571
|
// When condition is false, the operation should return null and not be added to batch
|
|
@@ -576,7 +576,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
576
576
|
const uow = createTestUOW();
|
|
577
577
|
uow.find("users", (b) => b.whereIndex("primary", () => true));
|
|
578
578
|
|
|
579
|
-
const compiler = createKyselyUOWCompiler(
|
|
579
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
580
580
|
const compiled = uow.compile(compiler);
|
|
581
581
|
|
|
582
582
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -593,7 +593,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
593
593
|
const userId = FragnoId.fromExternal("user123", 5);
|
|
594
594
|
uow.update("users", userId, (b) => b.set({ age: 31 }).check());
|
|
595
595
|
|
|
596
|
-
const compiler = createKyselyUOWCompiler(
|
|
596
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
597
597
|
const compiled = uow.compile(compiler);
|
|
598
598
|
const [batch] = compiled.mutationBatch;
|
|
599
599
|
assert(batch);
|
|
@@ -610,7 +610,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
610
610
|
const userId = FragnoId.fromExternal("user456", 3);
|
|
611
611
|
uow.delete("users", userId, (b) => b.check());
|
|
612
612
|
|
|
613
|
-
const compiler = createKyselyUOWCompiler(
|
|
613
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
614
614
|
const compiled = uow.compile(compiler);
|
|
615
615
|
const [batch] = compiled.mutationBatch;
|
|
616
616
|
assert(batch);
|
|
@@ -630,7 +630,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
630
630
|
uow.update("users", userId, (b) => b.set({ age: 30 }).check());
|
|
631
631
|
uow.update("posts", postId, (b) => b.set({ viewCount: 100 }).check());
|
|
632
632
|
|
|
633
|
-
const compiler = createKyselyUOWCompiler(
|
|
633
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
634
634
|
const compiled = uow.compile(compiler);
|
|
635
635
|
const [userBatch, postBatch] = compiled.mutationBatch;
|
|
636
636
|
|
|
@@ -657,7 +657,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
657
657
|
const userId = FragnoId.fromExternal("user1", 0);
|
|
658
658
|
uow.update("users", userId, (b) => b.set({ age: 25 }));
|
|
659
659
|
|
|
660
|
-
const compiler = createKyselyUOWCompiler(
|
|
660
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
661
661
|
const compiled = uow.compile(compiler);
|
|
662
662
|
const [batch] = compiled.mutationBatch;
|
|
663
663
|
assert(batch);
|
|
@@ -676,7 +676,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
676
676
|
b.whereIndex("primary").join((jb) => jb.author((ab) => ab.select(["name", "email"]))),
|
|
677
677
|
);
|
|
678
678
|
|
|
679
|
-
const compiler = createKyselyUOWCompiler(
|
|
679
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
680
680
|
const compiled = uow.compile(compiler);
|
|
681
681
|
|
|
682
682
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -700,7 +700,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
700
700
|
),
|
|
701
701
|
);
|
|
702
702
|
|
|
703
|
-
const compiler = createKyselyUOWCompiler(
|
|
703
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
704
704
|
const compiled = uow.compile(compiler);
|
|
705
705
|
|
|
706
706
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -719,7 +719,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
719
719
|
.join((jb) => jb.author((ab) => ab.select(["name"]).orderByIndex("idx_name", "desc"))),
|
|
720
720
|
);
|
|
721
721
|
|
|
722
|
-
const compiler = createKyselyUOWCompiler(
|
|
722
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
723
723
|
const compiled = uow.compile(compiler);
|
|
724
724
|
|
|
725
725
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -735,7 +735,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
735
735
|
b.whereIndex("primary").join((jb) => jb.author((ab) => ab.select(["name"]).pageSize(5))),
|
|
736
736
|
);
|
|
737
737
|
|
|
738
|
-
const compiler = createKyselyUOWCompiler(
|
|
738
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
739
739
|
const compiled = uow.compile(compiler);
|
|
740
740
|
|
|
741
741
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -766,7 +766,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
766
766
|
),
|
|
767
767
|
);
|
|
768
768
|
|
|
769
|
-
const compiler = createKyselyUOWCompiler(
|
|
769
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
770
770
|
const compiled = uow.compile(compiler);
|
|
771
771
|
|
|
772
772
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -785,7 +785,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
785
785
|
.join((jb) => jb.post((pb) => pb.select(["title"])).author((ab) => ab.select(["name"]))),
|
|
786
786
|
);
|
|
787
787
|
|
|
788
|
-
const compiler = createKyselyUOWCompiler(
|
|
788
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
789
789
|
const compiled = uow.compile(compiler);
|
|
790
790
|
|
|
791
791
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -803,7 +803,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
803
803
|
b.whereIndex("primary").join((jb) => jb.inviter((ib) => ib.select(["name", "email"]))),
|
|
804
804
|
);
|
|
805
805
|
|
|
806
|
-
const compiler = createKyselyUOWCompiler(
|
|
806
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
807
807
|
const compiled = uow.compile(compiler);
|
|
808
808
|
|
|
809
809
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -833,7 +833,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
833
833
|
),
|
|
834
834
|
);
|
|
835
835
|
|
|
836
|
-
const compiler = createKyselyUOWCompiler(
|
|
836
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
837
837
|
const compiled = uow.compile(compiler);
|
|
838
838
|
|
|
839
839
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -856,7 +856,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
856
856
|
.join((jb) => jb.post((pb) => pb.select(["title"])).tag((tb) => tb.select(["name"]))),
|
|
857
857
|
);
|
|
858
858
|
|
|
859
|
-
const compiler = createKyselyUOWCompiler(
|
|
859
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
860
860
|
const compiled = uow.compile(compiler);
|
|
861
861
|
|
|
862
862
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -881,7 +881,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
881
881
|
),
|
|
882
882
|
);
|
|
883
883
|
|
|
884
|
-
const compiler = createKyselyUOWCompiler(
|
|
884
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
885
885
|
const compiled = uow.compile(compiler);
|
|
886
886
|
|
|
887
887
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -897,7 +897,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
897
897
|
it("should handle UOW with no operations", () => {
|
|
898
898
|
const uow = createTestUOW();
|
|
899
899
|
|
|
900
|
-
const compiler = createKyselyUOWCompiler(
|
|
900
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
901
901
|
const compiled = uow.compile(compiler);
|
|
902
902
|
|
|
903
903
|
expect(compiled.retrievalBatch).toHaveLength(0);
|
|
@@ -908,7 +908,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
908
908
|
const uow = createTestUOW();
|
|
909
909
|
uow.find("users");
|
|
910
910
|
|
|
911
|
-
const compiler = createKyselyUOWCompiler(
|
|
911
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
912
912
|
const compiled = uow.compile(compiler);
|
|
913
913
|
|
|
914
914
|
expect(compiled.retrievalBatch).toHaveLength(1);
|
|
@@ -922,7 +922,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
922
922
|
email: "test@example.com",
|
|
923
923
|
});
|
|
924
924
|
|
|
925
|
-
const compiler = createKyselyUOWCompiler(
|
|
925
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
926
926
|
const compiled = uow.compile(compiler);
|
|
927
927
|
|
|
928
928
|
expect(compiled.retrievalBatch).toHaveLength(0);
|
|
@@ -949,7 +949,7 @@ describe("kysely-uow-compiler", () => {
|
|
|
949
949
|
content: "This is my first post",
|
|
950
950
|
});
|
|
951
951
|
|
|
952
|
-
const compiler = createKyselyUOWCompiler(
|
|
952
|
+
const compiler = createKyselyUOWCompiler(pool, "postgresql");
|
|
953
953
|
const compiled = uow.compile(compiler);
|
|
954
954
|
|
|
955
955
|
// Should have no retrieval operations
|
|
@@ -11,7 +11,7 @@ import { createKyselyQueryBuilder } from "./kysely-query-builder";
|
|
|
11
11
|
import { buildCondition, type Condition } from "../../query/condition-builder";
|
|
12
12
|
import { decodeCursor, serializeCursorValues } from "../../query/cursor";
|
|
13
13
|
import type { AnySelectClause } from "../../query/query";
|
|
14
|
-
import type
|
|
14
|
+
import { type TableNameMapper, createTableNameMapper } from "./kysely-shared";
|
|
15
15
|
import type { ConnectionPool } from "../../shared/connection-pool";
|
|
16
16
|
import type { SQLProvider } from "../../shared/providers";
|
|
17
17
|
|
|
@@ -24,24 +24,57 @@ type KyselyAny = Kysely<any>;
|
|
|
24
24
|
* This compiler translates UOW operations into Kysely CompiledQuery objects
|
|
25
25
|
* that can be executed as a batch/transaction.
|
|
26
26
|
*
|
|
27
|
-
* @param schema - The database schema
|
|
28
27
|
* @param pool - Connection pool for acquiring database connections
|
|
29
28
|
* @param provider - SQL provider (postgresql, mysql, sqlite, etc.)
|
|
30
|
-
* @param mapper - Optional table name mapper for namespace prefixing
|
|
29
|
+
* @param mapper - Optional table name mapper for namespace prefixing (fallback for operations without explicit namespace)
|
|
31
30
|
* @returns A UOWCompiler instance for Kysely
|
|
32
31
|
*/
|
|
33
|
-
export function createKyselyUOWCompiler
|
|
34
|
-
schema: TSchema,
|
|
32
|
+
export function createKyselyUOWCompiler(
|
|
35
33
|
pool: ConnectionPool<KyselyAny>,
|
|
36
34
|
provider: SQLProvider,
|
|
37
35
|
mapper?: TableNameMapper,
|
|
38
|
-
): UOWCompiler<
|
|
39
|
-
const queryCompiler = createKyselyQueryCompiler(schema, pool, provider, mapper);
|
|
36
|
+
): UOWCompiler<CompiledQuery> {
|
|
40
37
|
// Get kysely instance for query building (compilation doesn't execute, just builds SQL)
|
|
41
38
|
const kysely = pool.getDatabaseSync();
|
|
42
|
-
const queryBuilder = createKyselyQueryBuilder(kysely, provider, mapper);
|
|
43
39
|
|
|
44
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Get the mapper for a specific operation
|
|
42
|
+
* Uses operation's namespace if provided, otherwise falls back to the default mapper
|
|
43
|
+
*/
|
|
44
|
+
function getMapperForOperation(namespace: string | undefined): TableNameMapper | undefined {
|
|
45
|
+
if (namespace) {
|
|
46
|
+
return createTableNameMapper(namespace);
|
|
47
|
+
}
|
|
48
|
+
return mapper;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Cache query compilers and builders by namespace for performance
|
|
52
|
+
const compilerCache = new Map<string | undefined, ReturnType<typeof createKyselyQueryCompiler>>();
|
|
53
|
+
const builderCache = new Map<string | undefined, ReturnType<typeof createKyselyQueryBuilder>>();
|
|
54
|
+
|
|
55
|
+
function getQueryCompiler(schema: AnySchema, namespace: string | undefined) {
|
|
56
|
+
const cacheKey = namespace;
|
|
57
|
+
let compiler = compilerCache.get(cacheKey);
|
|
58
|
+
if (!compiler) {
|
|
59
|
+
const opMapper = getMapperForOperation(namespace);
|
|
60
|
+
compiler = createKyselyQueryCompiler(schema, pool, provider, opMapper);
|
|
61
|
+
compilerCache.set(cacheKey, compiler);
|
|
62
|
+
}
|
|
63
|
+
return compiler;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function getQueryBuilder(namespace: string | undefined) {
|
|
67
|
+
const cacheKey = namespace;
|
|
68
|
+
let builder = builderCache.get(cacheKey);
|
|
69
|
+
if (!builder) {
|
|
70
|
+
const opMapper = getMapperForOperation(namespace);
|
|
71
|
+
builder = createKyselyQueryBuilder(kysely, provider, opMapper);
|
|
72
|
+
builderCache.set(cacheKey, builder);
|
|
73
|
+
}
|
|
74
|
+
return builder;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function toTable(schema: AnySchema, name: unknown) {
|
|
45
78
|
const table = schema.tables[name as string];
|
|
46
79
|
if (!table) {
|
|
47
80
|
throw new Error(`Invalid table name ${name}.`);
|
|
@@ -50,7 +83,8 @@ export function createKyselyUOWCompiler<TSchema extends AnySchema>(
|
|
|
50
83
|
}
|
|
51
84
|
|
|
52
85
|
return {
|
|
53
|
-
compileRetrievalOperation(op: RetrievalOperation<
|
|
86
|
+
compileRetrievalOperation(op: RetrievalOperation<AnySchema>): CompiledQuery | null {
|
|
87
|
+
const queryCompiler = getQueryCompiler(op.schema, op.namespace);
|
|
54
88
|
switch (op.type) {
|
|
55
89
|
case "count": {
|
|
56
90
|
return queryCompiler.count(op.table.name, {
|
|
@@ -159,8 +193,9 @@ export function createKyselyUOWCompiler<TSchema extends AnySchema>(
|
|
|
159
193
|
}
|
|
160
194
|
}
|
|
161
195
|
|
|
162
|
-
// When we have joins or need to bypass buildFindOptions, use queryBuilder
|
|
196
|
+
// When we have joins or need to bypass buildFindOptions, use operation-specific queryBuilder
|
|
163
197
|
if (join && join.length > 0) {
|
|
198
|
+
const queryBuilder = getQueryBuilder(op.namespace);
|
|
164
199
|
return queryBuilder.findMany(op.table, {
|
|
165
200
|
// Safe cast: select from UOW matches SimplifyFindOptions requirement
|
|
166
201
|
select: (findManyOptions.select ?? true) as AnySelectClause,
|
|
@@ -182,8 +217,9 @@ export function createKyselyUOWCompiler<TSchema extends AnySchema>(
|
|
|
182
217
|
},
|
|
183
218
|
|
|
184
219
|
compileMutationOperation(
|
|
185
|
-
op: MutationOperation<
|
|
220
|
+
op: MutationOperation<AnySchema>,
|
|
186
221
|
): CompiledMutation<CompiledQuery> | null {
|
|
222
|
+
const queryCompiler = getQueryCompiler(op.schema, op.namespace);
|
|
187
223
|
switch (op.type) {
|
|
188
224
|
case "create":
|
|
189
225
|
// queryCompiler.create() calls encodeValues() which handles runtime defaults
|
|
@@ -193,7 +229,7 @@ export function createKyselyUOWCompiler<TSchema extends AnySchema>(
|
|
|
193
229
|
};
|
|
194
230
|
|
|
195
231
|
case "update": {
|
|
196
|
-
const table = toTable(op.table);
|
|
232
|
+
const table = toTable(op.schema, op.table);
|
|
197
233
|
const idColumn = table.getIdColumn();
|
|
198
234
|
const versionColumn = table.getVersionColumn();
|
|
199
235
|
|
|
@@ -226,7 +262,7 @@ export function createKyselyUOWCompiler<TSchema extends AnySchema>(
|
|
|
226
262
|
}
|
|
227
263
|
|
|
228
264
|
case "delete": {
|
|
229
|
-
const table = toTable(op.table);
|
|
265
|
+
const table = toTable(op.schema, op.table);
|
|
230
266
|
const idColumn = table.getIdColumn();
|
|
231
267
|
const versionColumn = table.getVersionColumn();
|
|
232
268
|
|