rawsql-ts 0.9.0-beta → 0.10.0-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +54 -7
  2. package/dist/esm/index.js +1 -0
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/esm/models/SqlPrintToken.js +2 -0
  5. package/dist/esm/models/SqlPrintToken.js.map +1 -1
  6. package/dist/esm/models/ValueComponent.js +13 -12
  7. package/dist/esm/models/ValueComponent.js.map +1 -1
  8. package/dist/esm/parsers/SqlPrintTokenParser.js +22 -6
  9. package/dist/esm/parsers/SqlPrintTokenParser.js.map +1 -1
  10. package/dist/esm/transformers/PostgreJsonQueryBuilder.js +215 -0
  11. package/dist/esm/transformers/PostgreJsonQueryBuilder.js.map +1 -0
  12. package/dist/esm/transformers/PostgresArrayEntityCteBuilder.js +231 -0
  13. package/dist/esm/transformers/PostgresArrayEntityCteBuilder.js.map +1 -0
  14. package/dist/esm/transformers/PostgresObjectEntityCteBuilder.js +283 -0
  15. package/dist/esm/transformers/PostgresObjectEntityCteBuilder.js.map +1 -0
  16. package/dist/esm/transformers/SqlPrinter.js +9 -3
  17. package/dist/esm/transformers/SqlPrinter.js.map +1 -1
  18. package/dist/esm/types/index.d.ts +1 -0
  19. package/dist/esm/types/models/SqlPrintToken.d.ts +2 -0
  20. package/dist/esm/types/models/ValueComponent.d.ts +2 -2
  21. package/dist/esm/types/transformers/PostgreJsonQueryBuilder.d.ts +78 -0
  22. package/dist/esm/types/transformers/PostgresArrayEntityCteBuilder.d.ts +97 -0
  23. package/dist/esm/types/transformers/PostgresObjectEntityCteBuilder.d.ts +140 -0
  24. package/dist/index.d.ts +1 -0
  25. package/dist/index.js +1 -0
  26. package/dist/index.js.map +1 -1
  27. package/dist/models/SqlPrintToken.d.ts +2 -0
  28. package/dist/models/SqlPrintToken.js +2 -0
  29. package/dist/models/SqlPrintToken.js.map +1 -1
  30. package/dist/models/ValueComponent.d.ts +2 -2
  31. package/dist/models/ValueComponent.js +13 -12
  32. package/dist/models/ValueComponent.js.map +1 -1
  33. package/dist/parsers/SelectQueryParser.js +3 -14
  34. package/dist/parsers/SelectQueryParser.js.map +1 -1
  35. package/dist/parsers/SqlPrintTokenParser.js +23 -7
  36. package/dist/parsers/SqlPrintTokenParser.js.map +1 -1
  37. package/dist/transformers/PostgreJsonQueryBuilder.d.ts +78 -0
  38. package/dist/transformers/PostgreJsonQueryBuilder.js +219 -0
  39. package/dist/transformers/PostgreJsonQueryBuilder.js.map +1 -0
  40. package/dist/transformers/PostgresArrayEntityCteBuilder.d.ts +97 -0
  41. package/dist/transformers/PostgresArrayEntityCteBuilder.js +235 -0
  42. package/dist/transformers/PostgresArrayEntityCteBuilder.js.map +1 -0
  43. package/dist/transformers/PostgresObjectEntityCteBuilder.d.ts +140 -0
  44. package/dist/transformers/PostgresObjectEntityCteBuilder.js +287 -0
  45. package/dist/transformers/PostgresObjectEntityCteBuilder.js.map +1 -0
  46. package/dist/transformers/SqlFormatter.js +6 -1
  47. package/dist/transformers/SqlFormatter.js.map +1 -1
  48. package/dist/transformers/SqlPrinter.js +9 -3
  49. package/dist/transformers/SqlPrinter.js.map +1 -1
  50. package/package.json +1 -1
@@ -0,0 +1,283 @@
1
+ import { CommonTable, SourceAliasExpression, SelectItem, SelectClause, FromClause, SourceExpression, TableSource } from '../models/Clause';
2
+ import { SimpleSelectQuery } from '../models/SimpleSelectQuery';
3
+ import { IdentifierString, ColumnReference, FunctionCall, ValueList, LiteralValue, BinaryExpression, CaseExpression, SwitchCaseArgument, CaseKeyValuePair, RawString } from '../models/ValueComponent';
4
+ /**
5
+ * PostgreSQL-specific builder for creating CTEs for object entities (object relationships).
6
+ * This class handles the creation of CTEs that build JSON/JSONB objects for object entities,
7
+ * processing them from the deepest level up to ensure proper dependency ordering.
8
+ *
9
+ * Features:
10
+ * - Depth-based CTE naming (cte_object_depth_N)
11
+ * - NULL handling for entity columns
12
+ * - JSONB/JSON object construction
13
+ * - Hierarchical processing of nested objects
14
+ *
15
+ * Why depth calculation is critical:
16
+ * 1. Object entities can be nested at multiple levels. We must process the deepest
17
+ * (most distant) objects first to ensure their JSON representations are available
18
+ * when building their parent entities.
19
+ * 2. Object entity processing is essentially a column compression operation. Entities
20
+ * at the same depth level can be processed simultaneously since they don't depend
21
+ * on each other.
22
+ *
23
+ * Example hierarchy:
24
+ * Order (root, depth 0)
25
+ * └─ Customer (depth 1)
26
+ * └─ Address (depth 2)
27
+ * └─ Shipping (depth 1)
28
+ * └─ Carrier (depth 2)
29
+ *
30
+ * Processing order: depth 2 → depth 1 → depth 0
31
+ */
32
+ export class PostgresObjectEntityCteBuilder {
33
+ buildObjectEntityCtes(initialCte, allEntities, mapping) {
34
+ const ctes = [initialCte];
35
+ let previousCteAlias = initialCte.aliasExpression.table.name; // Collect and sort object entities by depth
36
+ const objectEntityInfos = this.collectAndSortObjectEntities(mapping, allEntities);
37
+ // Group entities by depth
38
+ const entitiesByDepth = this.groupEntitiesByDepth(objectEntityInfos);
39
+ // Process each depth level, starting from the deepest
40
+ const depths = Array.from(entitiesByDepth.keys()).sort((a, b) => b - a);
41
+ for (const depth of depths) {
42
+ const entitiesAtDepth = entitiesByDepth.get(depth);
43
+ const cteAlias = `${PostgresObjectEntityCteBuilder.CTE_OBJECT_PREFIX}${depth}`;
44
+ // Build CTE for all entities at this depth
45
+ const cte = this.buildDepthCte(entitiesAtDepth, previousCteAlias, cteAlias, mapping, allEntities);
46
+ ctes.push(cte);
47
+ previousCteAlias = cteAlias;
48
+ }
49
+ return { ctes, lastCteAlias: previousCteAlias };
50
+ } /**
51
+ * Collect all object entities and calculate their depth from root.
52
+ *
53
+ * Depth calculation is crucial because:
54
+ * - It determines the processing order (deepest first)
55
+ * - It ensures dependencies are resolved before an entity is processed
56
+ * - It allows parallel processing of entities at the same depth level
57
+ *
58
+ * @param mapping The JSON mapping configuration
59
+ * @param allEntities Map of all entities in the mapping
60
+ * @returns Array of object entity information with calculated depths
61
+ */
62
+ collectAndSortObjectEntities(mapping, allEntities) {
63
+ const objectInfos = [];
64
+ // Helper function to calculate actual object nesting depth for a given OBJECT entity
65
+ const calculateActualObjectNestingDepth = (entityIdOfObject) => {
66
+ const initialEntity = allEntities.get(entityIdOfObject);
67
+ if (!initialEntity) {
68
+ throw new Error(`Entity ${entityIdOfObject} not found for depth calculation.`);
69
+ }
70
+ // If the object itself is root, its depth is 0. (This function should ideally be called for nested entities, not the root itself as a "parent CTE" subject)
71
+ if (initialEntity.isRoot)
72
+ return 0;
73
+ // If the object is not root and has no parentId, it's considered a top-level object, depth 1.
74
+ if (!initialEntity.parentId) {
75
+ return 1;
76
+ }
77
+ let currentParentIdInHierarchy = initialEntity.parentId;
78
+ let calculatedObjectDepth = 0;
79
+ const visitedInPath = new Set();
80
+ visitedInPath.add(entityIdOfObject); // Add the starting object itself to detect cycles
81
+ while (currentParentIdInHierarchy) {
82
+ if (visitedInPath.has(currentParentIdInHierarchy)) {
83
+ throw new Error(`Circular dependency detected: ${currentParentIdInHierarchy} already visited in path for ${entityIdOfObject}`);
84
+ }
85
+ visitedInPath.add(currentParentIdInHierarchy);
86
+ const parentEntityData = allEntities.get(currentParentIdInHierarchy);
87
+ if (!parentEntityData) {
88
+ throw new Error(`Parent entity ${currentParentIdInHierarchy} not found during depth calculation for ${entityIdOfObject}`);
89
+ }
90
+ let parentIsConsideredAnObjectForNesting = false;
91
+ if (parentEntityData.isRoot) {
92
+ parentIsConsideredAnObjectForNesting = true; // Root counts as an object ancestor
93
+ }
94
+ else {
95
+ // For non-root parents, find their definition in nestedEntities to check their type
96
+ const parentDefinition = mapping.nestedEntities.find(ne => ne.id === currentParentIdInHierarchy);
97
+ if (parentDefinition) {
98
+ if (parentDefinition.relationshipType === "object") {
99
+ parentIsConsideredAnObjectForNesting = true;
100
+ }
101
+ // If parentDefinition.relationshipType === "array", it's not an object ancestor for depth counting
102
+ }
103
+ else {
104
+ // This implies currentParentIdInHierarchy refers to an entity not defined as root or in nestedEntities
105
+ // This should ideally not happen with a consistent mapping.
106
+ throw new Error(`Parent entity ${currentParentIdInHierarchy} (ancestor of ${entityIdOfObject}) has no definition in mapping.nestedEntities and is not root.`);
107
+ }
108
+ }
109
+ if (parentIsConsideredAnObjectForNesting) {
110
+ calculatedObjectDepth++;
111
+ }
112
+ if (parentEntityData.isRoot) {
113
+ break; // Stop when the root is processed as the highest object ancestor
114
+ }
115
+ currentParentIdInHierarchy = parentEntityData.parentId; // Move to the next ancestor
116
+ }
117
+ return calculatedObjectDepth;
118
+ };
119
+ mapping.nestedEntities.forEach(nestedEntity => {
120
+ if (nestedEntity.relationshipType === "object") {
121
+ const entity = allEntities.get(nestedEntity.id);
122
+ // Ensure we don't process the root entity itself as a "parent" CTE,
123
+ // and that the entity actually exists.
124
+ if (entity && !entity.isRoot) {
125
+ objectInfos.push({
126
+ entity,
127
+ depth: calculateActualObjectNestingDepth(nestedEntity.id)
128
+ });
129
+ }
130
+ }
131
+ });
132
+ // The existing grouping and sorting by depth (b - a for descending) should still work correctly
133
+ // as it processes deepest levels first, regardless of the absolute depth numbers.
134
+ return objectInfos;
135
+ }
136
+ /**
137
+ * Group entities by their depth level.
138
+ *
139
+ * Grouping by depth allows us to:
140
+ * - Process all entities at the same level in a single CTE
141
+ * - Optimize query performance by reducing the number of CTEs
142
+ * - Maintain clear dependency ordering
143
+ *
144
+ * @param parentInfos Array of parent entity information with depths
145
+ * @returns Map of depth level to entities at that depth
146
+ */ groupEntitiesByDepth(objectInfos) {
147
+ const entitiesByDepth = new Map();
148
+ objectInfos.forEach(info => {
149
+ const depth = info.depth;
150
+ if (!entitiesByDepth.has(depth)) {
151
+ entitiesByDepth.set(depth, []);
152
+ }
153
+ entitiesByDepth.get(depth).push(info);
154
+ });
155
+ return entitiesByDepth;
156
+ }
157
+ /**
158
+ * Build a CTE that processes all entities at a specific depth level
159
+ */
160
+ buildDepthCte(entitiesAtDepth, previousCteAlias, cteAlias, mapping, allEntities) {
161
+ // Build SELECT items: * and JSON objects for all entities at this depth
162
+ const selectItems = [
163
+ // Select all columns from previous CTE
164
+ new SelectItem(new ColumnReference(null, new IdentifierString(PostgresObjectEntityCteBuilder.WILDCARD_COLUMN)))
165
+ ];
166
+ // Process each entity at this depth
167
+ for (const { entity } of entitiesAtDepth) {
168
+ const jsonColumn = this.buildEntityJsonColumn(entity, mapping, allEntities);
169
+ selectItems.push(jsonColumn);
170
+ }
171
+ // Create CTE that selects from previous CTE
172
+ const cteSelect = new SimpleSelectQuery({
173
+ selectClause: new SelectClause(selectItems),
174
+ fromClause: new FromClause(new SourceExpression(new TableSource(null, new IdentifierString(previousCteAlias)), null), null)
175
+ });
176
+ return new CommonTable(cteSelect, new SourceAliasExpression(cteAlias, null), null);
177
+ }
178
+ /**
179
+ * Build JSON column for a single entity with NULL handling
180
+ */
181
+ buildEntityJsonColumn(entity, mapping, allEntities) {
182
+ // Build JSON object arguments and NULL checks
183
+ const { jsonObjectArgs, nullChecks } = this.prepareEntityColumns(entity);
184
+ // Add child object relationships
185
+ this.addChildObjectRelationships(entity, jsonObjectArgs, mapping, allEntities);
186
+ // Create JSON object
187
+ const jsonObject = this.createJsonObject(jsonObjectArgs, mapping.useJsonb);
188
+ // Build NULL condition and CASE expression
189
+ const nullCondition = this.buildNullCondition(nullChecks);
190
+ const caseExpr = this.createCaseExpression(nullCondition, jsonObject);
191
+ // Add JSON object as named column
192
+ const jsonColumnName = `${entity.name.toLowerCase()}${PostgresObjectEntityCteBuilder.JSON_COLUMN_SUFFIX}`;
193
+ return new SelectItem(caseExpr, jsonColumnName);
194
+ }
195
+ /**
196
+ * Prepare entity columns and NULL checks.
197
+ *
198
+ * This method extracts column data and creates NULL checks for each column.
199
+ * The NULL checking is essential for handling outer joins correctly.
200
+ *
201
+ * In outer join scenarios, when there's no matching row in the joined table,
202
+ * all columns from that table will be NULL. Instead of creating an empty object
203
+ * with all NULL properties (e.g., {id: null, name: null, email: null}),
204
+ * we want to represent the absence of the entity as NULL itself.
205
+ *
206
+ * This ensures cleaner JSON output where missing relationships are represented
207
+ * as NULL rather than objects with all NULL fields.
208
+ *
209
+ * @param entity The entity whose columns are being processed
210
+ * @returns Object containing arrays of JSON object arguments and NULL check conditions
211
+ */
212
+ prepareEntityColumns(entity) {
213
+ const jsonObjectArgs = [];
214
+ const nullChecks = [];
215
+ Object.entries(entity.columns).forEach(([jsonKey, sqlColumn]) => {
216
+ jsonObjectArgs.push(new LiteralValue(jsonKey));
217
+ jsonObjectArgs.push(new ColumnReference(null, new IdentifierString(sqlColumn)));
218
+ // Collect NULL checks for each column
219
+ nullChecks.push(new BinaryExpression(new ColumnReference(null, new IdentifierString(sqlColumn)), "is", new LiteralValue(null)));
220
+ });
221
+ return { jsonObjectArgs, nullChecks };
222
+ }
223
+ /**
224
+ * Add child object relationships to JSON object arguments.
225
+ *
226
+ * This method processes nested object-type entities that are direct children of the current entity.
227
+ * For each child entity, it adds the property name and corresponding JSON column reference
228
+ * to the arguments array that will be used to build the parent's JSON object.
229
+ *
230
+ * The child JSON columns are expected to already exist in the data source (created by deeper
231
+ * level CTEs), as we process from the deepest level up to the root.
232
+ *
233
+ * Note: In this context, "child" refers to entities that have an object relationship (0..1)
234
+ * with their parent. From a data perspective, these are typically entities referenced via
235
+ * foreign keys, representing "parent" entities in traditional database terminology.
236
+ *
237
+ * @param entity The current entity being processed
238
+ * @param jsonObjectArgs Array to which JSON object arguments will be added
239
+ * @param mapping The JSON mapping configuration
240
+ * @param allEntities Map of all entities in the mapping
241
+ */
242
+ addChildObjectRelationships(entity, jsonObjectArgs, mapping, allEntities) {
243
+ const childEntities = mapping.nestedEntities.filter(ne => ne.parentId === entity.id && ne.relationshipType === "object");
244
+ childEntities.forEach(childEntity => {
245
+ const child = allEntities.get(childEntity.id);
246
+ if (child) {
247
+ jsonObjectArgs.push(new LiteralValue(childEntity.propertyName));
248
+ const jsonColumnName = `${child.name.toLowerCase()}${PostgresObjectEntityCteBuilder.JSON_COLUMN_SUFFIX}`;
249
+ jsonObjectArgs.push(new ColumnReference(null, new IdentifierString(jsonColumnName)));
250
+ }
251
+ });
252
+ }
253
+ /**
254
+ * Create JSON object function call
255
+ */
256
+ createJsonObject(args, useJsonb = false) {
257
+ const jsonBuildFunction = useJsonb ? "jsonb_build_object" : "json_build_object";
258
+ return new FunctionCall(null, new RawString(jsonBuildFunction), new ValueList(args), null);
259
+ }
260
+ /**
261
+ * Build NULL condition from NULL checks
262
+ */
263
+ buildNullCondition(nullChecks) {
264
+ return nullChecks.reduce((acc, check) => acc ? new BinaryExpression(acc, "and", check) : check);
265
+ }
266
+ /**
267
+ * Create CASE expression with NULL handling
268
+ */
269
+ createCaseExpression(nullCondition, jsonObject) {
270
+ return new CaseExpression(null, new SwitchCaseArgument([new CaseKeyValuePair(nullCondition, new LiteralValue(null))], jsonObject // ELSE return the JSON object
271
+ ));
272
+ }
273
+ }
274
+ PostgresObjectEntityCteBuilder.JSON_COLUMN_SUFFIX = '_json';
275
+ PostgresObjectEntityCteBuilder.CTE_OBJECT_PREFIX = 'cte_object_depth_';
276
+ PostgresObjectEntityCteBuilder.WILDCARD_COLUMN = '*'; /**
277
+ * Build CTEs for all object entities in the correct dependency order
278
+ * @param initialCte The starting CTE containing all raw data
279
+ * @param allEntities Map of all entities in the mapping
280
+ * @param mapping The JSON mapping configuration
281
+ * @returns Array of CTEs and the alias of the last CTE created
282
+ */
283
+ //# sourceMappingURL=PostgresObjectEntityCteBuilder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PostgresObjectEntityCteBuilder.js","sourceRoot":"","sources":["../../../src/transformers/PostgresObjectEntityCteBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,qBAAqB,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC3I,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAAE,gBAAgB,EA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AC,IAAI,CAAC,IAAI,eAAe,CAAC,IAAI,EAAE,IAAI,gBAAgB,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;YACzF,CAAC;QACL,CAAC,CAAC,CAAC;IACP,CAAC;IAED;;OAEG;IACK,gBAAgB,CAAC,IAAsB,EAAE,WAAoB,KAAK;QACtE,MAAM,iBAAiB,GAAG,QAAQ,CAAC,CAAC,CAAC,oBAAoB,CAAC,CAAC,CAAC,mBAAmB,CAAC;QAChF,OAAO,IAAI,YAAY,CACnB,IAAI,EACJ,IAAI,SAAS,CAAC,iBAAiB,CAAC,EAChC,IAAI,SAAS,CAAC,IAAI,CAAC,EACnB,IAAI,CACP,CAAC;IACN,CAAC;IAED;;OAEG;IACK,kBAAkB,CAAC,UAA4B;QACnD,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CACpC,GAAG,CAAC,CAAC,CAAC,IAAI,gBAAgB,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CACxD,CAAC;IACN,CAAC;IAED;;OAEG;IACK,oBAAoB,CAAC,aAA6B,EAAE,UAA0B;QAClF,OAAO,IAAI,cAAc,CACrB,IAAI,EACJ,IAAI,kBAAkB,CAClB,CAAC,IAAI,gBAAgB,CAAC,aAAa,EAAE,IAAI,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAC7D,UAAU,CAAE,8BAA8B;SAC7C,CACJ,CAAC;IACN,CAAC;;AApVuB,iDAAkB,GAAG,OAAO,CAAC;AAC7B,gDAAiB,GAAG,mBAAmB,CAAC;AACxC,8CAAe,GAAG,GAAG,CAAC,CAAI;;;;;;GAM/C"}
@@ -32,9 +32,15 @@ export class SqlPrinter {
32
32
  SqlPrintTokenContainerType.LimitClause,
33
33
  SqlPrintTokenContainerType.OffsetClause,
34
34
  SqlPrintTokenContainerType.SubQuerySource,
35
- SqlPrintTokenContainerType.BinarySelectQueryOperator,
36
- SqlPrintTokenContainerType.Values,
37
- SqlPrintTokenContainerType.WithClause
35
+ SqlPrintTokenContainerType.BinarySelectQueryOperator, SqlPrintTokenContainerType.Values,
36
+ SqlPrintTokenContainerType.WithClause,
37
+ SqlPrintTokenContainerType.SwitchCaseArgument,
38
+ SqlPrintTokenContainerType.CaseKeyValuePair,
39
+ SqlPrintTokenContainerType.CaseThenValue,
40
+ SqlPrintTokenContainerType.ElseClause,
41
+ SqlPrintTokenContainerType.CaseElseValue
42
+ // CaseExpression, SwitchCaseArgument, CaseKeyValuePair, and ElseClause
43
+ // are not included by default to maintain backward compatibility with tests
38
44
  //SqlPrintTokenContainerType.CommonTable
39
45
  ]);
40
46
  }
@@ -1 +1 @@
1
- {"version":3,"file":"SqlPrinter.js","sourceRoot":"","sources":["../../../src/transformers/SqlPrinter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,iBAAiB,EAAE,0BAA0B,EAAE,MAAM,yBAAyB,CAAC;AACvG,OAAO,EAAoB,WAAW,EAAiB,MAAM,eAAe,CAAC;AAkB7E;;GAEG;AACH,MAAM,OAAO,UAAU;IAkBnB;;OAEG;IACH,YAAY,OAQX;;QACG,IAAI,CAAC,UAAU,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,UAAU,mCAAI,EAAE,CAAC;QAC5C,IAAI,CAAC,UAAU,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,UAAU,mCAAI,CAAC,CAAC;QAE3C,8FAA8F;QAC9F,0EAA0E;QAC1E,IAAI,CAAC,OAAO,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,mCAAI,GAAG,CAAC;QAEvC,IAAI,CAAC,UAAU,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,UAAU,mCAAI,MAAM,CAAC;QAChD,IAAI,CAAC,QAAQ,GAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,mCAAI,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IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAChD,MAAM,KAAK,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;YACnC,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;QACxC,CAAC;QAED,oBAAoB;QACpB,IAAI,UAAU,KAAK,KAAK,EAAE,CAAC;YACvB,IAAI,CAAC,WAAW,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;QAC1C,CAAC;IACL,CAAC;CACJ"}
@@ -8,6 +8,7 @@ export * from './transformers/CTECollector';
8
8
  export * from './transformers/CTENormalizer';
9
9
  export * from './transformers/Formatter';
10
10
  export * from './transformers/SqlFormatter';
11
+ export * from './transformers/PostgreJsonQueryBuilder';
11
12
  export * from './transformers/QueryBuilder';
12
13
  export * from './transformers/SelectValueCollector';
13
14
  export * from './transformers/SelectableColumnCollector';
@@ -27,6 +27,8 @@ export declare enum SqlPrintTokenContainerType {
27
27
  SwitchCaseArgument = "SwitchCaseArgument",
28
28
  ElseClause = "ElseClause",
29
29
  CaseKeyValuePair = "CaseKeyValuePair",
30
+ CaseThenValue = "CaseThenValue",
31
+ CaseElseValue = "CaseElseValue",
30
32
  ParenExpression = "ParenExpression",
31
33
  CastExpression = "CastExpression",
32
34
  CaseExpression = "CaseExpression",
@@ -32,7 +32,7 @@ export declare class FunctionCall extends SqlComponent {
32
32
  qualifiedName: QualifiedName;
33
33
  argument: ValueComponent | null;
34
34
  over: OverExpression | null;
35
- constructor(namespaces: string[] | IdentifierString[] | null, name: string | RawString | IdentifierString, argument: ValueComponent | null, over: OverExpression | null);
35
+ constructor(namespaces: string | string[] | IdentifierString[] | null, name: string | RawString | IdentifierString, argument: ValueComponent | null, over: OverExpression | null);
36
36
  /**
37
37
  * For backward compatibility: returns the namespaces as IdentifierString[] | null (readonly)
38
38
  */
@@ -195,7 +195,7 @@ export declare class QualifiedName extends SqlComponent {
195
195
  namespaces: IdentifierString[] | null;
196
196
  /** The actual name (e.g. table, function, type, column) */
197
197
  name: RawString | IdentifierString;
198
- constructor(namespaces: string[] | IdentifierString[] | null, name: string | RawString | IdentifierString);
198
+ constructor(namespaces: string | string[] | IdentifierString[] | null, name: string | RawString | IdentifierString);
199
199
  /** Returns the full qualified name as a string (dot-separated) */
200
200
  toString(): string;
201
201
  }
@@ -0,0 +1,78 @@
1
+ import { SimpleSelectQuery } from '../models/SimpleSelectQuery';
2
+ /**
3
+ * Universal JSON mapping definition for creating any level of JSON structures.
4
+ * Supports flat arrays, nested objects, and unlimited hierarchical structures.
5
+ */
6
+ export interface JsonMapping {
7
+ rootName: string;
8
+ rootEntity: {
9
+ id: string;
10
+ name: string;
11
+ columns: {
12
+ [jsonKey: string]: string;
13
+ };
14
+ };
15
+ nestedEntities: Array<{
16
+ id: string;
17
+ name: string;
18
+ parentId: string;
19
+ propertyName: string;
20
+ relationshipType?: "object" | "array";
21
+ columns: {
22
+ [jsonKey: string]: string;
23
+ };
24
+ }>;
25
+ useJsonb?: boolean;
26
+ resultFormat?: "array" | "single";
27
+ emptyResult?: string;
28
+ }
29
+ /**
30
+ * PostgreSQL JSON query builder that transforms SimpleSelectQuery into queries
31
+ * that return JSON arrays or single JSON objects using PostgreSQL JSON functions.
32
+ */
33
+ export declare class PostgreJsonQueryBuilder {
34
+ private selectValueCollector;
35
+ private objectEntityCteBuilder;
36
+ private arrayEntityCteBuilder;
37
+ constructor();
38
+ /**
39
+ * Validates the JSON mapping and the original query.
40
+ * @param query Original query to transform
41
+ * @param mapping JSON mapping configuration
42
+ */
43
+ private validateMapping;
44
+ /**
45
+ * Build JSON query from original query and mapping configuration.
46
+ * @param originalQuery Original query to transform
47
+ * @param mapping JSON mapping configuration
48
+ * @returns Transformed query with JSON aggregation
49
+ */
50
+ buildJson(originalQuery: SimpleSelectQuery, mapping: JsonMapping): SimpleSelectQuery;
51
+ /**
52
+ * Builds the JSON structure using a unified CTE-based strategy.
53
+ * @param originalQuery Original query
54
+ * @param mapping JSON mapping configuration
55
+ * @returns Query with CTE-based JSON aggregation
56
+ */
57
+ private buildJsonWithCteStrategy;
58
+ /**
59
+ * Creates the initial Common Table Expression (CTE) from the original query.
60
+ * @param originalQuery The base SimpleSelectQuery.
61
+ * @returns An object containing the initial CTE and its alias.
62
+ */
63
+ private createInitialCte;
64
+ /**
65
+ * Builds the final SELECT query that constructs the root JSON object (or array of objects).
66
+ * This query uses all previously generated CTEs.
67
+ * @param finalCtesList The complete list of all CTEs (initial and array CTEs).
68
+ * @param lastCteAliasForFromClause Alias of the final CTE from which the root object will be built.
69
+ * @param allEntities Map of all processable entities.
70
+ * @param mapping JSON mapping configuration.
71
+ * @returns The final SimpleSelectQuery.
72
+ */
73
+ private buildFinalSelectQuery;
74
+ /**
75
+ * Build JSON object for entity, using parent JSON columns when available
76
+ */
77
+ private buildEntityJsonObject;
78
+ }
@@ -0,0 +1,97 @@
1
+ import { CommonTable } from '../models/Clause';
2
+ import { JsonMapping } from './PostgreJsonQueryBuilder';
3
+ import { ProcessableEntity } from './PostgresObjectEntityCteBuilder';
4
+ /**
5
+ * PostgreSQL-specific builder for creating CTEs for array entities (array relationships).
6
+ * This class handles the creation of CTEs that build JSON/JSONB arrays for child entities,
7
+ * processing them from the deepest level up to ensure proper dependency ordering.
8
+ *
9
+ * Features:
10
+ * - Depth-based CTE naming (cte_array_depth_N)
11
+ * - Row compression using GROUP BY operations
12
+ * - JSONB/JSON array aggregation
13
+ * - Hierarchical processing of nested arrays
14
+ * - Column exclusion to avoid duplication
15
+ *
16
+ * Why depth calculation is critical:
17
+ * 1. Array entities can be nested at multiple levels. We must process the deepest
18
+ * (most distant) arrays first to ensure their JSON representations are available
19
+ * when building their parent arrays.
20
+ * 2. Array entity processing is essentially a row compression operation using GROUP BY.
21
+ * Unlike parent entities which use column compression, arrays require grouping
22
+ * to aggregate multiple rows into JSON arrays.
23
+ *
24
+ * Example hierarchy:
25
+ * Order (root, depth 0)
26
+ * └─ Items (array, depth 1)
27
+ * └─ Details (array, depth 2)
28
+ *
29
+ * Processing order: depth 2 → depth 1 → depth 0
30
+ */
31
+ export declare class PostgresArrayEntityCteBuilder {
32
+ private static readonly CTE_ARRAY_PREFIX;
33
+ /**
34
+ * Build CTEs for all array entities in the correct dependency order
35
+ * @param ctesSoFar Array of CTEs built so far (starts with the initial CTE)
36
+ * @param aliasOfCteToBuildUpon Alias of the CTE from which the current array CTE will select
37
+ * @param allEntities Map of all entities in the mapping
38
+ * @param mapping The JSON mapping configuration
39
+ * @returns Object containing the updated list of all CTEs and the alias of the last CTE created
40
+ */
41
+ buildArrayEntityCtes(ctesSoFar: CommonTable[], aliasOfCteToBuildUpon: string, allEntities: Map<string, ProcessableEntity>, mapping: JsonMapping): {
42
+ updatedCtes: CommonTable[];
43
+ lastCteAlias: string;
44
+ };
45
+ /**
46
+ * Collect all array entities and calculate their depth from root.
47
+ *
48
+ * Depth calculation ensures proper processing order where deeper nested
49
+ * arrays are processed first, making their aggregated data available
50
+ * for parent array processing.
51
+ *
52
+ * @param mapping The JSON mapping configuration
53
+ * @param allEntities Map of all entities in the mapping
54
+ * @returns Array of array entity information with calculated depths, sorted deepest first
55
+ */
56
+ private collectAndSortArrayEntities;
57
+ /**
58
+ * Group array entities by their depth level.
59
+ *
60
+ * Grouping by depth allows us to:
61
+ * - Process all entities at the same level in a single CTE
62
+ * - Optimize query performance by reducing the number of CTEs
63
+ * - Maintain clear dependency ordering
64
+ *
65
+ * @param arrayInfos Array of array entity information with depths
66
+ * @returns Map of depth level to entities at that depth
67
+ */
68
+ private groupEntitiesByDepth;
69
+ /**
70
+ * Build a CTE that processes all array entities at a specific depth level.
71
+ *
72
+ * This method creates a single CTE that aggregates multiple array entities
73
+ * at the same depth, using GROUP BY to compress rows into JSON arrays.
74
+ *
75
+ * @param infos Array entities at this depth level
76
+ * @param currentCteAlias Alias of the CTE to build upon
77
+ * @param currentCtes All CTEs built so far
78
+ * @param depth Current depth level being processed
79
+ * @param mapping JSON mapping configuration
80
+ * @returns The new CTE and its alias
81
+ */
82
+ private buildDepthCte;
83
+ /**
84
+ * Build JSON aggregation function for an array entity.
85
+ *
86
+ * This method creates a jsonb_agg or json_agg function call that aggregates
87
+ * the entity's columns into a JSON array. It also handles nested relationships
88
+ * by including child entity properties in the JSON object.
89
+ *
90
+ * @param entity The array entity being processed
91
+ * @param nestedEntities All nested entities from the mapping
92
+ * @param allEntities Map of all entities (not used in current implementation)
93
+ * @param useJsonb Whether to use JSONB functions
94
+ * @returns Object containing the JSON aggregation function
95
+ */
96
+ private buildAggregationDetailsForArrayEntity;
97
+ }