rawsql-ts 0.11.0-beta → 0.11.1-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 (80) hide show
  1. package/README.md +1 -3
  2. package/dist/esm/index.js +2 -0
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/esm/index.min.js +13 -13
  5. package/dist/esm/index.min.js.map +4 -4
  6. package/dist/esm/src/index.js +2 -0
  7. package/dist/esm/src/index.js.map +1 -1
  8. package/dist/esm/src/tokenReaders/OperatorTokenReader.js +4 -0
  9. package/dist/esm/src/tokenReaders/OperatorTokenReader.js.map +1 -1
  10. package/dist/esm/src/transformers/CTEDependencyTracer.js +249 -0
  11. package/dist/esm/src/transformers/CTEDependencyTracer.js.map +1 -0
  12. package/dist/esm/src/transformers/JsonMappingUnifier.js +217 -0
  13. package/dist/esm/src/transformers/JsonMappingUnifier.js.map +1 -0
  14. package/dist/esm/src/transformers/ModelDrivenJsonMapping.js +103 -0
  15. package/dist/esm/src/transformers/ModelDrivenJsonMapping.js.map +1 -0
  16. package/dist/esm/src/transformers/PostgresArrayEntityCteBuilder.js +204 -16
  17. package/dist/esm/src/transformers/PostgresArrayEntityCteBuilder.js.map +1 -1
  18. package/dist/esm/src/transformers/PostgresJsonQueryBuilder.js +10 -9
  19. package/dist/esm/src/transformers/PostgresJsonQueryBuilder.js.map +1 -1
  20. package/dist/esm/src/transformers/PostgresObjectEntityCteBuilder.js +8 -5
  21. package/dist/esm/src/transformers/PostgresObjectEntityCteBuilder.js.map +1 -1
  22. package/dist/esm/src/transformers/SelectableColumnCollector.js +17 -5
  23. package/dist/esm/src/transformers/SelectableColumnCollector.js.map +1 -1
  24. package/dist/esm/src/transformers/UnifiedJsonMapping.js +2 -4
  25. package/dist/esm/src/transformers/UnifiedJsonMapping.js.map +1 -1
  26. package/dist/esm/src/transformers/UpstreamSelectQueryFinder.js +8 -1
  27. package/dist/esm/src/transformers/UpstreamSelectQueryFinder.js.map +1 -1
  28. package/dist/esm/src/utils/OperatorPrecedence.js +3 -1
  29. package/dist/esm/src/utils/OperatorPrecedence.js.map +1 -1
  30. package/dist/esm/src/utils/SchemaManager.js +0 -1
  31. package/dist/esm/src/utils/SchemaManager.js.map +1 -1
  32. package/dist/esm/tsconfig.browser.tsbuildinfo +1 -1
  33. package/dist/esm/types/src/index.d.ts +2 -0
  34. package/dist/esm/types/src/transformers/CTEDependencyTracer.d.ts +58 -0
  35. package/dist/esm/types/src/transformers/JsonMappingUnifier.d.ts +95 -0
  36. package/dist/esm/types/src/transformers/ModelDrivenJsonMapping.d.ts +62 -0
  37. package/dist/esm/types/src/transformers/PostgresArrayEntityCteBuilder.d.ts +80 -1
  38. package/dist/esm/types/src/transformers/PostgresJsonQueryBuilder.d.ts +0 -1
  39. package/dist/esm/types/src/transformers/SelectableColumnCollector.d.ts +14 -3
  40. package/dist/esm/types/src/transformers/UnifiedJsonMapping.d.ts +0 -1
  41. package/dist/esm/types/src/transformers/UpstreamSelectQueryFinder.d.ts +5 -1
  42. package/dist/index.min.js +13 -13
  43. package/dist/index.min.js.map +4 -4
  44. package/dist/src/index.d.ts +2 -0
  45. package/dist/src/index.js +10 -0
  46. package/dist/src/index.js.map +1 -1
  47. package/dist/src/tokenReaders/OperatorTokenReader.js +4 -0
  48. package/dist/src/tokenReaders/OperatorTokenReader.js.map +1 -1
  49. package/dist/src/transformers/CTEDependencyTracer.d.ts +58 -0
  50. package/dist/src/transformers/CTEDependencyTracer.js +253 -0
  51. package/dist/src/transformers/CTEDependencyTracer.js.map +1 -0
  52. package/dist/src/transformers/JsonMappingUnifier.d.ts +95 -0
  53. package/dist/src/transformers/JsonMappingUnifier.js +226 -0
  54. package/dist/src/transformers/JsonMappingUnifier.js.map +1 -0
  55. package/dist/src/transformers/ModelDrivenJsonMapping.d.ts +62 -0
  56. package/dist/src/transformers/ModelDrivenJsonMapping.js +110 -0
  57. package/dist/src/transformers/ModelDrivenJsonMapping.js.map +1 -0
  58. package/dist/src/transformers/PostgresArrayEntityCteBuilder.d.ts +80 -1
  59. package/dist/src/transformers/PostgresArrayEntityCteBuilder.js +204 -16
  60. package/dist/src/transformers/PostgresArrayEntityCteBuilder.js.map +1 -1
  61. package/dist/src/transformers/PostgresJsonQueryBuilder.d.ts +0 -1
  62. package/dist/src/transformers/PostgresJsonQueryBuilder.js +10 -9
  63. package/dist/src/transformers/PostgresJsonQueryBuilder.js.map +1 -1
  64. package/dist/src/transformers/PostgresObjectEntityCteBuilder.js +8 -5
  65. package/dist/src/transformers/PostgresObjectEntityCteBuilder.js.map +1 -1
  66. package/dist/src/transformers/SelectableColumnCollector.d.ts +14 -3
  67. package/dist/src/transformers/SelectableColumnCollector.js +17 -5
  68. package/dist/src/transformers/SelectableColumnCollector.js.map +1 -1
  69. package/dist/src/transformers/UnifiedJsonMapping.d.ts +0 -1
  70. package/dist/src/transformers/UnifiedJsonMapping.js +2 -4
  71. package/dist/src/transformers/UnifiedJsonMapping.js.map +1 -1
  72. package/dist/src/transformers/UpstreamSelectQueryFinder.d.ts +5 -1
  73. package/dist/src/transformers/UpstreamSelectQueryFinder.js +8 -1
  74. package/dist/src/transformers/UpstreamSelectQueryFinder.js.map +1 -1
  75. package/dist/src/utils/OperatorPrecedence.js +3 -1
  76. package/dist/src/utils/OperatorPrecedence.js.map +1 -1
  77. package/dist/src/utils/SchemaManager.js +0 -1
  78. package/dist/src/utils/SchemaManager.js.map +1 -1
  79. package/dist/tsconfig.tsbuildinfo +1 -1
  80. package/package.json +2 -2
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Unified JSON Mapping processor that supports both legacy and model-driven formats.
3
+ *
4
+ * This module provides backward compatibility while encouraging migration to the model-driven format.
5
+ * It automatically detects the input format and normalizes to a consistent internal representation.
6
+ */
7
+ import { JsonMapping } from './PostgresJsonQueryBuilder';
8
+ import { ModelDrivenJsonMapping } from './ModelDrivenJsonMapping';
9
+ /**
10
+ * Unified mapping format that can handle both legacy and model-driven inputs.
11
+ */
12
+ export interface UnifiedMappingInput {
13
+ typeInfo?: {
14
+ interface: string;
15
+ importPath: string;
16
+ };
17
+ structure?: any;
18
+ protectedStringFields?: string[];
19
+ rootName?: string;
20
+ rootEntity?: any;
21
+ nestedEntities?: any[];
22
+ columns?: any;
23
+ relationships?: any;
24
+ }
25
+ /**
26
+ * Result of mapping format detection and conversion.
27
+ */
28
+ export interface MappingProcessResult {
29
+ format: 'model-driven' | 'unified' | 'legacy';
30
+ jsonMapping: JsonMapping;
31
+ originalInput: UnifiedMappingInput;
32
+ metadata?: {
33
+ typeInfo?: {
34
+ interface: string;
35
+ importPath: string;
36
+ };
37
+ protectedStringFields?: string[];
38
+ typeProtection?: any;
39
+ };
40
+ }
41
+ /**
42
+ * Detects the format of a JSON mapping configuration.
43
+ *
44
+ * @param input - The mapping configuration to analyze
45
+ * @returns The detected format type
46
+ */
47
+ export declare function detectMappingFormat(input: UnifiedMappingInput): 'model-driven' | 'unified' | 'legacy';
48
+ /**
49
+ * Main processor that unifies all JSON mapping formats into a consistent JsonMapping.
50
+ *
51
+ * Features:
52
+ * - Automatic format detection
53
+ * - Backward compatibility with all existing formats
54
+ * - Metadata preservation for advanced features
55
+ * - Zero external dependencies
56
+ *
57
+ * @param input - Any supported JSON mapping format
58
+ * @returns Unified processing result with JsonMapping and metadata
59
+ */
60
+ export declare function processJsonMapping(input: UnifiedMappingInput): MappingProcessResult;
61
+ /**
62
+ * Convenience function for direct JsonMapping extraction.
63
+ *
64
+ * @param input - Any supported JSON mapping format
65
+ * @returns JsonMapping ready for use with PostgresJsonQueryBuilder
66
+ */
67
+ export declare function unifyJsonMapping(input: UnifiedMappingInput): JsonMapping;
68
+ /**
69
+ * Type guard to check if input uses model-driven format.
70
+ *
71
+ * @param input - Mapping input to check
72
+ * @returns True if input is model-driven format
73
+ */
74
+ export declare function isModelDrivenFormat(input: UnifiedMappingInput): input is ModelDrivenJsonMapping;
75
+ /**
76
+ * Type guard to check if input uses unified format.
77
+ *
78
+ * @param input - Mapping input to check
79
+ * @returns True if input is unified format
80
+ */
81
+ export declare function isUnifiedFormat(input: UnifiedMappingInput): boolean;
82
+ /**
83
+ * Type guard to check if input uses legacy format.
84
+ *
85
+ * @param input - Mapping input to check
86
+ * @returns True if input is legacy format
87
+ */
88
+ export declare function isLegacyFormat(input: UnifiedMappingInput): boolean;
89
+ /**
90
+ * Migration helper that suggests upgrading to model-driven format.
91
+ *
92
+ * @param input - Current mapping configuration
93
+ * @returns Suggestions for migration (if applicable)
94
+ */
95
+ export declare function suggestModelDrivenMigration(input: UnifiedMappingInput): string[];
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Model-driven JSON mapping structure that mirrors TypeScript model definitions.
3
+ * This approach provides intuitive, hierarchical mapping that closely resembles the target data structure.
4
+ */
5
+ import { JsonMapping } from './PostgresJsonQueryBuilder';
6
+ /**
7
+ * Supported field types for database column mapping.
8
+ */
9
+ export type FieldType = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'auto';
10
+ /**
11
+ * Field mapping configuration that can be either a simple column name or enhanced mapping with type control.
12
+ */
13
+ export type FieldMapping = string | {
14
+ column: string;
15
+ type?: FieldType;
16
+ } | {
17
+ from: string;
18
+ type?: FieldType;
19
+ };
20
+ /**
21
+ * Nested object or array structure definition.
22
+ */
23
+ export interface NestedStructure {
24
+ type: 'object' | 'array';
25
+ from: string;
26
+ structure: StructureFields;
27
+ }
28
+ /**
29
+ * Structure fields can contain either field mappings or nested structures.
30
+ */
31
+ export type StructureFields = {
32
+ [key: string]: FieldMapping | NestedStructure;
33
+ };
34
+ /**
35
+ * Model-driven JSON mapping that mirrors TypeScript interface structure.
36
+ * This design makes it easy to understand the relationship between models and database columns.
37
+ */
38
+ export interface ModelDrivenJsonMapping {
39
+ typeInfo: {
40
+ interface: string;
41
+ importPath: string;
42
+ };
43
+ structure: StructureFields;
44
+ }
45
+ /**
46
+ * Type protection configuration extracted from the model-driven mapping.
47
+ */
48
+ export interface TypeProtectionConfig {
49
+ protectedStringFields: string[];
50
+ }
51
+ /**
52
+ * Convert a model-driven JSON mapping to the traditional JsonMapping format.
53
+ * This enables backward compatibility with existing PostgresJsonQueryBuilder.
54
+ */
55
+ export declare function convertModelDrivenMapping(modelMapping: ModelDrivenJsonMapping): {
56
+ jsonMapping: JsonMapping;
57
+ typeProtection: TypeProtectionConfig;
58
+ };
59
+ /**
60
+ * Validate that a model-driven mapping structure is well-formed.
61
+ */
62
+ export declare function validateModelDrivenMapping(mapping: ModelDrivenJsonMapping): string[];
@@ -90,8 +90,87 @@ export declare class PostgresArrayEntityCteBuilder {
90
90
  * @param entity The array entity being processed
91
91
  * @param nestedEntities All nested entities from the mapping
92
92
  * @param allEntities Map of all entities (not used in current implementation)
93
- * @param useJsonb Whether to use JSONB functions
94
93
  * @returns Object containing the JSON aggregation function
95
94
  */
96
95
  private buildAggregationDetailsForArrayEntity;
96
+ /**
97
+ * Collects array entity columns organized by depth for the GROUP BY exclusion strategy.
98
+ *
99
+ * This method creates a mapping from depth levels to sets of column names that belong to
100
+ * array entities at each depth. This is used to determine which columns should be excluded
101
+ * from GROUP BY clauses when performing array aggregation at specific depths.
102
+ *
103
+ * @param mapping The JSON mapping configuration containing all entities
104
+ * @param currentDepth The current aggregation depth being processed
105
+ * @returns A map where keys are depth levels and values are sets of column names
106
+ */
107
+ private collectArrayEntityColumnsByDepth;
108
+ /**
109
+ * Calculates the depth of an entity in the hierarchy by traversing up to the root.
110
+ *
111
+ * @param entity The entity to calculate depth for
112
+ * @param mapping The JSON mapping containing all entities
113
+ * @returns The depth level (0 for root level, 1 for first level, etc.)
114
+ */
115
+ private calculateEntityDepth;
116
+ /**
117
+ * Adds all columns from an entity to the specified depth set.
118
+ *
119
+ * @param entity The entity whose columns should be added
120
+ * @param depth The depth level to add columns to
121
+ * @param arrayEntitiesByDepth The map to update
122
+ */
123
+ private addEntityColumnsToDepthSet;
124
+ /**
125
+ * Recursively collects columns from all descendant entities under a parent entity.
126
+ *
127
+ * This method ensures that all nested entities (at any depth) under an array entity
128
+ * have their columns properly categorized by the array entity's depth level.
129
+ *
130
+ * @param parentEntityId The ID of the parent entity
131
+ * @param targetDepth The depth level to assign collected columns to
132
+ * @param mapping The JSON mapping containing all entities
133
+ * @param arrayEntitiesByDepth The map to update with collected columns
134
+ */
135
+ private collectDescendantColumns;
136
+ /**
137
+ * Processes SELECT variables to determine which should be included in GROUP BY clauses.
138
+ *
139
+ * This method implements the core logic for deciding which columns from previous CTEs
140
+ * should be included in the GROUP BY clause when performing array aggregation. It handles
141
+ * special cases for JSON columns and applies depth-based filtering to prevent over-grouping.
142
+ *
143
+ * @param prevSelects SELECT variables from the previous CTE
144
+ * @param arrayColumns Columns that are being aggregated (should be excluded from GROUP BY)
145
+ * @param arrayEntitiesByDepth Map of depth levels to their column sets
146
+ * @param currentDepth The current aggregation depth being processed
147
+ * @param selectItems Output array for SELECT items
148
+ * @param groupByItems Output array for GROUP BY items
149
+ */
150
+ private processSelectVariablesForGroupBy;
151
+ /**
152
+ * Determines whether a column should be included in the GROUP BY clause.
153
+ *
154
+ * This method applies depth-based filtering and special handling for JSON columns
155
+ * to prevent over-grouping during array aggregation. It implements heuristics for
156
+ * excluding columns that belong to nested entities within array contexts.
157
+ *
158
+ * @param columnName The name of the column to evaluate
159
+ * @param arrayEntitiesByDepth Map of depth levels to their column sets
160
+ * @param currentDepth The current aggregation depth
161
+ * @returns True if the column should be included in GROUP BY, false otherwise
162
+ */
163
+ private shouldIncludeColumnInGroupBy;
164
+ /**
165
+ * Applies heuristics to determine if an entity JSON column should be included in GROUP BY.
166
+ *
167
+ * This method uses entity numbering patterns to identify deeply nested entities
168
+ * that should be excluded from GROUP BY when processing array aggregations.
169
+ * This is a simplified heuristic approach that works for current use cases.
170
+ *
171
+ * @param columnName The JSON column name (expected format: entity_N_json)
172
+ * @param currentDepth The current aggregation depth
173
+ * @returns True if the JSON column should be included, false otherwise
174
+ */
175
+ private shouldIncludeJsonColumn;
97
176
  }
@@ -23,7 +23,6 @@ export interface JsonMapping {
23
23
  [jsonKey: string]: string;
24
24
  };
25
25
  }>;
26
- useJsonb?: boolean;
27
26
  resultFormat?: "array" | "single";
28
27
  emptyResult?: string;
29
28
  }
@@ -20,12 +20,23 @@ import { SqlComponent, SqlComponentVisitor } from "../models/SqlComponent";
20
20
  import { ValueComponent } from "../models/ValueComponent";
21
21
  import { TableColumnResolver } from "./TableColumnResolver";
22
22
  /**
23
- * A visitor that collects all ColumnReference instances from a SQL query structure.
23
+ * A visitor that collects all ColumnReference instances from SimpleSelectQuery structures.
24
24
  * This visitor scans through all clauses and collects all unique ColumnReference objects.
25
25
  * It does not scan Common Table Expressions (CTEs) or subqueries.
26
26
  *
27
- * Important: Only collects column references to tables defined in the root FROM/JOIN clauses,
28
- * as these are the only columns that can be directly referenced in the query.
27
+ * IMPORTANT: This collector only supports SimpleSelectQuery. BinarySelectQuery
28
+ * (UNION/INTERSECT/EXCEPT) will throw an error and should be decomposed into
29
+ * individual SimpleSelectQuery branches before using this collector.
30
+ *
31
+ * Behavioral notes:
32
+ * - Only collects column references to tables defined in the root FROM/JOIN clauses
33
+ * - For aliased columns (e.g., 'title as name'), collects both the original column
34
+ * reference ('title') AND the alias ('name') to enable complete dependency tracking
35
+ *
36
+ * Use cases:
37
+ * - Dependency analysis and schema migration tools
38
+ * - Column usage tracking within individual SELECT branches
39
+ * - Security analysis for column-level access control
29
40
  */
30
41
  export declare class SelectableColumnCollector implements SqlComponentVisitor<void> {
31
42
  private handlers;
@@ -36,7 +36,6 @@ export interface UnifiedJsonMapping {
36
36
  relationshipType: 'object' | 'array';
37
37
  columns: Record<string, ColumnMappingConfig>;
38
38
  }>;
39
- useJsonb?: boolean;
40
39
  }
41
40
  /**
42
41
  * Type protection configuration extracted from the unified mapping.
@@ -3,7 +3,11 @@ import { SelectQuery, SimpleSelectQuery } from "../models/SelectQuery";
3
3
  * UpstreamSelectQueryFinder searches upstream queries for the specified columns.
4
4
  * If a query (including its upstream CTEs or subqueries) contains all columns,
5
5
  * it returns the highest such SelectQuery. Otherwise, it searches downstream.
6
- * For UNION queries, it checks each branch independently.
6
+ *
7
+ * For BinarySelectQuery (UNION/INTERSECT/EXCEPT), this finder processes each branch
8
+ * independently, as SelectableColumnCollector is designed for SimpleSelectQuery only.
9
+ * This approach ensures accurate column detection within individual SELECT branches
10
+ * while maintaining compatibility with compound query structures.
7
11
  */
8
12
  export declare class UpstreamSelectQueryFinder {
9
13
  private options;