linkgress-orm 0.0.2 → 0.1.0
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/LICENSE +21 -21
- package/README.md +196 -196
- package/dist/entity/db-column.d.ts +38 -1
- package/dist/entity/db-column.d.ts.map +1 -1
- package/dist/entity/db-column.js.map +1 -1
- package/dist/entity/db-context.d.ts +429 -50
- package/dist/entity/db-context.d.ts.map +1 -1
- package/dist/entity/db-context.js +884 -203
- package/dist/entity/db-context.js.map +1 -1
- package/dist/entity/entity-base.d.ts +8 -0
- package/dist/entity/entity-base.d.ts.map +1 -1
- package/dist/entity/entity-base.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/migration/db-schema-manager.js +77 -77
- package/dist/migration/enum-migrator.js +6 -6
- package/dist/query/collection-strategy.factory.d.ts.map +1 -1
- package/dist/query/collection-strategy.factory.js +7 -3
- package/dist/query/collection-strategy.factory.js.map +1 -1
- package/dist/query/collection-strategy.interface.d.ts +12 -6
- package/dist/query/collection-strategy.interface.d.ts.map +1 -1
- package/dist/query/conditions.d.ts +178 -24
- package/dist/query/conditions.d.ts.map +1 -1
- package/dist/query/conditions.js +165 -4
- package/dist/query/conditions.js.map +1 -1
- package/dist/query/cte-builder.d.ts +21 -5
- package/dist/query/cte-builder.d.ts.map +1 -1
- package/dist/query/cte-builder.js +31 -7
- package/dist/query/cte-builder.js.map +1 -1
- package/dist/query/grouped-query.d.ts +185 -8
- package/dist/query/grouped-query.d.ts.map +1 -1
- package/dist/query/grouped-query.js +516 -30
- package/dist/query/grouped-query.js.map +1 -1
- package/dist/query/join-builder.d.ts +5 -4
- package/dist/query/join-builder.d.ts.map +1 -1
- package/dist/query/join-builder.js +11 -33
- package/dist/query/join-builder.js.map +1 -1
- package/dist/query/query-builder.d.ts +89 -20
- package/dist/query/query-builder.d.ts.map +1 -1
- package/dist/query/query-builder.js +317 -168
- package/dist/query/query-builder.js.map +1 -1
- package/dist/query/query-utils.d.ts +45 -0
- package/dist/query/query-utils.d.ts.map +1 -0
- package/dist/query/query-utils.js +103 -0
- package/dist/query/query-utils.js.map +1 -0
- package/dist/query/sql-utils.d.ts +83 -0
- package/dist/query/sql-utils.d.ts.map +1 -0
- package/dist/query/sql-utils.js +218 -0
- package/dist/query/sql-utils.js.map +1 -0
- package/dist/query/strategies/cte-collection-strategy.d.ts +85 -0
- package/dist/query/strategies/cte-collection-strategy.d.ts.map +1 -0
- package/dist/query/strategies/cte-collection-strategy.js +338 -0
- package/dist/query/strategies/cte-collection-strategy.js.map +1 -0
- package/dist/query/strategies/lateral-collection-strategy.d.ts +59 -0
- package/dist/query/strategies/lateral-collection-strategy.d.ts.map +1 -0
- package/dist/query/strategies/lateral-collection-strategy.js +243 -0
- package/dist/query/strategies/lateral-collection-strategy.js.map +1 -0
- package/dist/query/strategies/temptable-collection-strategy.d.ts +21 -0
- package/dist/query/strategies/temptable-collection-strategy.d.ts.map +1 -1
- package/dist/query/strategies/temptable-collection-strategy.js +216 -94
- package/dist/query/strategies/temptable-collection-strategy.js.map +1 -1
- package/dist/query/subquery.d.ts +24 -1
- package/dist/query/subquery.d.ts.map +1 -1
- package/dist/query/subquery.js +38 -2
- package/dist/query/subquery.js.map +1 -1
- package/package.json +1 -1
- package/dist/query/strategies/jsonb-collection-strategy.d.ts +0 -51
- package/dist/query/strategies/jsonb-collection-strategy.d.ts.map +0 -1
- package/dist/query/strategies/jsonb-collection-strategy.js +0 -210
- package/dist/query/strategies/jsonb-collection-strategy.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grouped-query.d.ts","sourceRoot":"","sources":["../../src/query/grouped-query.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,
|
|
1
|
+
{"version":3,"file":"grouped-query.d.ts","sourceRoot":"","sources":["../../src/query/grouped-query.ts"],"names":[],"mappings":"AAAA,O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KAAK,SAAS,QAAQ,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,EAC7D,IAAI,GAAE,KAAwB,GAC7B,QAAQ,CAAC,KAAK,SAAS,QAAQ,GAAG,gBAAgB,CAAC,UAAU,CAAC,GAAG,KAAK,SAAS,OAAO,GAAG,gBAAgB,CAAC,UAAU,CAAC,EAAE,GAAG,gBAAgB,CAAC,UAAU,CAAC,EAAE,KAAK,CAAC;IAuBjK;;;OAGG;IACH,aAAa,CAAC,YAAY,EAAE,YAAY,GAAG;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,GAAG,EAAE,CAAA;KAAE;IAIzE;;OAEG;IACH,OAAO,CAAC,UAAU;CA6FnB"}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GroupedSelectQueryBuilder = exports.GroupedQueryBuilder = void 0;
|
|
3
|
+
exports.GroupedJoinedQueryBuilder = exports.GroupedSelectQueryBuilder = exports.GroupedQueryBuilder = void 0;
|
|
4
4
|
const conditions_1 = require("./conditions");
|
|
5
|
+
const query_utils_1 = require("./query-utils");
|
|
5
6
|
const subquery_1 = require("./subquery");
|
|
7
|
+
const query_builder_1 = require("./query-builder");
|
|
8
|
+
const cte_builder_1 = require("./cte-builder");
|
|
6
9
|
/**
|
|
7
10
|
* Create an aggregate field reference that can be used in conditions
|
|
8
11
|
*/
|
|
@@ -93,6 +96,36 @@ class GroupedQueryBuilder {
|
|
|
93
96
|
configurable: true,
|
|
94
97
|
});
|
|
95
98
|
}
|
|
99
|
+
// Add navigation properties (collections and single references)
|
|
100
|
+
const relationSchemas = new Map();
|
|
101
|
+
for (const [relName, relConfig] of Object.entries(this.schema.relations)) {
|
|
102
|
+
if (relConfig.targetTableBuilder) {
|
|
103
|
+
relationSchemas.set(relName, relConfig.targetTableBuilder.build());
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
for (const [relName, relConfig] of Object.entries(this.schema.relations)) {
|
|
107
|
+
if (relConfig.type === 'many') {
|
|
108
|
+
const targetSchema = relationSchemas.get(relName);
|
|
109
|
+
Object.defineProperty(mock, relName, {
|
|
110
|
+
get: () => {
|
|
111
|
+
return new query_builder_1.CollectionQueryBuilder(relName, relConfig.targetTable, relConfig.foreignKey || relConfig.foreignKeys?.[0] || '', this.schema.name, targetSchema);
|
|
112
|
+
},
|
|
113
|
+
enumerable: true,
|
|
114
|
+
configurable: true,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
const targetSchema = relationSchemas.get(relName);
|
|
119
|
+
Object.defineProperty(mock, relName, {
|
|
120
|
+
get: () => {
|
|
121
|
+
const refBuilder = new query_builder_1.ReferenceQueryBuilder(relName, relConfig.targetTable, relConfig.foreignKeys || [relConfig.foreignKey || ''], relConfig.matches || [], relConfig.isMandatory ?? false, targetSchema);
|
|
122
|
+
return refBuilder.createMockTargetRow();
|
|
123
|
+
},
|
|
124
|
+
enumerable: true,
|
|
125
|
+
configurable: true,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
96
129
|
// Add columns from manually joined tables
|
|
97
130
|
for (const join of this.manualJoins) {
|
|
98
131
|
if (join.isSubquery || !join.schema) {
|
|
@@ -166,35 +199,7 @@ class GroupedSelectQueryBuilder {
|
|
|
166
199
|
const mockGroup = this.createMockGroupedItem();
|
|
167
200
|
const mockResult = this.resultSelector(mockGroup);
|
|
168
201
|
const result = selector(mockResult);
|
|
169
|
-
|
|
170
|
-
if (Array.isArray(result) && result.length > 0 && Array.isArray(result[0])) {
|
|
171
|
-
for (const [fieldRef, direction] of result) {
|
|
172
|
-
if (fieldRef && typeof fieldRef === 'object' && '__fieldName' in fieldRef) {
|
|
173
|
-
this.orderByFields.push({
|
|
174
|
-
field: fieldRef.__dbColumnName || fieldRef.__fieldName,
|
|
175
|
-
direction: direction || 'ASC'
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
// Handle array of fields (all ASC)
|
|
181
|
-
else if (Array.isArray(result)) {
|
|
182
|
-
for (const fieldRef of result) {
|
|
183
|
-
if (fieldRef && typeof fieldRef === 'object' && '__fieldName' in fieldRef) {
|
|
184
|
-
this.orderByFields.push({
|
|
185
|
-
field: fieldRef.__dbColumnName || fieldRef.__fieldName,
|
|
186
|
-
direction: 'ASC'
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
// Handle single field
|
|
192
|
-
else if (result && typeof result === 'object' && '__fieldName' in result) {
|
|
193
|
-
this.orderByFields.push({
|
|
194
|
-
field: result.__dbColumnName || result.__fieldName,
|
|
195
|
-
direction: 'ASC'
|
|
196
|
-
});
|
|
197
|
-
}
|
|
202
|
+
(0, query_utils_1.parseOrderBy)(result, this.orderByFields);
|
|
198
203
|
return this;
|
|
199
204
|
}
|
|
200
205
|
/**
|
|
@@ -279,6 +284,167 @@ class GroupedSelectQueryBuilder {
|
|
|
279
284
|
};
|
|
280
285
|
return new subquery_1.Subquery(sqlBuilder, mode);
|
|
281
286
|
}
|
|
287
|
+
/**
|
|
288
|
+
* Build SQL for use in CTEs - public interface for CTE builder
|
|
289
|
+
* @internal
|
|
290
|
+
*/
|
|
291
|
+
buildCteQuery(queryContext) {
|
|
292
|
+
return this.buildQuery(queryContext);
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Add a LEFT JOIN to the grouped query result
|
|
296
|
+
* This wraps the grouped query as a subquery and joins to it
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* const result = await db.orders
|
|
300
|
+
* .select(o => ({ customerId: o.customerId, total: o.total }))
|
|
301
|
+
* .groupBy(o => ({ customerId: o.customerId }))
|
|
302
|
+
* .select(g => ({ customerId: g.key.customerId, totalSum: g.sum(o => o.total) }))
|
|
303
|
+
* .leftJoin(
|
|
304
|
+
* customerDetailsCte,
|
|
305
|
+
* (grouped, details) => eq(grouped.customerId, details.customerId),
|
|
306
|
+
* (grouped, details) => ({ ...grouped, details: details.items })
|
|
307
|
+
* )
|
|
308
|
+
* .toList();
|
|
309
|
+
*/
|
|
310
|
+
leftJoin(rightSource, condition, selector, alias) {
|
|
311
|
+
return this.joinInternal('LEFT', rightSource, condition, selector, alias);
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Add an INNER JOIN to the grouped query result
|
|
315
|
+
* This wraps the grouped query as a subquery and joins to it
|
|
316
|
+
*/
|
|
317
|
+
innerJoin(rightSource, condition, selector, alias) {
|
|
318
|
+
return this.joinInternal('INNER', rightSource, condition, selector, alias);
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Internal join implementation
|
|
322
|
+
*/
|
|
323
|
+
joinInternal(joinType, rightSource, condition, selector, alias) {
|
|
324
|
+
// Wrap this grouped query as a subquery
|
|
325
|
+
const leftSubquery = this.asSubquery('table');
|
|
326
|
+
const leftAlias = 'grouped_0';
|
|
327
|
+
// Determine the right alias and source info
|
|
328
|
+
let rightAlias;
|
|
329
|
+
let isCteJoin = false;
|
|
330
|
+
let cte;
|
|
331
|
+
if ((0, cte_builder_1.isCte)(rightSource)) {
|
|
332
|
+
rightAlias = rightSource.name;
|
|
333
|
+
isCteJoin = true;
|
|
334
|
+
cte = rightSource;
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
if (!alias) {
|
|
338
|
+
throw new Error('Alias is required when joining a subquery');
|
|
339
|
+
}
|
|
340
|
+
rightAlias = alias;
|
|
341
|
+
}
|
|
342
|
+
// Create mock for left (the grouped query result)
|
|
343
|
+
const mockLeft = this.createMockForSelection(leftAlias);
|
|
344
|
+
// Create mock for right - at runtime these are already FieldRef-like objects
|
|
345
|
+
const mockRight = (isCteJoin
|
|
346
|
+
? this.createMockForCte(cte)
|
|
347
|
+
: this.createMockForSubquery(rightAlias, rightSource));
|
|
348
|
+
// Evaluate the join condition
|
|
349
|
+
const joinCondition = condition(mockLeft, mockRight);
|
|
350
|
+
// Create the result selector
|
|
351
|
+
const createLeftMock = () => this.createMockForSelection(leftAlias);
|
|
352
|
+
const createRightMock = () => (isCteJoin
|
|
353
|
+
? this.createMockForCte(cte)
|
|
354
|
+
: this.createMockForSubquery(rightAlias, rightSource));
|
|
355
|
+
return new GroupedJoinedQueryBuilder(this.schema, this.client, leftSubquery, leftAlias, rightSource, rightAlias, joinType, joinCondition, selector, createLeftMock, createRightMock, this.executor, isCteJoin ? cte : undefined);
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Create a mock object for the current selection (for join conditions)
|
|
359
|
+
* The key is the alias used in the SELECT clause, so we use it as __dbColumnName
|
|
360
|
+
*/
|
|
361
|
+
createMockForSelection(alias) {
|
|
362
|
+
const mockGroup = this.createMockGroupedItem();
|
|
363
|
+
const mockResult = this.resultSelector(mockGroup);
|
|
364
|
+
// Wrap with alias - always use the key as the column name since
|
|
365
|
+
// that's what the subquery SELECT clause uses as the alias
|
|
366
|
+
const wrapped = {};
|
|
367
|
+
for (const [key, value] of Object.entries(mockResult)) {
|
|
368
|
+
// Preserve mapper if present
|
|
369
|
+
const mapper = (typeof value === 'object' && value !== null && typeof value.getMapper === 'function')
|
|
370
|
+
? { getMapper: () => value.getMapper() }
|
|
371
|
+
: {};
|
|
372
|
+
wrapped[key] = {
|
|
373
|
+
__fieldName: key,
|
|
374
|
+
__dbColumnName: key, // Use key as column name (the subquery alias)
|
|
375
|
+
__tableAlias: alias,
|
|
376
|
+
...mapper,
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
return wrapped;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Create a mock for a subquery result
|
|
383
|
+
*/
|
|
384
|
+
createMockForSubquery(alias, subquery) {
|
|
385
|
+
const selectionMetadata = subquery.getSelectionMetadata();
|
|
386
|
+
return new Proxy({}, {
|
|
387
|
+
get(target, prop) {
|
|
388
|
+
if (typeof prop === 'symbol')
|
|
389
|
+
return undefined;
|
|
390
|
+
if (selectionMetadata && prop in selectionMetadata) {
|
|
391
|
+
const value = selectionMetadata[prop];
|
|
392
|
+
if (typeof value === 'object' && value !== null && typeof value.getMapper === 'function') {
|
|
393
|
+
return {
|
|
394
|
+
__fieldName: prop,
|
|
395
|
+
__dbColumnName: prop,
|
|
396
|
+
__tableAlias: alias,
|
|
397
|
+
getMapper: () => value.getMapper(),
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return {
|
|
402
|
+
__fieldName: prop,
|
|
403
|
+
__dbColumnName: prop,
|
|
404
|
+
__tableAlias: alias,
|
|
405
|
+
};
|
|
406
|
+
},
|
|
407
|
+
has() { return true; },
|
|
408
|
+
ownKeys() { return []; },
|
|
409
|
+
getOwnPropertyDescriptor() {
|
|
410
|
+
return { enumerable: true, configurable: true };
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Create a mock for a CTE
|
|
416
|
+
*/
|
|
417
|
+
createMockForCte(cte) {
|
|
418
|
+
const alias = cte.name;
|
|
419
|
+
const selectionMetadata = cte.selectionMetadata;
|
|
420
|
+
return new Proxy({}, {
|
|
421
|
+
get(target, prop) {
|
|
422
|
+
if (typeof prop === 'symbol')
|
|
423
|
+
return undefined;
|
|
424
|
+
if (selectionMetadata && prop in selectionMetadata) {
|
|
425
|
+
const value = selectionMetadata[prop];
|
|
426
|
+
if (typeof value === 'object' && value !== null && typeof value.getMapper === 'function') {
|
|
427
|
+
return {
|
|
428
|
+
__fieldName: prop,
|
|
429
|
+
__dbColumnName: prop,
|
|
430
|
+
__tableAlias: alias,
|
|
431
|
+
getMapper: () => value.getMapper(),
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
return {
|
|
436
|
+
__fieldName: prop,
|
|
437
|
+
__dbColumnName: prop,
|
|
438
|
+
__tableAlias: alias,
|
|
439
|
+
};
|
|
440
|
+
},
|
|
441
|
+
has() { return true; },
|
|
442
|
+
ownKeys() { return []; },
|
|
443
|
+
getOwnPropertyDescriptor() {
|
|
444
|
+
return { enumerable: true, configurable: true };
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
}
|
|
282
448
|
/**
|
|
283
449
|
* Build the SQL query for grouped results
|
|
284
450
|
*/
|
|
@@ -370,8 +536,27 @@ class GroupedSelectQueryBuilder {
|
|
|
370
536
|
selectParts.push(`${fragmentSql} as "${alias}"`);
|
|
371
537
|
}
|
|
372
538
|
}
|
|
539
|
+
// Detect navigation property references in WHERE and add JOINs
|
|
540
|
+
const navigationJoins = [];
|
|
541
|
+
// Detect joins from the original selection (navigation properties used in select)
|
|
542
|
+
this.detectAndAddJoinsFromSelection(mockOriginalSelection, navigationJoins);
|
|
543
|
+
// Detect joins from WHERE condition
|
|
544
|
+
this.detectAndAddJoinsFromCondition(this.whereCond, navigationJoins);
|
|
373
545
|
// Build FROM clause with JOINs
|
|
374
546
|
let fromClause = `FROM "${this.schema.name}"`;
|
|
547
|
+
// Add navigation property JOINs first
|
|
548
|
+
for (const navJoin of navigationJoins) {
|
|
549
|
+
const joinType = navJoin.isMandatory ? 'INNER JOIN' : 'LEFT JOIN';
|
|
550
|
+
const targetTableName = navJoin.targetSchema
|
|
551
|
+
? `"${navJoin.targetSchema}"."${navJoin.targetTable}"`
|
|
552
|
+
: `"${navJoin.targetTable}"`;
|
|
553
|
+
// Build join condition: source.foreignKey = target.match
|
|
554
|
+
const joinConditions = navJoin.foreignKeys.map((fk, i) => {
|
|
555
|
+
const targetCol = navJoin.matches[i] || 'id';
|
|
556
|
+
return `"${this.schema.name}"."${fk}" = "${navJoin.alias}"."${targetCol}"`;
|
|
557
|
+
});
|
|
558
|
+
fromClause += `\n${joinType} ${targetTableName} AS "${navJoin.alias}" ON ${joinConditions.join(' AND ')}`;
|
|
559
|
+
}
|
|
375
560
|
// Add manual JOINs
|
|
376
561
|
for (const manualJoin of this.manualJoins) {
|
|
377
562
|
const joinTypeStr = manualJoin.type === 'INNER' ? 'INNER JOIN' : 'LEFT JOIN';
|
|
@@ -449,6 +634,36 @@ class GroupedSelectQueryBuilder {
|
|
|
449
634
|
configurable: true,
|
|
450
635
|
});
|
|
451
636
|
}
|
|
637
|
+
// Add navigation properties (collections and single references)
|
|
638
|
+
const relationSchemas = new Map();
|
|
639
|
+
for (const [relName, relConfig] of Object.entries(this.schema.relations)) {
|
|
640
|
+
if (relConfig.targetTableBuilder) {
|
|
641
|
+
relationSchemas.set(relName, relConfig.targetTableBuilder.build());
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
for (const [relName, relConfig] of Object.entries(this.schema.relations)) {
|
|
645
|
+
if (relConfig.type === 'many') {
|
|
646
|
+
const targetSchema = relationSchemas.get(relName);
|
|
647
|
+
Object.defineProperty(mock, relName, {
|
|
648
|
+
get: () => {
|
|
649
|
+
return new query_builder_1.CollectionQueryBuilder(relName, relConfig.targetTable, relConfig.foreignKey || relConfig.foreignKeys?.[0] || '', this.schema.name, targetSchema);
|
|
650
|
+
},
|
|
651
|
+
enumerable: true,
|
|
652
|
+
configurable: true,
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
else {
|
|
656
|
+
const targetSchema = relationSchemas.get(relName);
|
|
657
|
+
Object.defineProperty(mock, relName, {
|
|
658
|
+
get: () => {
|
|
659
|
+
const refBuilder = new query_builder_1.ReferenceQueryBuilder(relName, relConfig.targetTable, relConfig.foreignKeys || [relConfig.foreignKey || ''], relConfig.matches || [], relConfig.isMandatory ?? false, targetSchema);
|
|
660
|
+
return refBuilder.createMockTargetRow();
|
|
661
|
+
},
|
|
662
|
+
enumerable: true,
|
|
663
|
+
configurable: true,
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
}
|
|
452
667
|
// Add columns from manually joined tables
|
|
453
668
|
for (const join of this.manualJoins) {
|
|
454
669
|
if (join.isSubquery || !join.schema) {
|
|
@@ -498,6 +713,82 @@ class GroupedSelectQueryBuilder {
|
|
|
498
713
|
},
|
|
499
714
|
};
|
|
500
715
|
}
|
|
716
|
+
/**
|
|
717
|
+
* Detect navigation property references in a WHERE condition and add necessary JOINs
|
|
718
|
+
*/
|
|
719
|
+
detectAndAddJoinsFromCondition(condition, joins) {
|
|
720
|
+
if (!condition) {
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
// Get all field references from the condition
|
|
724
|
+
const fieldRefs = condition.getFieldRefs();
|
|
725
|
+
for (const fieldRef of fieldRefs) {
|
|
726
|
+
if ('__tableAlias' in fieldRef && fieldRef.__tableAlias) {
|
|
727
|
+
const tableAlias = fieldRef.__tableAlias;
|
|
728
|
+
// Check if this references a related table that isn't already joined
|
|
729
|
+
if (tableAlias !== this.schema.name && !joins.some(j => j.alias === tableAlias)) {
|
|
730
|
+
// Find the relation config for this navigation
|
|
731
|
+
const relation = this.schema.relations[tableAlias];
|
|
732
|
+
if (relation && relation.type === 'one') {
|
|
733
|
+
// Get target schema from targetTableBuilder if available
|
|
734
|
+
let targetSchema;
|
|
735
|
+
if (relation.targetTableBuilder) {
|
|
736
|
+
const targetTableSchema = relation.targetTableBuilder.build();
|
|
737
|
+
targetSchema = targetTableSchema.schema;
|
|
738
|
+
}
|
|
739
|
+
// Add a JOIN for this reference
|
|
740
|
+
joins.push({
|
|
741
|
+
alias: tableAlias,
|
|
742
|
+
targetTable: relation.targetTable,
|
|
743
|
+
targetSchema,
|
|
744
|
+
foreignKeys: relation.foreignKeys || [relation.foreignKey || ''],
|
|
745
|
+
matches: relation.matches || [],
|
|
746
|
+
isMandatory: relation.isMandatory ?? false,
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Detect navigation properties in a selection and add JOINs for them
|
|
755
|
+
*/
|
|
756
|
+
detectAndAddJoinsFromSelection(selection, joins) {
|
|
757
|
+
if (!selection || typeof selection !== 'object') {
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
for (const [, value] of Object.entries(selection)) {
|
|
761
|
+
if (value && typeof value === 'object' && '__tableAlias' in value && '__dbColumnName' in value) {
|
|
762
|
+
// This is a FieldRef with a table alias - check if it's from a related table
|
|
763
|
+
const tableAlias = value.__tableAlias;
|
|
764
|
+
if (tableAlias !== this.schema.name && !joins.some(j => j.alias === tableAlias)) {
|
|
765
|
+
// This references a related table - find the relation and add a JOIN
|
|
766
|
+
const relation = this.schema.relations[tableAlias];
|
|
767
|
+
if (relation && relation.type === 'one') {
|
|
768
|
+
// Get target schema from targetTableBuilder if available
|
|
769
|
+
let targetSchema;
|
|
770
|
+
if (relation.targetTableBuilder) {
|
|
771
|
+
const targetTableSchema = relation.targetTableBuilder.build();
|
|
772
|
+
targetSchema = targetTableSchema.schema;
|
|
773
|
+
}
|
|
774
|
+
// Add a JOIN for this reference
|
|
775
|
+
joins.push({
|
|
776
|
+
alias: tableAlias,
|
|
777
|
+
targetTable: relation.targetTable,
|
|
778
|
+
targetSchema,
|
|
779
|
+
foreignKeys: relation.foreignKeys || [relation.foreignKey || ''],
|
|
780
|
+
matches: relation.matches || [],
|
|
781
|
+
isMandatory: relation.isMandatory ?? false,
|
|
782
|
+
});
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
else if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
787
|
+
// Recursively check nested objects
|
|
788
|
+
this.detectAndAddJoinsFromSelection(value, joins);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
}
|
|
501
792
|
/**
|
|
502
793
|
* Build HAVING condition SQL - handles aggregate field refs specially
|
|
503
794
|
*/
|
|
@@ -585,4 +876,199 @@ class GroupedSelectQueryBuilder {
|
|
|
585
876
|
}
|
|
586
877
|
}
|
|
587
878
|
exports.GroupedSelectQueryBuilder = GroupedSelectQueryBuilder;
|
|
879
|
+
/**
|
|
880
|
+
* Query builder for grouped queries that have been joined
|
|
881
|
+
* This handles the case where a GroupedSelectQueryBuilder is joined with a CTE or subquery
|
|
882
|
+
*/
|
|
883
|
+
class GroupedJoinedQueryBuilder {
|
|
884
|
+
constructor(schema, client, leftSubquery, leftAlias, rightSource, rightAlias, joinType, joinCondition, resultSelector, createLeftMock, createRightMock, executor, cte) {
|
|
885
|
+
this.orderByFields = [];
|
|
886
|
+
this.additionalJoins = [];
|
|
887
|
+
this.schema = schema;
|
|
888
|
+
this.client = client;
|
|
889
|
+
this.leftSubquery = leftSubquery;
|
|
890
|
+
this.leftAlias = leftAlias;
|
|
891
|
+
this.rightSource = rightSource;
|
|
892
|
+
this.rightAlias = rightAlias;
|
|
893
|
+
this.joinType = joinType;
|
|
894
|
+
this.joinCondition = joinCondition;
|
|
895
|
+
this.resultSelector = resultSelector;
|
|
896
|
+
this.createLeftMock = createLeftMock;
|
|
897
|
+
this.createRightMock = createRightMock;
|
|
898
|
+
this.executor = executor;
|
|
899
|
+
this.cte = cte;
|
|
900
|
+
}
|
|
901
|
+
/**
|
|
902
|
+
* Limit results
|
|
903
|
+
*/
|
|
904
|
+
limit(count) {
|
|
905
|
+
this.limitValue = count;
|
|
906
|
+
return this;
|
|
907
|
+
}
|
|
908
|
+
/**
|
|
909
|
+
* Offset results
|
|
910
|
+
*/
|
|
911
|
+
offset(count) {
|
|
912
|
+
this.offsetValue = count;
|
|
913
|
+
return this;
|
|
914
|
+
}
|
|
915
|
+
orderBy(selector) {
|
|
916
|
+
const mockLeft = this.createLeftMock();
|
|
917
|
+
const mockRight = this.createRightMock();
|
|
918
|
+
const mockResult = this.resultSelector(mockLeft, mockRight);
|
|
919
|
+
const result = selector(mockResult);
|
|
920
|
+
(0, query_utils_1.parseOrderBy)(result, this.orderByFields, query_utils_1.getQualifiedFieldName);
|
|
921
|
+
return this;
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* Execute query and return results
|
|
925
|
+
*/
|
|
926
|
+
async toList() {
|
|
927
|
+
const context = {
|
|
928
|
+
ctes: new Map(),
|
|
929
|
+
cteCounter: 0,
|
|
930
|
+
paramCounter: 1,
|
|
931
|
+
allParams: [],
|
|
932
|
+
};
|
|
933
|
+
const { sql, params } = this.buildQuery(context);
|
|
934
|
+
const result = this.executor
|
|
935
|
+
? await this.executor.query(sql, params)
|
|
936
|
+
: await this.client.query(sql, params);
|
|
937
|
+
return result.rows;
|
|
938
|
+
}
|
|
939
|
+
/**
|
|
940
|
+
* Execute query and return first result or null
|
|
941
|
+
*/
|
|
942
|
+
async first() {
|
|
943
|
+
const results = await this.limit(1).toList();
|
|
944
|
+
return results.length > 0 ? results[0] : null;
|
|
945
|
+
}
|
|
946
|
+
/**
|
|
947
|
+
* Execute query and return first result or throw
|
|
948
|
+
*/
|
|
949
|
+
async firstOrThrow() {
|
|
950
|
+
const result = await this.first();
|
|
951
|
+
if (!result) {
|
|
952
|
+
throw new Error('No results found');
|
|
953
|
+
}
|
|
954
|
+
return result;
|
|
955
|
+
}
|
|
956
|
+
/**
|
|
957
|
+
* Convert to subquery for use in other queries
|
|
958
|
+
*/
|
|
959
|
+
asSubquery(mode = 'table') {
|
|
960
|
+
const sqlBuilder = (outerContext) => {
|
|
961
|
+
const context = {
|
|
962
|
+
ctes: new Map(),
|
|
963
|
+
cteCounter: 0,
|
|
964
|
+
paramCounter: outerContext.paramCounter,
|
|
965
|
+
allParams: outerContext.params,
|
|
966
|
+
};
|
|
967
|
+
const { sql } = this.buildQuery(context);
|
|
968
|
+
outerContext.paramCounter = context.paramCounter;
|
|
969
|
+
return sql;
|
|
970
|
+
};
|
|
971
|
+
// Preserve selection metadata for mappers
|
|
972
|
+
const mockLeft = this.createLeftMock();
|
|
973
|
+
const mockRight = this.createRightMock();
|
|
974
|
+
const selectionMetadata = this.resultSelector(mockLeft, mockRight);
|
|
975
|
+
return new subquery_1.Subquery(sqlBuilder, mode, selectionMetadata);
|
|
976
|
+
}
|
|
977
|
+
/**
|
|
978
|
+
* Build SQL for use in CTEs - public interface for CTE builder
|
|
979
|
+
* @internal
|
|
980
|
+
*/
|
|
981
|
+
buildCteQuery(queryContext) {
|
|
982
|
+
return this.buildQuery(queryContext);
|
|
983
|
+
}
|
|
984
|
+
/**
|
|
985
|
+
* Build the SQL query
|
|
986
|
+
*/
|
|
987
|
+
buildQuery(context) {
|
|
988
|
+
// Build CTE clause if needed
|
|
989
|
+
let cteClause = '';
|
|
990
|
+
if (this.cte) {
|
|
991
|
+
cteClause = `WITH "${this.cte.name}" AS (${this.cte.query})\n`;
|
|
992
|
+
context.allParams.push(...this.cte.params);
|
|
993
|
+
context.paramCounter += this.cte.params.length;
|
|
994
|
+
}
|
|
995
|
+
// Build SELECT clause from result selector
|
|
996
|
+
const mockLeft = this.createLeftMock();
|
|
997
|
+
const mockRight = this.createRightMock();
|
|
998
|
+
const mockResult = this.resultSelector(mockLeft, mockRight);
|
|
999
|
+
const selectParts = [];
|
|
1000
|
+
for (const [alias, value] of Object.entries(mockResult)) {
|
|
1001
|
+
if (typeof value === 'object' && value !== null && '__dbColumnName' in value) {
|
|
1002
|
+
const field = value;
|
|
1003
|
+
const tableAlias = field.__tableAlias;
|
|
1004
|
+
if (tableAlias) {
|
|
1005
|
+
selectParts.push(`"${tableAlias}"."${field.__dbColumnName}" as "${alias}"`);
|
|
1006
|
+
}
|
|
1007
|
+
else {
|
|
1008
|
+
selectParts.push(`"${field.__dbColumnName}" as "${alias}"`);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
else if (value instanceof conditions_1.SqlFragment) {
|
|
1012
|
+
const sqlBuildContext = {
|
|
1013
|
+
paramCounter: context.paramCounter,
|
|
1014
|
+
params: context.allParams,
|
|
1015
|
+
};
|
|
1016
|
+
const fragmentSql = value.buildSql(sqlBuildContext);
|
|
1017
|
+
context.paramCounter = sqlBuildContext.paramCounter;
|
|
1018
|
+
selectParts.push(`${fragmentSql} as "${alias}"`);
|
|
1019
|
+
}
|
|
1020
|
+
else {
|
|
1021
|
+
selectParts.push(`"${alias}"`);
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
// Build FROM clause with the left subquery
|
|
1025
|
+
const leftSqlContext = {
|
|
1026
|
+
paramCounter: context.paramCounter,
|
|
1027
|
+
params: context.allParams,
|
|
1028
|
+
};
|
|
1029
|
+
const leftSql = this.leftSubquery.buildSql(leftSqlContext);
|
|
1030
|
+
context.paramCounter = leftSqlContext.paramCounter;
|
|
1031
|
+
let fromClause = `FROM (${leftSql}) AS "${this.leftAlias}"`;
|
|
1032
|
+
// Build JOIN clause
|
|
1033
|
+
const joinTypeStr = this.joinType === 'INNER' ? 'INNER JOIN' : 'LEFT JOIN';
|
|
1034
|
+
const condBuilder = new conditions_1.ConditionBuilder();
|
|
1035
|
+
const { sql: condSql, params: condParams } = condBuilder.build(this.joinCondition, context.paramCounter);
|
|
1036
|
+
context.paramCounter += condParams.length;
|
|
1037
|
+
context.allParams.push(...condParams);
|
|
1038
|
+
if (this.cte) {
|
|
1039
|
+
// Join to CTE
|
|
1040
|
+
fromClause += `\n${joinTypeStr} "${this.rightAlias}" ON ${condSql}`;
|
|
1041
|
+
}
|
|
1042
|
+
else {
|
|
1043
|
+
// Join to subquery
|
|
1044
|
+
const rightSqlContext = {
|
|
1045
|
+
paramCounter: context.paramCounter,
|
|
1046
|
+
params: context.allParams,
|
|
1047
|
+
};
|
|
1048
|
+
const rightSql = this.rightSource.buildSql(rightSqlContext);
|
|
1049
|
+
context.paramCounter = rightSqlContext.paramCounter;
|
|
1050
|
+
fromClause += `\n${joinTypeStr} (${rightSql}) AS "${this.rightAlias}" ON ${condSql}`;
|
|
1051
|
+
}
|
|
1052
|
+
// Build ORDER BY clause
|
|
1053
|
+
let orderByClause = '';
|
|
1054
|
+
if (this.orderByFields.length > 0) {
|
|
1055
|
+
const orderParts = this.orderByFields.map(({ field, direction }) => `${field} ${direction}`);
|
|
1056
|
+
orderByClause = `ORDER BY ${orderParts.join(', ')}`;
|
|
1057
|
+
}
|
|
1058
|
+
// Build LIMIT/OFFSET
|
|
1059
|
+
let limitClause = '';
|
|
1060
|
+
if (this.limitValue !== undefined) {
|
|
1061
|
+
limitClause = `LIMIT ${this.limitValue}`;
|
|
1062
|
+
}
|
|
1063
|
+
if (this.offsetValue !== undefined) {
|
|
1064
|
+
limitClause += ` OFFSET ${this.offsetValue}`;
|
|
1065
|
+
}
|
|
1066
|
+
const finalQuery = `${cteClause}SELECT ${selectParts.join(', ')}\n${fromClause}\n${orderByClause}\n${limitClause}`.trim();
|
|
1067
|
+
return {
|
|
1068
|
+
sql: finalQuery,
|
|
1069
|
+
params: context.allParams,
|
|
1070
|
+
};
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
exports.GroupedJoinedQueryBuilder = GroupedJoinedQueryBuilder;
|
|
588
1074
|
//# sourceMappingURL=grouped-query.js.map
|