metal-orm 1.0.8 → 1.0.9
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/README.md +12 -1
- package/dist/decorators/index.cjs +2564 -0
- package/dist/decorators/index.cjs.map +1 -0
- package/dist/decorators/index.d.cts +53 -0
- package/dist/decorators/index.d.ts +53 -0
- package/dist/decorators/index.js +2530 -0
- package/dist/decorators/index.js.map +1 -0
- package/dist/index.cjs +4227 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +701 -0
- package/dist/index.d.ts +701 -0
- package/dist/index.js +4131 -0
- package/dist/index.js.map +1 -0
- package/dist/select-654m4qy8.d.cts +1522 -0
- package/dist/select-654m4qy8.d.ts +1522 -0
- package/package.json +27 -20
- package/src/codegen/typescript.ts +405 -393
- package/src/core/ast/aggregate-functions.ts +30 -0
- package/src/core/ast/builders.ts +43 -0
- package/src/core/ast/expression-builders.ts +310 -0
- package/src/core/ast/expression-nodes.ts +211 -0
- package/src/core/ast/expression-visitor.ts +99 -0
- package/src/core/ast/expression.ts +5 -0
- package/src/{utils → core/ast}/join-node.ts +20 -20
- package/src/{ast → core/ast}/join.ts +18 -18
- package/src/{ast → core/ast}/query.ts +113 -113
- package/src/core/ast/window-functions.ts +140 -0
- package/src/{dialect → core/dialect}/abstract.ts +94 -94
- package/src/{dialect → core/dialect}/mssql/index.ts +31 -31
- package/src/{dialect → core/dialect}/mysql/index.ts +31 -31
- package/src/{dialect → core/dialect}/postgres/index.ts +45 -45
- package/src/{dialect → core/dialect}/sqlite/index.ts +45 -45
- package/src/{constants → core/sql}/sql-operator-config.ts +39 -39
- package/src/decorators/bootstrap.ts +126 -0
- package/src/decorators/column.ts +78 -0
- package/src/decorators/entity.ts +36 -0
- package/src/decorators/index.ts +4 -0
- package/src/decorators/relations.ts +107 -0
- package/src/global.d.ts +1 -0
- package/src/index.ts +22 -22
- package/src/orm/db-executor.ts +11 -0
- package/src/orm/domain-event-bus.ts +52 -0
- package/src/{runtime → orm}/entity-meta.ts +52 -52
- package/src/orm/entity-metadata.ts +140 -0
- package/src/{runtime → orm}/entity.ts +252 -252
- package/src/{runtime → orm}/execute.ts +36 -36
- package/src/{runtime → orm}/hydration.ts +103 -103
- package/src/orm/identity-map.ts +37 -0
- package/src/{runtime → orm}/lazy-batch.ts +205 -205
- package/src/orm/orm-context.ts +154 -0
- package/src/orm/relation-change-processor.ts +140 -0
- package/src/{runtime → orm}/relations/belongs-to.ts +92 -92
- package/src/{runtime → orm}/relations/has-many.ts +111 -111
- package/src/{runtime → orm}/relations/many-to-many.ts +149 -149
- package/src/orm/runtime-types.ts +39 -0
- package/src/orm/transaction-runner.ts +17 -0
- package/src/orm/unit-of-work.ts +232 -0
- package/src/{builder/operations → query-builder}/column-selector.ts +78 -78
- package/src/{builder → query-builder}/delete-query-state.ts +38 -42
- package/src/{builder → query-builder}/delete.ts +46 -57
- package/src/{builder → query-builder}/hydration-manager.ts +87 -87
- package/src/{builder → query-builder}/hydration-planner.ts +182 -182
- package/src/{builder → query-builder}/insert-query-state.ts +51 -62
- package/src/{builder → query-builder}/insert.ts +48 -59
- package/src/{builder → query-builder}/query-ast-service.ts +208 -226
- package/src/{utils → query-builder}/raw-column-parser.ts +32 -32
- package/src/{builder → query-builder}/relation-conditions.ts +112 -112
- package/src/{builder/operations → query-builder}/relation-manager.ts +82 -82
- package/src/{builder → query-builder}/relation-projection-helper.ts +101 -101
- package/src/{builder → query-builder}/relation-service.ts +284 -284
- package/src/{builder → query-builder}/relation-types.ts +21 -21
- package/src/{builder → query-builder}/relation-utils.ts +12 -12
- package/src/{builder → query-builder}/select-query-builder-deps.ts +112 -94
- package/src/{builder → query-builder}/select-query-state.ts +179 -179
- package/src/{builder → query-builder}/select.ts +78 -69
- package/src/{builder → query-builder}/update-query-state.ts +55 -59
- package/src/{builder → query-builder}/update.ts +50 -61
- package/src/schema/column.ts +25 -25
- package/src/schema/relation.ts +116 -116
- package/src/schema/table.ts +34 -34
- package/src/schema/types.ts +76 -76
- package/.github/workflows/publish-metal-orm.yml +0 -38
- package/ROADMAP.md +0 -125
- package/docs/CHANGES.md +0 -104
- package/docs/advanced-features.md +0 -176
- package/docs/api-reference.md +0 -31
- package/docs/dml-operations.md +0 -156
- package/docs/getting-started.md +0 -171
- package/docs/hydration.md +0 -115
- package/docs/index.md +0 -36
- package/docs/multi-dialect-support.md +0 -59
- package/docs/query-builder.md +0 -135
- package/docs/runtime.md +0 -105
- package/docs/schema-definition.md +0 -112
- package/metadata.json +0 -5
- package/playground/api/playground-api.ts +0 -94
- package/playground/index.html +0 -15
- package/playground/src/App.css +0 -1
- package/playground/src/App.tsx +0 -114
- package/playground/src/components/CodeDisplay.tsx +0 -43
- package/playground/src/components/QueryExecutor.tsx +0 -189
- package/playground/src/components/ResultsTable.tsx +0 -67
- package/playground/src/components/ResultsTabs.tsx +0 -105
- package/playground/src/components/ScenarioList.tsx +0 -56
- package/playground/src/components/logo.svg +0 -45
- package/playground/src/data/scenarios.ts +0 -2
- package/playground/src/main.tsx +0 -9
- package/playground/src/services/PlaygroundApiService.ts +0 -60
- package/postcss.config.cjs +0 -5
- package/sql_sql-ansi-cheatsheet-2025.md +0 -264
- package/src/ast/expression.ts +0 -658
- package/src/builder/operations/cte-manager.ts +0 -34
- package/src/builder/operations/filter-manager.ts +0 -68
- package/src/builder/operations/join-manager.ts +0 -36
- package/src/builder/operations/pagination-manager.ts +0 -36
- package/src/playground/features/playground/api/types.ts +0 -16
- package/src/playground/features/playground/clients/MockClient.ts +0 -17
- package/src/playground/features/playground/clients/SqliteClient.ts +0 -57
- package/src/playground/features/playground/common/IDatabaseClient.ts +0 -10
- package/src/playground/features/playground/data/scenarios/aggregation.ts +0 -36
- package/src/playground/features/playground/data/scenarios/basics.ts +0 -25
- package/src/playground/features/playground/data/scenarios/edge_cases.ts +0 -57
- package/src/playground/features/playground/data/scenarios/filtering.ts +0 -94
- package/src/playground/features/playground/data/scenarios/hydration.ts +0 -27
- package/src/playground/features/playground/data/scenarios/index.ts +0 -29
- package/src/playground/features/playground/data/scenarios/ordering.ts +0 -25
- package/src/playground/features/playground/data/scenarios/pagination.ts +0 -16
- package/src/playground/features/playground/data/scenarios/relationships.ts +0 -75
- package/src/playground/features/playground/data/scenarios/types.ts +0 -70
- package/src/playground/features/playground/data/schema.ts +0 -91
- package/src/playground/features/playground/data/seed.ts +0 -104
- package/src/playground/features/playground/services/QueryExecutionService.ts +0 -121
- package/src/runtime/orm-context.ts +0 -539
- package/tests/belongs-to-many.test.ts +0 -57
- package/tests/between.test.ts +0 -43
- package/tests/case-expression.test.ts +0 -58
- package/tests/complex-exists.test.ts +0 -230
- package/tests/cte.test.ts +0 -118
- package/tests/dml.test.ts +0 -206
- package/tests/exists.test.ts +0 -127
- package/tests/like.test.ts +0 -33
- package/tests/orm-runtime.test.ts +0 -254
- package/tests/postgres.test.ts +0 -30
- package/tests/right-join.test.ts +0 -89
- package/tests/subquery-having.test.ts +0 -193
- package/tests/window-function.test.ts +0 -151
- package/tsconfig.json +0 -30
- package/tsup.config.ts +0 -10
- package/vite.config.ts +0 -22
- package/vitest.config.ts +0 -14
- /package/src/{constants → core/sql}/sql.ts +0 -0
- /package/src/{runtime → orm}/als.ts +0 -0
- /package/src/{utils → query-builder}/relation-alias.ts +0 -0
|
@@ -0,0 +1,1522 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supported column data types for database schema definitions
|
|
3
|
+
*/
|
|
4
|
+
type ColumnType = 'INT' | 'INTEGER' | 'VARCHAR' | 'TEXT' | 'JSON' | 'ENUM' | 'BOOLEAN' | 'int' | 'integer' | 'varchar' | 'text' | 'json' | 'enum' | 'boolean';
|
|
5
|
+
/**
|
|
6
|
+
* Definition of a database column
|
|
7
|
+
*/
|
|
8
|
+
interface ColumnDef<T extends ColumnType = ColumnType> {
|
|
9
|
+
/** Column name (filled at runtime by defineTable) */
|
|
10
|
+
name: string;
|
|
11
|
+
/** Data type of the column */
|
|
12
|
+
type: T;
|
|
13
|
+
/** Whether this column is a primary key */
|
|
14
|
+
primary?: boolean;
|
|
15
|
+
/** Whether this column cannot be null */
|
|
16
|
+
notNull?: boolean;
|
|
17
|
+
/** Additional arguments for the column type (e.g., VARCHAR length) */
|
|
18
|
+
args?: any[];
|
|
19
|
+
/** Table name this column belongs to (filled at runtime by defineTable) */
|
|
20
|
+
table?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Factory for creating column definitions with common data types
|
|
24
|
+
*/
|
|
25
|
+
declare const col: {
|
|
26
|
+
/**
|
|
27
|
+
* Creates an integer column definition
|
|
28
|
+
* @returns ColumnDef with INT type
|
|
29
|
+
*/
|
|
30
|
+
int: () => ColumnDef<"INT">;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a variable character column definition
|
|
33
|
+
* @param length - Maximum length of the string
|
|
34
|
+
* @returns ColumnDef with VARCHAR type
|
|
35
|
+
*/
|
|
36
|
+
varchar: (length: number) => ColumnDef<"VARCHAR">;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a JSON column definition
|
|
39
|
+
* @returns ColumnDef with JSON type
|
|
40
|
+
*/
|
|
41
|
+
json: () => ColumnDef<"JSON">;
|
|
42
|
+
/**
|
|
43
|
+
* Creates a boolean column definition
|
|
44
|
+
* @returns ColumnDef with BOOLEAN type
|
|
45
|
+
*/
|
|
46
|
+
boolean: () => ColumnDef<"BOOLEAN">;
|
|
47
|
+
/**
|
|
48
|
+
* Marks a column definition as a primary key
|
|
49
|
+
* @param def - Column definition to modify
|
|
50
|
+
* @returns Modified ColumnDef with primary: true
|
|
51
|
+
*/
|
|
52
|
+
primaryKey: <T extends ColumnType>(def: ColumnDef<T>) => ColumnDef<T>;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Types of relationships supported between tables
|
|
57
|
+
*/
|
|
58
|
+
declare const RelationKinds: {
|
|
59
|
+
/** One-to-many relationship */
|
|
60
|
+
readonly HasMany: "HAS_MANY";
|
|
61
|
+
/** Many-to-one relationship */
|
|
62
|
+
readonly BelongsTo: "BELONGS_TO";
|
|
63
|
+
/** Many-to-many relationship with pivot metadata */
|
|
64
|
+
readonly BelongsToMany: "BELONGS_TO_MANY";
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Type representing the supported relationship kinds
|
|
68
|
+
*/
|
|
69
|
+
type RelationType = (typeof RelationKinds)[keyof typeof RelationKinds];
|
|
70
|
+
type CascadeMode = 'none' | 'all' | 'persist' | 'remove' | 'link';
|
|
71
|
+
/**
|
|
72
|
+
* One-to-many relationship definition
|
|
73
|
+
*/
|
|
74
|
+
interface HasManyRelation<TTarget extends TableDef = TableDef> {
|
|
75
|
+
type: typeof RelationKinds.HasMany;
|
|
76
|
+
target: TTarget;
|
|
77
|
+
foreignKey: string;
|
|
78
|
+
localKey?: string;
|
|
79
|
+
cascade?: CascadeMode;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Many-to-one relationship definition
|
|
83
|
+
*/
|
|
84
|
+
interface BelongsToRelation<TTarget extends TableDef = TableDef> {
|
|
85
|
+
type: typeof RelationKinds.BelongsTo;
|
|
86
|
+
target: TTarget;
|
|
87
|
+
foreignKey: string;
|
|
88
|
+
localKey?: string;
|
|
89
|
+
cascade?: CascadeMode;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Many-to-many relationship definition with rich pivot metadata
|
|
93
|
+
*/
|
|
94
|
+
interface BelongsToManyRelation<TTarget extends TableDef = TableDef> {
|
|
95
|
+
type: typeof RelationKinds.BelongsToMany;
|
|
96
|
+
target: TTarget;
|
|
97
|
+
pivotTable: TableDef;
|
|
98
|
+
pivotForeignKeyToRoot: string;
|
|
99
|
+
pivotForeignKeyToTarget: string;
|
|
100
|
+
localKey?: string;
|
|
101
|
+
targetKey?: string;
|
|
102
|
+
pivotPrimaryKey?: string;
|
|
103
|
+
defaultPivotColumns?: string[];
|
|
104
|
+
cascade?: CascadeMode;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Union type representing any supported relationship definition
|
|
108
|
+
*/
|
|
109
|
+
type RelationDef = HasManyRelation | BelongsToRelation | BelongsToManyRelation;
|
|
110
|
+
/**
|
|
111
|
+
* Creates a one-to-many relationship definition
|
|
112
|
+
* @param target - Target table of the relationship
|
|
113
|
+
* @param foreignKey - Foreign key column name on the child table
|
|
114
|
+
* @param localKey - Local key column name (optional)
|
|
115
|
+
* @returns HasManyRelation definition
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* hasMany(usersTable, 'user_id')
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
declare const hasMany: <TTarget extends TableDef>(target: TTarget, foreignKey: string, localKey?: string, cascade?: CascadeMode) => HasManyRelation<TTarget>;
|
|
123
|
+
/**
|
|
124
|
+
* Creates a many-to-one relationship definition
|
|
125
|
+
* @param target - Target table of the relationship
|
|
126
|
+
* @param foreignKey - Foreign key column name on the child table
|
|
127
|
+
* @param localKey - Local key column name (optional)
|
|
128
|
+
* @returns BelongsToRelation definition
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* belongsTo(usersTable, 'user_id')
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
declare const belongsTo: <TTarget extends TableDef>(target: TTarget, foreignKey: string, localKey?: string, cascade?: CascadeMode) => BelongsToRelation<TTarget>;
|
|
136
|
+
/**
|
|
137
|
+
* Creates a many-to-many relationship definition with pivot metadata
|
|
138
|
+
* @param target - Target table
|
|
139
|
+
* @param pivotTable - Intermediate pivot table definition
|
|
140
|
+
* @param options - Pivot metadata configuration
|
|
141
|
+
* @returns BelongsToManyRelation definition
|
|
142
|
+
*/
|
|
143
|
+
declare const belongsToMany: <TTarget extends TableDef>(target: TTarget, pivotTable: TableDef, options: {
|
|
144
|
+
pivotForeignKeyToRoot: string;
|
|
145
|
+
pivotForeignKeyToTarget: string;
|
|
146
|
+
localKey?: string;
|
|
147
|
+
targetKey?: string;
|
|
148
|
+
pivotPrimaryKey?: string;
|
|
149
|
+
defaultPivotColumns?: string[];
|
|
150
|
+
cascade?: CascadeMode;
|
|
151
|
+
}) => BelongsToManyRelation<TTarget>;
|
|
152
|
+
|
|
153
|
+
interface TableHooks {
|
|
154
|
+
beforeInsert?(ctx: unknown, entity: any): Promise<void> | void;
|
|
155
|
+
afterInsert?(ctx: unknown, entity: any): Promise<void> | void;
|
|
156
|
+
beforeUpdate?(ctx: unknown, entity: any): Promise<void> | void;
|
|
157
|
+
afterUpdate?(ctx: unknown, entity: any): Promise<void> | void;
|
|
158
|
+
beforeDelete?(ctx: unknown, entity: any): Promise<void> | void;
|
|
159
|
+
afterDelete?(ctx: unknown, entity: any): Promise<void> | void;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Definition of a database table with its columns and relationships
|
|
163
|
+
* @typeParam T - Type of the columns record
|
|
164
|
+
*/
|
|
165
|
+
interface TableDef<T extends Record<string, ColumnDef> = Record<string, ColumnDef>> {
|
|
166
|
+
/** Name of the table */
|
|
167
|
+
name: string;
|
|
168
|
+
/** Record of column definitions keyed by column name */
|
|
169
|
+
columns: T;
|
|
170
|
+
/** Record of relationship definitions keyed by relation name */
|
|
171
|
+
relations: Record<string, RelationDef>;
|
|
172
|
+
/** Optional lifecycle hooks */
|
|
173
|
+
hooks?: TableHooks;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Creates a table definition with columns and relationships
|
|
177
|
+
* @typeParam T - Type of the columns record
|
|
178
|
+
* @param name - Name of the table
|
|
179
|
+
* @param columns - Record of column definitions
|
|
180
|
+
* @param relations - Record of relationship definitions (optional)
|
|
181
|
+
* @returns Complete table definition with runtime-filled column metadata
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```typescript
|
|
185
|
+
* const usersTable = defineTable('users', {
|
|
186
|
+
* id: col.primaryKey(col.int()),
|
|
187
|
+
* name: col.varchar(255),
|
|
188
|
+
* email: col.varchar(255)
|
|
189
|
+
* });
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
declare const defineTable: <T extends Record<string, ColumnDef>>(name: string, columns: T, relations?: Record<string, RelationDef>, hooks?: TableHooks) => TableDef<T>;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Maps a ColumnDef to its TypeScript type representation
|
|
196
|
+
*/
|
|
197
|
+
type ColumnToTs<T extends ColumnDef> = T['type'] extends 'INT' | 'INTEGER' | 'int' | 'integer' ? number : T['type'] extends 'BOOLEAN' | 'boolean' ? boolean : T['type'] extends 'JSON' | 'json' ? unknown : string;
|
|
198
|
+
/**
|
|
199
|
+
* Infers a row shape from a table definition
|
|
200
|
+
*/
|
|
201
|
+
type InferRow<TTable extends TableDef> = {
|
|
202
|
+
[K in keyof TTable['columns']]: ColumnToTs<TTable['columns'][K]>;
|
|
203
|
+
};
|
|
204
|
+
type RelationResult$1<T extends RelationDef> = T extends HasManyRelation<infer TTarget> ? InferRow<TTarget>[] : T extends BelongsToRelation<infer TTarget> ? InferRow<TTarget> | null : T extends BelongsToManyRelation<infer TTarget> ? (InferRow<TTarget> & {
|
|
205
|
+
_pivot?: any;
|
|
206
|
+
})[] : never;
|
|
207
|
+
/**
|
|
208
|
+
* Maps relation names to the expected row results
|
|
209
|
+
*/
|
|
210
|
+
type RelationMap<TTable extends TableDef> = {
|
|
211
|
+
[K in keyof TTable['relations']]: RelationResult$1<TTable['relations'][K]>;
|
|
212
|
+
};
|
|
213
|
+
interface HasManyCollection<TChild> {
|
|
214
|
+
load(): Promise<TChild[]>;
|
|
215
|
+
getItems(): TChild[];
|
|
216
|
+
add(data: Partial<TChild>): TChild;
|
|
217
|
+
attach(entity: TChild): void;
|
|
218
|
+
remove(entity: TChild): void;
|
|
219
|
+
clear(): void;
|
|
220
|
+
}
|
|
221
|
+
interface BelongsToReference<TParent> {
|
|
222
|
+
load(): Promise<TParent | null>;
|
|
223
|
+
get(): TParent | null;
|
|
224
|
+
set(data: Partial<TParent> | TParent | null): TParent | null;
|
|
225
|
+
}
|
|
226
|
+
interface ManyToManyCollection<TTarget> {
|
|
227
|
+
load(): Promise<TTarget[]>;
|
|
228
|
+
getItems(): TTarget[];
|
|
229
|
+
attach(target: TTarget | number | string): void;
|
|
230
|
+
detach(target: TTarget | number | string): void;
|
|
231
|
+
syncByIds(ids: (number | string)[]): Promise<void>;
|
|
232
|
+
}
|
|
233
|
+
type Entity<TTable extends TableDef, TRow = InferRow<TTable>> = TRow & {
|
|
234
|
+
[K in keyof RelationMap<TTable>]: TTable['relations'][K] extends HasManyRelation<infer TTarget> ? HasManyCollection<Entity<TTarget>> : TTable['relations'][K] extends BelongsToManyRelation<infer TTarget> ? ManyToManyCollection<Entity<TTarget>> : TTable['relations'][K] extends BelongsToRelation<infer TTarget> ? BelongsToReference<Entity<TTarget>> : never;
|
|
235
|
+
} & {
|
|
236
|
+
$load<K extends keyof RelationMap<TTable>>(relation: K): Promise<RelationMap<TTable>[K]>;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* SQL operators used in query conditions
|
|
241
|
+
*/
|
|
242
|
+
declare const SQL_OPERATORS: {
|
|
243
|
+
/** Equality operator */
|
|
244
|
+
readonly EQUALS: "=";
|
|
245
|
+
/** Not equals operator */
|
|
246
|
+
readonly NOT_EQUALS: "!=";
|
|
247
|
+
/** Greater than operator */
|
|
248
|
+
readonly GREATER_THAN: ">";
|
|
249
|
+
/** Greater than or equal operator */
|
|
250
|
+
readonly GREATER_OR_EQUAL: ">=";
|
|
251
|
+
/** Less than operator */
|
|
252
|
+
readonly LESS_THAN: "<";
|
|
253
|
+
/** Less than or equal operator */
|
|
254
|
+
readonly LESS_OR_EQUAL: "<=";
|
|
255
|
+
/** LIKE pattern matching operator */
|
|
256
|
+
readonly LIKE: "LIKE";
|
|
257
|
+
/** NOT LIKE pattern matching operator */
|
|
258
|
+
readonly NOT_LIKE: "NOT LIKE";
|
|
259
|
+
/** IN membership operator */
|
|
260
|
+
readonly IN: "IN";
|
|
261
|
+
/** NOT IN membership operator */
|
|
262
|
+
readonly NOT_IN: "NOT IN";
|
|
263
|
+
/** BETWEEN range operator */
|
|
264
|
+
readonly BETWEEN: "BETWEEN";
|
|
265
|
+
/** NOT BETWEEN range operator */
|
|
266
|
+
readonly NOT_BETWEEN: "NOT BETWEEN";
|
|
267
|
+
/** IS NULL null check operator */
|
|
268
|
+
readonly IS_NULL: "IS NULL";
|
|
269
|
+
/** IS NOT NULL null check operator */
|
|
270
|
+
readonly IS_NOT_NULL: "IS NOT NULL";
|
|
271
|
+
/** Logical AND operator */
|
|
272
|
+
readonly AND: "AND";
|
|
273
|
+
/** Logical OR operator */
|
|
274
|
+
readonly OR: "OR";
|
|
275
|
+
/** EXISTS operator */
|
|
276
|
+
readonly EXISTS: "EXISTS";
|
|
277
|
+
/** NOT EXISTS operator */
|
|
278
|
+
readonly NOT_EXISTS: "NOT EXISTS";
|
|
279
|
+
};
|
|
280
|
+
/**
|
|
281
|
+
* Type representing any supported SQL operator
|
|
282
|
+
*/
|
|
283
|
+
type SqlOperator = (typeof SQL_OPERATORS)[keyof typeof SQL_OPERATORS];
|
|
284
|
+
/**
|
|
285
|
+
* Types of SQL joins supported
|
|
286
|
+
*/
|
|
287
|
+
declare const JOIN_KINDS: {
|
|
288
|
+
/** INNER JOIN type */
|
|
289
|
+
readonly INNER: "INNER";
|
|
290
|
+
/** LEFT JOIN type */
|
|
291
|
+
readonly LEFT: "LEFT";
|
|
292
|
+
/** RIGHT JOIN type */
|
|
293
|
+
readonly RIGHT: "RIGHT";
|
|
294
|
+
/** CROSS JOIN type */
|
|
295
|
+
readonly CROSS: "CROSS";
|
|
296
|
+
};
|
|
297
|
+
/**
|
|
298
|
+
* Type representing any supported join kind
|
|
299
|
+
*/
|
|
300
|
+
type JoinKind = (typeof JOIN_KINDS)[keyof typeof JOIN_KINDS];
|
|
301
|
+
/**
|
|
302
|
+
* Ordering directions for result sorting
|
|
303
|
+
*/
|
|
304
|
+
declare const ORDER_DIRECTIONS: {
|
|
305
|
+
/** Ascending order */
|
|
306
|
+
readonly ASC: "ASC";
|
|
307
|
+
/** Descending order */
|
|
308
|
+
readonly DESC: "DESC";
|
|
309
|
+
};
|
|
310
|
+
/**
|
|
311
|
+
* Type representing any supported order direction
|
|
312
|
+
*/
|
|
313
|
+
type OrderDirection = (typeof ORDER_DIRECTIONS)[keyof typeof ORDER_DIRECTIONS];
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* AST node representing a literal value
|
|
317
|
+
*/
|
|
318
|
+
interface LiteralNode {
|
|
319
|
+
type: 'Literal';
|
|
320
|
+
/** The literal value (string, number, boolean, or null) */
|
|
321
|
+
value: string | number | boolean | null;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* AST node representing a column reference
|
|
325
|
+
*/
|
|
326
|
+
interface ColumnNode {
|
|
327
|
+
type: 'Column';
|
|
328
|
+
/** Table name the column belongs to */
|
|
329
|
+
table: string;
|
|
330
|
+
/** Column name */
|
|
331
|
+
name: string;
|
|
332
|
+
/** Optional alias for the column */
|
|
333
|
+
alias?: string;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* AST node representing a function call
|
|
337
|
+
*/
|
|
338
|
+
interface FunctionNode {
|
|
339
|
+
type: 'Function';
|
|
340
|
+
/** Function name (e.g., COUNT, SUM) */
|
|
341
|
+
name: string;
|
|
342
|
+
/** Function arguments */
|
|
343
|
+
args: (ColumnNode | LiteralNode | JsonPathNode)[];
|
|
344
|
+
/** Optional alias for the function result */
|
|
345
|
+
alias?: string;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* AST node representing a JSON path expression
|
|
349
|
+
*/
|
|
350
|
+
interface JsonPathNode {
|
|
351
|
+
type: 'JsonPath';
|
|
352
|
+
/** Source column */
|
|
353
|
+
column: ColumnNode;
|
|
354
|
+
/** JSON path expression */
|
|
355
|
+
path: string;
|
|
356
|
+
/** Optional alias for the result */
|
|
357
|
+
alias?: string;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* AST node representing a scalar subquery
|
|
361
|
+
*/
|
|
362
|
+
interface ScalarSubqueryNode {
|
|
363
|
+
type: 'ScalarSubquery';
|
|
364
|
+
/** Subquery to execute */
|
|
365
|
+
query: SelectQueryNode;
|
|
366
|
+
/** Optional alias for the subquery result */
|
|
367
|
+
alias?: string;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* AST node representing a CASE expression
|
|
371
|
+
*/
|
|
372
|
+
interface CaseExpressionNode {
|
|
373
|
+
type: 'CaseExpression';
|
|
374
|
+
/** WHEN-THEN conditions */
|
|
375
|
+
conditions: {
|
|
376
|
+
when: ExpressionNode;
|
|
377
|
+
then: OperandNode;
|
|
378
|
+
}[];
|
|
379
|
+
/** Optional ELSE clause */
|
|
380
|
+
else?: OperandNode;
|
|
381
|
+
/** Optional alias for the result */
|
|
382
|
+
alias?: string;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* AST node representing a window function
|
|
386
|
+
*/
|
|
387
|
+
interface WindowFunctionNode {
|
|
388
|
+
type: 'WindowFunction';
|
|
389
|
+
/** Window function name (e.g., ROW_NUMBER, RANK) */
|
|
390
|
+
name: string;
|
|
391
|
+
/** Function arguments */
|
|
392
|
+
args: (ColumnNode | LiteralNode | JsonPathNode)[];
|
|
393
|
+
/** Optional PARTITION BY clause */
|
|
394
|
+
partitionBy?: ColumnNode[];
|
|
395
|
+
/** Optional ORDER BY clause */
|
|
396
|
+
orderBy?: OrderByNode[];
|
|
397
|
+
/** Optional alias for the result */
|
|
398
|
+
alias?: string;
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Union type representing any operand that can be used in expressions
|
|
402
|
+
*/
|
|
403
|
+
type OperandNode = ColumnNode | LiteralNode | FunctionNode | JsonPathNode | ScalarSubqueryNode | CaseExpressionNode | WindowFunctionNode;
|
|
404
|
+
declare const isOperandNode: (node: any) => node is OperandNode;
|
|
405
|
+
declare const isFunctionNode: (node: any) => node is FunctionNode;
|
|
406
|
+
declare const isCaseExpressionNode: (node: any) => node is CaseExpressionNode;
|
|
407
|
+
declare const isWindowFunctionNode: (node: any) => node is WindowFunctionNode;
|
|
408
|
+
declare const isExpressionSelectionNode: (node: ColumnDef | FunctionNode | CaseExpressionNode | WindowFunctionNode) => node is FunctionNode | CaseExpressionNode | WindowFunctionNode;
|
|
409
|
+
/**
|
|
410
|
+
* AST node representing a binary expression (e.g., column = value)
|
|
411
|
+
*/
|
|
412
|
+
interface BinaryExpressionNode {
|
|
413
|
+
type: 'BinaryExpression';
|
|
414
|
+
/** Left operand */
|
|
415
|
+
left: OperandNode;
|
|
416
|
+
/** Comparison operator */
|
|
417
|
+
operator: SqlOperator;
|
|
418
|
+
/** Right operand */
|
|
419
|
+
right: OperandNode;
|
|
420
|
+
/** Optional escape character for LIKE expressions */
|
|
421
|
+
escape?: LiteralNode;
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* AST node representing a logical expression (AND/OR)
|
|
425
|
+
*/
|
|
426
|
+
interface LogicalExpressionNode {
|
|
427
|
+
type: 'LogicalExpression';
|
|
428
|
+
/** Logical operator (AND or OR) */
|
|
429
|
+
operator: 'AND' | 'OR';
|
|
430
|
+
/** Operands to combine */
|
|
431
|
+
operands: ExpressionNode[];
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* AST node representing a null check expression
|
|
435
|
+
*/
|
|
436
|
+
interface NullExpressionNode {
|
|
437
|
+
type: 'NullExpression';
|
|
438
|
+
/** Operand to check for null */
|
|
439
|
+
left: OperandNode;
|
|
440
|
+
/** Null check operator */
|
|
441
|
+
operator: 'IS NULL' | 'IS NOT NULL';
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* AST node representing an IN/NOT IN expression
|
|
445
|
+
*/
|
|
446
|
+
interface InExpressionNode {
|
|
447
|
+
type: 'InExpression';
|
|
448
|
+
/** Left operand to check */
|
|
449
|
+
left: OperandNode;
|
|
450
|
+
/** IN/NOT IN operator */
|
|
451
|
+
operator: 'IN' | 'NOT IN';
|
|
452
|
+
/** Values to check against */
|
|
453
|
+
right: OperandNode[];
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* AST node representing an EXISTS/NOT EXISTS expression
|
|
457
|
+
*/
|
|
458
|
+
interface ExistsExpressionNode {
|
|
459
|
+
type: 'ExistsExpression';
|
|
460
|
+
/** EXISTS/NOT EXISTS operator */
|
|
461
|
+
operator: SqlOperator;
|
|
462
|
+
/** Subquery to check */
|
|
463
|
+
subquery: SelectQueryNode;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* AST node representing a BETWEEN/NOT BETWEEN expression
|
|
467
|
+
*/
|
|
468
|
+
interface BetweenExpressionNode {
|
|
469
|
+
type: 'BetweenExpression';
|
|
470
|
+
/** Operand to check */
|
|
471
|
+
left: OperandNode;
|
|
472
|
+
/** BETWEEN/NOT BETWEEN operator */
|
|
473
|
+
operator: 'BETWEEN' | 'NOT BETWEEN';
|
|
474
|
+
/** Lower bound */
|
|
475
|
+
lower: OperandNode;
|
|
476
|
+
/** Upper bound */
|
|
477
|
+
upper: OperandNode;
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Union type representing any supported expression node
|
|
481
|
+
*/
|
|
482
|
+
type ExpressionNode = BinaryExpressionNode | LogicalExpressionNode | NullExpressionNode | InExpressionNode | ExistsExpressionNode | BetweenExpressionNode;
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* AST node representing a JOIN clause
|
|
486
|
+
*/
|
|
487
|
+
interface JoinNode {
|
|
488
|
+
type: 'Join';
|
|
489
|
+
/** Type of join (INNER, LEFT, RIGHT, etc.) */
|
|
490
|
+
kind: JoinKind;
|
|
491
|
+
/** Table to join */
|
|
492
|
+
table: TableNode;
|
|
493
|
+
/** Join condition expression */
|
|
494
|
+
condition: ExpressionNode;
|
|
495
|
+
/** Optional relation name for code generation */
|
|
496
|
+
relationName?: string;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* AST node representing a table reference in a query
|
|
501
|
+
*/
|
|
502
|
+
interface TableNode {
|
|
503
|
+
type: 'Table';
|
|
504
|
+
/** Table name */
|
|
505
|
+
name: string;
|
|
506
|
+
/** Optional schema name */
|
|
507
|
+
schema?: string;
|
|
508
|
+
/** Optional table alias */
|
|
509
|
+
alias?: string;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* AST node representing an ORDER BY clause
|
|
513
|
+
*/
|
|
514
|
+
interface OrderByNode {
|
|
515
|
+
type: 'OrderBy';
|
|
516
|
+
/** Column to order by */
|
|
517
|
+
column: ColumnNode;
|
|
518
|
+
/** Order direction (ASC or DESC) */
|
|
519
|
+
direction: OrderDirection;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Plan describing pivot columns needed for hydration
|
|
523
|
+
*/
|
|
524
|
+
interface HydrationPivotPlan {
|
|
525
|
+
table: string;
|
|
526
|
+
primaryKey: string;
|
|
527
|
+
aliasPrefix: string;
|
|
528
|
+
columns: string[];
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Plan for hydrating relationship data
|
|
532
|
+
*/
|
|
533
|
+
interface HydrationRelationPlan {
|
|
534
|
+
/** Name of the relationship */
|
|
535
|
+
name: string;
|
|
536
|
+
/** Alias prefix for the relationship */
|
|
537
|
+
aliasPrefix: string;
|
|
538
|
+
/** Type of relationship */
|
|
539
|
+
type: RelationType;
|
|
540
|
+
/** Target table name */
|
|
541
|
+
targetTable: string;
|
|
542
|
+
/** Target table primary key */
|
|
543
|
+
targetPrimaryKey: string;
|
|
544
|
+
/** Foreign key column */
|
|
545
|
+
foreignKey: string;
|
|
546
|
+
/** Local key column */
|
|
547
|
+
localKey: string;
|
|
548
|
+
/** Columns to include */
|
|
549
|
+
columns: string[];
|
|
550
|
+
/** Optional pivot plan for many-to-many relationships */
|
|
551
|
+
pivot?: HydrationPivotPlan;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Complete hydration plan for a query
|
|
555
|
+
*/
|
|
556
|
+
interface HydrationPlan {
|
|
557
|
+
/** Root table name */
|
|
558
|
+
rootTable: string;
|
|
559
|
+
/** Root table primary key */
|
|
560
|
+
rootPrimaryKey: string;
|
|
561
|
+
/** Root table columns */
|
|
562
|
+
rootColumns: string[];
|
|
563
|
+
/** Relationship hydration plans */
|
|
564
|
+
relations: HydrationRelationPlan[];
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Query metadata including hydration information
|
|
568
|
+
*/
|
|
569
|
+
interface QueryMetadata {
|
|
570
|
+
/** Optional hydration plan */
|
|
571
|
+
hydration?: HydrationPlan;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* AST node representing a Common Table Expression (CTE)
|
|
575
|
+
*/
|
|
576
|
+
interface CommonTableExpressionNode {
|
|
577
|
+
type: 'CommonTableExpression';
|
|
578
|
+
/** CTE name */
|
|
579
|
+
name: string;
|
|
580
|
+
/** Optional column names */
|
|
581
|
+
columns?: string[];
|
|
582
|
+
/** CTE query */
|
|
583
|
+
query: SelectQueryNode;
|
|
584
|
+
/** Whether the CTE is recursive */
|
|
585
|
+
recursive: boolean;
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* AST node representing a complete SELECT query
|
|
589
|
+
*/
|
|
590
|
+
interface SelectQueryNode {
|
|
591
|
+
type: 'SelectQuery';
|
|
592
|
+
/** Optional CTEs (WITH clauses) */
|
|
593
|
+
ctes?: CommonTableExpressionNode[];
|
|
594
|
+
/** FROM clause table */
|
|
595
|
+
from: TableNode;
|
|
596
|
+
/** SELECT clause columns */
|
|
597
|
+
columns: (ColumnNode | FunctionNode | ScalarSubqueryNode | CaseExpressionNode | WindowFunctionNode)[];
|
|
598
|
+
/** JOIN clauses */
|
|
599
|
+
joins: JoinNode[];
|
|
600
|
+
/** Optional WHERE clause */
|
|
601
|
+
where?: ExpressionNode;
|
|
602
|
+
/** Optional GROUP BY clause */
|
|
603
|
+
groupBy?: ColumnNode[];
|
|
604
|
+
/** Optional HAVING clause */
|
|
605
|
+
having?: ExpressionNode;
|
|
606
|
+
/** Optional ORDER BY clause */
|
|
607
|
+
orderBy?: OrderByNode[];
|
|
608
|
+
/** Optional LIMIT clause */
|
|
609
|
+
limit?: number;
|
|
610
|
+
/** Optional OFFSET clause */
|
|
611
|
+
offset?: number;
|
|
612
|
+
/** Optional query metadata */
|
|
613
|
+
meta?: QueryMetadata;
|
|
614
|
+
/** Optional DISTINCT clause */
|
|
615
|
+
distinct?: ColumnNode[];
|
|
616
|
+
}
|
|
617
|
+
interface InsertQueryNode {
|
|
618
|
+
type: 'InsertQuery';
|
|
619
|
+
/** Target table */
|
|
620
|
+
into: TableNode;
|
|
621
|
+
/** Column order for inserted values */
|
|
622
|
+
columns: ColumnNode[];
|
|
623
|
+
/** Rows of values to insert */
|
|
624
|
+
values: OperandNode[][];
|
|
625
|
+
/** Optional RETURNING clause */
|
|
626
|
+
returning?: ColumnNode[];
|
|
627
|
+
}
|
|
628
|
+
interface UpdateAssignmentNode {
|
|
629
|
+
/** Column to update */
|
|
630
|
+
column: ColumnNode;
|
|
631
|
+
/** Value to set */
|
|
632
|
+
value: OperandNode;
|
|
633
|
+
}
|
|
634
|
+
interface UpdateQueryNode {
|
|
635
|
+
type: 'UpdateQuery';
|
|
636
|
+
/** Table being updated */
|
|
637
|
+
table: TableNode;
|
|
638
|
+
/** Assignments for SET clause */
|
|
639
|
+
set: UpdateAssignmentNode[];
|
|
640
|
+
/** Optional WHERE clause */
|
|
641
|
+
where?: ExpressionNode;
|
|
642
|
+
/** Optional RETURNING clause */
|
|
643
|
+
returning?: ColumnNode[];
|
|
644
|
+
}
|
|
645
|
+
interface DeleteQueryNode {
|
|
646
|
+
type: 'DeleteQuery';
|
|
647
|
+
/** Table to delete from */
|
|
648
|
+
from: TableNode;
|
|
649
|
+
/** Optional WHERE clause */
|
|
650
|
+
where?: ExpressionNode;
|
|
651
|
+
/** Optional RETURNING clause */
|
|
652
|
+
returning?: ColumnNode[];
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Context for SQL compilation with parameter management
|
|
657
|
+
*/
|
|
658
|
+
interface CompilerContext {
|
|
659
|
+
/** Array of parameters */
|
|
660
|
+
params: unknown[];
|
|
661
|
+
/** Function to add a parameter and get its placeholder */
|
|
662
|
+
addParameter(value: unknown): string;
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Result of SQL compilation
|
|
666
|
+
*/
|
|
667
|
+
interface CompiledQuery {
|
|
668
|
+
/** Generated SQL string */
|
|
669
|
+
sql: string;
|
|
670
|
+
/** Parameters for the query */
|
|
671
|
+
params: unknown[];
|
|
672
|
+
}
|
|
673
|
+
interface SelectCompiler {
|
|
674
|
+
compileSelect(ast: SelectQueryNode): CompiledQuery;
|
|
675
|
+
}
|
|
676
|
+
interface InsertCompiler {
|
|
677
|
+
compileInsert(ast: InsertQueryNode): CompiledQuery;
|
|
678
|
+
}
|
|
679
|
+
interface UpdateCompiler {
|
|
680
|
+
compileUpdate(ast: UpdateQueryNode): CompiledQuery;
|
|
681
|
+
}
|
|
682
|
+
interface DeleteCompiler {
|
|
683
|
+
compileDelete(ast: DeleteQueryNode): CompiledQuery;
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Abstract base class for SQL dialect implementations
|
|
687
|
+
*/
|
|
688
|
+
declare abstract class Dialect implements SelectCompiler, InsertCompiler, UpdateCompiler, DeleteCompiler {
|
|
689
|
+
/**
|
|
690
|
+
* Compiles a SELECT query AST to SQL
|
|
691
|
+
* @param ast - Query AST to compile
|
|
692
|
+
* @returns Compiled query with SQL and parameters
|
|
693
|
+
*/
|
|
694
|
+
compileSelect(ast: SelectQueryNode): CompiledQuery;
|
|
695
|
+
compileInsert(ast: InsertQueryNode): CompiledQuery;
|
|
696
|
+
compileUpdate(ast: UpdateQueryNode): CompiledQuery;
|
|
697
|
+
compileDelete(ast: DeleteQueryNode): CompiledQuery;
|
|
698
|
+
/**
|
|
699
|
+
* Compiles SELECT query AST to SQL (to be implemented by concrete dialects)
|
|
700
|
+
* @param ast - Query AST
|
|
701
|
+
* @param ctx - Compiler context
|
|
702
|
+
* @returns SQL string
|
|
703
|
+
*/
|
|
704
|
+
protected abstract compileSelectAst(ast: SelectQueryNode, ctx: CompilerContext): string;
|
|
705
|
+
protected abstract compileInsertAst(ast: InsertQueryNode, ctx: CompilerContext): string;
|
|
706
|
+
protected abstract compileUpdateAst(ast: UpdateQueryNode, ctx: CompilerContext): string;
|
|
707
|
+
protected abstract compileDeleteAst(ast: DeleteQueryNode, ctx: CompilerContext): string;
|
|
708
|
+
/**
|
|
709
|
+
* Quotes an SQL identifier (to be implemented by concrete dialects)
|
|
710
|
+
* @param id - Identifier to quote
|
|
711
|
+
* @returns Quoted identifier
|
|
712
|
+
*/
|
|
713
|
+
abstract quoteIdentifier(id: string): string;
|
|
714
|
+
/**
|
|
715
|
+
* Compiles a WHERE clause
|
|
716
|
+
* @param where - WHERE expression
|
|
717
|
+
* @param ctx - Compiler context
|
|
718
|
+
* @returns SQL WHERE clause or empty string
|
|
719
|
+
*/
|
|
720
|
+
protected compileWhere(where: ExpressionNode | undefined, ctx: CompilerContext): string;
|
|
721
|
+
protected compileReturning(returning: ColumnNode[] | undefined, ctx: CompilerContext): string;
|
|
722
|
+
/**
|
|
723
|
+
* Generates subquery for EXISTS expressions
|
|
724
|
+
* Rule: Always forces SELECT 1, ignoring column list
|
|
725
|
+
* Maintains FROM, JOINs, WHERE, GROUP BY, ORDER BY, LIMIT/OFFSET
|
|
726
|
+
* Does not add ';' at the end
|
|
727
|
+
* @param ast - Query AST
|
|
728
|
+
* @param ctx - Compiler context
|
|
729
|
+
* @returns SQL for EXISTS subquery
|
|
730
|
+
*/
|
|
731
|
+
protected compileSelectForExists(ast: SelectQueryNode, ctx: CompilerContext): string;
|
|
732
|
+
/**
|
|
733
|
+
* Creates a new compiler context
|
|
734
|
+
* @returns Compiler context with parameter management
|
|
735
|
+
*/
|
|
736
|
+
protected createCompilerContext(): CompilerContext;
|
|
737
|
+
/**
|
|
738
|
+
* Formats a parameter placeholder
|
|
739
|
+
* @param index - Parameter index
|
|
740
|
+
* @returns Formatted placeholder string
|
|
741
|
+
*/
|
|
742
|
+
protected formatPlaceholder(index: number): string;
|
|
743
|
+
private readonly expressionCompilers;
|
|
744
|
+
private readonly operandCompilers;
|
|
745
|
+
protected constructor();
|
|
746
|
+
/**
|
|
747
|
+
* Registers an expression compiler for a specific node type
|
|
748
|
+
* @param type - Expression node type
|
|
749
|
+
* @param compiler - Compiler function
|
|
750
|
+
*/
|
|
751
|
+
protected registerExpressionCompiler<T extends ExpressionNode>(type: T['type'], compiler: (node: T, ctx: CompilerContext) => string): void;
|
|
752
|
+
/**
|
|
753
|
+
* Registers an operand compiler for a specific node type
|
|
754
|
+
* @param type - Operand node type
|
|
755
|
+
* @param compiler - Compiler function
|
|
756
|
+
*/
|
|
757
|
+
protected registerOperandCompiler<T extends OperandNode>(type: T['type'], compiler: (node: T, ctx: CompilerContext) => string): void;
|
|
758
|
+
/**
|
|
759
|
+
* Compiles an expression node
|
|
760
|
+
* @param node - Expression node to compile
|
|
761
|
+
* @param ctx - Compiler context
|
|
762
|
+
* @returns Compiled SQL expression
|
|
763
|
+
*/
|
|
764
|
+
protected compileExpression(node: ExpressionNode, ctx: CompilerContext): string;
|
|
765
|
+
/**
|
|
766
|
+
* Compiles an operand node
|
|
767
|
+
* @param node - Operand node to compile
|
|
768
|
+
* @param ctx - Compiler context
|
|
769
|
+
* @returns Compiled SQL operand
|
|
770
|
+
*/
|
|
771
|
+
protected compileOperand(node: OperandNode, ctx: CompilerContext): string;
|
|
772
|
+
private registerDefaultExpressionCompilers;
|
|
773
|
+
private registerDefaultOperandCompilers;
|
|
774
|
+
protected compileJsonPath(node: JsonPathNode): string;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* Node types that can be used in query projections
|
|
779
|
+
*/
|
|
780
|
+
type ProjectionNode = ColumnNode | FunctionNode | ScalarSubqueryNode | CaseExpressionNode | WindowFunctionNode;
|
|
781
|
+
/**
|
|
782
|
+
* Manages the state of a SELECT query being built
|
|
783
|
+
*/
|
|
784
|
+
declare class SelectQueryState {
|
|
785
|
+
/**
|
|
786
|
+
* Table definition for the query
|
|
787
|
+
*/
|
|
788
|
+
readonly table: TableDef;
|
|
789
|
+
/**
|
|
790
|
+
* Abstract Syntax Tree (AST) representation of the query
|
|
791
|
+
*/
|
|
792
|
+
readonly ast: SelectQueryNode;
|
|
793
|
+
/**
|
|
794
|
+
* Creates a new SelectQueryState instance
|
|
795
|
+
* @param table - Table definition
|
|
796
|
+
* @param ast - Optional existing AST
|
|
797
|
+
*/
|
|
798
|
+
constructor(table: TableDef, ast?: SelectQueryNode);
|
|
799
|
+
/**
|
|
800
|
+
* Creates a new SelectQueryState with updated AST
|
|
801
|
+
* @param nextAst - Updated AST
|
|
802
|
+
* @returns New SelectQueryState instance
|
|
803
|
+
*/
|
|
804
|
+
private clone;
|
|
805
|
+
/**
|
|
806
|
+
* Adds columns to the query
|
|
807
|
+
* @param newCols - Columns to add
|
|
808
|
+
* @returns New SelectQueryState with added columns
|
|
809
|
+
*/
|
|
810
|
+
withColumns(newCols: ProjectionNode[]): SelectQueryState;
|
|
811
|
+
/**
|
|
812
|
+
* Adds a join to the query
|
|
813
|
+
* @param join - Join node to add
|
|
814
|
+
* @returns New SelectQueryState with added join
|
|
815
|
+
*/
|
|
816
|
+
withJoin(join: JoinNode): SelectQueryState;
|
|
817
|
+
/**
|
|
818
|
+
* Adds a WHERE clause to the query
|
|
819
|
+
* @param predicate - WHERE predicate expression
|
|
820
|
+
* @returns New SelectQueryState with WHERE clause
|
|
821
|
+
*/
|
|
822
|
+
withWhere(predicate: ExpressionNode): SelectQueryState;
|
|
823
|
+
/**
|
|
824
|
+
* Adds a HAVING clause to the query
|
|
825
|
+
* @param predicate - HAVING predicate expression
|
|
826
|
+
* @returns New SelectQueryState with HAVING clause
|
|
827
|
+
*/
|
|
828
|
+
withHaving(predicate: ExpressionNode): SelectQueryState;
|
|
829
|
+
/**
|
|
830
|
+
* Adds GROUP BY columns to the query
|
|
831
|
+
* @param columns - Columns to group by
|
|
832
|
+
* @returns New SelectQueryState with GROUP BY clause
|
|
833
|
+
*/
|
|
834
|
+
withGroupBy(columns: ColumnNode[]): SelectQueryState;
|
|
835
|
+
/**
|
|
836
|
+
* Adds ORDER BY clauses to the query
|
|
837
|
+
* @param orderBy - ORDER BY nodes
|
|
838
|
+
* @returns New SelectQueryState with ORDER BY clause
|
|
839
|
+
*/
|
|
840
|
+
withOrderBy(orderBy: OrderByNode[]): SelectQueryState;
|
|
841
|
+
/**
|
|
842
|
+
* Adds DISTINCT columns to the query
|
|
843
|
+
* @param columns - Columns to make distinct
|
|
844
|
+
* @returns New SelectQueryState with DISTINCT clause
|
|
845
|
+
*/
|
|
846
|
+
withDistinct(columns: ColumnNode[]): SelectQueryState;
|
|
847
|
+
/**
|
|
848
|
+
* Adds a LIMIT clause to the query
|
|
849
|
+
* @param limit - Maximum number of rows to return
|
|
850
|
+
* @returns New SelectQueryState with LIMIT clause
|
|
851
|
+
*/
|
|
852
|
+
withLimit(limit: number): SelectQueryState;
|
|
853
|
+
/**
|
|
854
|
+
* Adds an OFFSET clause to the query
|
|
855
|
+
* @param offset - Number of rows to skip
|
|
856
|
+
* @returns New SelectQueryState with OFFSET clause
|
|
857
|
+
*/
|
|
858
|
+
withOffset(offset: number): SelectQueryState;
|
|
859
|
+
/**
|
|
860
|
+
* Adds a Common Table Expression (CTE) to the query
|
|
861
|
+
* @param cte - CTE node to add
|
|
862
|
+
* @returns New SelectQueryState with CTE
|
|
863
|
+
*/
|
|
864
|
+
withCte(cte: CommonTableExpressionNode): SelectQueryState;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
/**
|
|
868
|
+
* Manages hydration planning for query results
|
|
869
|
+
*/
|
|
870
|
+
declare class HydrationPlanner {
|
|
871
|
+
private readonly table;
|
|
872
|
+
private readonly plan?;
|
|
873
|
+
/**
|
|
874
|
+
* Creates a new HydrationPlanner instance
|
|
875
|
+
* @param table - Table definition
|
|
876
|
+
* @param plan - Optional existing hydration plan
|
|
877
|
+
*/
|
|
878
|
+
constructor(table: TableDef, plan?: HydrationPlan);
|
|
879
|
+
/**
|
|
880
|
+
* Captures root table columns for hydration planning
|
|
881
|
+
* @param columns - Columns to capture
|
|
882
|
+
* @returns Updated HydrationPlanner with captured columns
|
|
883
|
+
*/
|
|
884
|
+
captureRootColumns(columns: ProjectionNode[]): HydrationPlanner;
|
|
885
|
+
/**
|
|
886
|
+
* Includes a relation in the hydration plan
|
|
887
|
+
* @param rel - Relation definition
|
|
888
|
+
* @param relationName - Name of the relation
|
|
889
|
+
* @param aliasPrefix - Alias prefix for relation columns
|
|
890
|
+
* @param columns - Columns to include from the relation
|
|
891
|
+
* @returns Updated HydrationPlanner with included relation
|
|
892
|
+
*/
|
|
893
|
+
includeRelation(rel: RelationDef, relationName: string, aliasPrefix: string, columns: string[], pivot?: {
|
|
894
|
+
aliasPrefix: string;
|
|
895
|
+
columns: string[];
|
|
896
|
+
}): HydrationPlanner;
|
|
897
|
+
/**
|
|
898
|
+
* Gets the current hydration plan
|
|
899
|
+
* @returns Current hydration plan or undefined
|
|
900
|
+
*/
|
|
901
|
+
getPlan(): HydrationPlan | undefined;
|
|
902
|
+
/**
|
|
903
|
+
* Gets the current hydration plan or creates a default one
|
|
904
|
+
* @returns Current hydration plan or default plan
|
|
905
|
+
*/
|
|
906
|
+
private getPlanOrDefault;
|
|
907
|
+
/**
|
|
908
|
+
* Builds a relation plan for hydration
|
|
909
|
+
* @param rel - Relation definition
|
|
910
|
+
* @param relationName - Name of the relation
|
|
911
|
+
* @param aliasPrefix - Alias prefix for relation columns
|
|
912
|
+
* @param columns - Columns to include from the relation
|
|
913
|
+
* @returns Hydration relation plan
|
|
914
|
+
*/
|
|
915
|
+
private buildRelationPlan;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* Manages hydration planning for query results
|
|
920
|
+
*/
|
|
921
|
+
declare class HydrationManager {
|
|
922
|
+
private readonly table;
|
|
923
|
+
private readonly planner;
|
|
924
|
+
/**
|
|
925
|
+
* Creates a new HydrationManager instance
|
|
926
|
+
* @param table - Table definition
|
|
927
|
+
* @param planner - Hydration planner
|
|
928
|
+
*/
|
|
929
|
+
constructor(table: TableDef, planner: HydrationPlanner);
|
|
930
|
+
/**
|
|
931
|
+
* Creates a new HydrationManager with updated planner
|
|
932
|
+
* @param nextPlanner - Updated hydration planner
|
|
933
|
+
* @returns New HydrationManager instance
|
|
934
|
+
*/
|
|
935
|
+
private clone;
|
|
936
|
+
/**
|
|
937
|
+
* Handles column selection for hydration planning
|
|
938
|
+
* @param state - Current query state
|
|
939
|
+
* @param newColumns - Newly selected columns
|
|
940
|
+
* @returns Updated HydrationManager with captured columns
|
|
941
|
+
*/
|
|
942
|
+
onColumnsSelected(state: SelectQueryState, newColumns: ProjectionNode[]): HydrationManager;
|
|
943
|
+
/**
|
|
944
|
+
* Handles relation inclusion for hydration planning
|
|
945
|
+
* @param state - Current query state
|
|
946
|
+
* @param relation - Relation definition
|
|
947
|
+
* @param relationName - Name of the relation
|
|
948
|
+
* @param aliasPrefix - Alias prefix for the relation
|
|
949
|
+
* @param targetColumns - Target columns to include
|
|
950
|
+
* @returns Updated HydrationManager with included relation
|
|
951
|
+
*/
|
|
952
|
+
onRelationIncluded(state: SelectQueryState, relation: RelationDef, relationName: string, aliasPrefix: string, targetColumns: string[], pivot?: {
|
|
953
|
+
aliasPrefix: string;
|
|
954
|
+
columns: string[];
|
|
955
|
+
}): HydrationManager;
|
|
956
|
+
/**
|
|
957
|
+
* Applies hydration plan to the AST
|
|
958
|
+
* @param ast - Query AST to modify
|
|
959
|
+
* @returns AST with hydration metadata
|
|
960
|
+
*/
|
|
961
|
+
applyToAst(ast: SelectQueryNode): SelectQueryNode;
|
|
962
|
+
/**
|
|
963
|
+
* Gets the current hydration plan
|
|
964
|
+
* @returns Hydration plan or undefined if none exists
|
|
965
|
+
*/
|
|
966
|
+
getPlan(): HydrationPlan | undefined;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* Result of column selection operation
|
|
971
|
+
*/
|
|
972
|
+
interface ColumnSelectionResult {
|
|
973
|
+
/**
|
|
974
|
+
* Updated query state
|
|
975
|
+
*/
|
|
976
|
+
state: SelectQueryState;
|
|
977
|
+
/**
|
|
978
|
+
* Columns that were added
|
|
979
|
+
*/
|
|
980
|
+
addedColumns: ProjectionNode[];
|
|
981
|
+
}
|
|
982
|
+
/**
|
|
983
|
+
* Service for manipulating query AST (Abstract Syntax Tree)
|
|
984
|
+
*/
|
|
985
|
+
declare class QueryAstService {
|
|
986
|
+
private readonly table;
|
|
987
|
+
private readonly state;
|
|
988
|
+
/**
|
|
989
|
+
* Creates a new QueryAstService instance
|
|
990
|
+
* @param table - Table definition
|
|
991
|
+
* @param state - Current query state
|
|
992
|
+
*/
|
|
993
|
+
constructor(table: TableDef, state: SelectQueryState);
|
|
994
|
+
/**
|
|
995
|
+
* Selects columns for the query
|
|
996
|
+
* @param columns - Columns to select (key: alias, value: column definition or expression)
|
|
997
|
+
* @returns Column selection result with updated state and added columns
|
|
998
|
+
*/
|
|
999
|
+
select(columns: Record<string, ColumnDef | FunctionNode | CaseExpressionNode | WindowFunctionNode>): ColumnSelectionResult;
|
|
1000
|
+
/**
|
|
1001
|
+
* Selects raw column expressions (best-effort parser for simple references/functions)
|
|
1002
|
+
* @param cols - Raw column expressions
|
|
1003
|
+
* @returns Column selection result with updated state and added columns
|
|
1004
|
+
*/
|
|
1005
|
+
selectRaw(cols: string[]): ColumnSelectionResult;
|
|
1006
|
+
/**
|
|
1007
|
+
* Adds a Common Table Expression (CTE) to the query
|
|
1008
|
+
* @param name - Name of the CTE
|
|
1009
|
+
* @param query - Query for the CTE
|
|
1010
|
+
* @param columns - Optional column names for the CTE
|
|
1011
|
+
* @param recursive - Whether the CTE is recursive
|
|
1012
|
+
* @returns Updated query state with CTE
|
|
1013
|
+
*/
|
|
1014
|
+
withCte(name: string, query: SelectQueryNode, columns?: string[], recursive?: boolean): SelectQueryState;
|
|
1015
|
+
/**
|
|
1016
|
+
* Selects a subquery as a column
|
|
1017
|
+
* @param alias - Alias for the subquery
|
|
1018
|
+
* @param query - Subquery to select
|
|
1019
|
+
* @returns Updated query state with subquery selection
|
|
1020
|
+
*/
|
|
1021
|
+
selectSubquery(alias: string, query: SelectQueryNode): SelectQueryState;
|
|
1022
|
+
/**
|
|
1023
|
+
* Adds a JOIN clause to the query
|
|
1024
|
+
* @param join - Join node to add
|
|
1025
|
+
* @returns Updated query state with JOIN
|
|
1026
|
+
*/
|
|
1027
|
+
withJoin(join: JoinNode): SelectQueryState;
|
|
1028
|
+
/**
|
|
1029
|
+
* Adds a WHERE clause to the query
|
|
1030
|
+
* @param expr - Expression for the WHERE clause
|
|
1031
|
+
* @returns Updated query state with WHERE clause
|
|
1032
|
+
*/
|
|
1033
|
+
withWhere(expr: ExpressionNode): SelectQueryState;
|
|
1034
|
+
/**
|
|
1035
|
+
* Adds a GROUP BY clause to the query
|
|
1036
|
+
* @param col - Column to group by
|
|
1037
|
+
* @returns Updated query state with GROUP BY clause
|
|
1038
|
+
*/
|
|
1039
|
+
withGroupBy(col: ColumnDef | ColumnNode): SelectQueryState;
|
|
1040
|
+
/**
|
|
1041
|
+
* Adds a HAVING clause to the query
|
|
1042
|
+
* @param expr - Expression for the HAVING clause
|
|
1043
|
+
* @returns Updated query state with HAVING clause
|
|
1044
|
+
*/
|
|
1045
|
+
withHaving(expr: ExpressionNode): SelectQueryState;
|
|
1046
|
+
/**
|
|
1047
|
+
* Adds an ORDER BY clause to the query
|
|
1048
|
+
* @param col - Column to order by
|
|
1049
|
+
* @param direction - Order direction (ASC/DESC)
|
|
1050
|
+
* @returns Updated query state with ORDER BY clause
|
|
1051
|
+
*/
|
|
1052
|
+
withOrderBy(col: ColumnDef | ColumnNode, direction: OrderDirection): SelectQueryState;
|
|
1053
|
+
/**
|
|
1054
|
+
* Adds a DISTINCT clause to the query
|
|
1055
|
+
* @param cols - Columns to make distinct
|
|
1056
|
+
* @returns Updated query state with DISTINCT clause
|
|
1057
|
+
*/
|
|
1058
|
+
withDistinct(cols: ColumnNode[]): SelectQueryState;
|
|
1059
|
+
/**
|
|
1060
|
+
* Adds a LIMIT clause to the query
|
|
1061
|
+
* @param limit - Maximum number of rows to return
|
|
1062
|
+
* @returns Updated query state with LIMIT clause
|
|
1063
|
+
*/
|
|
1064
|
+
withLimit(limit: number): SelectQueryState;
|
|
1065
|
+
/**
|
|
1066
|
+
* Adds an OFFSET clause to the query
|
|
1067
|
+
* @param offset - Number of rows to skip
|
|
1068
|
+
* @returns Updated query state with OFFSET clause
|
|
1069
|
+
*/
|
|
1070
|
+
withOffset(offset: number): SelectQueryState;
|
|
1071
|
+
/**
|
|
1072
|
+
* Combines expressions with AND operator
|
|
1073
|
+
* @param existing - Existing expression
|
|
1074
|
+
* @param next - New expression to combine
|
|
1075
|
+
* @returns Combined expression
|
|
1076
|
+
*/
|
|
1077
|
+
private combineExpressions;
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
/**
|
|
1081
|
+
* Result of a relation operation
|
|
1082
|
+
*/
|
|
1083
|
+
interface RelationResult {
|
|
1084
|
+
/**
|
|
1085
|
+
* Updated query state
|
|
1086
|
+
*/
|
|
1087
|
+
state: SelectQueryState;
|
|
1088
|
+
/**
|
|
1089
|
+
* Updated hydration manager
|
|
1090
|
+
*/
|
|
1091
|
+
hydration: HydrationManager;
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
/**
|
|
1095
|
+
* Join kinds allowed when including a relation using `.include(...)`.
|
|
1096
|
+
*/
|
|
1097
|
+
type RelationIncludeJoinKind = typeof JOIN_KINDS.LEFT | typeof JOIN_KINDS.INNER;
|
|
1098
|
+
/**
|
|
1099
|
+
* Options for including a relation in a query
|
|
1100
|
+
*/
|
|
1101
|
+
interface RelationIncludeOptions {
|
|
1102
|
+
columns?: string[];
|
|
1103
|
+
aliasPrefix?: string;
|
|
1104
|
+
filter?: ExpressionNode;
|
|
1105
|
+
joinKind?: RelationIncludeJoinKind;
|
|
1106
|
+
pivot?: {
|
|
1107
|
+
columns?: string[];
|
|
1108
|
+
aliasPrefix?: string;
|
|
1109
|
+
};
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
/**
|
|
1113
|
+
* Service for handling relation operations (joins, includes, etc.)
|
|
1114
|
+
*/
|
|
1115
|
+
declare class RelationService {
|
|
1116
|
+
private readonly table;
|
|
1117
|
+
private readonly state;
|
|
1118
|
+
private readonly hydration;
|
|
1119
|
+
private readonly createQueryAstService;
|
|
1120
|
+
private readonly projectionHelper;
|
|
1121
|
+
/**
|
|
1122
|
+
* Creates a new RelationService instance
|
|
1123
|
+
* @param table - Table definition
|
|
1124
|
+
* @param state - Current query state
|
|
1125
|
+
* @param hydration - Hydration manager
|
|
1126
|
+
*/
|
|
1127
|
+
constructor(table: TableDef, state: SelectQueryState, hydration: HydrationManager, createQueryAstService: (table: TableDef, state: SelectQueryState) => QueryAstService);
|
|
1128
|
+
/**
|
|
1129
|
+
* Joins a relation to the query
|
|
1130
|
+
* @param relationName - Name of the relation to join
|
|
1131
|
+
* @param joinKind - Type of join to use
|
|
1132
|
+
* @param extraCondition - Additional join condition
|
|
1133
|
+
* @returns Relation result with updated state and hydration
|
|
1134
|
+
*/
|
|
1135
|
+
joinRelation(relationName: string, joinKind: JoinKind, extraCondition?: ExpressionNode): RelationResult;
|
|
1136
|
+
/**
|
|
1137
|
+
* Matches records based on a relation with an optional predicate
|
|
1138
|
+
* @param relationName - Name of the relation to match
|
|
1139
|
+
* @param predicate - Optional predicate expression
|
|
1140
|
+
* @returns Relation result with updated state and hydration
|
|
1141
|
+
*/
|
|
1142
|
+
match(relationName: string, predicate?: ExpressionNode): RelationResult;
|
|
1143
|
+
/**
|
|
1144
|
+
* Includes a relation in the query result
|
|
1145
|
+
* @param relationName - Name of the relation to include
|
|
1146
|
+
* @param options - Options for relation inclusion
|
|
1147
|
+
* @returns Relation result with updated state and hydration
|
|
1148
|
+
*/
|
|
1149
|
+
include(relationName: string, options?: RelationIncludeOptions): RelationResult;
|
|
1150
|
+
/**
|
|
1151
|
+
* Applies relation correlation to a query AST
|
|
1152
|
+
* @param relationName - Name of the relation
|
|
1153
|
+
* @param ast - Query AST to modify
|
|
1154
|
+
* @returns Modified query AST with relation correlation
|
|
1155
|
+
*/
|
|
1156
|
+
applyRelationCorrelation(relationName: string, ast: SelectQueryNode): SelectQueryNode;
|
|
1157
|
+
/**
|
|
1158
|
+
* Creates a join node for a relation
|
|
1159
|
+
* @param state - Current query state
|
|
1160
|
+
* @param relationName - Name of the relation
|
|
1161
|
+
* @param joinKind - Type of join to use
|
|
1162
|
+
* @param extraCondition - Additional join condition
|
|
1163
|
+
* @returns Updated query state with join
|
|
1164
|
+
*/
|
|
1165
|
+
private withJoin;
|
|
1166
|
+
/**
|
|
1167
|
+
* Selects columns for a relation
|
|
1168
|
+
* @param state - Current query state
|
|
1169
|
+
* @param hydration - Hydration manager
|
|
1170
|
+
* @param columns - Columns to select
|
|
1171
|
+
* @returns Relation result with updated state and hydration
|
|
1172
|
+
*/
|
|
1173
|
+
private selectColumns;
|
|
1174
|
+
/**
|
|
1175
|
+
* Gets a relation definition by name
|
|
1176
|
+
* @param relationName - Name of the relation
|
|
1177
|
+
* @returns Relation definition
|
|
1178
|
+
* @throws Error if relation is not found
|
|
1179
|
+
*/
|
|
1180
|
+
private getRelation;
|
|
1181
|
+
/**
|
|
1182
|
+
* Creates a QueryAstService instance
|
|
1183
|
+
* @param state - Current query state
|
|
1184
|
+
* @returns QueryAstService instance
|
|
1185
|
+
*/
|
|
1186
|
+
private astService;
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
/**
|
|
1190
|
+
* Dependencies for query builder operations
|
|
1191
|
+
*/
|
|
1192
|
+
interface SelectQueryBuilderDependencies {
|
|
1193
|
+
/**
|
|
1194
|
+
* Creates a new query state
|
|
1195
|
+
* @param table - Table definition
|
|
1196
|
+
* @returns New query state
|
|
1197
|
+
*/
|
|
1198
|
+
createState: (table: TableDef) => SelectQueryState;
|
|
1199
|
+
/**
|
|
1200
|
+
* Creates a new hydration manager
|
|
1201
|
+
* @param table - Table definition
|
|
1202
|
+
* @returns New hydration manager
|
|
1203
|
+
*/
|
|
1204
|
+
createHydration: (table: TableDef) => HydrationManager;
|
|
1205
|
+
/**
|
|
1206
|
+
* Creates a new hydration planner
|
|
1207
|
+
* @param table - Table definition
|
|
1208
|
+
* @returns Hydration planner
|
|
1209
|
+
*/
|
|
1210
|
+
createHydrationPlanner: (table: TableDef) => HydrationPlanner;
|
|
1211
|
+
/**
|
|
1212
|
+
* Creates a new query AST service
|
|
1213
|
+
* @param table - Table definition
|
|
1214
|
+
* @param state - Query state
|
|
1215
|
+
* @returns New query AST service
|
|
1216
|
+
*/
|
|
1217
|
+
createQueryAstService: (table: TableDef, state: SelectQueryState) => QueryAstService;
|
|
1218
|
+
/**
|
|
1219
|
+
* Creates a new relation service
|
|
1220
|
+
* @param table - Table definition
|
|
1221
|
+
* @param state - Query state
|
|
1222
|
+
* @param hydration - Hydration manager
|
|
1223
|
+
* @returns New relation service
|
|
1224
|
+
*/
|
|
1225
|
+
createRelationService: (table: TableDef, state: SelectQueryState, hydration: HydrationManager) => RelationService;
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
type QueryResult = {
|
|
1229
|
+
columns: string[];
|
|
1230
|
+
values: unknown[][];
|
|
1231
|
+
};
|
|
1232
|
+
interface DbExecutor {
|
|
1233
|
+
executeSql(sql: string, params?: unknown[]): Promise<QueryResult[]>;
|
|
1234
|
+
beginTransaction?(): Promise<void>;
|
|
1235
|
+
commitTransaction?(): Promise<void>;
|
|
1236
|
+
rollbackTransaction?(): Promise<void>;
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
declare enum EntityStatus {
|
|
1240
|
+
New = "new",
|
|
1241
|
+
Managed = "managed",
|
|
1242
|
+
Dirty = "dirty",
|
|
1243
|
+
Removed = "removed",
|
|
1244
|
+
Detached = "detached"
|
|
1245
|
+
}
|
|
1246
|
+
interface TrackedEntity {
|
|
1247
|
+
table: TableDef;
|
|
1248
|
+
entity: any;
|
|
1249
|
+
pk: string | number | null;
|
|
1250
|
+
status: EntityStatus;
|
|
1251
|
+
original: Record<string, any> | null;
|
|
1252
|
+
}
|
|
1253
|
+
type RelationKey = string;
|
|
1254
|
+
type RelationChange<T> = {
|
|
1255
|
+
kind: 'add';
|
|
1256
|
+
entity: T;
|
|
1257
|
+
} | {
|
|
1258
|
+
kind: 'attach';
|
|
1259
|
+
entity: T;
|
|
1260
|
+
} | {
|
|
1261
|
+
kind: 'remove';
|
|
1262
|
+
entity: T;
|
|
1263
|
+
} | {
|
|
1264
|
+
kind: 'detach';
|
|
1265
|
+
entity: T;
|
|
1266
|
+
};
|
|
1267
|
+
interface HasDomainEvents {
|
|
1268
|
+
domainEvents?: any[];
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
type DomainEventHandler$1<Context> = (event: any, ctx: Context) => Promise<void> | void;
|
|
1272
|
+
declare const addDomainEvent: (entity: HasDomainEvents, event: any) => void;
|
|
1273
|
+
|
|
1274
|
+
interface OrmInterceptor {
|
|
1275
|
+
beforeFlush?(ctx: OrmContext): Promise<void> | void;
|
|
1276
|
+
afterFlush?(ctx: OrmContext): Promise<void> | void;
|
|
1277
|
+
}
|
|
1278
|
+
type DomainEventHandler = DomainEventHandler$1<OrmContext>;
|
|
1279
|
+
interface OrmContextOptions {
|
|
1280
|
+
dialect: Dialect;
|
|
1281
|
+
executor: DbExecutor;
|
|
1282
|
+
interceptors?: OrmInterceptor[];
|
|
1283
|
+
domainEventHandlers?: Record<string, DomainEventHandler[]>;
|
|
1284
|
+
}
|
|
1285
|
+
declare class OrmContext {
|
|
1286
|
+
private readonly options;
|
|
1287
|
+
private readonly identityMap;
|
|
1288
|
+
private readonly unitOfWork;
|
|
1289
|
+
private readonly relationChanges;
|
|
1290
|
+
private readonly interceptors;
|
|
1291
|
+
private readonly domainEvents;
|
|
1292
|
+
constructor(options: OrmContextOptions);
|
|
1293
|
+
get dialect(): Dialect;
|
|
1294
|
+
get executor(): DbExecutor;
|
|
1295
|
+
get identityBuckets(): Map<string, Map<string, TrackedEntity>>;
|
|
1296
|
+
get tracked(): TrackedEntity[];
|
|
1297
|
+
getEntity(table: TableDef, pk: string | number): any | undefined;
|
|
1298
|
+
setEntity(table: TableDef, pk: string | number, entity: any): void;
|
|
1299
|
+
trackNew(table: TableDef, entity: any, pk?: string | number): void;
|
|
1300
|
+
trackManaged(table: TableDef, pk: string | number, entity: any): void;
|
|
1301
|
+
markDirty(entity: any): void;
|
|
1302
|
+
markRemoved(entity: any): void;
|
|
1303
|
+
registerRelationChange(root: any, relationKey: RelationKey, rootTable: TableDef, relationName: string, relation: RelationDef, change: RelationChange<any>): void;
|
|
1304
|
+
registerInterceptor(interceptor: OrmInterceptor): void;
|
|
1305
|
+
registerDomainEventHandler(name: string, handler: DomainEventHandler): void;
|
|
1306
|
+
saveChanges(): Promise<void>;
|
|
1307
|
+
getEntitiesForTable(table: TableDef): TrackedEntity[];
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
/**
|
|
1311
|
+
* Main query builder class for constructing SQL SELECT queries
|
|
1312
|
+
* @typeParam T - Result type for projections (unused)
|
|
1313
|
+
* @typeParam TTable - Table definition being queried
|
|
1314
|
+
*/
|
|
1315
|
+
declare class SelectQueryBuilder<T = any, TTable extends TableDef = TableDef> {
|
|
1316
|
+
private readonly env;
|
|
1317
|
+
private readonly context;
|
|
1318
|
+
private readonly columnSelector;
|
|
1319
|
+
private readonly relationManager;
|
|
1320
|
+
private readonly lazyRelations;
|
|
1321
|
+
/**
|
|
1322
|
+
* Creates a new SelectQueryBuilder instance
|
|
1323
|
+
* @param table - Table definition to query
|
|
1324
|
+
* @param state - Optional initial query state
|
|
1325
|
+
* @param hydration - Optional hydration manager
|
|
1326
|
+
* @param dependencies - Optional query builder dependencies
|
|
1327
|
+
*/
|
|
1328
|
+
constructor(table: TTable, state?: SelectQueryState, hydration?: HydrationManager, dependencies?: Partial<SelectQueryBuilderDependencies>, lazyRelations?: Set<string>);
|
|
1329
|
+
private clone;
|
|
1330
|
+
private resolveQueryNode;
|
|
1331
|
+
private createChildBuilder;
|
|
1332
|
+
private applyAst;
|
|
1333
|
+
private applyJoin;
|
|
1334
|
+
/**
|
|
1335
|
+
* Selects specific columns for the query
|
|
1336
|
+
* @param columns - Record of column definitions, function nodes, case expressions, or window functions
|
|
1337
|
+
* @returns New query builder instance with selected columns
|
|
1338
|
+
*/
|
|
1339
|
+
select(columns: Record<string, ColumnDef | FunctionNode | CaseExpressionNode | WindowFunctionNode>): SelectQueryBuilder<T, TTable>;
|
|
1340
|
+
/**
|
|
1341
|
+
* Selects raw column expressions
|
|
1342
|
+
* @param cols - Column expressions as strings
|
|
1343
|
+
* @returns New query builder instance with raw column selections
|
|
1344
|
+
*/
|
|
1345
|
+
selectRaw(...cols: string[]): SelectQueryBuilder<T, TTable>;
|
|
1346
|
+
/**
|
|
1347
|
+
* Adds a Common Table Expression (CTE) to the query
|
|
1348
|
+
* @param name - Name of the CTE
|
|
1349
|
+
* @param query - Query builder or query node for the CTE
|
|
1350
|
+
* @param columns - Optional column names for the CTE
|
|
1351
|
+
* @returns New query builder instance with the CTE
|
|
1352
|
+
*/
|
|
1353
|
+
with(name: string, query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, columns?: string[]): SelectQueryBuilder<T, TTable>;
|
|
1354
|
+
/**
|
|
1355
|
+
* Adds a recursive Common Table Expression (CTE) to the query
|
|
1356
|
+
* @param name - Name of the CTE
|
|
1357
|
+
* @param query - Query builder or query node for the CTE
|
|
1358
|
+
* @param columns - Optional column names for the CTE
|
|
1359
|
+
* @returns New query builder instance with the recursive CTE
|
|
1360
|
+
*/
|
|
1361
|
+
withRecursive(name: string, query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, columns?: string[]): SelectQueryBuilder<T, TTable>;
|
|
1362
|
+
/**
|
|
1363
|
+
* Selects a subquery as a column
|
|
1364
|
+
* @param alias - Alias for the subquery column
|
|
1365
|
+
* @param sub - Query builder or query node for the subquery
|
|
1366
|
+
* @returns New query builder instance with the subquery selection
|
|
1367
|
+
*/
|
|
1368
|
+
selectSubquery(alias: string, sub: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode): SelectQueryBuilder<T, TTable>;
|
|
1369
|
+
/**
|
|
1370
|
+
* Adds an INNER JOIN to the query
|
|
1371
|
+
* @param table - Table to join
|
|
1372
|
+
* @param condition - Join condition expression
|
|
1373
|
+
* @returns New query builder instance with the INNER JOIN
|
|
1374
|
+
*/
|
|
1375
|
+
innerJoin(table: TableDef, condition: BinaryExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
1376
|
+
/**
|
|
1377
|
+
* Adds a LEFT JOIN to the query
|
|
1378
|
+
* @param table - Table to join
|
|
1379
|
+
* @param condition - Join condition expression
|
|
1380
|
+
* @returns New query builder instance with the LEFT JOIN
|
|
1381
|
+
*/
|
|
1382
|
+
leftJoin(table: TableDef, condition: BinaryExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
1383
|
+
/**
|
|
1384
|
+
* Adds a RIGHT JOIN to the query
|
|
1385
|
+
* @param table - Table to join
|
|
1386
|
+
* @param condition - Join condition expression
|
|
1387
|
+
* @returns New query builder instance with the RIGHT JOIN
|
|
1388
|
+
*/
|
|
1389
|
+
rightJoin(table: TableDef, condition: BinaryExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
1390
|
+
/**
|
|
1391
|
+
* Matches records based on a relationship
|
|
1392
|
+
* @param relationName - Name of the relationship to match
|
|
1393
|
+
* @param predicate - Optional predicate expression
|
|
1394
|
+
* @returns New query builder instance with the relationship match
|
|
1395
|
+
*/
|
|
1396
|
+
match(relationName: string, predicate?: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
1397
|
+
/**
|
|
1398
|
+
* Joins a related table
|
|
1399
|
+
* @param relationName - Name of the relationship to join
|
|
1400
|
+
* @param joinKind - Type of join (defaults to INNER)
|
|
1401
|
+
* @param extraCondition - Optional additional join condition
|
|
1402
|
+
* @returns New query builder instance with the relationship join
|
|
1403
|
+
*/
|
|
1404
|
+
joinRelation(relationName: string, joinKind?: JoinKind, extraCondition?: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
1405
|
+
/**
|
|
1406
|
+
* Includes related data in the query results
|
|
1407
|
+
* @param relationName - Name of the relationship to include
|
|
1408
|
+
* @param options - Optional include options
|
|
1409
|
+
* @returns New query builder instance with the relationship inclusion
|
|
1410
|
+
*/
|
|
1411
|
+
include(relationName: string, options?: RelationIncludeOptions): SelectQueryBuilder<T, TTable>;
|
|
1412
|
+
includeLazy<K extends keyof RelationMap<TTable>>(relationName: K): SelectQueryBuilder<T, TTable>;
|
|
1413
|
+
getLazyRelations(): (keyof RelationMap<TTable>)[];
|
|
1414
|
+
getTable(): TTable;
|
|
1415
|
+
execute(ctx: OrmContext): Promise<Entity<TTable>[]>;
|
|
1416
|
+
/**
|
|
1417
|
+
* Adds a WHERE condition to the query
|
|
1418
|
+
* @param expr - Expression for the WHERE clause
|
|
1419
|
+
* @returns New query builder instance with the WHERE condition
|
|
1420
|
+
*/
|
|
1421
|
+
where(expr: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
1422
|
+
/**
|
|
1423
|
+
* Adds a GROUP BY clause to the query
|
|
1424
|
+
* @param col - Column definition or column node to group by
|
|
1425
|
+
* @returns New query builder instance with the GROUP BY clause
|
|
1426
|
+
*/
|
|
1427
|
+
groupBy(col: ColumnDef | ColumnNode): SelectQueryBuilder<T, TTable>;
|
|
1428
|
+
/**
|
|
1429
|
+
* Adds a HAVING condition to the query
|
|
1430
|
+
* @param expr - Expression for the HAVING clause
|
|
1431
|
+
* @returns New query builder instance with the HAVING condition
|
|
1432
|
+
*/
|
|
1433
|
+
having(expr: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
1434
|
+
/**
|
|
1435
|
+
* Adds an ORDER BY clause to the query
|
|
1436
|
+
* @param col - Column definition or column node to order by
|
|
1437
|
+
* @param direction - Order direction (defaults to ASC)
|
|
1438
|
+
* @returns New query builder instance with the ORDER BY clause
|
|
1439
|
+
*/
|
|
1440
|
+
orderBy(col: ColumnDef | ColumnNode, direction?: OrderDirection): SelectQueryBuilder<T, TTable>;
|
|
1441
|
+
/**
|
|
1442
|
+
* Adds a DISTINCT clause to the query
|
|
1443
|
+
* @param cols - Columns to make distinct
|
|
1444
|
+
* @returns New query builder instance with the DISTINCT clause
|
|
1445
|
+
*/
|
|
1446
|
+
distinct(...cols: (ColumnDef | ColumnNode)[]): SelectQueryBuilder<T, TTable>;
|
|
1447
|
+
/**
|
|
1448
|
+
* Adds a LIMIT clause to the query
|
|
1449
|
+
* @param n - Maximum number of rows to return
|
|
1450
|
+
* @returns New query builder instance with the LIMIT clause
|
|
1451
|
+
*/
|
|
1452
|
+
limit(n: number): SelectQueryBuilder<T, TTable>;
|
|
1453
|
+
/**
|
|
1454
|
+
* Adds an OFFSET clause to the query
|
|
1455
|
+
* @param n - Number of rows to skip
|
|
1456
|
+
* @returns New query builder instance with the OFFSET clause
|
|
1457
|
+
*/
|
|
1458
|
+
offset(n: number): SelectQueryBuilder<T, TTable>;
|
|
1459
|
+
/**
|
|
1460
|
+
* Adds a WHERE EXISTS condition to the query
|
|
1461
|
+
* @param subquery - Subquery to check for existence
|
|
1462
|
+
* @returns New query builder instance with the WHERE EXISTS condition
|
|
1463
|
+
*/
|
|
1464
|
+
whereExists(subquery: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode): SelectQueryBuilder<T, TTable>;
|
|
1465
|
+
/**
|
|
1466
|
+
* Adds a WHERE NOT EXISTS condition to the query
|
|
1467
|
+
* @param subquery - Subquery to check for non-existence
|
|
1468
|
+
* @returns New query builder instance with the WHERE NOT EXISTS condition
|
|
1469
|
+
*/
|
|
1470
|
+
whereNotExists(subquery: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode): SelectQueryBuilder<T, TTable>;
|
|
1471
|
+
/**
|
|
1472
|
+
* Adds a WHERE EXISTS condition based on a relationship
|
|
1473
|
+
* @param relationName - Name of the relationship to check
|
|
1474
|
+
* @param callback - Optional callback to modify the relationship query
|
|
1475
|
+
* @returns New query builder instance with the relationship existence check
|
|
1476
|
+
*/
|
|
1477
|
+
whereHas(relationName: string, callback?: <TChildTable extends TableDef>(qb: SelectQueryBuilder<any, TChildTable>) => SelectQueryBuilder<any, TChildTable>): SelectQueryBuilder<T, TTable>;
|
|
1478
|
+
/**
|
|
1479
|
+
* Adds a WHERE NOT EXISTS condition based on a relationship
|
|
1480
|
+
* @param relationName - Name of the relationship to check
|
|
1481
|
+
* @param callback - Optional callback to modify the relationship query
|
|
1482
|
+
* @returns New query builder instance with the relationship non-existence check
|
|
1483
|
+
*/
|
|
1484
|
+
whereHasNot(relationName: string, callback?: <TChildTable extends TableDef>(qb: SelectQueryBuilder<any, TChildTable>) => SelectQueryBuilder<any, TChildTable>): SelectQueryBuilder<T, TTable>;
|
|
1485
|
+
/**
|
|
1486
|
+
* Compiles the query to SQL for a specific dialect
|
|
1487
|
+
* @param dialect - Database dialect to compile for
|
|
1488
|
+
* @returns Compiled query with SQL and parameters
|
|
1489
|
+
*/
|
|
1490
|
+
compile(dialect: Dialect): CompiledQuery;
|
|
1491
|
+
/**
|
|
1492
|
+
* Converts the query to SQL string for a specific dialect
|
|
1493
|
+
* @param dialect - Database dialect to generate SQL for
|
|
1494
|
+
* @returns SQL string representation of the query
|
|
1495
|
+
*/
|
|
1496
|
+
toSql(dialect: Dialect): string;
|
|
1497
|
+
/**
|
|
1498
|
+
* Gets the hydration plan for the query
|
|
1499
|
+
* @returns Hydration plan or undefined if none exists
|
|
1500
|
+
*/
|
|
1501
|
+
getHydrationPlan(): HydrationPlan | undefined;
|
|
1502
|
+
/**
|
|
1503
|
+
* Gets the Abstract Syntax Tree (AST) representation of the query
|
|
1504
|
+
* @returns Query AST with hydration applied
|
|
1505
|
+
*/
|
|
1506
|
+
getAST(): SelectQueryNode;
|
|
1507
|
+
}
|
|
1508
|
+
/**
|
|
1509
|
+
* Creates a column node for use in expressions
|
|
1510
|
+
* @param table - Table name
|
|
1511
|
+
* @param name - Column name
|
|
1512
|
+
* @returns ColumnNode with the specified table and name
|
|
1513
|
+
*/
|
|
1514
|
+
declare const createColumn: (table: string, name: string) => ColumnNode;
|
|
1515
|
+
/**
|
|
1516
|
+
* Creates a literal value node for use in expressions
|
|
1517
|
+
* @param val - Literal value (string or number)
|
|
1518
|
+
* @returns LiteralNode with the specified value
|
|
1519
|
+
*/
|
|
1520
|
+
declare const createLiteral: (val: string | number) => LiteralNode;
|
|
1521
|
+
|
|
1522
|
+
export { createLiteral as $, RelationKinds as A, type BinaryExpressionNode as B, type ColumnDef as C, type DeleteQueryNode as D, type ExpressionNode as E, type FunctionNode as F, type RelationType as G, type HydrationPlan as H, type InExpressionNode as I, type JsonPathNode as J, type CascadeMode as K, type LogicalExpressionNode as L, type ManyToManyCollection as M, type NullExpressionNode as N, type OperandNode as O, type RelationDef as P, hasMany as Q, type RelationMap as R, type SelectQueryNode as S, type TableDef as T, type UpdateQueryNode as U, belongsTo as V, type WindowFunctionNode as W, belongsToMany as X, type ColumnToTs as Y, type InferRow as Z, createColumn as _, type ColumnNode as a, isOperandNode as a0, isFunctionNode as a1, isCaseExpressionNode as a2, isWindowFunctionNode as a3, isExpressionSelectionNode as a4, addDomainEvent as a5, EntityStatus as a6, type QueryResult as a7, type DbExecutor as a8, type RelationKey as a9, type RelationChange as aa, type HasDomainEvents as ab, type OrmInterceptor as ac, type DomainEventHandler as ad, type OrmContextOptions as ae, type LiteralNode as b, type BetweenExpressionNode as c, type CaseExpressionNode as d, type ExistsExpressionNode as e, type OrderDirection as f, type ScalarSubqueryNode as g, type InsertQueryNode as h, type InsertCompiler as i, type CompiledQuery as j, type UpdateCompiler as k, type DeleteCompiler as l, Dialect as m, type CompilerContext as n, OrmContext as o, type Entity as p, type HasManyRelation as q, type BelongsToRelation as r, type BelongsToManyRelation as s, type HasManyCollection as t, type BelongsToReference as u, SelectQueryBuilder as v, type TableHooks as w, defineTable as x, type ColumnType as y, col as z };
|