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,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
+ }
@@ -0,0 +1,235 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PostgresArrayEntityCteBuilder = void 0;
4
+ const Clause_1 = require("../models/Clause");
5
+ const SimpleSelectQuery_1 = require("../models/SimpleSelectQuery");
6
+ const ValueComponent_1 = require("../models/ValueComponent");
7
+ const SelectValueCollector_1 = require("./SelectValueCollector");
8
+ /**
9
+ * PostgreSQL-specific builder for creating CTEs for array entities (array relationships).
10
+ * This class handles the creation of CTEs that build JSON/JSONB arrays for child entities,
11
+ * processing them from the deepest level up to ensure proper dependency ordering.
12
+ *
13
+ * Features:
14
+ * - Depth-based CTE naming (cte_array_depth_N)
15
+ * - Row compression using GROUP BY operations
16
+ * - JSONB/JSON array aggregation
17
+ * - Hierarchical processing of nested arrays
18
+ * - Column exclusion to avoid duplication
19
+ *
20
+ * Why depth calculation is critical:
21
+ * 1. Array entities can be nested at multiple levels. We must process the deepest
22
+ * (most distant) arrays first to ensure their JSON representations are available
23
+ * when building their parent arrays.
24
+ * 2. Array entity processing is essentially a row compression operation using GROUP BY.
25
+ * Unlike parent entities which use column compression, arrays require grouping
26
+ * to aggregate multiple rows into JSON arrays.
27
+ *
28
+ * Example hierarchy:
29
+ * Order (root, depth 0)
30
+ * └─ Items (array, depth 1)
31
+ * └─ Details (array, depth 2)
32
+ *
33
+ * Processing order: depth 2 → depth 1 → depth 0
34
+ */
35
+ class PostgresArrayEntityCteBuilder {
36
+ /**
37
+ * Build CTEs for all array entities in the correct dependency order
38
+ * @param ctesSoFar Array of CTEs built so far (starts with the initial CTE)
39
+ * @param aliasOfCteToBuildUpon Alias of the CTE from which the current array CTE will select
40
+ * @param allEntities Map of all entities in the mapping
41
+ * @param mapping The JSON mapping configuration
42
+ * @returns Object containing the updated list of all CTEs and the alias of the last CTE created
43
+ */
44
+ buildArrayEntityCtes(ctesSoFar, aliasOfCteToBuildUpon, allEntities, mapping) {
45
+ let currentCtes = [...ctesSoFar];
46
+ let currentCteAlias = aliasOfCteToBuildUpon;
47
+ // Collect and sort array entities by depth
48
+ const sortedArrayInfos = this.collectAndSortArrayEntities(mapping, allEntities);
49
+ if (sortedArrayInfos.length === 0) {
50
+ return { updatedCtes: currentCtes, lastCteAlias: currentCteAlias };
51
+ }
52
+ // Group array entities by depth level for batch processing
53
+ const entitiesByDepth = this.groupEntitiesByDepth(sortedArrayInfos);
54
+ // Process from deepest to shallowest (depth-first)
55
+ const depths = Array.from(entitiesByDepth.keys()).sort((a, b) => b - a);
56
+ for (const depth of depths) {
57
+ const infos = entitiesByDepth.get(depth);
58
+ // Build CTE for all entities at this depth
59
+ const { cte, newCteAlias } = this.buildDepthCte(infos, currentCteAlias, currentCtes, depth, mapping);
60
+ currentCtes.push(cte);
61
+ currentCteAlias = newCteAlias;
62
+ }
63
+ return { updatedCtes: currentCtes, lastCteAlias: currentCteAlias };
64
+ }
65
+ /**
66
+ * Collect all array entities and calculate their depth from root.
67
+ *
68
+ * Depth calculation ensures proper processing order where deeper nested
69
+ * arrays are processed first, making their aggregated data available
70
+ * for parent array processing.
71
+ *
72
+ * @param mapping The JSON mapping configuration
73
+ * @param allEntities Map of all entities in the mapping
74
+ * @returns Array of array entity information with calculated depths, sorted deepest first
75
+ */
76
+ collectAndSortArrayEntities(mapping, allEntities) {
77
+ const arrayEntityInfos = [];
78
+ // Helper function to calculate depth for an entity
79
+ const getDepth = (entityId) => {
80
+ const entity = allEntities.get(entityId);
81
+ if (!entity || entity.isRoot)
82
+ return 0;
83
+ if (!entity.parentId)
84
+ return 1;
85
+ return 1 + getDepth(entity.parentId);
86
+ };
87
+ // Collect all array-type nested entities
88
+ mapping.nestedEntities.forEach(ne => {
89
+ if (ne.relationshipType === "array") {
90
+ const currentArrayEntity = allEntities.get(ne.id);
91
+ const parentEntity = allEntities.get(ne.parentId);
92
+ if (!currentArrayEntity || !parentEntity) {
93
+ throw new Error(`Configuration error: Array entity '${ne.id}' or its parent '${ne.parentId}' not found.`);
94
+ }
95
+ // Determine the linking column from parent entity
96
+ // This assumes the first column of the parent is a suitable key for linking.
97
+ // More robust linking might require explicit configuration in the mapping.
98
+ const parentSqlColumns = Object.values(parentEntity.columns);
99
+ if (parentSqlColumns.length === 0) {
100
+ throw new Error(`Configuration error: Parent entity '${parentEntity.name}' (ID: ${parentEntity.id}) must have at least one column defined to serve as a linking key for child array '${ne.name}'.`);
101
+ }
102
+ const parentIdColumnSqlName = parentSqlColumns[0];
103
+ arrayEntityInfos.push({
104
+ entity: currentArrayEntity,
105
+ parentEntity: parentEntity,
106
+ parentIdColumnSqlName: parentIdColumnSqlName,
107
+ depth: getDepth(ne.id)
108
+ });
109
+ }
110
+ });
111
+ // Sort by depth, deepest arrays (higher depth number) processed first (bottom-up for arrays)
112
+ arrayEntityInfos.sort((a, b) => b.depth - a.depth);
113
+ return arrayEntityInfos;
114
+ }
115
+ /**
116
+ * Group array entities by their depth level.
117
+ *
118
+ * Grouping by depth allows us to:
119
+ * - Process all entities at the same level in a single CTE
120
+ * - Optimize query performance by reducing the number of CTEs
121
+ * - Maintain clear dependency ordering
122
+ *
123
+ * @param arrayInfos Array of array entity information with depths
124
+ * @returns Map of depth level to entities at that depth
125
+ */
126
+ groupEntitiesByDepth(arrayInfos) {
127
+ const entitiesByDepth = new Map();
128
+ arrayInfos.forEach(info => {
129
+ const depth = info.depth;
130
+ if (!entitiesByDepth.has(depth)) {
131
+ entitiesByDepth.set(depth, []);
132
+ }
133
+ entitiesByDepth.get(depth).push(info);
134
+ });
135
+ return entitiesByDepth;
136
+ }
137
+ /**
138
+ * Build a CTE that processes all array entities at a specific depth level.
139
+ *
140
+ * This method creates a single CTE that aggregates multiple array entities
141
+ * at the same depth, using GROUP BY to compress rows into JSON arrays.
142
+ *
143
+ * @param infos Array entities at this depth level
144
+ * @param currentCteAlias Alias of the CTE to build upon
145
+ * @param currentCtes All CTEs built so far
146
+ * @param depth Current depth level being processed
147
+ * @param mapping JSON mapping configuration
148
+ * @returns The new CTE and its alias
149
+ */
150
+ buildDepthCte(infos, currentCteAlias, currentCtes, depth, mapping) {
151
+ var _a;
152
+ // Collect columns that will be compressed into arrays
153
+ const arrayColumns = new Set();
154
+ infos.forEach(info => {
155
+ Object.values(info.entity.columns).forEach(col => arrayColumns.add(col));
156
+ });
157
+ // Get columns from previous CTE
158
+ const prevCte = (_a = currentCtes.find(c => c.aliasExpression.table.name === currentCteAlias)) === null || _a === void 0 ? void 0 : _a.query;
159
+ if (!prevCte) {
160
+ throw new Error(`CTE not found: ${currentCteAlias}`);
161
+ }
162
+ const prevSelects = new SelectValueCollector_1.SelectValueCollector(null, currentCtes).collect(prevCte);
163
+ // Build SELECT items: columns that are NOT being compressed (for GROUP BY)
164
+ const groupByItems = [];
165
+ const selectItems = [];
166
+ prevSelects.forEach(sv => {
167
+ if (!arrayColumns.has(sv.name)) {
168
+ selectItems.push(new Clause_1.SelectItem(new ValueComponent_1.ColumnReference(null, new ValueComponent_1.IdentifierString(sv.name)), sv.name));
169
+ groupByItems.push(new ValueComponent_1.ColumnReference(null, new ValueComponent_1.IdentifierString(sv.name)));
170
+ }
171
+ });
172
+ // Add JSON aggregation columns for each array entity at this depth
173
+ for (const info of infos) {
174
+ const agg = this.buildAggregationDetailsForArrayEntity(info.entity, mapping.nestedEntities, new Map(), // allEntities - not needed for array aggregation
175
+ mapping.useJsonb);
176
+ selectItems.push(new Clause_1.SelectItem(agg.jsonAgg, info.entity.propertyName));
177
+ }
178
+ // Create the new CTE
179
+ const cteAlias = `${PostgresArrayEntityCteBuilder.CTE_ARRAY_PREFIX}${depth}`;
180
+ const cteSelect = new SimpleSelectQuery_1.SimpleSelectQuery({
181
+ selectClause: new Clause_1.SelectClause(selectItems),
182
+ fromClause: new Clause_1.FromClause(new Clause_1.SourceExpression(new Clause_1.TableSource(null, new ValueComponent_1.IdentifierString(currentCteAlias)), null), null),
183
+ groupByClause: groupByItems.length > 0 ? new Clause_1.GroupByClause(groupByItems) : null,
184
+ });
185
+ const cte = new Clause_1.CommonTable(cteSelect, new Clause_1.SourceAliasExpression(cteAlias, null), null);
186
+ return { cte, newCteAlias: cteAlias };
187
+ }
188
+ /**
189
+ * Build JSON aggregation function for an array entity.
190
+ *
191
+ * This method creates a jsonb_agg or json_agg function call that aggregates
192
+ * the entity's columns into a JSON array. It also handles nested relationships
193
+ * by including child entity properties in the JSON object.
194
+ *
195
+ * @param entity The array entity being processed
196
+ * @param nestedEntities All nested entities from the mapping
197
+ * @param allEntities Map of all entities (not used in current implementation)
198
+ * @param useJsonb Whether to use JSONB functions
199
+ * @returns Object containing the JSON aggregation function
200
+ */
201
+ buildAggregationDetailsForArrayEntity(entity, nestedEntities, allEntities, useJsonb = false) {
202
+ // Build JSON object for array elements
203
+ const jsonBuildFunction = useJsonb ? "jsonb_build_object" : "json_build_object";
204
+ const args = [];
205
+ // Add the entity's own columns
206
+ Object.entries(entity.columns).forEach(([jsonKey, sqlColumn]) => {
207
+ args.push(new ValueComponent_1.LiteralValue(jsonKey));
208
+ args.push(new ValueComponent_1.ColumnReference(null, new ValueComponent_1.IdentifierString(sqlColumn)));
209
+ });
210
+ // Find and process child entities (both object and array types)
211
+ const childEntities = nestedEntities.filter((ne) => ne.parentId === entity.id);
212
+ childEntities.forEach((childEntity) => {
213
+ args.push(new ValueComponent_1.LiteralValue(childEntity.propertyName));
214
+ if (childEntity.relationshipType === "object") {
215
+ // For object relationships, use pre-computed JSON column
216
+ const jsonColumnName = `${childEntity.name.toLowerCase()}_json`;
217
+ args.push(new ValueComponent_1.ColumnReference(null, new ValueComponent_1.IdentifierString(jsonColumnName)));
218
+ }
219
+ else if (childEntity.relationshipType === "array") {
220
+ // For array relationships, use the column directly
221
+ args.push(new ValueComponent_1.ColumnReference(null, new ValueComponent_1.IdentifierString(childEntity.propertyName)));
222
+ }
223
+ });
224
+ // Create JSON object
225
+ const jsonObject = new ValueComponent_1.FunctionCall(null, new ValueComponent_1.RawString(jsonBuildFunction), new ValueComponent_1.ValueList(args), null);
226
+ // Create JSON aggregation
227
+ const jsonAggFunction = useJsonb ? "jsonb_agg" : "json_agg";
228
+ const jsonAgg = new ValueComponent_1.FunctionCall(null, new ValueComponent_1.RawString(jsonAggFunction), new ValueComponent_1.ValueList([jsonObject]), null);
229
+ return { jsonAgg };
230
+ }
231
+ }
232
+ exports.PostgresArrayEntityCteBuilder = PostgresArrayEntityCteBuilder;
233
+ // Constants for consistent naming conventions
234
+ PostgresArrayEntityCteBuilder.CTE_ARRAY_PREFIX = 'cte_array_depth_';
235
+ //# sourceMappingURL=PostgresArrayEntityCteBuilder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PostgresArrayEntityCteBuilder.js","sourceRoot":"","sources":["../../src/transformers/PostgresArrayEntityCteBuilder.ts"],"names":[],"mappings":";;;AAAA,6CAA0J;AAC1J,mEAAgE;AAChE,6DAA+I;AAG/I,iEAA8D;AAY9D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAa,6BAA6B;IAItC;;;;;;;OAOG;IACI,oBAAoB,CACvB,SAAwB,EACxB,qB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,IAAI,0BAAS,CAAC,eAAe,CAAC,EAC9B,IAAI,0BAAS,CAAC,CAAC,UAAU,CAAC,CAAC,EAC3B,IAAI,CACP,CAAC;QAEF,OAAO,EAAE,OAAO,EAAE,CAAC;IACvB,CAAC;;AA/QL,sEAgRC;AA/QG,8CAA8C;AACtB,8CAAgB,GAAG,kBAAkB,CAAC"}
@@ -0,0 +1,140 @@
1
+ import { CommonTable } from '../models/Clause';
2
+ import { JsonMapping } from './PostgreJsonQueryBuilder';
3
+ /**
4
+ * Entity with processing metadata
5
+ */
6
+ export interface ProcessableEntity {
7
+ id: string;
8
+ name: string;
9
+ columns: {
10
+ [jsonKey: string]: string;
11
+ };
12
+ isRoot: boolean;
13
+ propertyName: string;
14
+ parentId?: string;
15
+ relationshipType?: "object" | "array";
16
+ }
17
+ /**
18
+ * PostgreSQL-specific builder for creating CTEs for object entities (object relationships).
19
+ * This class handles the creation of CTEs that build JSON/JSONB objects for object entities,
20
+ * processing them from the deepest level up to ensure proper dependency ordering.
21
+ *
22
+ * Features:
23
+ * - Depth-based CTE naming (cte_object_depth_N)
24
+ * - NULL handling for entity columns
25
+ * - JSONB/JSON object construction
26
+ * - Hierarchical processing of nested objects
27
+ *
28
+ * Why depth calculation is critical:
29
+ * 1. Object entities can be nested at multiple levels. We must process the deepest
30
+ * (most distant) objects first to ensure their JSON representations are available
31
+ * when building their parent entities.
32
+ * 2. Object entity processing is essentially a column compression operation. Entities
33
+ * at the same depth level can be processed simultaneously since they don't depend
34
+ * on each other.
35
+ *
36
+ * Example hierarchy:
37
+ * Order (root, depth 0)
38
+ * └─ Customer (depth 1)
39
+ * └─ Address (depth 2)
40
+ * └─ Shipping (depth 1)
41
+ * └─ Carrier (depth 2)
42
+ *
43
+ * Processing order: depth 2 → depth 1 → depth 0
44
+ */
45
+ export declare class PostgresObjectEntityCteBuilder {
46
+ private static readonly JSON_COLUMN_SUFFIX;
47
+ private static readonly CTE_OBJECT_PREFIX;
48
+ private static readonly WILDCARD_COLUMN; /**
49
+ * Build CTEs for all object entities in the correct dependency order
50
+ * @param initialCte The starting CTE containing all raw data
51
+ * @param allEntities Map of all entities in the mapping
52
+ * @param mapping The JSON mapping configuration
53
+ * @returns Array of CTEs and the alias of the last CTE created
54
+ */
55
+ buildObjectEntityCtes(initialCte: CommonTable, allEntities: Map<string, ProcessableEntity>, mapping: JsonMapping): {
56
+ ctes: CommonTable[];
57
+ lastCteAlias: string;
58
+ }; /**
59
+ * Collect all object entities and calculate their depth from root.
60
+ *
61
+ * Depth calculation is crucial because:
62
+ * - It determines the processing order (deepest first)
63
+ * - It ensures dependencies are resolved before an entity is processed
64
+ * - It allows parallel processing of entities at the same depth level
65
+ *
66
+ * @param mapping The JSON mapping configuration
67
+ * @param allEntities Map of all entities in the mapping
68
+ * @returns Array of object entity information with calculated depths
69
+ */
70
+ private collectAndSortObjectEntities;
71
+ /**
72
+ * Group entities by their depth level.
73
+ *
74
+ * Grouping by depth allows us to:
75
+ * - Process all entities at the same level in a single CTE
76
+ * - Optimize query performance by reducing the number of CTEs
77
+ * - Maintain clear dependency ordering
78
+ *
79
+ * @param parentInfos Array of parent entity information with depths
80
+ * @returns Map of depth level to entities at that depth
81
+ */ private groupEntitiesByDepth;
82
+ /**
83
+ * Build a CTE that processes all entities at a specific depth level
84
+ */
85
+ private buildDepthCte;
86
+ /**
87
+ * Build JSON column for a single entity with NULL handling
88
+ */
89
+ private buildEntityJsonColumn;
90
+ /**
91
+ * Prepare entity columns and NULL checks.
92
+ *
93
+ * This method extracts column data and creates NULL checks for each column.
94
+ * The NULL checking is essential for handling outer joins correctly.
95
+ *
96
+ * In outer join scenarios, when there's no matching row in the joined table,
97
+ * all columns from that table will be NULL. Instead of creating an empty object
98
+ * with all NULL properties (e.g., {id: null, name: null, email: null}),
99
+ * we want to represent the absence of the entity as NULL itself.
100
+ *
101
+ * This ensures cleaner JSON output where missing relationships are represented
102
+ * as NULL rather than objects with all NULL fields.
103
+ *
104
+ * @param entity The entity whose columns are being processed
105
+ * @returns Object containing arrays of JSON object arguments and NULL check conditions
106
+ */
107
+ private prepareEntityColumns;
108
+ /**
109
+ * Add child object relationships to JSON object arguments.
110
+ *
111
+ * This method processes nested object-type entities that are direct children of the current entity.
112
+ * For each child entity, it adds the property name and corresponding JSON column reference
113
+ * to the arguments array that will be used to build the parent's JSON object.
114
+ *
115
+ * The child JSON columns are expected to already exist in the data source (created by deeper
116
+ * level CTEs), as we process from the deepest level up to the root.
117
+ *
118
+ * Note: In this context, "child" refers to entities that have an object relationship (0..1)
119
+ * with their parent. From a data perspective, these are typically entities referenced via
120
+ * foreign keys, representing "parent" entities in traditional database terminology.
121
+ *
122
+ * @param entity The current entity being processed
123
+ * @param jsonObjectArgs Array to which JSON object arguments will be added
124
+ * @param mapping The JSON mapping configuration
125
+ * @param allEntities Map of all entities in the mapping
126
+ */
127
+ private addChildObjectRelationships;
128
+ /**
129
+ * Create JSON object function call
130
+ */
131
+ private createJsonObject;
132
+ /**
133
+ * Build NULL condition from NULL checks
134
+ */
135
+ private buildNullCondition;
136
+ /**
137
+ * Create CASE expression with NULL handling
138
+ */
139
+ private createCaseExpression;
140
+ }