@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
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
type TableNameMapper,
|
|
15
15
|
parseDrizzle,
|
|
16
16
|
type DBType,
|
|
17
|
+
createTableNameMapper,
|
|
17
18
|
} from "./shared";
|
|
18
19
|
import { encodeValues, ReferenceSubquery } from "../../query/result-transform";
|
|
19
20
|
import { serialize } from "../../schema/serialize";
|
|
@@ -33,30 +34,42 @@ export type DrizzleCompiledQuery = {
|
|
|
33
34
|
* This compiler translates UOW operations into Drizzle query functions
|
|
34
35
|
* that can be executed as a batch/transaction.
|
|
35
36
|
*
|
|
36
|
-
* @param schema - The database schema
|
|
37
37
|
* @param pool - Connection pool for acquiring database connections
|
|
38
38
|
* @param provider - SQL provider (sqlite, mysql, postgresql)
|
|
39
|
-
* @param mapper - Optional table name mapper for namespace prefixing
|
|
39
|
+
* @param mapper - Optional table name mapper for namespace prefixing (fallback for operations without explicit namespace)
|
|
40
40
|
* @returns A UOWCompiler instance for Drizzle
|
|
41
41
|
*/
|
|
42
|
-
export function createDrizzleUOWCompiler
|
|
43
|
-
schema: TSchema,
|
|
42
|
+
export function createDrizzleUOWCompiler(
|
|
44
43
|
pool: ConnectionPool<DBType>,
|
|
45
44
|
provider: "sqlite" | "mysql" | "postgresql",
|
|
46
45
|
mapper?: TableNameMapper,
|
|
47
|
-
): UOWCompiler<
|
|
46
|
+
): UOWCompiler<DrizzleCompiledQuery> {
|
|
48
47
|
// Get db synchronously for compilation (doesn't execute, just builds SQL)
|
|
49
48
|
// TODO: We don't even need a Drizzle instance with a db client attached here. `drizzle({ schema })` is enough.
|
|
50
49
|
const dbRaw = pool.getDatabaseSync();
|
|
51
50
|
const [db, drizzleTables] = parseDrizzle(dbRaw);
|
|
52
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Get the mapper for a specific operation
|
|
54
|
+
* Uses operation's namespace if provided, otherwise falls back to the default mapper
|
|
55
|
+
*/
|
|
56
|
+
function getMapperForOperation(namespace: string | undefined): TableNameMapper | undefined {
|
|
57
|
+
if (namespace) {
|
|
58
|
+
return createTableNameMapper(namespace);
|
|
59
|
+
}
|
|
60
|
+
return mapper;
|
|
61
|
+
}
|
|
62
|
+
|
|
53
63
|
/**
|
|
54
64
|
* Convert a Fragno table to a Drizzle table
|
|
55
65
|
* @throws Error if table is not found in Drizzle schema
|
|
56
66
|
*/
|
|
57
|
-
function toDrizzleTable(table: AnyTable): TableType {
|
|
58
|
-
//
|
|
59
|
-
const
|
|
67
|
+
function toDrizzleTable(table: AnyTable, namespace: string | undefined): TableType {
|
|
68
|
+
// Get the mapper for this operation's namespace
|
|
69
|
+
const opMapper = getMapperForOperation(namespace);
|
|
70
|
+
|
|
71
|
+
// Map logical table name to physical table name using the operation-specific mapper
|
|
72
|
+
const physicalTableName = opMapper ? opMapper.toPhysical(table.ormName) : table.ormName;
|
|
60
73
|
const out = drizzleTables[physicalTableName];
|
|
61
74
|
if (out) {
|
|
62
75
|
return out;
|
|
@@ -71,13 +84,17 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
71
84
|
* Convert a Fragno column to a Drizzle column
|
|
72
85
|
* @throws Error if column is not found in Drizzle table
|
|
73
86
|
*/
|
|
74
|
-
function toDrizzleColumn(
|
|
87
|
+
function toDrizzleColumn(
|
|
88
|
+
schema: AnySchema,
|
|
89
|
+
namespace: string | undefined,
|
|
90
|
+
col: AnyColumn,
|
|
91
|
+
): ColumnType {
|
|
75
92
|
const fragnoTable = schema.tables[col.tableName];
|
|
76
93
|
if (!fragnoTable) {
|
|
77
94
|
throw new Error(`[Drizzle] Unknown table ${col.tableName} for column ${col.ormName}.`);
|
|
78
95
|
}
|
|
79
96
|
|
|
80
|
-
const table = toDrizzleTable(fragnoTable);
|
|
97
|
+
const table = toDrizzleTable(fragnoTable, namespace);
|
|
81
98
|
const out = table[col.ormName];
|
|
82
99
|
if (out) {
|
|
83
100
|
return out;
|
|
@@ -89,13 +106,17 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
89
106
|
/**
|
|
90
107
|
* Build a WHERE clause from a condition using Drizzle's query builder
|
|
91
108
|
*/
|
|
92
|
-
function buildWhere(
|
|
109
|
+
function buildWhere(
|
|
110
|
+
schema: AnySchema,
|
|
111
|
+
namespace: string | undefined,
|
|
112
|
+
condition: Condition,
|
|
113
|
+
): Drizzle.SQL | undefined {
|
|
93
114
|
if (condition.type === "compare") {
|
|
94
|
-
const left = toDrizzleColumn(condition.a);
|
|
115
|
+
const left = toDrizzleColumn(schema, namespace, condition.a);
|
|
95
116
|
const op = condition.operator;
|
|
96
117
|
let right = condition.b;
|
|
97
118
|
if (right instanceof Column) {
|
|
98
|
-
right = toDrizzleColumn(right);
|
|
119
|
+
right = toDrizzleColumn(schema, namespace, right);
|
|
99
120
|
} else {
|
|
100
121
|
// Handle string references - convert external ID to internal ID via subquery
|
|
101
122
|
if (condition.a.role === "reference" && typeof right === "string") {
|
|
@@ -181,11 +202,11 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
181
202
|
}
|
|
182
203
|
|
|
183
204
|
if (condition.type === "and") {
|
|
184
|
-
return Drizzle.and(...condition.items.map((item) => buildWhere(item)));
|
|
205
|
+
return Drizzle.and(...condition.items.map((item) => buildWhere(schema, namespace, item)));
|
|
185
206
|
}
|
|
186
207
|
|
|
187
208
|
if (condition.type === "not") {
|
|
188
|
-
const result = buildWhere(condition.item);
|
|
209
|
+
const result = buildWhere(schema, namespace, condition.item);
|
|
189
210
|
if (!result) {
|
|
190
211
|
return;
|
|
191
212
|
}
|
|
@@ -193,7 +214,7 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
193
214
|
return Drizzle.not(result);
|
|
194
215
|
}
|
|
195
216
|
|
|
196
|
-
return Drizzle.or(...condition.items.map((item) => buildWhere(item)));
|
|
217
|
+
return Drizzle.or(...condition.items.map((item) => buildWhere(schema, namespace, item)));
|
|
197
218
|
}
|
|
198
219
|
|
|
199
220
|
/**
|
|
@@ -229,7 +250,7 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
229
250
|
* Get table from schema by name
|
|
230
251
|
* @throws Error if table is not found in schema
|
|
231
252
|
*/
|
|
232
|
-
function getTable(name: unknown): AnyTable {
|
|
253
|
+
function getTable(schema: AnySchema, name: unknown): AnyTable {
|
|
233
254
|
const table = schema.tables[name as string];
|
|
234
255
|
if (!table) {
|
|
235
256
|
throw new Error(`Invalid table name ${name}.`);
|
|
@@ -260,6 +281,8 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
260
281
|
* Process joins recursively to support nested joins with orderBy and limit
|
|
261
282
|
*/
|
|
262
283
|
function processJoins(
|
|
284
|
+
schema: AnySchema,
|
|
285
|
+
namespace: string | undefined,
|
|
263
286
|
joins: CompiledJoin[],
|
|
264
287
|
): Record<string, Drizzle.DBQueryConfig<"many", boolean>> {
|
|
265
288
|
const result: Record<string, Drizzle.DBQueryConfig<"many", boolean>> = {};
|
|
@@ -286,7 +309,7 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
286
309
|
let joinOrderBy: Drizzle.SQL[] | undefined;
|
|
287
310
|
if (options.orderBy && options.orderBy.length > 0) {
|
|
288
311
|
joinOrderBy = options.orderBy.map(([col, direction]) => {
|
|
289
|
-
const drizzleCol = toDrizzleColumn(col);
|
|
312
|
+
const drizzleCol = toDrizzleColumn(schema, namespace, col);
|
|
290
313
|
return direction === "asc" ? Drizzle.asc(drizzleCol) : Drizzle.desc(drizzleCol);
|
|
291
314
|
});
|
|
292
315
|
}
|
|
@@ -294,7 +317,7 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
294
317
|
// Build WHERE clause for this join if provided
|
|
295
318
|
let joinWhere: Drizzle.SQL | undefined;
|
|
296
319
|
if (options.where) {
|
|
297
|
-
joinWhere = buildWhere(options.where);
|
|
320
|
+
joinWhere = buildWhere(schema, namespace, options.where);
|
|
298
321
|
}
|
|
299
322
|
|
|
300
323
|
// Build the join config
|
|
@@ -307,7 +330,7 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
307
330
|
|
|
308
331
|
// Recursively process nested joins
|
|
309
332
|
if (options.join && options.join.length > 0) {
|
|
310
|
-
joinConfig.with = processJoins(options.join);
|
|
333
|
+
joinConfig.with = processJoins(schema, namespace, options.join);
|
|
311
334
|
}
|
|
312
335
|
|
|
313
336
|
result[joinName] = joinConfig;
|
|
@@ -317,7 +340,8 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
317
340
|
}
|
|
318
341
|
|
|
319
342
|
return {
|
|
320
|
-
compileRetrievalOperation(op: RetrievalOperation<
|
|
343
|
+
compileRetrievalOperation(op: RetrievalOperation<AnySchema>): DrizzleCompiledQuery | null {
|
|
344
|
+
const schema = op.schema;
|
|
321
345
|
switch (op.type) {
|
|
322
346
|
case "count": {
|
|
323
347
|
// Build WHERE clause
|
|
@@ -329,11 +353,11 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
329
353
|
return null;
|
|
330
354
|
}
|
|
331
355
|
if (condition !== true) {
|
|
332
|
-
whereClause = buildWhere(condition);
|
|
356
|
+
whereClause = buildWhere(schema, op.namespace, condition);
|
|
333
357
|
}
|
|
334
358
|
}
|
|
335
359
|
|
|
336
|
-
const drizzleTable = toDrizzleTable(op.table);
|
|
360
|
+
const drizzleTable = toDrizzleTable(op.table, op.namespace);
|
|
337
361
|
const query = db.select({ count: Drizzle.count() }).from(drizzleTable);
|
|
338
362
|
|
|
339
363
|
const compiledQuery = whereClause ? query.where(whereClause).toSQL() : query.toSQL();
|
|
@@ -377,7 +401,7 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
377
401
|
let orderBy: Drizzle.SQL[] | undefined;
|
|
378
402
|
if (indexColumns.length > 0) {
|
|
379
403
|
orderBy = indexColumns.map((col) => {
|
|
380
|
-
const drizzleCol = toDrizzleColumn(col);
|
|
404
|
+
const drizzleCol = toDrizzleColumn(schema, op.namespace, col);
|
|
381
405
|
return orderDirection === "asc" ? Drizzle.asc(drizzleCol) : Drizzle.desc(drizzleCol);
|
|
382
406
|
});
|
|
383
407
|
}
|
|
@@ -413,7 +437,7 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
413
437
|
return null;
|
|
414
438
|
}
|
|
415
439
|
if (condition !== true) {
|
|
416
|
-
const clause = buildWhere(condition);
|
|
440
|
+
const clause = buildWhere(schema, op.namespace, condition);
|
|
417
441
|
if (clause) {
|
|
418
442
|
whereClauses.push(clause);
|
|
419
443
|
}
|
|
@@ -436,12 +460,12 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
436
460
|
|
|
437
461
|
if (indexColumns.length === 1) {
|
|
438
462
|
// Simple single-column case
|
|
439
|
-
const col = toDrizzleColumn(indexColumns[0]!);
|
|
463
|
+
const col = toDrizzleColumn(schema, op.namespace, indexColumns[0]!);
|
|
440
464
|
const val = serializedValues[indexColumns[0]!.ormName];
|
|
441
465
|
whereClauses.push(useGreaterThan ? Drizzle.gt(col, val) : Drizzle.lt(col, val));
|
|
442
466
|
} else {
|
|
443
467
|
// Multi-column tuple comparison using SQL
|
|
444
|
-
const drizzleCols = indexColumns.map((c) => toDrizzleColumn(c));
|
|
468
|
+
const drizzleCols = indexColumns.map((c) => toDrizzleColumn(schema, op.namespace, c));
|
|
445
469
|
const vals = indexColumns.map((c) => serializedValues[c.ormName]);
|
|
446
470
|
const operator = useGreaterThan ? ">" : "<";
|
|
447
471
|
// Safe cast: building a SQL comparison expression for cursor pagination
|
|
@@ -469,23 +493,37 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
469
493
|
|
|
470
494
|
// Process joins recursively to support nested joins
|
|
471
495
|
if (joins) {
|
|
472
|
-
queryConfig.with = processJoins(joins);
|
|
496
|
+
queryConfig.with = processJoins(schema, op.namespace, joins);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// For multi-schema support: get the mapper for the operation's namespace
|
|
500
|
+
const opMapper = getMapperForOperation(op.namespace);
|
|
501
|
+
const physicalTableName = opMapper
|
|
502
|
+
? opMapper.toPhysical(op.table.ormName)
|
|
503
|
+
: op.table.ormName;
|
|
504
|
+
const tableQuery = db.query[physicalTableName];
|
|
505
|
+
|
|
506
|
+
if (!tableQuery) {
|
|
507
|
+
throw new Error(
|
|
508
|
+
`[Drizzle] Table ${op.table.ormName} (physical: ${physicalTableName}) not found in db.query. ` +
|
|
509
|
+
`Available tables: ${Object.keys(db.query).join(", ")}`,
|
|
510
|
+
);
|
|
473
511
|
}
|
|
474
512
|
|
|
475
|
-
const
|
|
476
|
-
const compiledQuery = db.query[physicalTableName].findMany(queryConfig).toSQL();
|
|
513
|
+
const compiledQuery = tableQuery.findMany(queryConfig).toSQL();
|
|
477
514
|
return compiledQuery;
|
|
478
515
|
}
|
|
479
516
|
}
|
|
480
517
|
},
|
|
481
518
|
|
|
482
519
|
compileMutationOperation(
|
|
483
|
-
op: MutationOperation<
|
|
520
|
+
op: MutationOperation<AnySchema>,
|
|
484
521
|
): CompiledMutation<DrizzleCompiledQuery> | null {
|
|
522
|
+
const schema = op.schema;
|
|
485
523
|
switch (op.type) {
|
|
486
524
|
case "create": {
|
|
487
|
-
const table = getTable(op.table);
|
|
488
|
-
const drizzleTable = toDrizzleTable(table);
|
|
525
|
+
const table = getTable(schema, op.table);
|
|
526
|
+
const drizzleTable = toDrizzleTable(table, op.namespace);
|
|
489
527
|
// encodeValues now handles runtime defaults automatically
|
|
490
528
|
const encodedValues = encodeValues(op.values, table, true, provider);
|
|
491
529
|
const values = processReferenceSubqueries(encodedValues);
|
|
@@ -498,10 +536,10 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
498
536
|
}
|
|
499
537
|
|
|
500
538
|
case "update": {
|
|
501
|
-
const table = getTable(op.table);
|
|
539
|
+
const table = getTable(schema, op.table);
|
|
502
540
|
const idColumn = table.getIdColumn();
|
|
503
541
|
const versionColumn = table.getVersionColumn();
|
|
504
|
-
const drizzleTable = toDrizzleTable(table);
|
|
542
|
+
const drizzleTable = toDrizzleTable(table, op.namespace);
|
|
505
543
|
|
|
506
544
|
const externalId = typeof op.id === "string" ? op.id : op.id.externalId;
|
|
507
545
|
const versionToCheck = getVersionToCheck(op.id, op.checkVersion);
|
|
@@ -523,7 +561,8 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
523
561
|
return null;
|
|
524
562
|
}
|
|
525
563
|
|
|
526
|
-
const whereClause =
|
|
564
|
+
const whereClause =
|
|
565
|
+
condition === true ? undefined : buildWhere(schema, op.namespace, condition);
|
|
527
566
|
const encodedSetValues = encodeValues(op.set, table, false, provider);
|
|
528
567
|
const setValues = processReferenceSubqueries(encodedSetValues);
|
|
529
568
|
|
|
@@ -541,10 +580,10 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
541
580
|
}
|
|
542
581
|
|
|
543
582
|
case "delete": {
|
|
544
|
-
const table = getTable(op.table);
|
|
583
|
+
const table = getTable(schema, op.table);
|
|
545
584
|
const idColumn = table.getIdColumn();
|
|
546
585
|
const versionColumn = table.getVersionColumn();
|
|
547
|
-
const drizzleTable = toDrizzleTable(table);
|
|
586
|
+
const drizzleTable = toDrizzleTable(table, op.namespace);
|
|
548
587
|
|
|
549
588
|
if (!op.id) {
|
|
550
589
|
throw new Error(
|
|
@@ -582,7 +621,8 @@ export function createDrizzleUOWCompiler<TSchema extends AnySchema>(
|
|
|
582
621
|
return null;
|
|
583
622
|
}
|
|
584
623
|
|
|
585
|
-
const whereClause =
|
|
624
|
+
const whereClause =
|
|
625
|
+
condition === true ? undefined : buildWhere(schema, op.namespace, condition);
|
|
586
626
|
|
|
587
627
|
const compiledQuery = db.delete(drizzleTable).where(whereClause).toSQL();
|
|
588
628
|
return {
|
|
@@ -143,17 +143,14 @@ function transformJoinArraysToObjects(
|
|
|
143
143
|
return transformedRow;
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
export function createDrizzleUOWDecoder
|
|
147
|
-
_schema: TSchema,
|
|
148
|
-
provider: SQLProvider,
|
|
149
|
-
): UOWDecoder<TSchema, DrizzleResult> {
|
|
146
|
+
export function createDrizzleUOWDecoder(provider: SQLProvider): UOWDecoder<DrizzleResult> {
|
|
150
147
|
return (rawResults, ops) => {
|
|
151
148
|
if (rawResults.length !== ops.length) {
|
|
152
149
|
throw new Error("rawResults and ops must have the same length");
|
|
153
150
|
}
|
|
154
151
|
|
|
155
152
|
return rawResults.map((result, index) => {
|
|
156
|
-
const op = ops[index] as RetrievalOperation<
|
|
153
|
+
const op = ops[index] as RetrievalOperation<AnySchema>;
|
|
157
154
|
if (!op) {
|
|
158
155
|
throw new Error("op must be defined");
|
|
159
156
|
}
|
|
@@ -787,7 +787,7 @@ describe("KyselyAdapter PGLite", () => {
|
|
|
787
787
|
});
|
|
788
788
|
});
|
|
789
789
|
|
|
790
|
-
it("should handle timestamps and
|
|
790
|
+
it("should handle timestamps and time zones correctly", async () => {
|
|
791
791
|
const queryEngine = adapter.createQueryEngine(testSchema, "test");
|
|
792
792
|
|
|
793
793
|
// Create a user
|
|
@@ -826,7 +826,7 @@ describe("KyselyAdapter PGLite", () => {
|
|
|
826
826
|
const specificDate = new Date("2024-06-15T14:30:00Z");
|
|
827
827
|
expect(specificDate.toISOString()).toBe("2024-06-15T14:30:00.000Z");
|
|
828
828
|
|
|
829
|
-
// Verify that dates from different
|
|
829
|
+
// Verify that dates from different time zones are handled correctly
|
|
830
830
|
const localDate = new Date("2024-06-15T14:30:00");
|
|
831
831
|
expect(localDate).toBeInstanceOf(Date);
|
|
832
832
|
expect(typeof localDate.getTimezoneOffset()).toBe("number");
|
|
@@ -28,6 +28,7 @@ export interface KyselyConfig {
|
|
|
28
28
|
export class KyselyAdapter implements DatabaseAdapter<KyselyUOWConfig> {
|
|
29
29
|
#connectionPool: ConnectionPool<KyselyAny>;
|
|
30
30
|
#provider: SQLProvider;
|
|
31
|
+
#schemaNamespaceMap = new WeakMap<AnySchema, string>();
|
|
31
32
|
|
|
32
33
|
constructor(config: KyselyConfig) {
|
|
33
34
|
this.#connectionPool = createKyselyConnectionPool(config.db);
|
|
@@ -46,13 +47,27 @@ export class KyselyAdapter implements DatabaseAdapter<KyselyUOWConfig> {
|
|
|
46
47
|
await this.#connectionPool.close();
|
|
47
48
|
}
|
|
48
49
|
|
|
50
|
+
createTableNameMapper(namespace: string) {
|
|
51
|
+
return createTableNameMapper(namespace);
|
|
52
|
+
}
|
|
53
|
+
|
|
49
54
|
createQueryEngine<T extends AnySchema>(
|
|
50
55
|
schema: T,
|
|
51
56
|
namespace: string,
|
|
52
57
|
): AbstractQuery<T, KyselyUOWConfig> {
|
|
58
|
+
// Register schema-namespace mapping
|
|
59
|
+
this.#schemaNamespaceMap.set(schema, namespace);
|
|
60
|
+
|
|
53
61
|
// Only create mapper if namespace is non-empty
|
|
54
62
|
const mapper = namespace ? createTableNameMapper(namespace) : undefined;
|
|
55
|
-
return fromKysely(
|
|
63
|
+
return fromKysely(
|
|
64
|
+
schema,
|
|
65
|
+
this.#connectionPool,
|
|
66
|
+
this.#provider,
|
|
67
|
+
mapper,
|
|
68
|
+
undefined,
|
|
69
|
+
this.#schemaNamespaceMap,
|
|
70
|
+
);
|
|
56
71
|
}
|
|
57
72
|
|
|
58
73
|
async isConnectionHealthy(): Promise<boolean> {
|
|
@@ -77,6 +77,8 @@ class UpdateManySpecialBuilder<TTable extends AnyTable> {
|
|
|
77
77
|
* @param pool - Connection pool for acquiring database connections
|
|
78
78
|
* @param provider - SQL provider (postgresql, mysql, sqlite, etc.)
|
|
79
79
|
* @param mapper - Optional table name mapper for namespace prefixing
|
|
80
|
+
* @param uowConfig - Optional UOW configuration
|
|
81
|
+
* @param schemaNamespaceMap - Optional WeakMap for schema-to-namespace lookups
|
|
80
82
|
* @returns An AbstractQuery instance for performing database operations
|
|
81
83
|
*
|
|
82
84
|
* @example
|
|
@@ -96,9 +98,10 @@ export function fromKysely<T extends AnySchema>(
|
|
|
96
98
|
provider: SQLProvider,
|
|
97
99
|
mapper?: TableNameMapper,
|
|
98
100
|
uowConfig?: KyselyUOWConfig,
|
|
101
|
+
schemaNamespaceMap?: WeakMap<AnySchema, string>,
|
|
99
102
|
): AbstractQuery<T, KyselyUOWConfig> {
|
|
100
103
|
function createUOW(opts: { name?: string; config?: KyselyUOWConfig }) {
|
|
101
|
-
const uowCompiler = createKyselyUOWCompiler(
|
|
104
|
+
const uowCompiler = createKyselyUOWCompiler(pool, provider, mapper);
|
|
102
105
|
|
|
103
106
|
const executor: UOWExecutor<CompiledQuery, unknown> = {
|
|
104
107
|
async executeRetrievalPhase(retrievalBatch: CompiledQuery[]) {
|
|
@@ -133,7 +136,7 @@ export function fromKysely<T extends AnySchema>(
|
|
|
133
136
|
};
|
|
134
137
|
|
|
135
138
|
// Create a decoder function to transform raw results into application format
|
|
136
|
-
const decoder: UOWDecoder<
|
|
139
|
+
const decoder: UOWDecoder<unknown> = (rawResults, ops) => {
|
|
137
140
|
if (rawResults.length !== ops.length) {
|
|
138
141
|
throw new Error("rawResults and ops must have the same length");
|
|
139
142
|
}
|
|
@@ -204,20 +207,28 @@ export function fromKysely<T extends AnySchema>(
|
|
|
204
207
|
|
|
205
208
|
const { onQuery, ...restUowConfig } = opts.config ?? {};
|
|
206
209
|
|
|
207
|
-
return new UnitOfWork(
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
210
|
+
return new UnitOfWork(
|
|
211
|
+
schema,
|
|
212
|
+
uowCompiler,
|
|
213
|
+
executor,
|
|
214
|
+
decoder,
|
|
215
|
+
opts.name,
|
|
216
|
+
{
|
|
217
|
+
...restUowConfig,
|
|
218
|
+
onQuery: (query) => {
|
|
219
|
+
// CompiledMutation has { query: CompiledQuery, expectedAffectedRows: number | null }
|
|
220
|
+
// CompiledQuery has { query: QueryAST, sql: string, parameters: unknown[] }
|
|
221
|
+
// Check for expectedAffectedRows to distinguish CompiledMutation from CompiledQuery
|
|
222
|
+
const actualQuery =
|
|
223
|
+
query && typeof query === "object" && "expectedAffectedRows" in query
|
|
224
|
+
? (query as CompiledMutation<CompiledQuery>).query
|
|
225
|
+
: (query as CompiledQuery);
|
|
226
|
+
|
|
227
|
+
opts.config?.onQuery?.(actualQuery);
|
|
228
|
+
},
|
|
219
229
|
},
|
|
220
|
-
|
|
230
|
+
schemaNamespaceMap,
|
|
231
|
+
);
|
|
221
232
|
}
|
|
222
233
|
|
|
223
234
|
return {
|