linkgress-orm 0.0.1 → 0.0.3
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/database/database-client.interface.d.ts +19 -3
- package/dist/database/database-client.interface.d.ts.map +1 -1
- package/dist/database/database-client.interface.js +7 -0
- package/dist/database/database-client.interface.js.map +1 -1
- package/dist/database/pg-client.d.ts +12 -3
- package/dist/database/pg-client.d.ts.map +1 -1
- package/dist/database/pg-client.js +47 -12
- package/dist/database/pg-client.js.map +1 -1
- package/dist/database/postgres-client.d.ts +13 -3
- package/dist/database/postgres-client.d.ts.map +1 -1
- package/dist/database/postgres-client.js +42 -10
- package/dist/database/postgres-client.js.map +1 -1
- package/dist/entity/db-context.d.ts +18 -0
- package/dist/entity/db-context.d.ts.map +1 -1
- package/dist/entity/db-context.js +11 -1
- package/dist/entity/db-context.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -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/conditions.d.ts +44 -1
- package/dist/query/conditions.d.ts.map +1 -1
- package/dist/query/conditions.js +107 -4
- package/dist/query/conditions.js.map +1 -1
- package/dist/query/cte-builder.js +5 -5
- package/dist/query/query-builder.d.ts.map +1 -1
- package/dist/query/query-builder.js +100 -51
- package/dist/query/query-builder.js.map +1 -1
- package/dist/query/strategies/jsonb-collection-strategy.js +38 -38
- package/dist/query/strategies/temptable-collection-strategy.js +62 -62
- package/package.json +1 -1
|
@@ -110,20 +110,20 @@ class JsonbCollectionStrategy {
|
|
|
110
110
|
const distinctClause = isDistinct ? 'DISTINCT ' : '';
|
|
111
111
|
// Build the jsonb_agg ORDER BY clause
|
|
112
112
|
const jsonbAggOrderBy = orderByClause ? ` ORDER BY ${orderByClause}` : '';
|
|
113
|
-
const cteSQL = `
|
|
114
|
-
SELECT
|
|
115
|
-
"__fk_${foreignKey}" as parent_id,
|
|
116
|
-
jsonb_agg(
|
|
117
|
-
jsonb_build_object(${jsonbFields})${jsonbAggOrderBy}
|
|
118
|
-
) as data
|
|
119
|
-
FROM (
|
|
120
|
-
SELECT ${distinctClause}${allSelectFields.join(', ')}
|
|
121
|
-
FROM "${targetTable}"
|
|
122
|
-
${whereSQL}
|
|
123
|
-
${orderBySQL}
|
|
124
|
-
${limitOffsetClause}
|
|
125
|
-
) sub
|
|
126
|
-
GROUP BY "__fk_${foreignKey}"
|
|
113
|
+
const cteSQL = `
|
|
114
|
+
SELECT
|
|
115
|
+
"__fk_${foreignKey}" as parent_id,
|
|
116
|
+
jsonb_agg(
|
|
117
|
+
jsonb_build_object(${jsonbFields})${jsonbAggOrderBy}
|
|
118
|
+
) as data
|
|
119
|
+
FROM (
|
|
120
|
+
SELECT ${distinctClause}${allSelectFields.join(', ')}
|
|
121
|
+
FROM "${targetTable}"
|
|
122
|
+
${whereSQL}
|
|
123
|
+
${orderBySQL}
|
|
124
|
+
${limitOffsetClause}
|
|
125
|
+
) sub
|
|
126
|
+
GROUP BY "__fk_${foreignKey}"
|
|
127
127
|
`.trim();
|
|
128
128
|
return cteSQL;
|
|
129
129
|
}
|
|
@@ -151,23 +151,23 @@ GROUP BY "__fk_${foreignKey}"
|
|
|
151
151
|
const distinctClause = isDistinct ? 'DISTINCT ' : '';
|
|
152
152
|
// Build the array_agg ORDER BY clause
|
|
153
153
|
const arrayAggOrderBy = orderByClause ? ` ORDER BY ${orderByClause}` : '';
|
|
154
|
-
const cteSQL = `
|
|
155
|
-
SELECT
|
|
156
|
-
"__fk_${foreignKey}" as parent_id,
|
|
157
|
-
array_agg(
|
|
158
|
-
"${arrayField}"${arrayAggOrderBy}
|
|
159
|
-
) as data
|
|
160
|
-
FROM (
|
|
161
|
-
SELECT ${distinctClause}"__fk_${foreignKey}", "${arrayField}"
|
|
162
|
-
FROM (
|
|
163
|
-
SELECT "${foreignKey}" as "__fk_${foreignKey}", "${arrayField}"
|
|
164
|
-
FROM "${targetTable}"
|
|
165
|
-
${whereSQL}
|
|
166
|
-
${orderBySQL}
|
|
167
|
-
${limitOffsetClause}
|
|
168
|
-
) inner_sub
|
|
169
|
-
) sub
|
|
170
|
-
GROUP BY "__fk_${foreignKey}"
|
|
154
|
+
const cteSQL = `
|
|
155
|
+
SELECT
|
|
156
|
+
"__fk_${foreignKey}" as parent_id,
|
|
157
|
+
array_agg(
|
|
158
|
+
"${arrayField}"${arrayAggOrderBy}
|
|
159
|
+
) as data
|
|
160
|
+
FROM (
|
|
161
|
+
SELECT ${distinctClause}"__fk_${foreignKey}", "${arrayField}"
|
|
162
|
+
FROM (
|
|
163
|
+
SELECT "${foreignKey}" as "__fk_${foreignKey}", "${arrayField}"
|
|
164
|
+
FROM "${targetTable}"
|
|
165
|
+
${whereSQL}
|
|
166
|
+
${orderBySQL}
|
|
167
|
+
${limitOffsetClause}
|
|
168
|
+
) inner_sub
|
|
169
|
+
) sub
|
|
170
|
+
GROUP BY "__fk_${foreignKey}"
|
|
171
171
|
`.trim();
|
|
172
172
|
return cteSQL;
|
|
173
173
|
}
|
|
@@ -195,13 +195,13 @@ GROUP BY "__fk_${foreignKey}"
|
|
|
195
195
|
default:
|
|
196
196
|
throw new Error(`Unknown aggregation type: ${aggregationType}`);
|
|
197
197
|
}
|
|
198
|
-
const cteSQL = `
|
|
199
|
-
SELECT
|
|
200
|
-
"${foreignKey}" as parent_id,
|
|
201
|
-
${aggregateExpression} as data
|
|
202
|
-
FROM "${targetTable}"
|
|
203
|
-
${whereSQL}
|
|
204
|
-
GROUP BY "${foreignKey}"
|
|
198
|
+
const cteSQL = `
|
|
199
|
+
SELECT
|
|
200
|
+
"${foreignKey}" as parent_id,
|
|
201
|
+
${aggregateExpression} as data
|
|
202
|
+
FROM "${targetTable}"
|
|
203
|
+
${whereSQL}
|
|
204
|
+
GROUP BY "${foreignKey}"
|
|
205
205
|
`.trim();
|
|
206
206
|
return cteSQL;
|
|
207
207
|
}
|
|
@@ -74,20 +74,20 @@ class TempTableCollectionStrategy {
|
|
|
74
74
|
// Determine if we're doing client-side grouping (JSONB/array) or server-side (scalar)
|
|
75
75
|
const isClientSideGrouping = config.aggregationType === 'jsonb' || config.aggregationType === 'array';
|
|
76
76
|
// Combine everything into a single multi-statement query
|
|
77
|
-
const multiStatementSQL = `
|
|
78
|
-
-- Create temporary table for parent IDs
|
|
79
|
-
CREATE TEMP TABLE ${tempTableName} (
|
|
80
|
-
id integer PRIMARY KEY
|
|
81
|
-
) ON COMMIT DROP;
|
|
82
|
-
|
|
83
|
-
-- Insert parent IDs
|
|
84
|
-
INSERT INTO ${tempTableName} VALUES ${valuePlaceholders};
|
|
85
|
-
|
|
86
|
-
-- Query and return the data
|
|
87
|
-
${aggregationSQL};
|
|
88
|
-
|
|
89
|
-
-- Cleanup
|
|
90
|
-
DROP TABLE IF EXISTS ${tempTableName};
|
|
77
|
+
const multiStatementSQL = `
|
|
78
|
+
-- Create temporary table for parent IDs
|
|
79
|
+
CREATE TEMP TABLE ${tempTableName} (
|
|
80
|
+
id integer PRIMARY KEY
|
|
81
|
+
) ON COMMIT DROP;
|
|
82
|
+
|
|
83
|
+
-- Insert parent IDs
|
|
84
|
+
INSERT INTO ${tempTableName} VALUES ${valuePlaceholders};
|
|
85
|
+
|
|
86
|
+
-- Query and return the data
|
|
87
|
+
${aggregationSQL};
|
|
88
|
+
|
|
89
|
+
-- Cleanup
|
|
90
|
+
DROP TABLE IF EXISTS ${tempTableName};
|
|
91
91
|
`.trim();
|
|
92
92
|
// Execute multi-statement query using querySimple (no parameters)
|
|
93
93
|
const executor = context.executor || client;
|
|
@@ -158,10 +158,10 @@ DROP TABLE IF EXISTS ${tempTableName};
|
|
|
158
158
|
async buildAggregationLegacy(config, context, client) {
|
|
159
159
|
const tempTableName = `tmp_parent_ids_${config.counter}`;
|
|
160
160
|
// Create temp table (without ON COMMIT DROP to persist across queries in the same session)
|
|
161
|
-
const createTableSQL = `
|
|
162
|
-
CREATE TEMP TABLE IF NOT EXISTS ${tempTableName} (
|
|
163
|
-
id integer PRIMARY KEY
|
|
164
|
-
)
|
|
161
|
+
const createTableSQL = `
|
|
162
|
+
CREATE TEMP TABLE IF NOT EXISTS ${tempTableName} (
|
|
163
|
+
id integer PRIMARY KEY
|
|
164
|
+
)
|
|
165
165
|
`.trim();
|
|
166
166
|
// Use executor from context if available for query logging
|
|
167
167
|
if (context.executor) {
|
|
@@ -186,9 +186,9 @@ CREATE TEMP TABLE IF NOT EXISTS ${tempTableName} (
|
|
|
186
186
|
const selectExpression = `"${tempTableName}_agg".data`;
|
|
187
187
|
// Execute aggregation query and store results in another temp table
|
|
188
188
|
const aggTempTableName = `${tempTableName}_agg`;
|
|
189
|
-
const createAggTableSQL = `
|
|
190
|
-
CREATE TEMP TABLE ${aggTempTableName} AS
|
|
191
|
-
${aggregationSQL}
|
|
189
|
+
const createAggTableSQL = `
|
|
190
|
+
CREATE TEMP TABLE ${aggTempTableName} AS
|
|
191
|
+
${aggregationSQL}
|
|
192
192
|
`.trim();
|
|
193
193
|
// Use executor from context if available for query logging
|
|
194
194
|
if (context.executor) {
|
|
@@ -287,14 +287,14 @@ ${aggregationSQL}
|
|
|
287
287
|
limitOffsetClause += ` OFFSET ${offsetValue}`;
|
|
288
288
|
}
|
|
289
289
|
// Simple SELECT with foreign key for grouping
|
|
290
|
-
const sql = `
|
|
291
|
-
SELECT
|
|
292
|
-
"${foreignKey}" as parent_id,
|
|
293
|
-
${selectFields}
|
|
294
|
-
FROM "${targetTable}"
|
|
295
|
-
WHERE "${foreignKey}" IN (SELECT id FROM ${tempTableName})${additionalWhere}
|
|
296
|
-
${orderBySQL}
|
|
297
|
-
${limitOffsetClause}
|
|
290
|
+
const sql = `
|
|
291
|
+
SELECT
|
|
292
|
+
"${foreignKey}" as parent_id,
|
|
293
|
+
${selectFields}
|
|
294
|
+
FROM "${targetTable}"
|
|
295
|
+
WHERE "${foreignKey}" IN (SELECT id FROM ${tempTableName})${additionalWhere}
|
|
296
|
+
${orderBySQL}
|
|
297
|
+
${limitOffsetClause}
|
|
298
298
|
`.trim();
|
|
299
299
|
return sql;
|
|
300
300
|
}
|
|
@@ -361,20 +361,20 @@ ${limitOffsetClause}
|
|
|
361
361
|
// Build jsonb_agg ORDER BY clause
|
|
362
362
|
const jsonbAggOrderBy = orderByClause ? ` ORDER BY ${orderByClause}` : '';
|
|
363
363
|
// Build the SQL - matching JSONB strategy approach
|
|
364
|
-
const sql = `
|
|
365
|
-
SELECT
|
|
366
|
-
t."${foreignKey}" as parent_id,
|
|
367
|
-
jsonb_agg(
|
|
368
|
-
jsonb_build_object(${jsonbFields})${jsonbAggOrderBy}
|
|
369
|
-
) as data
|
|
370
|
-
FROM (
|
|
371
|
-
SELECT *
|
|
372
|
-
FROM "${targetTable}"
|
|
373
|
-
WHERE "${foreignKey}" IN (SELECT id FROM ${tempTableName})${additionalWhere}
|
|
374
|
-
${orderBySQL}
|
|
375
|
-
${limitOffsetClause}
|
|
376
|
-
) t
|
|
377
|
-
GROUP BY t."${foreignKey}"
|
|
364
|
+
const sql = `
|
|
365
|
+
SELECT
|
|
366
|
+
t."${foreignKey}" as parent_id,
|
|
367
|
+
jsonb_agg(
|
|
368
|
+
jsonb_build_object(${jsonbFields})${jsonbAggOrderBy}
|
|
369
|
+
) as data
|
|
370
|
+
FROM (
|
|
371
|
+
SELECT *
|
|
372
|
+
FROM "${targetTable}"
|
|
373
|
+
WHERE "${foreignKey}" IN (SELECT id FROM ${tempTableName})${additionalWhere}
|
|
374
|
+
${orderBySQL}
|
|
375
|
+
${limitOffsetClause}
|
|
376
|
+
) t
|
|
377
|
+
GROUP BY t."${foreignKey}"
|
|
378
378
|
`.trim();
|
|
379
379
|
return sql;
|
|
380
380
|
}
|
|
@@ -401,18 +401,18 @@ GROUP BY t."${foreignKey}"
|
|
|
401
401
|
// Build array_agg ORDER BY clause
|
|
402
402
|
const arrayAggOrderBy = orderByClause ? ` ORDER BY ${orderByClause}` : '';
|
|
403
403
|
// Build the SQL - matching JSONB strategy approach
|
|
404
|
-
const sql = `
|
|
405
|
-
SELECT
|
|
406
|
-
t."${foreignKey}" as parent_id,
|
|
407
|
-
array_agg(t."${arrayField}"${arrayAggOrderBy}) as data
|
|
408
|
-
FROM (
|
|
409
|
-
SELECT "${foreignKey}", "${arrayField}"
|
|
410
|
-
FROM "${targetTable}"
|
|
411
|
-
WHERE "${foreignKey}" IN (SELECT id FROM ${tempTableName})${additionalWhere}
|
|
412
|
-
${orderBySQL}
|
|
413
|
-
${limitOffsetClause}
|
|
414
|
-
) t
|
|
415
|
-
GROUP BY t."${foreignKey}"
|
|
404
|
+
const sql = `
|
|
405
|
+
SELECT
|
|
406
|
+
t."${foreignKey}" as parent_id,
|
|
407
|
+
array_agg(t."${arrayField}"${arrayAggOrderBy}) as data
|
|
408
|
+
FROM (
|
|
409
|
+
SELECT "${foreignKey}", "${arrayField}"
|
|
410
|
+
FROM "${targetTable}"
|
|
411
|
+
WHERE "${foreignKey}" IN (SELECT id FROM ${tempTableName})${additionalWhere}
|
|
412
|
+
${orderBySQL}
|
|
413
|
+
${limitOffsetClause}
|
|
414
|
+
) t
|
|
415
|
+
GROUP BY t."${foreignKey}"
|
|
416
416
|
`.trim();
|
|
417
417
|
return sql;
|
|
418
418
|
}
|
|
@@ -441,13 +441,13 @@ GROUP BY t."${foreignKey}"
|
|
|
441
441
|
default:
|
|
442
442
|
throw new Error(`Unknown aggregation type: ${aggregationType}`);
|
|
443
443
|
}
|
|
444
|
-
const sql = `
|
|
445
|
-
SELECT
|
|
446
|
-
tmp.id as parent_id,
|
|
447
|
-
COALESCE(${aggregateExpression}, ${config.defaultValue}) as data
|
|
448
|
-
FROM ${tempTableName} tmp
|
|
449
|
-
LEFT JOIN "${targetTable}" t ON t."${foreignKey}" = tmp.id${additionalWhere}
|
|
450
|
-
GROUP BY tmp.id
|
|
444
|
+
const sql = `
|
|
445
|
+
SELECT
|
|
446
|
+
tmp.id as parent_id,
|
|
447
|
+
COALESCE(${aggregateExpression}, ${config.defaultValue}) as data
|
|
448
|
+
FROM ${tempTableName} tmp
|
|
449
|
+
LEFT JOIN "${targetTable}" t ON t."${foreignKey}" = tmp.id${additionalWhere}
|
|
450
|
+
GROUP BY tmp.id
|
|
451
451
|
`.trim();
|
|
452
452
|
return sql;
|
|
453
453
|
}
|
package/package.json
CHANGED