metal-orm 1.0.33 → 1.0.35
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 +7 -5
- package/dist/index.cjs +295 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2587 -98
- package/dist/index.d.ts +2587 -98
- package/dist/index.js +285 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -6
- package/scripts/generate-entities.mjs +1 -1
- package/src/index.ts +1 -0
- package/src/orm/entity.ts +5 -5
- package/src/orm/execute.ts +4 -4
- package/src/orm/orm-session.ts +229 -229
- package/src/query-builder/select.ts +885 -886
- package/src/schema/column.ts +216 -214
- package/src/schema/types.ts +49 -47
- package/dist/decorators/index.cjs +0 -4968
- package/dist/decorators/index.cjs.map +0 -1
- package/dist/decorators/index.d.cts +0 -70
- package/dist/decorators/index.d.ts +0 -70
- package/dist/decorators/index.js +0 -4933
- package/dist/decorators/index.js.map +0 -1
- package/dist/select-BuMpVcVt.d.cts +0 -2424
- package/dist/select-BuMpVcVt.d.ts +0 -2424
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,716 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Supported column data types for database schema definitions
|
|
3
|
+
*/
|
|
4
|
+
type ColumnType = 'INT' | 'INTEGER' | 'BIGINT' | 'VARCHAR' | 'TEXT' | 'JSON' | 'ENUM' | 'DECIMAL' | 'FLOAT' | 'DOUBLE' | 'UUID' | 'BINARY' | 'VARBINARY' | 'BLOB' | 'BYTEA' | 'DATE' | 'DATETIME' | 'TIMESTAMP' | 'TIMESTAMPTZ' | 'BOOLEAN' | 'int' | 'integer' | 'bigint' | 'varchar' | 'text' | 'json' | 'enum' | 'decimal' | 'float' | 'double' | 'uuid' | 'binary' | 'varbinary' | 'blob' | 'bytea' | 'date' | 'datetime' | 'timestamp' | 'timestamptz' | 'boolean';
|
|
5
|
+
type ReferentialAction = 'NO ACTION' | 'RESTRICT' | 'CASCADE' | 'SET NULL' | 'SET DEFAULT';
|
|
6
|
+
interface RawDefaultValue {
|
|
7
|
+
raw: string;
|
|
8
|
+
}
|
|
9
|
+
type DefaultValue = unknown | RawDefaultValue;
|
|
10
|
+
interface ForeignKeyReference {
|
|
11
|
+
/** Target table name */
|
|
12
|
+
table: string;
|
|
13
|
+
/** Target column name */
|
|
14
|
+
column: string;
|
|
15
|
+
/** Optional constraint name */
|
|
16
|
+
name?: string;
|
|
17
|
+
/** ON DELETE action */
|
|
18
|
+
onDelete?: ReferentialAction;
|
|
19
|
+
/** ON UPDATE action */
|
|
20
|
+
onUpdate?: ReferentialAction;
|
|
21
|
+
/** Whether the constraint is deferrable (Postgres) */
|
|
22
|
+
deferrable?: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Definition of a database column
|
|
26
|
+
*/
|
|
27
|
+
interface ColumnDef<T extends ColumnType = ColumnType, TRuntime = unknown> {
|
|
28
|
+
/** Column name (filled at runtime by defineTable) */
|
|
29
|
+
name: string;
|
|
30
|
+
/** Data type of the column */
|
|
31
|
+
type: T;
|
|
32
|
+
/** Optional override for the inferred TypeScript type */
|
|
33
|
+
tsType?: TRuntime;
|
|
34
|
+
/** Whether this column is a primary key */
|
|
35
|
+
primary?: boolean;
|
|
36
|
+
/** Whether this column cannot be null */
|
|
37
|
+
notNull?: boolean;
|
|
38
|
+
/** Whether this column must be unique (or name of the unique constraint) */
|
|
39
|
+
unique?: boolean | string;
|
|
40
|
+
/** Default value for the column */
|
|
41
|
+
default?: DefaultValue;
|
|
42
|
+
/** Whether the column auto-increments / identity */
|
|
43
|
+
autoIncrement?: boolean;
|
|
44
|
+
/** Identity strategy where supported */
|
|
45
|
+
generated?: 'always' | 'byDefault';
|
|
46
|
+
/** Inline check constraint expression */
|
|
47
|
+
check?: string;
|
|
48
|
+
/** Foreign key reference */
|
|
49
|
+
references?: ForeignKeyReference;
|
|
50
|
+
/** Column comment/description */
|
|
51
|
+
comment?: string;
|
|
52
|
+
/** Additional arguments for the column type (e.g., VARCHAR length) */
|
|
53
|
+
args?: any[];
|
|
54
|
+
/** Table name this column belongs to (filled at runtime by defineTable) */
|
|
55
|
+
table?: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Factory for creating column definitions with common data types
|
|
59
|
+
*/
|
|
60
|
+
declare const col: {
|
|
61
|
+
/**
|
|
62
|
+
* Creates an integer column definition
|
|
63
|
+
* @returns ColumnDef with INT type
|
|
64
|
+
*/
|
|
65
|
+
int: () => ColumnDef<"INT">;
|
|
66
|
+
/**
|
|
67
|
+
* Creates a big integer column definition
|
|
68
|
+
*/
|
|
69
|
+
bigint: () => ColumnDef<"BIGINT">;
|
|
70
|
+
/**
|
|
71
|
+
* Creates a variable character column definition
|
|
72
|
+
* @param length - Maximum length of the string
|
|
73
|
+
* @returns ColumnDef with VARCHAR type
|
|
74
|
+
*/
|
|
75
|
+
varchar: (length: number) => ColumnDef<"VARCHAR">;
|
|
76
|
+
/**
|
|
77
|
+
* Creates a fixed precision decimal column definition
|
|
78
|
+
*/
|
|
79
|
+
decimal: (precision: number, scale?: number) => ColumnDef<"DECIMAL">;
|
|
80
|
+
/**
|
|
81
|
+
* Creates a floating point column definition
|
|
82
|
+
*/
|
|
83
|
+
float: (precision?: number) => ColumnDef<"FLOAT">;
|
|
84
|
+
/**
|
|
85
|
+
* Creates a UUID column definition
|
|
86
|
+
*/
|
|
87
|
+
uuid: () => ColumnDef<"UUID">;
|
|
88
|
+
/**
|
|
89
|
+
* Creates a binary large object column definition
|
|
90
|
+
*/
|
|
91
|
+
blob: () => ColumnDef<"BLOB">;
|
|
92
|
+
/**
|
|
93
|
+
* Creates a fixed-length binary column definition
|
|
94
|
+
*/
|
|
95
|
+
binary: (length?: number) => ColumnDef<"BINARY">;
|
|
96
|
+
/**
|
|
97
|
+
* Creates a variable-length binary column definition
|
|
98
|
+
*/
|
|
99
|
+
varbinary: (length?: number) => ColumnDef<"VARBINARY">;
|
|
100
|
+
/**
|
|
101
|
+
* Creates a Postgres bytea column definition
|
|
102
|
+
*/
|
|
103
|
+
bytea: () => ColumnDef<"BYTEA">;
|
|
104
|
+
/**
|
|
105
|
+
* Creates a timestamp column definition
|
|
106
|
+
*/
|
|
107
|
+
timestamp: <TRuntime = string>() => ColumnDef<"TIMESTAMP", TRuntime>;
|
|
108
|
+
/**
|
|
109
|
+
* Creates a timestamptz column definition
|
|
110
|
+
*/
|
|
111
|
+
timestamptz: <TRuntime = string>() => ColumnDef<"TIMESTAMPTZ", TRuntime>;
|
|
112
|
+
/**
|
|
113
|
+
* Creates a date column definition
|
|
114
|
+
*/
|
|
115
|
+
date: <TRuntime = string>() => ColumnDef<"DATE", TRuntime>;
|
|
116
|
+
/**
|
|
117
|
+
* Creates a datetime column definition
|
|
118
|
+
*/
|
|
119
|
+
datetime: <TRuntime = string>() => ColumnDef<"DATETIME", TRuntime>;
|
|
120
|
+
/**
|
|
121
|
+
* Creates a JSON column definition
|
|
122
|
+
* @returns ColumnDef with JSON type
|
|
123
|
+
*/
|
|
124
|
+
json: () => ColumnDef<"JSON">;
|
|
125
|
+
/**
|
|
126
|
+
* Creates a boolean column definition
|
|
127
|
+
* @returns ColumnDef with BOOLEAN type
|
|
128
|
+
*/
|
|
129
|
+
boolean: () => ColumnDef<"BOOLEAN">;
|
|
130
|
+
/**
|
|
131
|
+
* Creates an enum column definition
|
|
132
|
+
* @param values - Enum values
|
|
133
|
+
*/
|
|
134
|
+
enum: (values: string[]) => ColumnDef<"ENUM">;
|
|
135
|
+
/**
|
|
136
|
+
* Marks a column definition as a primary key
|
|
137
|
+
* @param def - Column definition to modify
|
|
138
|
+
* @returns Modified ColumnDef with primary: true
|
|
139
|
+
*/
|
|
140
|
+
primaryKey: <T extends ColumnType>(def: ColumnDef<T>) => ColumnDef<T>;
|
|
141
|
+
/**
|
|
142
|
+
* Marks a column as NOT NULL
|
|
143
|
+
*/
|
|
144
|
+
notNull: <T extends ColumnType>(def: ColumnDef<T>) => ColumnDef<T>;
|
|
145
|
+
/**
|
|
146
|
+
* Marks a column as UNIQUE
|
|
147
|
+
*/
|
|
148
|
+
unique: <T extends ColumnType>(def: ColumnDef<T>, name?: string) => ColumnDef<T>;
|
|
149
|
+
/**
|
|
150
|
+
* Sets a default value for the column
|
|
151
|
+
*/
|
|
152
|
+
default: <T extends ColumnType>(def: ColumnDef<T>, value: unknown) => ColumnDef<T>;
|
|
153
|
+
/**
|
|
154
|
+
* Sets a raw SQL default value for the column
|
|
155
|
+
*/
|
|
156
|
+
defaultRaw: <T extends ColumnType>(def: ColumnDef<T>, expression: string) => ColumnDef<T>;
|
|
157
|
+
/**
|
|
158
|
+
* Marks a column as auto-increment / identity
|
|
159
|
+
*/
|
|
160
|
+
autoIncrement: <T extends ColumnType>(def: ColumnDef<T>, strategy?: ColumnDef["generated"]) => ColumnDef<T>;
|
|
161
|
+
/**
|
|
162
|
+
* Adds a foreign key reference
|
|
163
|
+
*/
|
|
164
|
+
references: <T extends ColumnType>(def: ColumnDef<T>, ref: ForeignKeyReference) => ColumnDef<T>;
|
|
165
|
+
/**
|
|
166
|
+
* Adds a check constraint to the column
|
|
167
|
+
*/
|
|
168
|
+
check: <T extends ColumnType>(def: ColumnDef<T>, expression: string) => ColumnDef<T>;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Types of relationships supported between tables
|
|
173
|
+
*/
|
|
174
|
+
declare const RelationKinds: {
|
|
175
|
+
/** One-to-one relationship */
|
|
176
|
+
readonly HasOne: "HAS_ONE";
|
|
177
|
+
/** One-to-many relationship */
|
|
178
|
+
readonly HasMany: "HAS_MANY";
|
|
179
|
+
/** Many-to-one relationship */
|
|
180
|
+
readonly BelongsTo: "BELONGS_TO";
|
|
181
|
+
/** Many-to-many relationship with pivot metadata */
|
|
182
|
+
readonly BelongsToMany: "BELONGS_TO_MANY";
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Type representing the supported relationship kinds
|
|
186
|
+
*/
|
|
187
|
+
type RelationType = (typeof RelationKinds)[keyof typeof RelationKinds];
|
|
188
|
+
type CascadeMode = 'none' | 'all' | 'persist' | 'remove' | 'link';
|
|
189
|
+
/**
|
|
190
|
+
* One-to-many relationship definition
|
|
191
|
+
*/
|
|
192
|
+
interface HasManyRelation<TTarget extends TableDef = TableDef> {
|
|
193
|
+
type: typeof RelationKinds.HasMany;
|
|
194
|
+
target: TTarget;
|
|
195
|
+
foreignKey: string;
|
|
196
|
+
localKey?: string;
|
|
197
|
+
cascade?: CascadeMode;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* One-to-one relationship definition
|
|
201
|
+
*/
|
|
202
|
+
interface HasOneRelation<TTarget extends TableDef = TableDef> {
|
|
203
|
+
type: typeof RelationKinds.HasOne;
|
|
204
|
+
target: TTarget;
|
|
205
|
+
foreignKey: string;
|
|
206
|
+
localKey?: string;
|
|
207
|
+
cascade?: CascadeMode;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Many-to-one relationship definition
|
|
211
|
+
*/
|
|
212
|
+
interface BelongsToRelation<TTarget extends TableDef = TableDef> {
|
|
213
|
+
type: typeof RelationKinds.BelongsTo;
|
|
214
|
+
target: TTarget;
|
|
215
|
+
foreignKey: string;
|
|
216
|
+
localKey?: string;
|
|
217
|
+
cascade?: CascadeMode;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Many-to-many relationship definition with rich pivot metadata
|
|
221
|
+
*/
|
|
222
|
+
interface BelongsToManyRelation<TTarget extends TableDef = TableDef> {
|
|
223
|
+
type: typeof RelationKinds.BelongsToMany;
|
|
224
|
+
target: TTarget;
|
|
225
|
+
pivotTable: TableDef;
|
|
226
|
+
pivotForeignKeyToRoot: string;
|
|
227
|
+
pivotForeignKeyToTarget: string;
|
|
228
|
+
localKey?: string;
|
|
229
|
+
targetKey?: string;
|
|
230
|
+
pivotPrimaryKey?: string;
|
|
231
|
+
defaultPivotColumns?: string[];
|
|
232
|
+
cascade?: CascadeMode;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Union type representing any supported relationship definition
|
|
236
|
+
*/
|
|
237
|
+
type RelationDef = HasManyRelation | HasOneRelation | BelongsToRelation | BelongsToManyRelation;
|
|
238
|
+
/**
|
|
239
|
+
* Creates a one-to-many relationship definition
|
|
240
|
+
* @param target - Target table of the relationship
|
|
241
|
+
* @param foreignKey - Foreign key column name on the child table
|
|
242
|
+
* @param localKey - Local key column name (optional)
|
|
243
|
+
* @returns HasManyRelation definition
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* ```typescript
|
|
247
|
+
* hasMany(usersTable, 'user_id')
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
declare const hasMany: <TTarget extends TableDef>(target: TTarget, foreignKey: string, localKey?: string, cascade?: CascadeMode) => HasManyRelation<TTarget>;
|
|
251
|
+
/**
|
|
252
|
+
* Creates a one-to-one relationship definition
|
|
253
|
+
* @param target - Target table of the relationship
|
|
254
|
+
* @param foreignKey - Foreign key column name on the child table
|
|
255
|
+
* @param localKey - Local key column name (optional)
|
|
256
|
+
* @returns HasOneRelation definition
|
|
257
|
+
*/
|
|
258
|
+
declare const hasOne: <TTarget extends TableDef>(target: TTarget, foreignKey: string, localKey?: string, cascade?: CascadeMode) => HasOneRelation<TTarget>;
|
|
259
|
+
/**
|
|
260
|
+
* Creates a many-to-one relationship definition
|
|
261
|
+
* @param target - Target table of the relationship
|
|
262
|
+
* @param foreignKey - Foreign key column name on the child table
|
|
263
|
+
* @param localKey - Local key column name (optional)
|
|
264
|
+
* @returns BelongsToRelation definition
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```typescript
|
|
268
|
+
* belongsTo(usersTable, 'user_id')
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
declare const belongsTo: <TTarget extends TableDef>(target: TTarget, foreignKey: string, localKey?: string, cascade?: CascadeMode) => BelongsToRelation<TTarget>;
|
|
272
|
+
/**
|
|
273
|
+
* Creates a many-to-many relationship definition with pivot metadata
|
|
274
|
+
* @param target - Target table
|
|
275
|
+
* @param pivotTable - Intermediate pivot table definition
|
|
276
|
+
* @param options - Pivot metadata configuration
|
|
277
|
+
* @returns BelongsToManyRelation definition
|
|
278
|
+
*/
|
|
279
|
+
declare const belongsToMany: <TTarget extends TableDef>(target: TTarget, pivotTable: TableDef, options: {
|
|
280
|
+
pivotForeignKeyToRoot: string;
|
|
281
|
+
pivotForeignKeyToTarget: string;
|
|
282
|
+
localKey?: string;
|
|
283
|
+
targetKey?: string;
|
|
284
|
+
pivotPrimaryKey?: string;
|
|
285
|
+
defaultPivotColumns?: string[];
|
|
286
|
+
cascade?: CascadeMode;
|
|
287
|
+
}) => BelongsToManyRelation<TTarget>;
|
|
288
|
+
|
|
289
|
+
interface IndexColumn {
|
|
290
|
+
column: string;
|
|
291
|
+
order?: 'ASC' | 'DESC';
|
|
292
|
+
nulls?: 'FIRST' | 'LAST';
|
|
293
|
+
}
|
|
294
|
+
interface IndexDef {
|
|
295
|
+
name?: string;
|
|
296
|
+
columns: (string | IndexColumn)[];
|
|
297
|
+
unique?: boolean;
|
|
298
|
+
where?: string;
|
|
299
|
+
}
|
|
300
|
+
interface CheckConstraint {
|
|
301
|
+
name?: string;
|
|
302
|
+
expression: string;
|
|
303
|
+
}
|
|
304
|
+
interface TableOptions {
|
|
305
|
+
schema?: string;
|
|
306
|
+
primaryKey?: string[];
|
|
307
|
+
indexes?: IndexDef[];
|
|
308
|
+
checks?: CheckConstraint[];
|
|
309
|
+
comment?: string;
|
|
310
|
+
engine?: string;
|
|
311
|
+
charset?: string;
|
|
312
|
+
collation?: string;
|
|
313
|
+
}
|
|
314
|
+
interface TableHooks {
|
|
315
|
+
beforeInsert?(ctx: unknown, entity: any): Promise<void> | void;
|
|
316
|
+
afterInsert?(ctx: unknown, entity: any): Promise<void> | void;
|
|
317
|
+
beforeUpdate?(ctx: unknown, entity: any): Promise<void> | void;
|
|
318
|
+
afterUpdate?(ctx: unknown, entity: any): Promise<void> | void;
|
|
319
|
+
beforeDelete?(ctx: unknown, entity: any): Promise<void> | void;
|
|
320
|
+
afterDelete?(ctx: unknown, entity: any): Promise<void> | void;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Definition of a database table with its columns and relationships
|
|
324
|
+
* @typeParam T - Type of the columns record
|
|
325
|
+
*/
|
|
326
|
+
interface TableDef<T extends Record<string, ColumnDef> = Record<string, ColumnDef>> {
|
|
327
|
+
/** Name of the table */
|
|
328
|
+
name: string;
|
|
329
|
+
/** Optional schema/catalog name */
|
|
330
|
+
schema?: string;
|
|
331
|
+
/** Record of column definitions keyed by column name */
|
|
332
|
+
columns: T;
|
|
333
|
+
/** Record of relationship definitions keyed by relation name */
|
|
334
|
+
relations: Record<string, RelationDef>;
|
|
335
|
+
/** Optional lifecycle hooks */
|
|
336
|
+
hooks?: TableHooks;
|
|
337
|
+
/** Composite primary key definition (falls back to column.primary flags) */
|
|
338
|
+
primaryKey?: string[];
|
|
339
|
+
/** Secondary indexes */
|
|
340
|
+
indexes?: IndexDef[];
|
|
341
|
+
/** Table-level check constraints */
|
|
342
|
+
checks?: CheckConstraint[];
|
|
343
|
+
/** Table comment/description */
|
|
344
|
+
comment?: string;
|
|
345
|
+
/** Dialect-specific options */
|
|
346
|
+
engine?: string;
|
|
347
|
+
charset?: string;
|
|
348
|
+
collation?: string;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Creates a table definition with columns and relationships
|
|
352
|
+
* @typeParam T - Type of the columns record
|
|
353
|
+
* @param name - Name of the table
|
|
354
|
+
* @param columns - Record of column definitions
|
|
355
|
+
* @param relations - Record of relationship definitions (optional)
|
|
356
|
+
* @returns Complete table definition with runtime-filled column metadata
|
|
357
|
+
*
|
|
358
|
+
* @example
|
|
359
|
+
* ```typescript
|
|
360
|
+
* const usersTable = defineTable('users', {
|
|
361
|
+
* id: col.primaryKey(col.int()),
|
|
362
|
+
* name: col.varchar(255),
|
|
363
|
+
* email: col.varchar(255)
|
|
364
|
+
* });
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
367
|
+
declare const defineTable: <T extends Record<string, ColumnDef>>(name: string, columns: T, relations?: Record<string, RelationDef>, hooks?: TableHooks, options?: TableOptions) => TableDef<T>;
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Resolves a relation definition to its target table type.
|
|
371
|
+
*/
|
|
372
|
+
type RelationTargetTable<TRel extends RelationDef> = TRel extends HasManyRelation<infer TTarget> ? TTarget : TRel extends HasOneRelation<infer TTarget> ? TTarget : TRel extends BelongsToRelation<infer TTarget> ? TTarget : TRel extends BelongsToManyRelation<infer TTarget> ? TTarget : never;
|
|
373
|
+
/**
|
|
374
|
+
* Maps a ColumnDef to its TypeScript type representation
|
|
375
|
+
*/
|
|
376
|
+
type ColumnToTs<T extends ColumnDef> = T['tsType'] extends undefined ? T['type'] extends 'INT' | 'INTEGER' | 'int' | 'integer' ? number : T['type'] extends 'BIGINT' | 'bigint' ? number | bigint : T['type'] extends 'DECIMAL' | 'decimal' | 'FLOAT' | 'float' | 'DOUBLE' | 'double' ? number : T['type'] extends 'BOOLEAN' | 'boolean' ? boolean : T['type'] extends 'JSON' | 'json' ? unknown : T['type'] extends 'BLOB' | 'blob' | 'BINARY' | 'binary' | 'VARBINARY' | 'varbinary' | 'BYTEA' | 'bytea' ? Buffer : T['type'] extends 'DATE' | 'date' | 'DATETIME' | 'datetime' | 'TIMESTAMP' | 'timestamp' | 'TIMESTAMPTZ' | 'timestamptz' ? string : string : Exclude<T['tsType'], undefined>;
|
|
377
|
+
/**
|
|
378
|
+
* Infers a row shape from a table definition
|
|
379
|
+
*/
|
|
380
|
+
type InferRow<TTable extends TableDef> = {
|
|
381
|
+
[K in keyof TTable['columns']]: ColumnToTs<TTable['columns'][K]>;
|
|
382
|
+
};
|
|
383
|
+
type RelationResult$1<T extends RelationDef> = T extends HasManyRelation<infer TTarget> ? InferRow<TTarget>[] : T extends HasOneRelation<infer TTarget> ? InferRow<TTarget> | null : T extends BelongsToRelation<infer TTarget> ? InferRow<TTarget> | null : T extends BelongsToManyRelation<infer TTarget> ? (InferRow<TTarget> & {
|
|
384
|
+
_pivot?: any;
|
|
385
|
+
})[] : never;
|
|
386
|
+
/**
|
|
387
|
+
* Maps relation names to the expected row results
|
|
388
|
+
*/
|
|
389
|
+
type RelationMap<TTable extends TableDef> = {
|
|
390
|
+
[K in keyof TTable['relations']]: RelationResult$1<TTable['relations'][K]>;
|
|
391
|
+
};
|
|
392
|
+
interface HasManyCollection<TChild> {
|
|
393
|
+
load(): Promise<TChild[]>;
|
|
394
|
+
getItems(): TChild[];
|
|
395
|
+
add(data: Partial<TChild>): TChild;
|
|
396
|
+
attach(entity: TChild): void;
|
|
397
|
+
remove(entity: TChild): void;
|
|
398
|
+
clear(): void;
|
|
399
|
+
}
|
|
400
|
+
interface BelongsToReference<TParent> {
|
|
401
|
+
load(): Promise<TParent | null>;
|
|
402
|
+
get(): TParent | null;
|
|
403
|
+
set(data: Partial<TParent> | TParent | null): TParent | null;
|
|
404
|
+
}
|
|
405
|
+
interface HasOneReference<TChild> {
|
|
406
|
+
load(): Promise<TChild | null>;
|
|
407
|
+
get(): TChild | null;
|
|
408
|
+
set(data: Partial<TChild> | TChild | null): TChild | null;
|
|
409
|
+
}
|
|
410
|
+
interface ManyToManyCollection<TTarget> {
|
|
411
|
+
load(): Promise<TTarget[]>;
|
|
412
|
+
getItems(): TTarget[];
|
|
413
|
+
attach(target: TTarget | number | string): void;
|
|
414
|
+
detach(target: TTarget | number | string): void;
|
|
415
|
+
syncByIds(ids: (number | string)[]): Promise<void>;
|
|
416
|
+
}
|
|
417
|
+
type EntityInstance<TTable extends TableDef, TRow = InferRow<TTable>> = TRow & {
|
|
418
|
+
[K in keyof RelationMap<TTable>]: TTable['relations'][K] extends HasManyRelation<infer TTarget> ? HasManyCollection<EntityInstance<TTarget>> : TTable['relations'][K] extends HasOneRelation<infer TTarget> ? HasOneReference<EntityInstance<TTarget>> : TTable['relations'][K] extends BelongsToManyRelation<infer TTarget> ? ManyToManyCollection<EntityInstance<TTarget>> : TTable['relations'][K] extends BelongsToRelation<infer TTarget> ? BelongsToReference<EntityInstance<TTarget>> : never;
|
|
419
|
+
} & {
|
|
420
|
+
$load<K extends keyof RelationMap<TTable>>(relation: K): Promise<RelationMap<TTable>[K]>;
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* SQL operators used in query conditions
|
|
425
|
+
*/
|
|
426
|
+
declare const SQL_OPERATORS: {
|
|
427
|
+
/** Equality operator */
|
|
428
|
+
readonly EQUALS: "=";
|
|
429
|
+
/** Not equals operator */
|
|
430
|
+
readonly NOT_EQUALS: "!=";
|
|
431
|
+
/** Greater than operator */
|
|
432
|
+
readonly GREATER_THAN: ">";
|
|
433
|
+
/** Greater than or equal operator */
|
|
434
|
+
readonly GREATER_OR_EQUAL: ">=";
|
|
435
|
+
/** Less than operator */
|
|
436
|
+
readonly LESS_THAN: "<";
|
|
437
|
+
/** Less than or equal operator */
|
|
438
|
+
readonly LESS_OR_EQUAL: "<=";
|
|
439
|
+
/** LIKE pattern matching operator */
|
|
440
|
+
readonly LIKE: "LIKE";
|
|
441
|
+
/** NOT LIKE pattern matching operator */
|
|
442
|
+
readonly NOT_LIKE: "NOT LIKE";
|
|
443
|
+
/** IN membership operator */
|
|
444
|
+
readonly IN: "IN";
|
|
445
|
+
/** NOT IN membership operator */
|
|
446
|
+
readonly NOT_IN: "NOT IN";
|
|
447
|
+
/** BETWEEN range operator */
|
|
448
|
+
readonly BETWEEN: "BETWEEN";
|
|
449
|
+
/** NOT BETWEEN range operator */
|
|
450
|
+
readonly NOT_BETWEEN: "NOT BETWEEN";
|
|
451
|
+
/** IS NULL null check operator */
|
|
452
|
+
readonly IS_NULL: "IS NULL";
|
|
453
|
+
/** IS NOT NULL null check operator */
|
|
454
|
+
readonly IS_NOT_NULL: "IS NOT NULL";
|
|
455
|
+
/** Logical AND operator */
|
|
456
|
+
readonly AND: "AND";
|
|
457
|
+
/** Logical OR operator */
|
|
458
|
+
readonly OR: "OR";
|
|
459
|
+
/** EXISTS operator */
|
|
460
|
+
readonly EXISTS: "EXISTS";
|
|
461
|
+
/** NOT EXISTS operator */
|
|
462
|
+
readonly NOT_EXISTS: "NOT EXISTS";
|
|
463
|
+
};
|
|
464
|
+
/**
|
|
465
|
+
* Type representing any supported SQL operator
|
|
466
|
+
*/
|
|
467
|
+
type SqlOperator = (typeof SQL_OPERATORS)[keyof typeof SQL_OPERATORS];
|
|
468
|
+
/**
|
|
469
|
+
* Types of SQL joins supported
|
|
470
|
+
*/
|
|
471
|
+
declare const JOIN_KINDS: {
|
|
472
|
+
/** INNER JOIN type */
|
|
473
|
+
readonly INNER: "INNER";
|
|
474
|
+
/** LEFT JOIN type */
|
|
475
|
+
readonly LEFT: "LEFT";
|
|
476
|
+
/** RIGHT JOIN type */
|
|
477
|
+
readonly RIGHT: "RIGHT";
|
|
478
|
+
/** CROSS JOIN type */
|
|
479
|
+
readonly CROSS: "CROSS";
|
|
480
|
+
};
|
|
481
|
+
/**
|
|
482
|
+
* Type representing any supported join kind
|
|
483
|
+
*/
|
|
484
|
+
type JoinKind = (typeof JOIN_KINDS)[keyof typeof JOIN_KINDS];
|
|
485
|
+
/**
|
|
486
|
+
* Ordering directions for result sorting
|
|
487
|
+
*/
|
|
488
|
+
declare const ORDER_DIRECTIONS: {
|
|
489
|
+
/** Ascending order */
|
|
490
|
+
readonly ASC: "ASC";
|
|
491
|
+
/** Descending order */
|
|
492
|
+
readonly DESC: "DESC";
|
|
493
|
+
};
|
|
494
|
+
/**
|
|
495
|
+
* Type representing any supported order direction
|
|
496
|
+
*/
|
|
497
|
+
type OrderDirection = (typeof ORDER_DIRECTIONS)[keyof typeof ORDER_DIRECTIONS];
|
|
498
|
+
/**
|
|
499
|
+
* Supported database dialects
|
|
500
|
+
*/
|
|
501
|
+
declare const SUPPORTED_DIALECTS: {
|
|
502
|
+
/** MySQL database dialect */
|
|
503
|
+
readonly MYSQL: "mysql";
|
|
504
|
+
/** SQLite database dialect */
|
|
505
|
+
readonly SQLITE: "sqlite";
|
|
506
|
+
/** Microsoft SQL Server dialect */
|
|
507
|
+
readonly MSSQL: "mssql";
|
|
508
|
+
/** PostgreSQL database dialect */
|
|
509
|
+
readonly POSTGRES: "postgres";
|
|
510
|
+
};
|
|
511
|
+
/**
|
|
512
|
+
* Type representing any supported database dialect
|
|
513
|
+
*/
|
|
514
|
+
type DialectName$1 = (typeof SUPPORTED_DIALECTS)[keyof typeof SUPPORTED_DIALECTS];
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Minimal column reference used by AST builders.
|
|
518
|
+
* Accepts any object with a name and optional table/alias fields
|
|
519
|
+
* (schema ColumnDef/TableDef remain structurally compatible).
|
|
520
|
+
*/
|
|
521
|
+
interface ColumnRef {
|
|
522
|
+
name: string;
|
|
523
|
+
table?: string;
|
|
524
|
+
alias?: string;
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Minimal table reference used by AST builders.
|
|
528
|
+
* Keeps AST decoupled from full schema TableDef shape.
|
|
529
|
+
*/
|
|
530
|
+
interface TableRef {
|
|
531
|
+
name: string;
|
|
532
|
+
schema?: string;
|
|
533
|
+
alias?: string;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* AST node representing a literal value
|
|
538
|
+
*/
|
|
539
|
+
interface LiteralNode {
|
|
540
|
+
type: 'Literal';
|
|
541
|
+
/** The literal value (string, number, boolean, or null) */
|
|
542
|
+
value: string | number | boolean | null;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* AST node representing a column reference
|
|
546
|
+
*/
|
|
547
|
+
interface ColumnNode {
|
|
548
|
+
type: 'Column';
|
|
549
|
+
/** Table name the column belongs to */
|
|
550
|
+
table: string;
|
|
551
|
+
/** Column name */
|
|
552
|
+
name: string;
|
|
553
|
+
/** Optional alias for the column */
|
|
554
|
+
alias?: string;
|
|
555
|
+
/** Optional scope marker (e.g., 'outer' for correlated references) */
|
|
556
|
+
scope?: 'outer' | 'default';
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* AST node representing a function call
|
|
560
|
+
*/
|
|
561
|
+
interface FunctionNode {
|
|
562
|
+
type: 'Function';
|
|
563
|
+
/** Function name (e.g., COUNT, SUM) */
|
|
564
|
+
name: string;
|
|
565
|
+
/** Optional canonical function key for dialect-aware rendering */
|
|
566
|
+
fn?: string;
|
|
567
|
+
/** Function arguments */
|
|
568
|
+
args: OperandNode[];
|
|
569
|
+
/** Optional alias for the function result */
|
|
570
|
+
alias?: string;
|
|
571
|
+
/** Optional ORDER BY clause used by aggregations like GROUP_CONCAT */
|
|
572
|
+
orderBy?: OrderByNode[];
|
|
573
|
+
/** Optional separator argument used by GROUP_CONCAT-like functions */
|
|
574
|
+
separator?: OperandNode;
|
|
575
|
+
/** Optional DISTINCT modifier */
|
|
576
|
+
distinct?: boolean;
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* AST node representing a JSON path expression
|
|
580
|
+
*/
|
|
581
|
+
interface JsonPathNode {
|
|
582
|
+
type: 'JsonPath';
|
|
583
|
+
/** Source column */
|
|
584
|
+
column: ColumnNode;
|
|
585
|
+
/** JSON path expression */
|
|
586
|
+
path: string;
|
|
587
|
+
/** Optional alias for the result */
|
|
588
|
+
alias?: string;
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* AST node representing a scalar subquery
|
|
592
|
+
*/
|
|
593
|
+
interface ScalarSubqueryNode {
|
|
594
|
+
type: 'ScalarSubquery';
|
|
595
|
+
/** Subquery to execute */
|
|
596
|
+
query: SelectQueryNode;
|
|
597
|
+
/** Optional alias for the subquery result */
|
|
598
|
+
alias?: string;
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* AST node representing a CASE expression
|
|
602
|
+
*/
|
|
603
|
+
interface CaseExpressionNode {
|
|
604
|
+
type: 'CaseExpression';
|
|
605
|
+
/** WHEN-THEN conditions */
|
|
606
|
+
conditions: {
|
|
607
|
+
when: ExpressionNode;
|
|
608
|
+
then: OperandNode;
|
|
609
|
+
}[];
|
|
610
|
+
/** Optional ELSE clause */
|
|
611
|
+
else?: OperandNode;
|
|
612
|
+
/** Optional alias for the result */
|
|
613
|
+
alias?: string;
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* AST node representing a window function
|
|
617
|
+
*/
|
|
618
|
+
interface WindowFunctionNode {
|
|
619
|
+
type: 'WindowFunction';
|
|
620
|
+
/** Window function name (e.g., ROW_NUMBER, RANK) */
|
|
621
|
+
name: string;
|
|
622
|
+
/** Function arguments */
|
|
623
|
+
args: (ColumnNode | LiteralNode | JsonPathNode)[];
|
|
624
|
+
/** Optional PARTITION BY clause */
|
|
625
|
+
partitionBy?: ColumnNode[];
|
|
626
|
+
/** Optional ORDER BY clause */
|
|
627
|
+
orderBy?: OrderByNode[];
|
|
628
|
+
/** Optional alias for the result */
|
|
629
|
+
alias?: string;
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* Union type representing any operand that can be used in expressions
|
|
633
|
+
*/
|
|
634
|
+
type OperandNode = ColumnNode | LiteralNode | FunctionNode | JsonPathNode | ScalarSubqueryNode | CaseExpressionNode | WindowFunctionNode;
|
|
635
|
+
declare const isOperandNode: (node: any) => node is OperandNode;
|
|
636
|
+
declare const isFunctionNode: (node: any) => node is FunctionNode;
|
|
637
|
+
declare const isCaseExpressionNode: (node: any) => node is CaseExpressionNode;
|
|
638
|
+
declare const isWindowFunctionNode: (node: any) => node is WindowFunctionNode;
|
|
639
|
+
declare const isExpressionSelectionNode: (node: ColumnRef | FunctionNode | CaseExpressionNode | WindowFunctionNode) => node is FunctionNode | CaseExpressionNode | WindowFunctionNode;
|
|
640
|
+
/**
|
|
641
|
+
* AST node representing a binary expression (e.g., column = value)
|
|
642
|
+
*/
|
|
643
|
+
interface BinaryExpressionNode {
|
|
644
|
+
type: 'BinaryExpression';
|
|
645
|
+
/** Left operand */
|
|
646
|
+
left: OperandNode;
|
|
647
|
+
/** Comparison operator */
|
|
648
|
+
operator: SqlOperator;
|
|
649
|
+
/** Right operand */
|
|
650
|
+
right: OperandNode;
|
|
651
|
+
/** Optional escape character for LIKE expressions */
|
|
652
|
+
escape?: LiteralNode;
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* AST node representing a logical expression (AND/OR)
|
|
656
|
+
*/
|
|
657
|
+
interface LogicalExpressionNode {
|
|
658
|
+
type: 'LogicalExpression';
|
|
659
|
+
/** Logical operator (AND or OR) */
|
|
660
|
+
operator: 'AND' | 'OR';
|
|
661
|
+
/** Operands to combine */
|
|
662
|
+
operands: ExpressionNode[];
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* AST node representing a null check expression
|
|
666
|
+
*/
|
|
667
|
+
interface NullExpressionNode {
|
|
668
|
+
type: 'NullExpression';
|
|
669
|
+
/** Operand to check for null */
|
|
670
|
+
left: OperandNode;
|
|
671
|
+
/** Null check operator */
|
|
672
|
+
operator: 'IS NULL' | 'IS NOT NULL';
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* AST node representing an IN/NOT IN expression
|
|
676
|
+
*/
|
|
677
|
+
interface InExpressionNode {
|
|
678
|
+
type: 'InExpression';
|
|
679
|
+
/** Left operand to check */
|
|
680
|
+
left: OperandNode;
|
|
681
|
+
/** IN/NOT IN operator */
|
|
682
|
+
operator: 'IN' | 'NOT IN';
|
|
683
|
+
/** Values to check against */
|
|
684
|
+
right: OperandNode[];
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* AST node representing an EXISTS/NOT EXISTS expression
|
|
688
|
+
*/
|
|
689
|
+
interface ExistsExpressionNode {
|
|
690
|
+
type: 'ExistsExpression';
|
|
691
|
+
/** EXISTS/NOT EXISTS operator */
|
|
692
|
+
operator: SqlOperator;
|
|
693
|
+
/** Subquery to check */
|
|
694
|
+
subquery: SelectQueryNode;
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* AST node representing a BETWEEN/NOT BETWEEN expression
|
|
698
|
+
*/
|
|
699
|
+
interface BetweenExpressionNode {
|
|
700
|
+
type: 'BetweenExpression';
|
|
701
|
+
/** Operand to check */
|
|
702
|
+
left: OperandNode;
|
|
703
|
+
/** BETWEEN/NOT BETWEEN operator */
|
|
704
|
+
operator: 'BETWEEN' | 'NOT BETWEEN';
|
|
705
|
+
/** Lower bound */
|
|
706
|
+
lower: OperandNode;
|
|
707
|
+
/** Upper bound */
|
|
708
|
+
upper: OperandNode;
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* Union type representing any supported expression node
|
|
712
|
+
*/
|
|
713
|
+
type ExpressionNode = BinaryExpressionNode | LogicalExpressionNode | NullExpressionNode | InExpressionNode | ExistsExpressionNode | BetweenExpressionNode;
|
|
3
714
|
|
|
4
715
|
type LiteralValue = LiteralNode['value'];
|
|
5
716
|
type ValueOperandInput = OperandNode | LiteralValue;
|
|
@@ -195,132 +906,1843 @@ declare const lead: (col: ColumnRef | ColumnNode, offset?: number, defaultValue?
|
|
|
195
906
|
* @param col - Column to get first value from
|
|
196
907
|
* @returns Window function node for FIRST_VALUE
|
|
197
908
|
*/
|
|
198
|
-
declare const firstValue: (col: ColumnRef | ColumnNode) => WindowFunctionNode;
|
|
909
|
+
declare const firstValue: (col: ColumnRef | ColumnNode) => WindowFunctionNode;
|
|
910
|
+
/**
|
|
911
|
+
* Creates a LAST_VALUE window function
|
|
912
|
+
* @param col - Column to get last value from
|
|
913
|
+
* @returns Window function node for LAST_VALUE
|
|
914
|
+
*/
|
|
915
|
+
declare const lastValue: (col: ColumnRef | ColumnNode) => WindowFunctionNode;
|
|
916
|
+
/**
|
|
917
|
+
* Creates a custom window function
|
|
918
|
+
* @param name - Window function name
|
|
919
|
+
* @param args - Function arguments
|
|
920
|
+
* @param partitionBy - Optional PARTITION BY columns
|
|
921
|
+
* @param orderBy - Optional ORDER BY clauses
|
|
922
|
+
* @returns Window function node
|
|
923
|
+
*/
|
|
924
|
+
declare const windowFunction: (name: string, args?: (ColumnRef | ColumnNode | LiteralNode | JsonPathNode)[], partitionBy?: (ColumnRef | ColumnNode)[], orderBy?: {
|
|
925
|
+
column: ColumnRef | ColumnNode;
|
|
926
|
+
direction: OrderDirection;
|
|
927
|
+
}[]) => WindowFunctionNode;
|
|
928
|
+
|
|
929
|
+
/**
|
|
930
|
+
* Creates a COUNT function expression
|
|
931
|
+
* @param col - Column to count
|
|
932
|
+
* @returns Function node with COUNT
|
|
933
|
+
*/
|
|
934
|
+
declare const count: (col: ColumnRef | ColumnNode) => FunctionNode;
|
|
935
|
+
/**
|
|
936
|
+
* Creates a SUM function expression
|
|
937
|
+
* @param col - Column to sum
|
|
938
|
+
* @returns Function node with SUM
|
|
939
|
+
*/
|
|
940
|
+
declare const sum: (col: ColumnRef | ColumnNode) => FunctionNode;
|
|
941
|
+
/**
|
|
942
|
+
* Creates an AVG function expression
|
|
943
|
+
* @param col - Column to average
|
|
944
|
+
* @returns Function node with AVG
|
|
945
|
+
*/
|
|
946
|
+
declare const avg: (col: ColumnRef | ColumnNode) => FunctionNode;
|
|
947
|
+
/**
|
|
948
|
+
* Creates a MIN function expression
|
|
949
|
+
* @param col - Column to take the minimum of
|
|
950
|
+
* @returns Function node with MIN
|
|
951
|
+
*/
|
|
952
|
+
declare const min: (col: ColumnRef | ColumnNode) => FunctionNode;
|
|
953
|
+
/**
|
|
954
|
+
* Creates a MAX function expression
|
|
955
|
+
* @param col - Column to take the maximum of
|
|
956
|
+
* @returns Function node with MAX
|
|
957
|
+
*/
|
|
958
|
+
declare const max: (col: ColumnRef | ColumnNode) => FunctionNode;
|
|
959
|
+
type GroupConcatOrderByInput = {
|
|
960
|
+
column: ColumnRef | ColumnNode;
|
|
961
|
+
direction?: OrderDirection;
|
|
962
|
+
};
|
|
963
|
+
type GroupConcatOptions = {
|
|
964
|
+
separator?: ValueOperandInput;
|
|
965
|
+
orderBy?: GroupConcatOrderByInput[];
|
|
966
|
+
};
|
|
967
|
+
/**
|
|
968
|
+
* Aggregates grouped strings into a single value.
|
|
969
|
+
*/
|
|
970
|
+
declare const groupConcat: (col: ColumnRef | ColumnNode, options?: GroupConcatOptions) => FunctionNode;
|
|
971
|
+
|
|
972
|
+
/**
|
|
973
|
+
* Visitor for expression nodes
|
|
974
|
+
*/
|
|
975
|
+
interface ExpressionVisitor<R> {
|
|
976
|
+
visitBinaryExpression?(node: BinaryExpressionNode): R;
|
|
977
|
+
visitLogicalExpression?(node: LogicalExpressionNode): R;
|
|
978
|
+
visitNullExpression?(node: NullExpressionNode): R;
|
|
979
|
+
visitInExpression?(node: InExpressionNode): R;
|
|
980
|
+
visitExistsExpression?(node: ExistsExpressionNode): R;
|
|
981
|
+
visitBetweenExpression?(node: BetweenExpressionNode): R;
|
|
982
|
+
otherwise?(node: ExpressionNode): R;
|
|
983
|
+
}
|
|
984
|
+
/**
|
|
985
|
+
* Visitor for operand nodes
|
|
986
|
+
*/
|
|
987
|
+
interface OperandVisitor<R> {
|
|
988
|
+
visitColumn?(node: ColumnNode): R;
|
|
989
|
+
visitLiteral?(node: LiteralNode): R;
|
|
990
|
+
visitFunction?(node: FunctionNode): R;
|
|
991
|
+
visitJsonPath?(node: JsonPathNode): R;
|
|
992
|
+
visitScalarSubquery?(node: ScalarSubqueryNode): R;
|
|
993
|
+
visitCaseExpression?(node: CaseExpressionNode): R;
|
|
994
|
+
visitWindowFunction?(node: WindowFunctionNode): R;
|
|
995
|
+
otherwise?(node: OperandNode): R;
|
|
996
|
+
}
|
|
997
|
+
type ExpressionDispatch = <R>(node: any, visitor: ExpressionVisitor<R>) => R;
|
|
998
|
+
type OperandDispatch = <R>(node: any, visitor: OperandVisitor<R>) => R;
|
|
999
|
+
/**
|
|
1000
|
+
* Registers a dispatcher for a custom expression node type.
|
|
1001
|
+
* Allows new node kinds without modifying the core switch.
|
|
1002
|
+
*/
|
|
1003
|
+
declare const registerExpressionDispatcher: (type: string, dispatcher: ExpressionDispatch) => void;
|
|
1004
|
+
/**
|
|
1005
|
+
* Registers a dispatcher for a custom operand node type.
|
|
1006
|
+
* Allows new node kinds without modifying the core switch.
|
|
1007
|
+
*/
|
|
1008
|
+
declare const registerOperandDispatcher: (type: string, dispatcher: OperandDispatch) => void;
|
|
1009
|
+
/**
|
|
1010
|
+
* Clears all registered dispatchers. Primarily for tests.
|
|
1011
|
+
*/
|
|
1012
|
+
declare const clearExpressionDispatchers: () => void;
|
|
1013
|
+
declare const clearOperandDispatchers: () => void;
|
|
1014
|
+
/**
|
|
1015
|
+
* Dispatches an expression node to the visitor
|
|
1016
|
+
* @param node - Expression node to visit
|
|
1017
|
+
* @param visitor - Visitor implementation
|
|
1018
|
+
*/
|
|
1019
|
+
declare const visitExpression: <R>(node: ExpressionNode, visitor: ExpressionVisitor<R>) => R;
|
|
1020
|
+
/**
|
|
1021
|
+
* Dispatches an operand node to the visitor
|
|
1022
|
+
* @param node - Operand node to visit
|
|
1023
|
+
* @param visitor - Visitor implementation
|
|
1024
|
+
*/
|
|
1025
|
+
declare const visitOperand: <R>(node: OperandNode, visitor: OperandVisitor<R>) => R;
|
|
1026
|
+
|
|
1027
|
+
/**
|
|
1028
|
+
* Adapts a schema ColumnDef to an AST-friendly ColumnRef.
|
|
1029
|
+
*/
|
|
1030
|
+
declare const toColumnRef: (col: ColumnRef | ColumnDef) => ColumnRef;
|
|
1031
|
+
/**
|
|
1032
|
+
* Adapts a schema TableDef to an AST-friendly TableRef.
|
|
1033
|
+
*/
|
|
1034
|
+
declare const toTableRef: (table: TableRef | TableDef) => TableRef;
|
|
1035
|
+
|
|
1036
|
+
/**
|
|
1037
|
+
* AST node representing a JOIN clause
|
|
1038
|
+
*/
|
|
1039
|
+
interface JoinNode {
|
|
1040
|
+
type: 'Join';
|
|
1041
|
+
/** Type of join (INNER, LEFT, RIGHT, etc.) */
|
|
1042
|
+
kind: JoinKind;
|
|
1043
|
+
/** Table to join */
|
|
1044
|
+
table: TableSourceNode;
|
|
1045
|
+
/** Join condition expression */
|
|
1046
|
+
condition: ExpressionNode;
|
|
1047
|
+
/** Optional metadata for non-SQL concerns (e.g., relation name) */
|
|
1048
|
+
meta?: Record<string, unknown>;
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
/**
|
|
1052
|
+
* AST node representing a table reference in a query
|
|
1053
|
+
*/
|
|
1054
|
+
interface TableNode {
|
|
1055
|
+
type: 'Table';
|
|
1056
|
+
/** Table name */
|
|
1057
|
+
name: string;
|
|
1058
|
+
/** Optional schema name */
|
|
1059
|
+
schema?: string;
|
|
1060
|
+
/** Optional table alias */
|
|
1061
|
+
alias?: string;
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* AST node representing a function used as a table source (table-valued function)
|
|
1065
|
+
*/
|
|
1066
|
+
interface FunctionTableNode {
|
|
1067
|
+
type: 'FunctionTable';
|
|
1068
|
+
/** Function name */
|
|
1069
|
+
name: string;
|
|
1070
|
+
/** Optional schema for the function (some dialects) */
|
|
1071
|
+
schema?: string;
|
|
1072
|
+
/** Function arguments as operand nodes */
|
|
1073
|
+
args?: any[];
|
|
1074
|
+
/** Optional alias for the function table */
|
|
1075
|
+
alias?: string;
|
|
1076
|
+
/** LATERAL flag */
|
|
1077
|
+
lateral?: boolean;
|
|
1078
|
+
/** WITH ORDINALITY flag */
|
|
1079
|
+
withOrdinality?: boolean;
|
|
1080
|
+
/** Optional column aliases */
|
|
1081
|
+
columnAliases?: string[];
|
|
1082
|
+
}
|
|
1083
|
+
/**
|
|
1084
|
+
* AST node representing a derived table (subquery with an alias)
|
|
1085
|
+
*/
|
|
1086
|
+
interface DerivedTableNode {
|
|
1087
|
+
type: 'DerivedTable';
|
|
1088
|
+
/** Subquery providing the rows */
|
|
1089
|
+
query: SelectQueryNode;
|
|
1090
|
+
/** Required alias for the derived table */
|
|
1091
|
+
alias: string;
|
|
1092
|
+
/** Optional column aliases */
|
|
1093
|
+
columnAliases?: string[];
|
|
1094
|
+
}
|
|
1095
|
+
type TableSourceNode = TableNode | FunctionTableNode | DerivedTableNode;
|
|
1096
|
+
/**
|
|
1097
|
+
* AST node representing an ORDER BY clause
|
|
1098
|
+
*/
|
|
1099
|
+
interface OrderByNode {
|
|
1100
|
+
type: 'OrderBy';
|
|
1101
|
+
/** Column to order by */
|
|
1102
|
+
column: ColumnNode;
|
|
1103
|
+
/** Order direction (ASC or DESC) */
|
|
1104
|
+
direction: OrderDirection;
|
|
1105
|
+
}
|
|
1106
|
+
/**
|
|
1107
|
+
* AST node representing a Common Table Expression (CTE)
|
|
1108
|
+
*/
|
|
1109
|
+
interface CommonTableExpressionNode {
|
|
1110
|
+
type: 'CommonTableExpression';
|
|
1111
|
+
/** CTE name */
|
|
1112
|
+
name: string;
|
|
1113
|
+
/** Optional column names */
|
|
1114
|
+
columns?: string[];
|
|
1115
|
+
/** CTE query */
|
|
1116
|
+
query: SelectQueryNode;
|
|
1117
|
+
/** Whether the CTE is recursive */
|
|
1118
|
+
recursive: boolean;
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Supported set operation kinds for compound SELECT queries
|
|
1122
|
+
*/
|
|
1123
|
+
type SetOperationKind = 'UNION' | 'UNION ALL' | 'INTERSECT' | 'EXCEPT';
|
|
1124
|
+
/**
|
|
1125
|
+
* AST node representing a set operation (UNION, INTERSECT, etc.)
|
|
1126
|
+
*/
|
|
1127
|
+
interface SetOperationNode {
|
|
1128
|
+
type: 'SetOperation';
|
|
1129
|
+
/** Operator to combine queries */
|
|
1130
|
+
operator: SetOperationKind;
|
|
1131
|
+
/** Right-hand query in the compound expression */
|
|
1132
|
+
query: SelectQueryNode;
|
|
1133
|
+
}
|
|
1134
|
+
/**
|
|
1135
|
+
* AST node representing a complete SELECT query
|
|
1136
|
+
*/
|
|
1137
|
+
interface SelectQueryNode {
|
|
1138
|
+
type: 'SelectQuery';
|
|
1139
|
+
/** Optional CTEs (WITH clauses) */
|
|
1140
|
+
ctes?: CommonTableExpressionNode[];
|
|
1141
|
+
/** FROM clause table (either a Table or a FunctionTable) */
|
|
1142
|
+
from: TableSourceNode;
|
|
1143
|
+
/** SELECT clause columns */
|
|
1144
|
+
columns: (ColumnNode | FunctionNode | ScalarSubqueryNode | CaseExpressionNode | WindowFunctionNode)[];
|
|
1145
|
+
/** JOIN clauses */
|
|
1146
|
+
joins: JoinNode[];
|
|
1147
|
+
/** Optional WHERE clause */
|
|
1148
|
+
where?: ExpressionNode;
|
|
1149
|
+
/** Optional GROUP BY clause */
|
|
1150
|
+
groupBy?: ColumnNode[];
|
|
1151
|
+
/** Optional HAVING clause */
|
|
1152
|
+
having?: ExpressionNode;
|
|
1153
|
+
/** Optional ORDER BY clause */
|
|
1154
|
+
orderBy?: OrderByNode[];
|
|
1155
|
+
/** Optional LIMIT clause */
|
|
1156
|
+
limit?: number;
|
|
1157
|
+
/** Optional OFFSET clause */
|
|
1158
|
+
offset?: number;
|
|
1159
|
+
/** Optional query metadata */
|
|
1160
|
+
meta?: Record<string, unknown>;
|
|
1161
|
+
/** Optional DISTINCT clause */
|
|
1162
|
+
distinct?: ColumnNode[];
|
|
1163
|
+
/** Optional set operations chaining this query with others */
|
|
1164
|
+
setOps?: SetOperationNode[];
|
|
1165
|
+
}
|
|
1166
|
+
interface InsertQueryNode {
|
|
1167
|
+
type: 'InsertQuery';
|
|
1168
|
+
/** Target table */
|
|
1169
|
+
into: TableNode;
|
|
1170
|
+
/** Column order for inserted values */
|
|
1171
|
+
columns: ColumnNode[];
|
|
1172
|
+
/** Rows of values to insert */
|
|
1173
|
+
values: OperandNode[][];
|
|
1174
|
+
/** Optional RETURNING clause */
|
|
1175
|
+
returning?: ColumnNode[];
|
|
1176
|
+
}
|
|
1177
|
+
interface UpdateAssignmentNode {
|
|
1178
|
+
/** Column to update */
|
|
1179
|
+
column: ColumnNode;
|
|
1180
|
+
/** Value to set */
|
|
1181
|
+
value: OperandNode;
|
|
1182
|
+
}
|
|
1183
|
+
interface UpdateQueryNode {
|
|
1184
|
+
type: 'UpdateQuery';
|
|
1185
|
+
/** Table being updated */
|
|
1186
|
+
table: TableNode;
|
|
1187
|
+
/** Assignments for SET clause */
|
|
1188
|
+
set: UpdateAssignmentNode[];
|
|
1189
|
+
/** Optional WHERE clause */
|
|
1190
|
+
where?: ExpressionNode;
|
|
1191
|
+
/** Optional RETURNING clause */
|
|
1192
|
+
returning?: ColumnNode[];
|
|
1193
|
+
}
|
|
1194
|
+
interface DeleteQueryNode {
|
|
1195
|
+
type: 'DeleteQuery';
|
|
1196
|
+
/** Table to delete from */
|
|
1197
|
+
from: TableNode;
|
|
1198
|
+
/** Optional WHERE clause */
|
|
1199
|
+
where?: ExpressionNode;
|
|
1200
|
+
/** Optional RETURNING clause */
|
|
1201
|
+
returning?: ColumnNode[];
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
/**
|
|
1205
|
+
* Plan describing pivot columns needed for hydration
|
|
1206
|
+
*/
|
|
1207
|
+
interface HydrationPivotPlan {
|
|
1208
|
+
table: string;
|
|
1209
|
+
primaryKey: string;
|
|
1210
|
+
aliasPrefix: string;
|
|
1211
|
+
columns: string[];
|
|
1212
|
+
}
|
|
1213
|
+
/**
|
|
1214
|
+
* Plan for hydrating relationship data
|
|
1215
|
+
*/
|
|
1216
|
+
interface HydrationRelationPlan {
|
|
1217
|
+
/** Name of the relationship */
|
|
1218
|
+
name: string;
|
|
1219
|
+
/** Alias prefix for the relationship */
|
|
1220
|
+
aliasPrefix: string;
|
|
1221
|
+
/** Type of relationship */
|
|
1222
|
+
type: RelationType;
|
|
1223
|
+
/** Target table name */
|
|
1224
|
+
targetTable: string;
|
|
1225
|
+
/** Target table primary key */
|
|
1226
|
+
targetPrimaryKey: string;
|
|
1227
|
+
/** Foreign key column */
|
|
1228
|
+
foreignKey: string;
|
|
1229
|
+
/** Local key column */
|
|
1230
|
+
localKey: string;
|
|
1231
|
+
/** Columns to include */
|
|
1232
|
+
columns: string[];
|
|
1233
|
+
/** Optional pivot plan for many-to-many relationships */
|
|
1234
|
+
pivot?: HydrationPivotPlan;
|
|
1235
|
+
}
|
|
1236
|
+
/**
|
|
1237
|
+
* Complete hydration plan for a query
|
|
1238
|
+
*/
|
|
1239
|
+
interface HydrationPlan {
|
|
1240
|
+
/** Root table name */
|
|
1241
|
+
rootTable: string;
|
|
1242
|
+
/** Root table primary key */
|
|
1243
|
+
rootPrimaryKey: string;
|
|
1244
|
+
/** Root table columns */
|
|
1245
|
+
rootColumns: string[];
|
|
1246
|
+
/** Relationship hydration plans */
|
|
1247
|
+
relations: HydrationRelationPlan[];
|
|
1248
|
+
}
|
|
1249
|
+
/**
|
|
1250
|
+
* Metadata bag for attaching hydration to query ASTs without coupling AST types.
|
|
1251
|
+
*/
|
|
1252
|
+
interface HydrationMetadata {
|
|
1253
|
+
hydration?: HydrationPlan;
|
|
1254
|
+
[key: string]: unknown;
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
interface FunctionRenderContext {
|
|
1258
|
+
node: FunctionNode;
|
|
1259
|
+
compiledArgs: string[];
|
|
1260
|
+
/** Helper to compile additional operands (e.g., separators or ORDER BY columns) */
|
|
1261
|
+
compileOperand: (operand: OperandNode) => string;
|
|
1262
|
+
}
|
|
1263
|
+
type FunctionRenderer = (ctx: FunctionRenderContext) => string;
|
|
1264
|
+
interface FunctionStrategy {
|
|
1265
|
+
/**
|
|
1266
|
+
* Returns a renderer for a specific function name (e.g. "DATE_ADD").
|
|
1267
|
+
* Returns undefined if this dialect doesn't support the function.
|
|
1268
|
+
*/
|
|
1269
|
+
getRenderer(functionName: string): FunctionRenderer | undefined;
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
/**
|
|
1273
|
+
* Context for SQL compilation with parameter management
|
|
1274
|
+
*/
|
|
1275
|
+
interface CompilerContext {
|
|
1276
|
+
/** Array of parameters */
|
|
1277
|
+
params: unknown[];
|
|
1278
|
+
/** Function to add a parameter and get its placeholder */
|
|
1279
|
+
addParameter(value: unknown): string;
|
|
1280
|
+
}
|
|
1281
|
+
/**
|
|
1282
|
+
* Result of SQL compilation
|
|
1283
|
+
*/
|
|
1284
|
+
interface CompiledQuery {
|
|
1285
|
+
/** Generated SQL string */
|
|
1286
|
+
sql: string;
|
|
1287
|
+
/** Parameters for the query */
|
|
1288
|
+
params: unknown[];
|
|
1289
|
+
}
|
|
1290
|
+
interface SelectCompiler {
|
|
1291
|
+
compileSelect(ast: SelectQueryNode): CompiledQuery;
|
|
1292
|
+
}
|
|
1293
|
+
interface InsertCompiler {
|
|
1294
|
+
compileInsert(ast: InsertQueryNode): CompiledQuery;
|
|
1295
|
+
}
|
|
1296
|
+
interface UpdateCompiler {
|
|
1297
|
+
compileUpdate(ast: UpdateQueryNode): CompiledQuery;
|
|
1298
|
+
}
|
|
1299
|
+
interface DeleteCompiler {
|
|
1300
|
+
compileDelete(ast: DeleteQueryNode): CompiledQuery;
|
|
1301
|
+
}
|
|
1302
|
+
/**
|
|
1303
|
+
* Abstract base class for SQL dialect implementations
|
|
1304
|
+
*/
|
|
1305
|
+
declare abstract class Dialect implements SelectCompiler, InsertCompiler, UpdateCompiler, DeleteCompiler {
|
|
1306
|
+
/** Dialect identifier used for function rendering and formatting */
|
|
1307
|
+
protected abstract readonly dialect: DialectName$1;
|
|
1308
|
+
/**
|
|
1309
|
+
* Compiles a SELECT query AST to SQL
|
|
1310
|
+
* @param ast - Query AST to compile
|
|
1311
|
+
* @returns Compiled query with SQL and parameters
|
|
1312
|
+
*/
|
|
1313
|
+
compileSelect(ast: SelectQueryNode): CompiledQuery;
|
|
1314
|
+
compileInsert(ast: InsertQueryNode): CompiledQuery;
|
|
1315
|
+
compileUpdate(ast: UpdateQueryNode): CompiledQuery;
|
|
1316
|
+
compileDelete(ast: DeleteQueryNode): CompiledQuery;
|
|
1317
|
+
supportsReturning(): boolean;
|
|
1318
|
+
/**
|
|
1319
|
+
* Compiles SELECT query AST to SQL (to be implemented by concrete dialects)
|
|
1320
|
+
* @param ast - Query AST
|
|
1321
|
+
* @param ctx - Compiler context
|
|
1322
|
+
* @returns SQL string
|
|
1323
|
+
*/
|
|
1324
|
+
protected abstract compileSelectAst(ast: SelectQueryNode, ctx: CompilerContext): string;
|
|
1325
|
+
protected abstract compileInsertAst(ast: InsertQueryNode, ctx: CompilerContext): string;
|
|
1326
|
+
protected abstract compileUpdateAst(ast: UpdateQueryNode, ctx: CompilerContext): string;
|
|
1327
|
+
protected abstract compileDeleteAst(ast: DeleteQueryNode, ctx: CompilerContext): string;
|
|
1328
|
+
/**
|
|
1329
|
+
* Quotes an SQL identifier (to be implemented by concrete dialects)
|
|
1330
|
+
* @param id - Identifier to quote
|
|
1331
|
+
* @returns Quoted identifier
|
|
1332
|
+
*/
|
|
1333
|
+
abstract quoteIdentifier(id: string): string;
|
|
1334
|
+
/**
|
|
1335
|
+
* Compiles a WHERE clause
|
|
1336
|
+
* @param where - WHERE expression
|
|
1337
|
+
* @param ctx - Compiler context
|
|
1338
|
+
* @returns SQL WHERE clause or empty string
|
|
1339
|
+
*/
|
|
1340
|
+
protected compileWhere(where: ExpressionNode | undefined, ctx: CompilerContext): string;
|
|
1341
|
+
protected compileReturning(returning: ColumnNode[] | undefined, ctx: CompilerContext): string;
|
|
1342
|
+
/**
|
|
1343
|
+
* Generates subquery for EXISTS expressions
|
|
1344
|
+
* Rule: Always forces SELECT 1, ignoring column list
|
|
1345
|
+
* Maintains FROM, JOINs, WHERE, GROUP BY, ORDER BY, LIMIT/OFFSET
|
|
1346
|
+
* Does not add ';' at the end
|
|
1347
|
+
* @param ast - Query AST
|
|
1348
|
+
* @param ctx - Compiler context
|
|
1349
|
+
* @returns SQL for EXISTS subquery
|
|
1350
|
+
*/
|
|
1351
|
+
protected compileSelectForExists(ast: SelectQueryNode, ctx: CompilerContext): string;
|
|
1352
|
+
/**
|
|
1353
|
+
* Creates a new compiler context
|
|
1354
|
+
* @returns Compiler context with parameter management
|
|
1355
|
+
*/
|
|
1356
|
+
protected createCompilerContext(): CompilerContext;
|
|
1357
|
+
/**
|
|
1358
|
+
* Formats a parameter placeholder
|
|
1359
|
+
* @param index - Parameter index
|
|
1360
|
+
* @returns Formatted placeholder string
|
|
1361
|
+
*/
|
|
1362
|
+
protected formatPlaceholder(index: number): string;
|
|
1363
|
+
/**
|
|
1364
|
+
* Whether the current dialect supports a given set operation.
|
|
1365
|
+
* Override in concrete dialects to restrict support.
|
|
1366
|
+
*/
|
|
1367
|
+
protected supportsSetOperation(kind: SetOperationKind): boolean;
|
|
1368
|
+
/**
|
|
1369
|
+
* Validates set-operation semantics:
|
|
1370
|
+
* - Ensures the dialect supports requested operators.
|
|
1371
|
+
* - Enforces that only the outermost compound query may have ORDER/LIMIT/OFFSET.
|
|
1372
|
+
* @param ast - Query to validate
|
|
1373
|
+
* @param isOutermost - Whether this node is the outermost compound query
|
|
1374
|
+
*/
|
|
1375
|
+
protected validateSetOperations(ast: SelectQueryNode, isOutermost?: boolean): void;
|
|
1376
|
+
/**
|
|
1377
|
+
* Hoists CTEs from set-operation operands to the outermost query so WITH appears once.
|
|
1378
|
+
* @param ast - Query AST
|
|
1379
|
+
* @returns Normalized AST without inner CTEs and a list of hoisted CTEs
|
|
1380
|
+
*/
|
|
1381
|
+
private hoistCtes;
|
|
1382
|
+
/**
|
|
1383
|
+
* Normalizes a SELECT AST before compilation (validation + CTE hoisting).
|
|
1384
|
+
* @param ast - Query AST
|
|
1385
|
+
* @returns Normalized query AST
|
|
1386
|
+
*/
|
|
1387
|
+
protected normalizeSelectAst(ast: SelectQueryNode): SelectQueryNode;
|
|
1388
|
+
private readonly expressionCompilers;
|
|
1389
|
+
private readonly operandCompilers;
|
|
1390
|
+
protected readonly functionStrategy: FunctionStrategy;
|
|
1391
|
+
protected constructor(functionStrategy?: FunctionStrategy);
|
|
1392
|
+
/**
|
|
1393
|
+
* Creates a new Dialect instance (for testing purposes)
|
|
1394
|
+
* @param functionStrategy - Optional function strategy
|
|
1395
|
+
* @returns New Dialect instance
|
|
1396
|
+
*/
|
|
1397
|
+
static create(functionStrategy?: FunctionStrategy): Dialect;
|
|
1398
|
+
/**
|
|
1399
|
+
* Registers an expression compiler for a specific node type
|
|
1400
|
+
* @param type - Expression node type
|
|
1401
|
+
* @param compiler - Compiler function
|
|
1402
|
+
*/
|
|
1403
|
+
protected registerExpressionCompiler<T extends ExpressionNode>(type: T['type'], compiler: (node: T, ctx: CompilerContext) => string): void;
|
|
1404
|
+
/**
|
|
1405
|
+
* Registers an operand compiler for a specific node type
|
|
1406
|
+
* @param type - Operand node type
|
|
1407
|
+
* @param compiler - Compiler function
|
|
1408
|
+
*/
|
|
1409
|
+
protected registerOperandCompiler<T extends OperandNode>(type: T['type'], compiler: (node: T, ctx: CompilerContext) => string): void;
|
|
1410
|
+
/**
|
|
1411
|
+
* Compiles an expression node
|
|
1412
|
+
* @param node - Expression node to compile
|
|
1413
|
+
* @param ctx - Compiler context
|
|
1414
|
+
* @returns Compiled SQL expression
|
|
1415
|
+
*/
|
|
1416
|
+
protected compileExpression(node: ExpressionNode, ctx: CompilerContext): string;
|
|
1417
|
+
/**
|
|
1418
|
+
* Compiles an operand node
|
|
1419
|
+
* @param node - Operand node to compile
|
|
1420
|
+
* @param ctx - Compiler context
|
|
1421
|
+
* @returns Compiled SQL operand
|
|
1422
|
+
*/
|
|
1423
|
+
protected compileOperand(node: OperandNode, ctx: CompilerContext): string;
|
|
1424
|
+
private registerDefaultExpressionCompilers;
|
|
1425
|
+
private registerDefaultOperandCompilers;
|
|
1426
|
+
protected compileJsonPath(node: JsonPathNode): string;
|
|
1427
|
+
/**
|
|
1428
|
+
* Compiles a function operand, using the dialect's function strategy.
|
|
1429
|
+
*/
|
|
1430
|
+
protected compileFunctionOperand(fnNode: FunctionNode, ctx: CompilerContext): string;
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
type DialectKey = 'postgres' | 'mysql' | 'sqlite' | 'mssql' | (string & {});
|
|
1434
|
+
|
|
1435
|
+
/**
|
|
1436
|
+
* Node types that can be used in query projections
|
|
1437
|
+
*/
|
|
1438
|
+
type ProjectionNode = ColumnNode | FunctionNode | ScalarSubqueryNode | CaseExpressionNode | WindowFunctionNode;
|
|
1439
|
+
/**
|
|
1440
|
+
* Manages the state of a SELECT query being built
|
|
1441
|
+
*/
|
|
1442
|
+
declare class SelectQueryState {
|
|
1443
|
+
/**
|
|
1444
|
+
* Table definition for the query
|
|
1445
|
+
*/
|
|
1446
|
+
readonly table: TableDef;
|
|
1447
|
+
/**
|
|
1448
|
+
* Abstract Syntax Tree (AST) representation of the query
|
|
1449
|
+
*/
|
|
1450
|
+
readonly ast: SelectQueryNode;
|
|
1451
|
+
/**
|
|
1452
|
+
* Creates a new SelectQueryState instance
|
|
1453
|
+
* @param table - Table definition
|
|
1454
|
+
* @param ast - Optional existing AST
|
|
1455
|
+
*/
|
|
1456
|
+
constructor(table: TableDef, ast?: SelectQueryNode);
|
|
1457
|
+
/**
|
|
1458
|
+
* Creates a new SelectQueryState with updated AST
|
|
1459
|
+
* @param nextAst - Updated AST
|
|
1460
|
+
* @returns New SelectQueryState instance
|
|
1461
|
+
*/
|
|
1462
|
+
private clone;
|
|
1463
|
+
/**
|
|
1464
|
+
* Adds columns to the query
|
|
1465
|
+
* @param newCols - Columns to add
|
|
1466
|
+
* @returns New SelectQueryState with added columns
|
|
1467
|
+
*/
|
|
1468
|
+
withColumns(newCols: ProjectionNode[]): SelectQueryState;
|
|
1469
|
+
/**
|
|
1470
|
+
* Adds a join to the query
|
|
1471
|
+
* @param join - Join node to add
|
|
1472
|
+
* @returns New SelectQueryState with added join
|
|
1473
|
+
*/
|
|
1474
|
+
withJoin(join: JoinNode): SelectQueryState;
|
|
1475
|
+
/**
|
|
1476
|
+
* Replaces the FROM clause.
|
|
1477
|
+
* @param from - Table source for the FROM clause
|
|
1478
|
+
* @returns New SelectQueryState with updated FROM
|
|
1479
|
+
*/
|
|
1480
|
+
withFrom(from: TableSourceNode): SelectQueryState;
|
|
1481
|
+
/**
|
|
1482
|
+
* Adds a WHERE clause to the query
|
|
1483
|
+
* @param predicate - WHERE predicate expression
|
|
1484
|
+
* @returns New SelectQueryState with WHERE clause
|
|
1485
|
+
*/
|
|
1486
|
+
withWhere(predicate: ExpressionNode): SelectQueryState;
|
|
1487
|
+
/**
|
|
1488
|
+
* Adds a HAVING clause to the query
|
|
1489
|
+
* @param predicate - HAVING predicate expression
|
|
1490
|
+
* @returns New SelectQueryState with HAVING clause
|
|
1491
|
+
*/
|
|
1492
|
+
withHaving(predicate: ExpressionNode): SelectQueryState;
|
|
1493
|
+
/**
|
|
1494
|
+
* Adds GROUP BY columns to the query
|
|
1495
|
+
* @param columns - Columns to group by
|
|
1496
|
+
* @returns New SelectQueryState with GROUP BY clause
|
|
1497
|
+
*/
|
|
1498
|
+
withGroupBy(columns: ColumnNode[]): SelectQueryState;
|
|
1499
|
+
/**
|
|
1500
|
+
* Adds ORDER BY clauses to the query
|
|
1501
|
+
* @param orderBy - ORDER BY nodes
|
|
1502
|
+
* @returns New SelectQueryState with ORDER BY clause
|
|
1503
|
+
*/
|
|
1504
|
+
withOrderBy(orderBy: OrderByNode[]): SelectQueryState;
|
|
1505
|
+
/**
|
|
1506
|
+
* Adds DISTINCT columns to the query
|
|
1507
|
+
* @param columns - Columns to make distinct
|
|
1508
|
+
* @returns New SelectQueryState with DISTINCT clause
|
|
1509
|
+
*/
|
|
1510
|
+
withDistinct(columns: ColumnNode[]): SelectQueryState;
|
|
1511
|
+
/**
|
|
1512
|
+
* Adds a LIMIT clause to the query
|
|
1513
|
+
* @param limit - Maximum number of rows to return
|
|
1514
|
+
* @returns New SelectQueryState with LIMIT clause
|
|
1515
|
+
*/
|
|
1516
|
+
withLimit(limit: number): SelectQueryState;
|
|
1517
|
+
/**
|
|
1518
|
+
* Adds an OFFSET clause to the query
|
|
1519
|
+
* @param offset - Number of rows to skip
|
|
1520
|
+
* @returns New SelectQueryState with OFFSET clause
|
|
1521
|
+
*/
|
|
1522
|
+
withOffset(offset: number): SelectQueryState;
|
|
1523
|
+
/**
|
|
1524
|
+
* Adds a Common Table Expression (CTE) to the query
|
|
1525
|
+
* @param cte - CTE node to add
|
|
1526
|
+
* @returns New SelectQueryState with CTE
|
|
1527
|
+
*/
|
|
1528
|
+
withCte(cte: CommonTableExpressionNode): SelectQueryState;
|
|
1529
|
+
/**
|
|
1530
|
+
* Adds a set operation (UNION/INTERSECT/EXCEPT) to the query
|
|
1531
|
+
* @param op - Set operation node to add
|
|
1532
|
+
* @returns New SelectQueryState with set operation
|
|
1533
|
+
*/
|
|
1534
|
+
withSetOperation(op: SetOperationNode): SelectQueryState;
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
/**
|
|
1538
|
+
* Manages hydration planning for query results
|
|
1539
|
+
*/
|
|
1540
|
+
declare class HydrationPlanner {
|
|
1541
|
+
private readonly table;
|
|
1542
|
+
private readonly plan?;
|
|
1543
|
+
/**
|
|
1544
|
+
* Creates a new HydrationPlanner instance
|
|
1545
|
+
* @param table - Table definition
|
|
1546
|
+
* @param plan - Optional existing hydration plan
|
|
1547
|
+
*/
|
|
1548
|
+
constructor(table: TableDef, plan?: HydrationPlan);
|
|
1549
|
+
/**
|
|
1550
|
+
* Captures root table columns for hydration planning
|
|
1551
|
+
* @param columns - Columns to capture
|
|
1552
|
+
* @returns Updated HydrationPlanner with captured columns
|
|
1553
|
+
*/
|
|
1554
|
+
captureRootColumns(columns: ProjectionNode[]): HydrationPlanner;
|
|
1555
|
+
/**
|
|
1556
|
+
* Includes a relation in the hydration plan
|
|
1557
|
+
* @param rel - Relation definition
|
|
1558
|
+
* @param relationName - Name of the relation
|
|
1559
|
+
* @param aliasPrefix - Alias prefix for relation columns
|
|
1560
|
+
* @param columns - Columns to include from the relation
|
|
1561
|
+
* @returns Updated HydrationPlanner with included relation
|
|
1562
|
+
*/
|
|
1563
|
+
includeRelation(rel: RelationDef, relationName: string, aliasPrefix: string, columns: string[], pivot?: {
|
|
1564
|
+
aliasPrefix: string;
|
|
1565
|
+
columns: string[];
|
|
1566
|
+
}): HydrationPlanner;
|
|
1567
|
+
/**
|
|
1568
|
+
* Gets the current hydration plan
|
|
1569
|
+
* @returns Current hydration plan or undefined
|
|
1570
|
+
*/
|
|
1571
|
+
getPlan(): HydrationPlan | undefined;
|
|
1572
|
+
/**
|
|
1573
|
+
* Gets the current hydration plan or creates a default one
|
|
1574
|
+
* @returns Current hydration plan or default plan
|
|
1575
|
+
*/
|
|
1576
|
+
private getPlanOrDefault;
|
|
1577
|
+
/**
|
|
1578
|
+
* Builds a relation plan for hydration
|
|
1579
|
+
* @param rel - Relation definition
|
|
1580
|
+
* @param relationName - Name of the relation
|
|
1581
|
+
* @param aliasPrefix - Alias prefix for relation columns
|
|
1582
|
+
* @param columns - Columns to include from the relation
|
|
1583
|
+
* @returns Hydration relation plan
|
|
1584
|
+
*/
|
|
1585
|
+
private buildRelationPlan;
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
/**
|
|
1589
|
+
* Manages hydration planning for query results
|
|
1590
|
+
*/
|
|
1591
|
+
declare class HydrationManager {
|
|
1592
|
+
private readonly table;
|
|
1593
|
+
private readonly planner;
|
|
1594
|
+
/**
|
|
1595
|
+
* Creates a new HydrationManager instance
|
|
1596
|
+
* @param table - Table definition
|
|
1597
|
+
* @param planner - Hydration planner
|
|
1598
|
+
*/
|
|
1599
|
+
constructor(table: TableDef, planner: HydrationPlanner);
|
|
1600
|
+
/**
|
|
1601
|
+
* Creates a new HydrationManager with updated planner
|
|
1602
|
+
* @param nextPlanner - Updated hydration planner
|
|
1603
|
+
* @returns New HydrationManager instance
|
|
1604
|
+
*/
|
|
1605
|
+
private clone;
|
|
1606
|
+
/**
|
|
1607
|
+
* Handles column selection for hydration planning
|
|
1608
|
+
* @param state - Current query state
|
|
1609
|
+
* @param newColumns - Newly selected columns
|
|
1610
|
+
* @returns Updated HydrationManager with captured columns
|
|
1611
|
+
*/
|
|
1612
|
+
onColumnsSelected(state: SelectQueryState, newColumns: ProjectionNode[]): HydrationManager;
|
|
1613
|
+
/**
|
|
1614
|
+
* Handles relation inclusion for hydration planning
|
|
1615
|
+
* @param state - Current query state
|
|
1616
|
+
* @param relation - Relation definition
|
|
1617
|
+
* @param relationName - Name of the relation
|
|
1618
|
+
* @param aliasPrefix - Alias prefix for the relation
|
|
1619
|
+
* @param targetColumns - Target columns to include
|
|
1620
|
+
* @returns Updated HydrationManager with included relation
|
|
1621
|
+
*/
|
|
1622
|
+
onRelationIncluded(state: SelectQueryState, relation: RelationDef, relationName: string, aliasPrefix: string, targetColumns: string[], pivot?: {
|
|
1623
|
+
aliasPrefix: string;
|
|
1624
|
+
columns: string[];
|
|
1625
|
+
}): HydrationManager;
|
|
1626
|
+
/**
|
|
1627
|
+
* Applies hydration plan to the AST
|
|
1628
|
+
* @param ast - Query AST to modify
|
|
1629
|
+
* @returns AST with hydration metadata
|
|
1630
|
+
*/
|
|
1631
|
+
applyToAst(ast: SelectQueryNode): SelectQueryNode;
|
|
1632
|
+
/**
|
|
1633
|
+
* Gets the current hydration plan
|
|
1634
|
+
* @returns Hydration plan or undefined if none exists
|
|
1635
|
+
*/
|
|
1636
|
+
getPlan(): HydrationPlan | undefined;
|
|
1637
|
+
/**
|
|
1638
|
+
* Attaches hydration metadata to a query AST node.
|
|
1639
|
+
*/
|
|
1640
|
+
private attachHydrationMeta;
|
|
1641
|
+
/**
|
|
1642
|
+
* Determines whether the query needs pagination rewriting to keep LIMIT/OFFSET
|
|
1643
|
+
* applied to parent rows when eager-loading multiplicative relations.
|
|
1644
|
+
*/
|
|
1645
|
+
private requiresParentPagination;
|
|
1646
|
+
private hasMultiplyingRelations;
|
|
1647
|
+
/**
|
|
1648
|
+
* Rewrites the query using CTEs so LIMIT/OFFSET target distinct parent rows
|
|
1649
|
+
* instead of the joined result set.
|
|
1650
|
+
*
|
|
1651
|
+
* The strategy:
|
|
1652
|
+
* - Hoist the original query (minus limit/offset) into a base CTE.
|
|
1653
|
+
* - Select distinct parent ids from that base CTE with the original ordering and pagination.
|
|
1654
|
+
* - Join the base CTE against the paged ids to retrieve the joined rows for just that page.
|
|
1655
|
+
*/
|
|
1656
|
+
private wrapForParentPagination;
|
|
1657
|
+
private nextCteName;
|
|
1658
|
+
private getProjectionNames;
|
|
1659
|
+
private buildProjectionAliasMap;
|
|
1660
|
+
private mapOrderBy;
|
|
1661
|
+
private buildPagingColumns;
|
|
1662
|
+
}
|
|
1663
|
+
|
|
1664
|
+
/**
|
|
1665
|
+
* Result of column selection operation
|
|
1666
|
+
*/
|
|
1667
|
+
interface ColumnSelectionResult {
|
|
1668
|
+
/**
|
|
1669
|
+
* Updated query state
|
|
1670
|
+
*/
|
|
1671
|
+
state: SelectQueryState;
|
|
1672
|
+
/**
|
|
1673
|
+
* Columns that were added
|
|
1674
|
+
*/
|
|
1675
|
+
addedColumns: ProjectionNode[];
|
|
1676
|
+
}
|
|
1677
|
+
/**
|
|
1678
|
+
* Service for manipulating query AST (Abstract Syntax Tree)
|
|
1679
|
+
*/
|
|
1680
|
+
declare class QueryAstService {
|
|
1681
|
+
private readonly table;
|
|
1682
|
+
private readonly state;
|
|
1683
|
+
/**
|
|
1684
|
+
* Creates a new QueryAstService instance
|
|
1685
|
+
* @param table - Table definition
|
|
1686
|
+
* @param state - Current query state
|
|
1687
|
+
*/
|
|
1688
|
+
constructor(table: TableDef, state: SelectQueryState);
|
|
1689
|
+
/**
|
|
1690
|
+
* Selects columns for the query
|
|
1691
|
+
* @param columns - Columns to select (key: alias, value: column definition or expression)
|
|
1692
|
+
* @returns Column selection result with updated state and added columns
|
|
1693
|
+
*/
|
|
1694
|
+
select(columns: Record<string, ColumnDef | FunctionNode | CaseExpressionNode | WindowFunctionNode>): ColumnSelectionResult;
|
|
1695
|
+
/**
|
|
1696
|
+
* Selects raw column expressions (best-effort parser for simple references/functions)
|
|
1697
|
+
* @param cols - Raw column expressions
|
|
1698
|
+
* @returns Column selection result with updated state and added columns
|
|
1699
|
+
*/
|
|
1700
|
+
selectRaw(cols: string[]): ColumnSelectionResult;
|
|
1701
|
+
/**
|
|
1702
|
+
* Adds a Common Table Expression (CTE) to the query
|
|
1703
|
+
* @param name - Name of the CTE
|
|
1704
|
+
* @param query - Query for the CTE
|
|
1705
|
+
* @param columns - Optional column names for the CTE
|
|
1706
|
+
* @param recursive - Whether the CTE is recursive
|
|
1707
|
+
* @returns Updated query state with CTE
|
|
1708
|
+
*/
|
|
1709
|
+
withCte(name: string, query: SelectQueryNode, columns?: string[], recursive?: boolean): SelectQueryState;
|
|
1710
|
+
/**
|
|
1711
|
+
* Adds a set operation (UNION/UNION ALL/INTERSECT/EXCEPT) to the query
|
|
1712
|
+
* @param operator - Set operator
|
|
1713
|
+
* @param query - Right-hand side query
|
|
1714
|
+
* @returns Updated query state with set operation
|
|
1715
|
+
*/
|
|
1716
|
+
withSetOperation(operator: SetOperationKind, query: SelectQueryNode): SelectQueryState;
|
|
1717
|
+
/**
|
|
1718
|
+
* Replaces the FROM clause for the current query.
|
|
1719
|
+
* @param from - Table source to use in the FROM clause
|
|
1720
|
+
* @returns Updated query state with new FROM
|
|
1721
|
+
*/
|
|
1722
|
+
withFrom(from: TableSourceNode): SelectQueryState;
|
|
1723
|
+
/**
|
|
1724
|
+
* Selects a subquery as a column
|
|
1725
|
+
* @param alias - Alias for the subquery
|
|
1726
|
+
* @param query - Subquery to select
|
|
1727
|
+
* @returns Updated query state with subquery selection
|
|
1728
|
+
*/
|
|
1729
|
+
selectSubquery(alias: string, query: SelectQueryNode): SelectQueryState;
|
|
1730
|
+
/**
|
|
1731
|
+
* Adds a JOIN clause to the query
|
|
1732
|
+
* @param join - Join node to add
|
|
1733
|
+
* @returns Updated query state with JOIN
|
|
1734
|
+
*/
|
|
1735
|
+
withJoin(join: JoinNode): SelectQueryState;
|
|
1736
|
+
/**
|
|
1737
|
+
* Adds a WHERE clause to the query
|
|
1738
|
+
* @param expr - Expression for the WHERE clause
|
|
1739
|
+
* @returns Updated query state with WHERE clause
|
|
1740
|
+
*/
|
|
1741
|
+
withWhere(expr: ExpressionNode): SelectQueryState;
|
|
1742
|
+
/**
|
|
1743
|
+
* Adds a GROUP BY clause to the query
|
|
1744
|
+
* @param col - Column to group by
|
|
1745
|
+
* @returns Updated query state with GROUP BY clause
|
|
1746
|
+
*/
|
|
1747
|
+
withGroupBy(col: ColumnDef | ColumnNode): SelectQueryState;
|
|
1748
|
+
/**
|
|
1749
|
+
* Adds a HAVING clause to the query
|
|
1750
|
+
* @param expr - Expression for the HAVING clause
|
|
1751
|
+
* @returns Updated query state with HAVING clause
|
|
1752
|
+
*/
|
|
1753
|
+
withHaving(expr: ExpressionNode): SelectQueryState;
|
|
1754
|
+
/**
|
|
1755
|
+
* Adds an ORDER BY clause to the query
|
|
1756
|
+
* @param col - Column to order by
|
|
1757
|
+
* @param direction - Order direction (ASC/DESC)
|
|
1758
|
+
* @returns Updated query state with ORDER BY clause
|
|
1759
|
+
*/
|
|
1760
|
+
withOrderBy(col: ColumnDef | ColumnNode, direction: OrderDirection): SelectQueryState;
|
|
1761
|
+
/**
|
|
1762
|
+
* Adds a DISTINCT clause to the query
|
|
1763
|
+
* @param cols - Columns to make distinct
|
|
1764
|
+
* @returns Updated query state with DISTINCT clause
|
|
1765
|
+
*/
|
|
1766
|
+
withDistinct(cols: ColumnNode[]): SelectQueryState;
|
|
1767
|
+
/**
|
|
1768
|
+
* Adds a LIMIT clause to the query
|
|
1769
|
+
* @param limit - Maximum number of rows to return
|
|
1770
|
+
* @returns Updated query state with LIMIT clause
|
|
1771
|
+
*/
|
|
1772
|
+
withLimit(limit: number): SelectQueryState;
|
|
1773
|
+
/**
|
|
1774
|
+
* Adds an OFFSET clause to the query
|
|
1775
|
+
* @param offset - Number of rows to skip
|
|
1776
|
+
* @returns Updated query state with OFFSET clause
|
|
1777
|
+
*/
|
|
1778
|
+
withOffset(offset: number): SelectQueryState;
|
|
1779
|
+
/**
|
|
1780
|
+
* Combines expressions with AND operator
|
|
1781
|
+
* @param existing - Existing expression
|
|
1782
|
+
* @param next - New expression to combine
|
|
1783
|
+
* @returns Combined expression
|
|
1784
|
+
*/
|
|
1785
|
+
private combineExpressions;
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
/**
|
|
1789
|
+
* Result of a relation operation
|
|
1790
|
+
*/
|
|
1791
|
+
interface RelationResult {
|
|
1792
|
+
/**
|
|
1793
|
+
* Updated query state
|
|
1794
|
+
*/
|
|
1795
|
+
state: SelectQueryState;
|
|
1796
|
+
/**
|
|
1797
|
+
* Updated hydration manager
|
|
1798
|
+
*/
|
|
1799
|
+
hydration: HydrationManager;
|
|
1800
|
+
}
|
|
1801
|
+
|
|
1802
|
+
/**
|
|
1803
|
+
* Join kinds allowed when including a relation using `.include(...)`.
|
|
1804
|
+
*/
|
|
1805
|
+
type RelationIncludeJoinKind = typeof JOIN_KINDS.LEFT | typeof JOIN_KINDS.INNER;
|
|
1806
|
+
/**
|
|
1807
|
+
* Options for including a relation in a query
|
|
1808
|
+
*/
|
|
1809
|
+
interface RelationIncludeOptions {
|
|
1810
|
+
columns?: string[];
|
|
1811
|
+
aliasPrefix?: string;
|
|
1812
|
+
filter?: ExpressionNode;
|
|
1813
|
+
joinKind?: RelationIncludeJoinKind;
|
|
1814
|
+
pivot?: {
|
|
1815
|
+
columns?: string[];
|
|
1816
|
+
aliasPrefix?: string;
|
|
1817
|
+
};
|
|
1818
|
+
}
|
|
1819
|
+
|
|
1820
|
+
/**
|
|
1821
|
+
* Service for handling relation operations (joins, includes, etc.)
|
|
1822
|
+
*/
|
|
1823
|
+
declare class RelationService {
|
|
1824
|
+
private readonly table;
|
|
1825
|
+
private readonly state;
|
|
1826
|
+
private readonly hydration;
|
|
1827
|
+
private readonly createQueryAstService;
|
|
1828
|
+
private readonly projectionHelper;
|
|
1829
|
+
/**
|
|
1830
|
+
* Creates a new RelationService instance
|
|
1831
|
+
* @param table - Table definition
|
|
1832
|
+
* @param state - Current query state
|
|
1833
|
+
* @param hydration - Hydration manager
|
|
1834
|
+
*/
|
|
1835
|
+
constructor(table: TableDef, state: SelectQueryState, hydration: HydrationManager, createQueryAstService: (table: TableDef, state: SelectQueryState) => QueryAstService);
|
|
1836
|
+
/**
|
|
1837
|
+
* Joins a relation to the query
|
|
1838
|
+
* @param relationName - Name of the relation to join
|
|
1839
|
+
* @param joinKind - Type of join to use
|
|
1840
|
+
* @param extraCondition - Additional join condition
|
|
1841
|
+
* @returns Relation result with updated state and hydration
|
|
1842
|
+
*/
|
|
1843
|
+
joinRelation(relationName: string, joinKind: JoinKind, extraCondition?: ExpressionNode): RelationResult;
|
|
1844
|
+
/**
|
|
1845
|
+
* Matches records based on a relation with an optional predicate
|
|
1846
|
+
* @param relationName - Name of the relation to match
|
|
1847
|
+
* @param predicate - Optional predicate expression
|
|
1848
|
+
* @returns Relation result with updated state and hydration
|
|
1849
|
+
*/
|
|
1850
|
+
match(relationName: string, predicate?: ExpressionNode): RelationResult;
|
|
1851
|
+
/**
|
|
1852
|
+
* Includes a relation in the query result
|
|
1853
|
+
* @param relationName - Name of the relation to include
|
|
1854
|
+
* @param options - Options for relation inclusion
|
|
1855
|
+
* @returns Relation result with updated state and hydration
|
|
1856
|
+
*/
|
|
1857
|
+
include(relationName: string, options?: RelationIncludeOptions): RelationResult;
|
|
1858
|
+
/**
|
|
1859
|
+
* Applies relation correlation to a query AST
|
|
1860
|
+
* @param relationName - Name of the relation
|
|
1861
|
+
* @param ast - Query AST to modify
|
|
1862
|
+
* @returns Modified query AST with relation correlation
|
|
1863
|
+
*/
|
|
1864
|
+
applyRelationCorrelation(relationName: string, ast: SelectQueryNode, additionalCorrelation?: ExpressionNode): SelectQueryNode;
|
|
1865
|
+
/**
|
|
1866
|
+
* Creates a join node for a relation
|
|
1867
|
+
* @param state - Current query state
|
|
1868
|
+
* @param relationName - Name of the relation
|
|
1869
|
+
* @param joinKind - Type of join to use
|
|
1870
|
+
* @param extraCondition - Additional join condition
|
|
1871
|
+
* @returns Updated query state with join
|
|
1872
|
+
*/
|
|
1873
|
+
private withJoin;
|
|
1874
|
+
/**
|
|
1875
|
+
* Selects columns for a relation
|
|
1876
|
+
* @param state - Current query state
|
|
1877
|
+
* @param hydration - Hydration manager
|
|
1878
|
+
* @param columns - Columns to select
|
|
1879
|
+
* @returns Relation result with updated state and hydration
|
|
1880
|
+
*/
|
|
1881
|
+
private selectColumns;
|
|
1882
|
+
/**
|
|
1883
|
+
* Gets a relation definition by name
|
|
1884
|
+
* @param relationName - Name of the relation
|
|
1885
|
+
* @returns Relation definition
|
|
1886
|
+
* @throws Error if relation is not found
|
|
1887
|
+
*/
|
|
1888
|
+
private getRelation;
|
|
1889
|
+
/**
|
|
1890
|
+
* Creates a QueryAstService instance
|
|
1891
|
+
* @param state - Current query state
|
|
1892
|
+
* @returns QueryAstService instance
|
|
1893
|
+
*/
|
|
1894
|
+
private astService;
|
|
1895
|
+
private rootTableName;
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
/**
|
|
1899
|
+
* Dependencies for query builder operations
|
|
1900
|
+
*/
|
|
1901
|
+
interface SelectQueryBuilderDependencies {
|
|
1902
|
+
/**
|
|
1903
|
+
* Creates a new query state
|
|
1904
|
+
* @param table - Table definition
|
|
1905
|
+
* @returns New query state
|
|
1906
|
+
*/
|
|
1907
|
+
createState: (table: TableDef) => SelectQueryState;
|
|
1908
|
+
/**
|
|
1909
|
+
* Creates a new hydration manager
|
|
1910
|
+
* @param table - Table definition
|
|
1911
|
+
* @returns New hydration manager
|
|
1912
|
+
*/
|
|
1913
|
+
createHydration: (table: TableDef) => HydrationManager;
|
|
1914
|
+
/**
|
|
1915
|
+
* Creates a new hydration planner
|
|
1916
|
+
* @param table - Table definition
|
|
1917
|
+
* @returns Hydration planner
|
|
1918
|
+
*/
|
|
1919
|
+
createHydrationPlanner: (table: TableDef) => HydrationPlanner;
|
|
1920
|
+
/**
|
|
1921
|
+
* Creates a new query AST service
|
|
1922
|
+
* @param table - Table definition
|
|
1923
|
+
* @param state - Query state
|
|
1924
|
+
* @returns New query AST service
|
|
1925
|
+
*/
|
|
1926
|
+
createQueryAstService: (table: TableDef, state: SelectQueryState) => QueryAstService;
|
|
1927
|
+
/**
|
|
1928
|
+
* Creates a new relation service
|
|
1929
|
+
* @param table - Table definition
|
|
1930
|
+
* @param state - Query state
|
|
1931
|
+
* @param hydration - Hydration manager
|
|
1932
|
+
* @returns New relation service
|
|
1933
|
+
*/
|
|
1934
|
+
createRelationService: (table: TableDef, state: SelectQueryState, hydration: HydrationManager) => RelationService;
|
|
1935
|
+
}
|
|
1936
|
+
|
|
1937
|
+
type QueryResult = {
|
|
1938
|
+
columns: string[];
|
|
1939
|
+
values: unknown[][];
|
|
1940
|
+
};
|
|
1941
|
+
interface DbExecutor {
|
|
1942
|
+
executeSql(sql: string, params?: unknown[]): Promise<QueryResult[]>;
|
|
1943
|
+
beginTransaction?(): Promise<void>;
|
|
1944
|
+
commitTransaction?(): Promise<void>;
|
|
1945
|
+
rollbackTransaction?(): Promise<void>;
|
|
1946
|
+
}
|
|
1947
|
+
/**
|
|
1948
|
+
* Convert an array of row objects into a QueryResult.
|
|
1949
|
+
*/
|
|
1950
|
+
declare function rowsToQueryResult(rows: Array<Record<string, unknown>>): QueryResult;
|
|
199
1951
|
/**
|
|
200
|
-
*
|
|
201
|
-
* @param col - Column to get last value from
|
|
202
|
-
* @returns Window function node for LAST_VALUE
|
|
1952
|
+
* Minimal contract that most SQL clients can implement.
|
|
203
1953
|
*/
|
|
204
|
-
|
|
1954
|
+
interface SimpleQueryRunner {
|
|
1955
|
+
query(sql: string, params?: unknown[]): Promise<Array<Record<string, unknown>>>;
|
|
1956
|
+
beginTransaction?(): Promise<void>;
|
|
1957
|
+
commitTransaction?(): Promise<void>;
|
|
1958
|
+
rollbackTransaction?(): Promise<void>;
|
|
1959
|
+
}
|
|
205
1960
|
/**
|
|
206
|
-
*
|
|
207
|
-
* @param name - Window function name
|
|
208
|
-
* @param args - Function arguments
|
|
209
|
-
* @param partitionBy - Optional PARTITION BY columns
|
|
210
|
-
* @param orderBy - Optional ORDER BY clauses
|
|
211
|
-
* @returns Window function node
|
|
1961
|
+
* Generic factory: turn any SimpleQueryRunner into a DbExecutor.
|
|
212
1962
|
*/
|
|
213
|
-
declare
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
1963
|
+
declare function createExecutorFromQueryRunner(runner: SimpleQueryRunner): DbExecutor;
|
|
1964
|
+
|
|
1965
|
+
type EntityConstructor = new (...args: any[]) => any;
|
|
1966
|
+
type EntityOrTableTarget = EntityConstructor | TableDef;
|
|
1967
|
+
type EntityOrTableTargetResolver = EntityOrTableTarget | (() => EntityOrTableTarget);
|
|
217
1968
|
|
|
218
1969
|
/**
|
|
219
|
-
*
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
*/
|
|
223
|
-
|
|
1970
|
+
* Entity status enum representing the lifecycle state of an entity
|
|
1971
|
+
*/
|
|
1972
|
+
declare enum EntityStatus {
|
|
1973
|
+
/** Entity is newly created and not yet persisted */
|
|
1974
|
+
New = "new",
|
|
1975
|
+
/** Entity is managed by the ORM and synchronized with the database */
|
|
1976
|
+
Managed = "managed",
|
|
1977
|
+
/** Entity has been modified but not yet persisted */
|
|
1978
|
+
Dirty = "dirty",
|
|
1979
|
+
/** Entity has been marked for removal */
|
|
1980
|
+
Removed = "removed",
|
|
1981
|
+
/** Entity is detached from the ORM context */
|
|
1982
|
+
Detached = "detached"
|
|
1983
|
+
}
|
|
224
1984
|
/**
|
|
225
|
-
*
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
*/
|
|
229
|
-
|
|
1985
|
+
* Represents an entity being tracked by the ORM
|
|
1986
|
+
*/
|
|
1987
|
+
interface TrackedEntity {
|
|
1988
|
+
/** The table definition this entity belongs to */
|
|
1989
|
+
table: TableDef;
|
|
1990
|
+
/** The actual entity instance */
|
|
1991
|
+
entity: any;
|
|
1992
|
+
/** Primary key value of the entity */
|
|
1993
|
+
pk: string | number | null;
|
|
1994
|
+
/** Current status of the entity */
|
|
1995
|
+
status: EntityStatus;
|
|
1996
|
+
/** Original values of the entity when it was loaded */
|
|
1997
|
+
original: Record<string, any> | null;
|
|
1998
|
+
}
|
|
230
1999
|
/**
|
|
231
|
-
*
|
|
232
|
-
|
|
233
|
-
|
|
2000
|
+
* Type representing a key for relation navigation
|
|
2001
|
+
*/
|
|
2002
|
+
type RelationKey = string;
|
|
2003
|
+
/**
|
|
2004
|
+
* Represents a change operation on a relation
|
|
2005
|
+
* @typeParam T - Type of the related entity
|
|
2006
|
+
*/
|
|
2007
|
+
type RelationChange<T> = {
|
|
2008
|
+
kind: 'add';
|
|
2009
|
+
entity: T;
|
|
2010
|
+
} | {
|
|
2011
|
+
kind: 'attach';
|
|
2012
|
+
entity: T;
|
|
2013
|
+
} | {
|
|
2014
|
+
kind: 'remove';
|
|
2015
|
+
entity: T;
|
|
2016
|
+
} | {
|
|
2017
|
+
kind: 'detach';
|
|
2018
|
+
entity: T;
|
|
2019
|
+
};
|
|
2020
|
+
/**
|
|
2021
|
+
* Represents a relation change entry in the unit of work
|
|
2022
|
+
*/
|
|
2023
|
+
interface RelationChangeEntry {
|
|
2024
|
+
/** Root entity that owns the relation */
|
|
2025
|
+
root: any;
|
|
2026
|
+
/** Key of the relation being changed */
|
|
2027
|
+
relationKey: RelationKey;
|
|
2028
|
+
/** Table definition of the root entity */
|
|
2029
|
+
rootTable: TableDef;
|
|
2030
|
+
/** Name of the relation */
|
|
2031
|
+
relationName: string;
|
|
2032
|
+
/** Relation definition */
|
|
2033
|
+
relation: RelationDef;
|
|
2034
|
+
/** The change being applied */
|
|
2035
|
+
change: RelationChange<any>;
|
|
2036
|
+
}
|
|
2037
|
+
/**
|
|
2038
|
+
* Represents a domain event that can be emitted by entities
|
|
2039
|
+
* @typeParam TType - Type of the event (string literal)
|
|
234
2040
|
*/
|
|
235
|
-
|
|
2041
|
+
interface DomainEvent<TType extends string = string> {
|
|
2042
|
+
/** Type identifier for the event */
|
|
2043
|
+
readonly type: TType;
|
|
2044
|
+
/** Timestamp when the event occurred */
|
|
2045
|
+
readonly occurredAt?: Date;
|
|
2046
|
+
}
|
|
236
2047
|
/**
|
|
237
|
-
*
|
|
238
|
-
* @param col - Column to take the minimum of
|
|
239
|
-
* @returns Function node with MIN
|
|
2048
|
+
* Type representing any domain event
|
|
240
2049
|
*/
|
|
241
|
-
|
|
2050
|
+
type AnyDomainEvent = DomainEvent<string>;
|
|
242
2051
|
/**
|
|
243
|
-
*
|
|
244
|
-
* @param col - Column to take the maximum of
|
|
245
|
-
* @returns Function node with MAX
|
|
2052
|
+
* Type representing ORM-specific domain events
|
|
246
2053
|
*/
|
|
247
|
-
|
|
248
|
-
type GroupConcatOrderByInput = {
|
|
249
|
-
column: ColumnRef | ColumnNode;
|
|
250
|
-
direction?: OrderDirection;
|
|
251
|
-
};
|
|
252
|
-
type GroupConcatOptions = {
|
|
253
|
-
separator?: ValueOperandInput;
|
|
254
|
-
orderBy?: GroupConcatOrderByInput[];
|
|
255
|
-
};
|
|
2054
|
+
type OrmDomainEvent = AnyDomainEvent;
|
|
256
2055
|
/**
|
|
257
|
-
*
|
|
2056
|
+
* Interface for entities that can emit domain events
|
|
2057
|
+
* @typeParam E - Type of domain events this entity can emit
|
|
258
2058
|
*/
|
|
259
|
-
|
|
2059
|
+
interface HasDomainEvents<E extends DomainEvent = AnyDomainEvent> {
|
|
2060
|
+
/** Array of domain events emitted by this entity */
|
|
2061
|
+
domainEvents?: E[];
|
|
2062
|
+
}
|
|
260
2063
|
|
|
261
2064
|
/**
|
|
262
|
-
*
|
|
2065
|
+
* Strategy interface for converting database names to TypeScript identifiers
|
|
263
2066
|
*/
|
|
264
|
-
interface
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
2067
|
+
interface NamingStrategy {
|
|
2068
|
+
/**
|
|
2069
|
+
* Converts a table name to a TypeScript symbol name
|
|
2070
|
+
* @param table - Table node, function table node, or name
|
|
2071
|
+
* @returns Valid TypeScript identifier
|
|
2072
|
+
*/
|
|
2073
|
+
tableToSymbol(table: TableSourceNode | string): string;
|
|
2074
|
+
/**
|
|
2075
|
+
* Converts a column reference to a property name
|
|
2076
|
+
* @param column - Column node
|
|
2077
|
+
* @returns Valid TypeScript property name
|
|
2078
|
+
*/
|
|
2079
|
+
columnToProperty(column: ColumnNode): string;
|
|
2080
|
+
}
|
|
2081
|
+
|
|
2082
|
+
interface QueryContext {
|
|
2083
|
+
sql: string;
|
|
2084
|
+
params: unknown[];
|
|
2085
|
+
}
|
|
2086
|
+
type QueryInterceptor = (ctx: QueryContext, next: () => Promise<QueryResult[]>) => Promise<QueryResult[]>;
|
|
2087
|
+
declare class InterceptorPipeline {
|
|
2088
|
+
private interceptors;
|
|
2089
|
+
use(interceptor: QueryInterceptor): void;
|
|
2090
|
+
run(ctx: QueryContext, executor: DbExecutor): Promise<QueryResult[]>;
|
|
2091
|
+
}
|
|
2092
|
+
|
|
2093
|
+
interface OrmOptions<E extends DomainEvent = OrmDomainEvent> {
|
|
2094
|
+
dialect: Dialect;
|
|
2095
|
+
executorFactory: DbExecutorFactory;
|
|
2096
|
+
interceptors?: InterceptorPipeline;
|
|
2097
|
+
namingStrategy?: NamingStrategy;
|
|
2098
|
+
}
|
|
2099
|
+
interface DbExecutorFactory {
|
|
2100
|
+
createExecutor(options?: {
|
|
2101
|
+
tx?: ExternalTransaction;
|
|
2102
|
+
}): DbExecutor;
|
|
2103
|
+
createTransactionalExecutor(): DbExecutor;
|
|
2104
|
+
}
|
|
2105
|
+
interface ExternalTransaction {
|
|
2106
|
+
}
|
|
2107
|
+
declare class Orm<E extends DomainEvent = OrmDomainEvent> {
|
|
2108
|
+
readonly dialect: Dialect;
|
|
2109
|
+
readonly interceptors: InterceptorPipeline;
|
|
2110
|
+
readonly namingStrategy: NamingStrategy;
|
|
2111
|
+
private readonly executorFactory;
|
|
2112
|
+
constructor(opts: OrmOptions<E>);
|
|
2113
|
+
createSession(options?: {
|
|
2114
|
+
tx?: ExternalTransaction;
|
|
2115
|
+
}): OrmSession<E>;
|
|
2116
|
+
transaction<T>(fn: (session: OrmSession<E>) => Promise<T>): Promise<T>;
|
|
2117
|
+
}
|
|
2118
|
+
|
|
2119
|
+
declare class IdentityMap {
|
|
2120
|
+
private readonly buckets;
|
|
2121
|
+
get bucketsMap(): Map<string, Map<string, TrackedEntity>>;
|
|
2122
|
+
getEntity(table: TableDef, pk: string | number): any | undefined;
|
|
2123
|
+
register(tracked: TrackedEntity): void;
|
|
2124
|
+
remove(tracked: TrackedEntity): void;
|
|
2125
|
+
getEntitiesForTable(table: TableDef): TrackedEntity[];
|
|
2126
|
+
clear(): void;
|
|
2127
|
+
private toIdentityKey;
|
|
2128
|
+
}
|
|
2129
|
+
|
|
2130
|
+
declare class UnitOfWork {
|
|
2131
|
+
private readonly dialect;
|
|
2132
|
+
private readonly executor;
|
|
2133
|
+
private readonly identityMap;
|
|
2134
|
+
private readonly hookContext;
|
|
2135
|
+
private readonly trackedEntities;
|
|
2136
|
+
constructor(dialect: Dialect, executor: DbExecutor, identityMap: IdentityMap, hookContext: () => unknown);
|
|
2137
|
+
get identityBuckets(): Map<string, Map<string, TrackedEntity>>;
|
|
2138
|
+
getTracked(): TrackedEntity[];
|
|
2139
|
+
getEntity(table: TableDef, pk: string | number): any | undefined;
|
|
2140
|
+
getEntitiesForTable(table: TableDef): TrackedEntity[];
|
|
2141
|
+
findTracked(entity: any): TrackedEntity | undefined;
|
|
2142
|
+
setEntity(table: TableDef, pk: string | number, entity: any): void;
|
|
2143
|
+
trackNew(table: TableDef, entity: any, pk?: string | number): void;
|
|
2144
|
+
trackManaged(table: TableDef, pk: string | number, entity: any): void;
|
|
2145
|
+
markDirty(entity: any): void;
|
|
2146
|
+
markRemoved(entity: any): void;
|
|
2147
|
+
flush(): Promise<void>;
|
|
2148
|
+
reset(): void;
|
|
2149
|
+
private flushInsert;
|
|
2150
|
+
private flushUpdate;
|
|
2151
|
+
private flushDelete;
|
|
2152
|
+
private runHook;
|
|
2153
|
+
private computeChanges;
|
|
2154
|
+
private extractColumns;
|
|
2155
|
+
private executeCompiled;
|
|
2156
|
+
private getReturningColumns;
|
|
2157
|
+
private applyReturningResults;
|
|
2158
|
+
private normalizeColumnName;
|
|
2159
|
+
private registerIdentity;
|
|
2160
|
+
private createSnapshot;
|
|
2161
|
+
private getPrimaryKeyValue;
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
type EventOfType<E extends DomainEvent, TType extends E['type']> = Extract<E, {
|
|
2165
|
+
type: TType;
|
|
2166
|
+
}>;
|
|
2167
|
+
type DomainEventHandler<E extends DomainEvent, Context> = (event: E, ctx: Context) => Promise<void> | void;
|
|
2168
|
+
type InitialHandlers<E extends DomainEvent, Context> = {
|
|
2169
|
+
[K in E['type']]?: DomainEventHandler<EventOfType<E, K>, Context>[];
|
|
2170
|
+
};
|
|
2171
|
+
declare class DomainEventBus<E extends DomainEvent, Context> {
|
|
2172
|
+
private readonly handlers;
|
|
2173
|
+
constructor(initialHandlers?: InitialHandlers<E, Context>);
|
|
2174
|
+
on<TType extends E['type']>(type: TType, handler: DomainEventHandler<EventOfType<E, TType>, Context>): void;
|
|
2175
|
+
register<TType extends E['type']>(type: TType, handler: DomainEventHandler<EventOfType<E, TType>, Context>): void;
|
|
2176
|
+
dispatch(trackedEntities: Iterable<TrackedEntity>, ctx: Context): Promise<void>;
|
|
272
2177
|
}
|
|
2178
|
+
declare const addDomainEvent: <E extends DomainEvent>(entity: HasDomainEvents<E>, event: E) => void;
|
|
2179
|
+
|
|
2180
|
+
declare class RelationChangeProcessor {
|
|
2181
|
+
private readonly unitOfWork;
|
|
2182
|
+
private readonly dialect;
|
|
2183
|
+
private readonly executor;
|
|
2184
|
+
private readonly relationChanges;
|
|
2185
|
+
constructor(unitOfWork: UnitOfWork, dialect: Dialect, executor: DbExecutor);
|
|
2186
|
+
registerChange(entry: RelationChangeEntry): void;
|
|
2187
|
+
reset(): void;
|
|
2188
|
+
process(): Promise<void>;
|
|
2189
|
+
private handleHasManyChange;
|
|
2190
|
+
private handleHasOneChange;
|
|
2191
|
+
private handleBelongsToChange;
|
|
2192
|
+
private handleBelongsToManyChange;
|
|
2193
|
+
private assignHasManyForeignKey;
|
|
2194
|
+
private detachHasManyChild;
|
|
2195
|
+
private assignHasOneForeignKey;
|
|
2196
|
+
private detachHasOneChild;
|
|
2197
|
+
private insertPivotRow;
|
|
2198
|
+
private deletePivotRow;
|
|
2199
|
+
private resolvePrimaryKeyValue;
|
|
2200
|
+
}
|
|
2201
|
+
|
|
273
2202
|
/**
|
|
274
|
-
*
|
|
2203
|
+
* Represents a single SQL query log entry
|
|
275
2204
|
*/
|
|
276
|
-
interface
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
visitScalarSubquery?(node: ScalarSubqueryNode): R;
|
|
282
|
-
visitCaseExpression?(node: CaseExpressionNode): R;
|
|
283
|
-
visitWindowFunction?(node: WindowFunctionNode): R;
|
|
284
|
-
otherwise?(node: OperandNode): R;
|
|
2205
|
+
interface QueryLogEntry {
|
|
2206
|
+
/** The SQL query that was executed */
|
|
2207
|
+
sql: string;
|
|
2208
|
+
/** Parameters used in the query */
|
|
2209
|
+
params?: unknown[];
|
|
285
2210
|
}
|
|
286
|
-
type ExpressionDispatch = <R>(node: any, visitor: ExpressionVisitor<R>) => R;
|
|
287
|
-
type OperandDispatch = <R>(node: any, visitor: OperandVisitor<R>) => R;
|
|
288
2211
|
/**
|
|
289
|
-
*
|
|
290
|
-
*
|
|
2212
|
+
* Function type for query logging callbacks
|
|
2213
|
+
* @param entry - The query log entry to process
|
|
291
2214
|
*/
|
|
292
|
-
|
|
2215
|
+
type QueryLogger = (entry: QueryLogEntry) => void;
|
|
293
2216
|
/**
|
|
294
|
-
*
|
|
295
|
-
*
|
|
2217
|
+
* Creates a wrapped database executor that logs all SQL queries
|
|
2218
|
+
* @param executor - Original database executor to wrap
|
|
2219
|
+
* @param logger - Optional logger function to receive query log entries
|
|
2220
|
+
* @returns Wrapped executor that logs queries before execution
|
|
296
2221
|
*/
|
|
297
|
-
declare const
|
|
2222
|
+
declare const createQueryLoggingExecutor: (executor: DbExecutor, logger?: QueryLogger) => DbExecutor;
|
|
2223
|
+
|
|
298
2224
|
/**
|
|
299
|
-
*
|
|
2225
|
+
* Context for SQL query execution
|
|
300
2226
|
*/
|
|
301
|
-
|
|
302
|
-
|
|
2227
|
+
interface ExecutionContext {
|
|
2228
|
+
/** Database dialect to use for SQL generation */
|
|
2229
|
+
dialect: Dialect;
|
|
2230
|
+
/** Database executor for running SQL queries */
|
|
2231
|
+
executor: DbExecutor;
|
|
2232
|
+
/** Interceptor pipeline for query processing */
|
|
2233
|
+
interceptors: InterceptorPipeline;
|
|
2234
|
+
}
|
|
2235
|
+
|
|
2236
|
+
interface EntityContext {
|
|
2237
|
+
dialect: Dialect;
|
|
2238
|
+
executor: DbExecutor;
|
|
2239
|
+
getEntity(table: TableDef, pk: any): any;
|
|
2240
|
+
setEntity(table: TableDef, pk: any, entity: any): void;
|
|
2241
|
+
trackNew(table: TableDef, entity: any, pk?: any): void;
|
|
2242
|
+
trackManaged(table: TableDef, pk: any, entity: any): void;
|
|
2243
|
+
markDirty(entity: any): void;
|
|
2244
|
+
markRemoved(entity: any): void;
|
|
2245
|
+
getEntitiesForTable(table: TableDef): TrackedEntity[];
|
|
2246
|
+
registerRelationChange(root: any, relationKey: RelationKey, rootTable: TableDef, relationName: string, relation: RelationDef, change: RelationChange<any>): void;
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
interface HydrationContext<E extends DomainEvent = AnyDomainEvent> {
|
|
2250
|
+
identityMap: IdentityMap;
|
|
2251
|
+
unitOfWork: UnitOfWork;
|
|
2252
|
+
domainEvents: DomainEventBus<E, OrmSession<E>>;
|
|
2253
|
+
relationChanges: RelationChangeProcessor;
|
|
2254
|
+
entityContext: EntityContext;
|
|
2255
|
+
}
|
|
2256
|
+
|
|
2257
|
+
interface OrmInterceptor {
|
|
2258
|
+
beforeFlush?(ctx: EntityContext): Promise<void> | void;
|
|
2259
|
+
afterFlush?(ctx: EntityContext): Promise<void> | void;
|
|
2260
|
+
}
|
|
2261
|
+
interface OrmSessionOptions<E extends DomainEvent = OrmDomainEvent> {
|
|
2262
|
+
orm: Orm<E>;
|
|
2263
|
+
executor: DbExecutor;
|
|
2264
|
+
queryLogger?: QueryLogger;
|
|
2265
|
+
interceptors?: OrmInterceptor[];
|
|
2266
|
+
domainEventHandlers?: InitialHandlers<E, OrmSession<E>>;
|
|
2267
|
+
}
|
|
2268
|
+
declare class OrmSession<E extends DomainEvent = OrmDomainEvent> implements EntityContext {
|
|
2269
|
+
readonly orm: Orm<E>;
|
|
2270
|
+
readonly executor: DbExecutor;
|
|
2271
|
+
readonly identityMap: IdentityMap;
|
|
2272
|
+
readonly unitOfWork: UnitOfWork;
|
|
2273
|
+
readonly domainEvents: DomainEventBus<E, OrmSession<E>>;
|
|
2274
|
+
readonly relationChanges: RelationChangeProcessor;
|
|
2275
|
+
private readonly interceptors;
|
|
2276
|
+
constructor(opts: OrmSessionOptions<E>);
|
|
2277
|
+
get dialect(): Dialect;
|
|
2278
|
+
get identityBuckets(): Map<string, Map<string, TrackedEntity>>;
|
|
2279
|
+
get tracked(): TrackedEntity[];
|
|
2280
|
+
getEntity(table: TableDef, pk: any): any | undefined;
|
|
2281
|
+
setEntity(table: TableDef, pk: any, entity: any): void;
|
|
2282
|
+
trackNew(table: TableDef, entity: any, pk?: any): void;
|
|
2283
|
+
trackManaged(table: TableDef, pk: any, entity: any): void;
|
|
2284
|
+
markDirty(entity: any): void;
|
|
2285
|
+
markRemoved(entity: any): void;
|
|
2286
|
+
registerRelationChange: (root: any, relationKey: RelationKey, rootTable: TableDef, relationName: string, relation: RelationDef, change: RelationChange<any>) => void;
|
|
2287
|
+
getEntitiesForTable(table: TableDef): TrackedEntity[];
|
|
2288
|
+
registerInterceptor(interceptor: OrmInterceptor): void;
|
|
2289
|
+
registerDomainEventHandler<TType extends E['type']>(type: TType, handler: DomainEventHandler<Extract<E, {
|
|
2290
|
+
type: TType;
|
|
2291
|
+
}>, OrmSession<E>>): void;
|
|
2292
|
+
find<TTable extends TableDef>(entityClass: EntityConstructor, id: any): Promise<EntityInstance<TTable> | null>;
|
|
2293
|
+
findOne<TTable extends TableDef>(qb: SelectQueryBuilder<any, TTable>): Promise<EntityInstance<TTable> | null>;
|
|
2294
|
+
findMany<TTable extends TableDef>(qb: SelectQueryBuilder<any, TTable>): Promise<EntityInstance<TTable>[]>;
|
|
2295
|
+
persist(entity: object): Promise<void>;
|
|
2296
|
+
remove(entity: object): Promise<void>;
|
|
2297
|
+
flush(): Promise<void>;
|
|
2298
|
+
commit(): Promise<void>;
|
|
2299
|
+
rollback(): Promise<void>;
|
|
2300
|
+
getExecutionContext(): ExecutionContext;
|
|
2301
|
+
getHydrationContext(): HydrationContext<E>;
|
|
2302
|
+
}
|
|
2303
|
+
|
|
2304
|
+
type SelectDialectInput = Dialect | DialectKey;
|
|
2305
|
+
|
|
2306
|
+
type ColumnSelectionValue = ColumnDef | FunctionNode | CaseExpressionNode | WindowFunctionNode;
|
|
2307
|
+
type DeepSelectConfig<TTable extends TableDef> = {
|
|
2308
|
+
root?: (keyof TTable['columns'] & string)[];
|
|
2309
|
+
} & {
|
|
2310
|
+
[K in keyof TTable['relations'] & string]?: (keyof RelationTargetTable<TTable['relations'][K]>['columns'] & string)[];
|
|
2311
|
+
};
|
|
2312
|
+
type WhereHasOptions = {
|
|
2313
|
+
correlate?: ExpressionNode;
|
|
2314
|
+
};
|
|
2315
|
+
type RelationCallback = <TChildTable extends TableDef>(qb: SelectQueryBuilder<any, TChildTable>) => SelectQueryBuilder<any, TChildTable>;
|
|
303
2316
|
/**
|
|
304
|
-
|
|
305
|
-
*
|
|
306
|
-
|
|
2317
|
+
|
|
2318
|
+
* Main query builder class for constructing SQL SELECT queries
|
|
2319
|
+
|
|
2320
|
+
* @typeParam T - Result type for projections (unused)
|
|
2321
|
+
|
|
2322
|
+
* @typeParam TTable - Table definition being queried
|
|
2323
|
+
|
|
307
2324
|
*/
|
|
308
|
-
declare
|
|
2325
|
+
declare class SelectQueryBuilder<T = any, TTable extends TableDef = TableDef> {
|
|
2326
|
+
private readonly env;
|
|
2327
|
+
private readonly context;
|
|
2328
|
+
private readonly columnSelector;
|
|
2329
|
+
private readonly relationManager;
|
|
2330
|
+
private readonly lazyRelations;
|
|
2331
|
+
/**
|
|
2332
|
+
|
|
2333
|
+
* Creates a new SelectQueryBuilder instance
|
|
2334
|
+
|
|
2335
|
+
* @param table - Table definition to query
|
|
2336
|
+
|
|
2337
|
+
* @param state - Optional initial query state
|
|
2338
|
+
|
|
2339
|
+
* @param hydration - Optional hydration manager
|
|
2340
|
+
|
|
2341
|
+
* @param dependencies - Optional query builder dependencies
|
|
2342
|
+
|
|
2343
|
+
*/
|
|
2344
|
+
constructor(table: TTable, state?: SelectQueryState, hydration?: HydrationManager, dependencies?: Partial<SelectQueryBuilderDependencies>, lazyRelations?: Set<string>);
|
|
2345
|
+
private clone;
|
|
2346
|
+
/**
|
|
2347
|
+
* Applies an alias to the root FROM table.
|
|
2348
|
+
* @param alias - Alias to apply
|
|
2349
|
+
*/
|
|
2350
|
+
as(alias: string): SelectQueryBuilder<T, TTable>;
|
|
2351
|
+
private resolveQueryNode;
|
|
2352
|
+
private applyCorrelation;
|
|
2353
|
+
private createChildBuilder;
|
|
2354
|
+
private applyAst;
|
|
2355
|
+
private applyJoin;
|
|
2356
|
+
private applySetOperation;
|
|
2357
|
+
/**
|
|
2358
|
+
|
|
2359
|
+
* Selects specific columns for the query
|
|
2360
|
+
|
|
2361
|
+
* @param columns - Record of column definitions, function nodes, case expressions, or window functions
|
|
2362
|
+
|
|
2363
|
+
* @returns New query builder instance with selected columns
|
|
2364
|
+
|
|
2365
|
+
*/
|
|
2366
|
+
select(columns: Record<string, ColumnSelectionValue>): SelectQueryBuilder<T, TTable>;
|
|
2367
|
+
/**
|
|
2368
|
+
* Selects columns from the root table by name (typed).
|
|
2369
|
+
* @param cols - Column names on the root table
|
|
2370
|
+
*/
|
|
2371
|
+
selectColumns<K extends keyof TTable['columns'] & string>(...cols: K[]): SelectQueryBuilder<T, TTable>;
|
|
2372
|
+
/**
|
|
2373
|
+
|
|
2374
|
+
* Selects raw column expressions
|
|
2375
|
+
|
|
2376
|
+
* @param cols - Column expressions as strings
|
|
2377
|
+
|
|
2378
|
+
* @returns New query builder instance with raw column selections
|
|
2379
|
+
|
|
2380
|
+
*/
|
|
2381
|
+
selectRaw(...cols: string[]): SelectQueryBuilder<T, TTable>;
|
|
2382
|
+
/**
|
|
2383
|
+
|
|
2384
|
+
* Adds a Common Table Expression (CTE) to the query
|
|
2385
|
+
|
|
2386
|
+
* @param name - Name of the CTE
|
|
2387
|
+
|
|
2388
|
+
* @param query - Query builder or query node for the CTE
|
|
2389
|
+
|
|
2390
|
+
* @param columns - Optional column names for the CTE
|
|
2391
|
+
|
|
2392
|
+
* @returns New query builder instance with the CTE
|
|
2393
|
+
|
|
2394
|
+
*/
|
|
2395
|
+
with(name: string, query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, columns?: string[]): SelectQueryBuilder<T, TTable>;
|
|
2396
|
+
/**
|
|
2397
|
+
|
|
2398
|
+
* Adds a recursive Common Table Expression (CTE) to the query
|
|
2399
|
+
|
|
2400
|
+
* @param name - Name of the CTE
|
|
2401
|
+
|
|
2402
|
+
* @param query - Query builder or query node for the CTE
|
|
2403
|
+
|
|
2404
|
+
* @param columns - Optional column names for the CTE
|
|
2405
|
+
|
|
2406
|
+
* @returns New query builder instance with the recursive CTE
|
|
2407
|
+
|
|
2408
|
+
*/
|
|
2409
|
+
withRecursive(name: string, query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, columns?: string[]): SelectQueryBuilder<T, TTable>;
|
|
2410
|
+
/**
|
|
2411
|
+
* Replaces the FROM clause with a derived table (subquery with alias)
|
|
2412
|
+
* @param subquery - Subquery to use as the FROM source
|
|
2413
|
+
* @param alias - Alias for the derived table
|
|
2414
|
+
* @param columnAliases - Optional column alias list
|
|
2415
|
+
* @returns New query builder instance with updated FROM
|
|
2416
|
+
*/
|
|
2417
|
+
fromSubquery(subquery: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, alias: string, columnAliases?: string[]): SelectQueryBuilder<T, TTable>;
|
|
2418
|
+
/**
|
|
2419
|
+
|
|
2420
|
+
* Selects a subquery as a column
|
|
2421
|
+
|
|
2422
|
+
* @param alias - Alias for the subquery column
|
|
2423
|
+
|
|
2424
|
+
* @param sub - Query builder or query node for the subquery
|
|
2425
|
+
|
|
2426
|
+
* @returns New query builder instance with the subquery selection
|
|
2427
|
+
|
|
2428
|
+
*/
|
|
2429
|
+
selectSubquery(alias: string, sub: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode): SelectQueryBuilder<T, TTable>;
|
|
2430
|
+
/**
|
|
2431
|
+
* Adds a JOIN against a derived table (subquery with alias)
|
|
2432
|
+
* @param subquery - Subquery to join
|
|
2433
|
+
* @param alias - Alias for the derived table
|
|
2434
|
+
* @param condition - Join condition expression
|
|
2435
|
+
* @param joinKind - Join kind (defaults to INNER)
|
|
2436
|
+
* @param columnAliases - Optional column alias list for the derived table
|
|
2437
|
+
* @returns New query builder instance with the derived-table join
|
|
2438
|
+
*/
|
|
2439
|
+
joinSubquery(subquery: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, alias: string, condition: BinaryExpressionNode, joinKind?: JoinKind, columnAliases?: string[]): SelectQueryBuilder<T, TTable>;
|
|
2440
|
+
/**
|
|
2441
|
+
|
|
2442
|
+
* Adds an INNER JOIN to the query
|
|
2443
|
+
|
|
2444
|
+
* @param table - Table to join
|
|
2445
|
+
|
|
2446
|
+
* @param condition - Join condition expression
|
|
2447
|
+
|
|
2448
|
+
* @returns New query builder instance with the INNER JOIN
|
|
2449
|
+
|
|
2450
|
+
*/
|
|
2451
|
+
innerJoin(table: TableDef, condition: BinaryExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2452
|
+
/**
|
|
2453
|
+
|
|
2454
|
+
* Adds a LEFT JOIN to the query
|
|
2455
|
+
|
|
2456
|
+
* @param table - Table to join
|
|
2457
|
+
|
|
2458
|
+
* @param condition - Join condition expression
|
|
2459
|
+
|
|
2460
|
+
* @returns New query builder instance with the LEFT JOIN
|
|
2461
|
+
|
|
2462
|
+
*/
|
|
2463
|
+
leftJoin(table: TableDef, condition: BinaryExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2464
|
+
/**
|
|
2465
|
+
|
|
2466
|
+
* Adds a RIGHT JOIN to the query
|
|
2467
|
+
|
|
2468
|
+
* @param table - Table to join
|
|
2469
|
+
|
|
2470
|
+
* @param condition - Join condition expression
|
|
2471
|
+
|
|
2472
|
+
* @returns New query builder instance with the RIGHT JOIN
|
|
2473
|
+
|
|
2474
|
+
*/
|
|
2475
|
+
rightJoin(table: TableDef, condition: BinaryExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2476
|
+
/**
|
|
2477
|
+
|
|
2478
|
+
* Matches records based on a relationship
|
|
2479
|
+
|
|
2480
|
+
* @param relationName - Name of the relationship to match
|
|
2481
|
+
|
|
2482
|
+
* @param predicate - Optional predicate expression
|
|
2483
|
+
|
|
2484
|
+
* @returns New query builder instance with the relationship match
|
|
2485
|
+
|
|
2486
|
+
*/
|
|
2487
|
+
match(relationName: string, predicate?: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2488
|
+
/**
|
|
2489
|
+
|
|
2490
|
+
* Joins a related table
|
|
2491
|
+
|
|
2492
|
+
* @param relationName - Name of the relationship to join
|
|
2493
|
+
|
|
2494
|
+
* @param joinKind - Type of join (defaults to INNER)
|
|
2495
|
+
|
|
2496
|
+
* @param extraCondition - Optional additional join condition
|
|
2497
|
+
|
|
2498
|
+
* @returns New query builder instance with the relationship join
|
|
2499
|
+
|
|
2500
|
+
*/
|
|
2501
|
+
joinRelation(relationName: string, joinKind?: JoinKind, extraCondition?: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2502
|
+
/**
|
|
2503
|
+
|
|
2504
|
+
* Includes related data in the query results
|
|
2505
|
+
|
|
2506
|
+
* @param relationName - Name of the relationship to include
|
|
2507
|
+
|
|
2508
|
+
* @param options - Optional include options
|
|
2509
|
+
|
|
2510
|
+
* @returns New query builder instance with the relationship inclusion
|
|
2511
|
+
|
|
2512
|
+
*/
|
|
2513
|
+
include(relationName: string, options?: RelationIncludeOptions): SelectQueryBuilder<T, TTable>;
|
|
2514
|
+
includeLazy<K extends keyof RelationMap<TTable>>(relationName: K): SelectQueryBuilder<T, TTable>;
|
|
2515
|
+
/**
|
|
2516
|
+
* Selects columns for a related table in a single hop.
|
|
2517
|
+
*/
|
|
2518
|
+
selectRelationColumns<K extends keyof TTable['relations'] & string, TRel extends RelationDef = TTable['relations'][K], TTarget extends TableDef = RelationTargetTable<TRel>, C extends keyof TTarget['columns'] & string = keyof TTarget['columns'] & string>(relationName: K, ...cols: C[]): SelectQueryBuilder<T, TTable>;
|
|
2519
|
+
/**
|
|
2520
|
+
* Convenience alias for selecting specific columns from a relation.
|
|
2521
|
+
*/
|
|
2522
|
+
includePick<K extends keyof TTable['relations'] & string, TRel extends RelationDef = TTable['relations'][K], TTarget extends TableDef = RelationTargetTable<TRel>, C extends keyof TTarget['columns'] & string = keyof TTarget['columns'] & string>(relationName: K, cols: C[]): SelectQueryBuilder<T, TTable>;
|
|
2523
|
+
/**
|
|
2524
|
+
* Selects columns for the root table and relations from a single config object.
|
|
2525
|
+
*/
|
|
2526
|
+
selectColumnsDeep(config: DeepSelectConfig<TTable>): SelectQueryBuilder<T, TTable>;
|
|
2527
|
+
getLazyRelations(): (keyof RelationMap<TTable>)[];
|
|
2528
|
+
getTable(): TTable;
|
|
2529
|
+
execute(ctx: OrmSession): Promise<EntityInstance<TTable>[]>;
|
|
2530
|
+
executeWithContexts(execCtx: ExecutionContext, hydCtx: HydrationContext): Promise<EntityInstance<TTable>[]>;
|
|
2531
|
+
/**
|
|
2532
|
+
|
|
2533
|
+
* Adds a WHERE condition to the query
|
|
2534
|
+
|
|
2535
|
+
* @param expr - Expression for the WHERE clause
|
|
2536
|
+
|
|
2537
|
+
* @returns New query builder instance with the WHERE condition
|
|
2538
|
+
|
|
2539
|
+
*/
|
|
2540
|
+
where(expr: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2541
|
+
/**
|
|
2542
|
+
|
|
2543
|
+
* Adds a GROUP BY clause to the query
|
|
2544
|
+
|
|
2545
|
+
* @param col - Column definition or column node to group by
|
|
2546
|
+
|
|
2547
|
+
* @returns New query builder instance with the GROUP BY clause
|
|
2548
|
+
|
|
2549
|
+
*/
|
|
2550
|
+
groupBy(col: ColumnDef | ColumnNode): SelectQueryBuilder<T, TTable>;
|
|
2551
|
+
/**
|
|
2552
|
+
|
|
2553
|
+
* Adds a HAVING condition to the query
|
|
2554
|
+
|
|
2555
|
+
* @param expr - Expression for the HAVING clause
|
|
2556
|
+
|
|
2557
|
+
* @returns New query builder instance with the HAVING condition
|
|
2558
|
+
|
|
2559
|
+
*/
|
|
2560
|
+
having(expr: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2561
|
+
/**
|
|
2562
|
+
|
|
2563
|
+
* Adds an ORDER BY clause to the query
|
|
2564
|
+
|
|
2565
|
+
* @param col - Column definition or column node to order by
|
|
2566
|
+
|
|
2567
|
+
* @param direction - Order direction (defaults to ASC)
|
|
2568
|
+
|
|
2569
|
+
* @returns New query builder instance with the ORDER BY clause
|
|
2570
|
+
|
|
2571
|
+
*/
|
|
2572
|
+
orderBy(col: ColumnDef | ColumnNode, direction?: OrderDirection): SelectQueryBuilder<T, TTable>;
|
|
2573
|
+
/**
|
|
2574
|
+
|
|
2575
|
+
* Adds a DISTINCT clause to the query
|
|
2576
|
+
|
|
2577
|
+
* @param cols - Columns to make distinct
|
|
2578
|
+
|
|
2579
|
+
* @returns New query builder instance with the DISTINCT clause
|
|
2580
|
+
|
|
2581
|
+
*/
|
|
2582
|
+
distinct(...cols: (ColumnDef | ColumnNode)[]): SelectQueryBuilder<T, TTable>;
|
|
2583
|
+
/**
|
|
2584
|
+
|
|
2585
|
+
* Adds a LIMIT clause to the query
|
|
2586
|
+
|
|
2587
|
+
* @param n - Maximum number of rows to return
|
|
2588
|
+
|
|
2589
|
+
* @returns New query builder instance with the LIMIT clause
|
|
2590
|
+
|
|
2591
|
+
*/
|
|
2592
|
+
limit(n: number): SelectQueryBuilder<T, TTable>;
|
|
2593
|
+
/**
|
|
2594
|
+
|
|
2595
|
+
* Adds an OFFSET clause to the query
|
|
2596
|
+
|
|
2597
|
+
* @param n - Number of rows to skip
|
|
2598
|
+
|
|
2599
|
+
* @returns New query builder instance with the OFFSET clause
|
|
2600
|
+
|
|
2601
|
+
*/
|
|
2602
|
+
offset(n: number): SelectQueryBuilder<T, TTable>;
|
|
2603
|
+
/**
|
|
2604
|
+
|
|
2605
|
+
* Combines this query with another using UNION
|
|
2606
|
+
|
|
2607
|
+
* @param query - Query to union with
|
|
2608
|
+
|
|
2609
|
+
* @returns New query builder instance with the set operation
|
|
2610
|
+
|
|
2611
|
+
*/
|
|
2612
|
+
union(query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode): SelectQueryBuilder<T, TTable>;
|
|
2613
|
+
/**
|
|
2614
|
+
|
|
2615
|
+
* Combines this query with another using UNION ALL
|
|
2616
|
+
|
|
2617
|
+
* @param query - Query to union with
|
|
2618
|
+
|
|
2619
|
+
* @returns New query builder instance with the set operation
|
|
2620
|
+
|
|
2621
|
+
*/
|
|
2622
|
+
unionAll(query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode): SelectQueryBuilder<T, TTable>;
|
|
2623
|
+
/**
|
|
2624
|
+
|
|
2625
|
+
* Combines this query with another using INTERSECT
|
|
2626
|
+
|
|
2627
|
+
* @param query - Query to intersect with
|
|
2628
|
+
|
|
2629
|
+
* @returns New query builder instance with the set operation
|
|
2630
|
+
|
|
2631
|
+
*/
|
|
2632
|
+
intersect(query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode): SelectQueryBuilder<T, TTable>;
|
|
2633
|
+
/**
|
|
2634
|
+
|
|
2635
|
+
* Combines this query with another using EXCEPT
|
|
2636
|
+
|
|
2637
|
+
* @param query - Query to subtract
|
|
2638
|
+
|
|
2639
|
+
* @returns New query builder instance with the set operation
|
|
2640
|
+
|
|
2641
|
+
*/
|
|
2642
|
+
except(query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode): SelectQueryBuilder<T, TTable>;
|
|
2643
|
+
/**
|
|
2644
|
+
|
|
2645
|
+
* Adds a WHERE EXISTS condition to the query
|
|
2646
|
+
|
|
2647
|
+
* @param subquery - Subquery to check for existence
|
|
2648
|
+
|
|
2649
|
+
* @returns New query builder instance with the WHERE EXISTS condition
|
|
2650
|
+
|
|
2651
|
+
*/
|
|
2652
|
+
whereExists(subquery: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, correlate?: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2653
|
+
/**
|
|
2654
|
+
|
|
2655
|
+
* Adds a WHERE NOT EXISTS condition to the query
|
|
2656
|
+
|
|
2657
|
+
* @param subquery - Subquery to check for non-existence
|
|
2658
|
+
|
|
2659
|
+
* @returns New query builder instance with the WHERE NOT EXISTS condition
|
|
2660
|
+
|
|
2661
|
+
*/
|
|
2662
|
+
whereNotExists(subquery: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, correlate?: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2663
|
+
/**
|
|
2664
|
+
|
|
2665
|
+
* Adds a WHERE EXISTS condition based on a relationship
|
|
2666
|
+
|
|
2667
|
+
* @param relationName - Name of the relationship to check
|
|
2668
|
+
|
|
2669
|
+
* @param callback - Optional callback to modify the relationship query
|
|
2670
|
+
|
|
2671
|
+
* @returns New query builder instance with the relationship existence check
|
|
2672
|
+
|
|
2673
|
+
*/
|
|
2674
|
+
whereHas(relationName: string, callbackOrOptions?: RelationCallback | WhereHasOptions, maybeOptions?: WhereHasOptions): SelectQueryBuilder<T, TTable>;
|
|
2675
|
+
/**
|
|
2676
|
+
|
|
2677
|
+
* Adds a WHERE NOT EXISTS condition based on a relationship
|
|
2678
|
+
|
|
2679
|
+
* @param relationName - Name of the relationship to check
|
|
2680
|
+
|
|
2681
|
+
* @param callback - Optional callback to modify the relationship query
|
|
2682
|
+
|
|
2683
|
+
* @returns New query builder instance with the relationship non-existence check
|
|
2684
|
+
|
|
2685
|
+
*/
|
|
2686
|
+
whereHasNot(relationName: string, callbackOrOptions?: RelationCallback | WhereHasOptions, maybeOptions?: WhereHasOptions): SelectQueryBuilder<T, TTable>;
|
|
2687
|
+
/**
|
|
2688
|
+
|
|
2689
|
+
* Compiles the query to SQL for a specific dialect
|
|
2690
|
+
|
|
2691
|
+
* @param dialect - Database dialect to compile for
|
|
2692
|
+
|
|
2693
|
+
* @returns Compiled query with SQL and parameters
|
|
2694
|
+
|
|
2695
|
+
*/
|
|
2696
|
+
compile(dialect: SelectDialectInput): CompiledQuery;
|
|
2697
|
+
/**
|
|
2698
|
+
|
|
2699
|
+
* Converts the query to SQL string for a specific dialect
|
|
2700
|
+
|
|
2701
|
+
* @param dialect - Database dialect to generate SQL for
|
|
2702
|
+
|
|
2703
|
+
* @returns SQL string representation of the query
|
|
2704
|
+
|
|
2705
|
+
*/
|
|
2706
|
+
toSql(dialect: SelectDialectInput): string;
|
|
2707
|
+
/**
|
|
2708
|
+
|
|
2709
|
+
* Gets the hydration plan for the query
|
|
2710
|
+
|
|
2711
|
+
* @returns Hydration plan or undefined if none exists
|
|
2712
|
+
|
|
2713
|
+
*/
|
|
2714
|
+
getHydrationPlan(): HydrationPlan | undefined;
|
|
2715
|
+
/**
|
|
2716
|
+
|
|
2717
|
+
* Gets the Abstract Syntax Tree (AST) representation of the query
|
|
2718
|
+
|
|
2719
|
+
* @returns Query AST with hydration applied
|
|
2720
|
+
|
|
2721
|
+
*/
|
|
2722
|
+
getAST(): SelectQueryNode;
|
|
2723
|
+
}
|
|
309
2724
|
/**
|
|
310
|
-
* Dispatches an operand node to the visitor
|
|
311
|
-
* @param node - Operand node to visit
|
|
312
|
-
* @param visitor - Visitor implementation
|
|
313
|
-
*/
|
|
314
|
-
declare const visitOperand: <R>(node: OperandNode, visitor: OperandVisitor<R>) => R;
|
|
315
2725
|
|
|
316
|
-
|
|
317
|
-
|
|
2726
|
+
* Creates a column node for use in expressions
|
|
2727
|
+
|
|
2728
|
+
* @param table - Table name
|
|
2729
|
+
|
|
2730
|
+
* @param name - Column name
|
|
2731
|
+
|
|
2732
|
+
* @returns ColumnNode with the specified table and name
|
|
2733
|
+
|
|
318
2734
|
*/
|
|
319
|
-
declare const
|
|
2735
|
+
declare const createColumn: (table: string, name: string) => ColumnNode;
|
|
320
2736
|
/**
|
|
321
|
-
|
|
2737
|
+
|
|
2738
|
+
* Creates a literal value node for use in expressions
|
|
2739
|
+
|
|
2740
|
+
* @param val - Literal value (string or number)
|
|
2741
|
+
|
|
2742
|
+
* @returns LiteralNode with the specified value
|
|
2743
|
+
|
|
322
2744
|
*/
|
|
323
|
-
declare const
|
|
2745
|
+
declare const createLiteral: (val: string | number) => LiteralNode;
|
|
324
2746
|
|
|
325
2747
|
/**
|
|
326
2748
|
* Build a typed selection map from a TableDef.
|
|
@@ -1198,8 +3620,8 @@ declare class TypeScriptGenerator implements ExpressionVisitor<string>, OperandV
|
|
|
1198
3620
|
private mapOp;
|
|
1199
3621
|
}
|
|
1200
3622
|
|
|
1201
|
-
declare const createEntityProxy: <TTable extends TableDef, TLazy extends keyof RelationMap<TTable> = keyof RelationMap<TTable>>(ctx: EntityContext, table: TTable, row: Record<string, any>, lazyRelations?: TLazy[]) =>
|
|
1202
|
-
declare const createEntityFromRow: <TTable extends TableDef>(ctx: EntityContext, table: TTable, row: Record<string, any>, lazyRelations?: (keyof RelationMap<TTable>)[]) =>
|
|
3623
|
+
declare const createEntityProxy: <TTable extends TableDef, TLazy extends keyof RelationMap<TTable> = keyof RelationMap<TTable>>(ctx: EntityContext, table: TTable, row: Record<string, any>, lazyRelations?: TLazy[]) => EntityInstance<TTable>;
|
|
3624
|
+
declare const createEntityFromRow: <TTable extends TableDef>(ctx: EntityContext, table: TTable, row: Record<string, any>, lazyRelations?: (keyof RelationMap<TTable>)[]) => EntityInstance<TTable>;
|
|
1203
3625
|
|
|
1204
3626
|
type Rows$3 = Record<string, any>[];
|
|
1205
3627
|
declare const loadHasManyRelation: (ctx: EntityContext, rootTable: TableDef, _relationName: string, relation: HasManyRelation) => Promise<Map<string, Rows$3>>;
|
|
@@ -1302,8 +3724,75 @@ declare class DefaultManyToManyCollection<TTarget> implements ManyToManyCollecti
|
|
|
1302
3724
|
toJSON(): TTarget[];
|
|
1303
3725
|
}
|
|
1304
3726
|
|
|
1305
|
-
declare function executeHydrated<TTable extends TableDef>(session: OrmSession, qb: SelectQueryBuilder<any, TTable>): Promise<
|
|
1306
|
-
declare function executeHydratedWithContexts<TTable extends TableDef>(_execCtx: ExecutionContext, hydCtx: HydrationContext, qb: SelectQueryBuilder<any, TTable>): Promise<
|
|
3727
|
+
declare function executeHydrated<TTable extends TableDef>(session: OrmSession, qb: SelectQueryBuilder<any, TTable>): Promise<EntityInstance<TTable>[]>;
|
|
3728
|
+
declare function executeHydratedWithContexts<TTable extends TableDef>(_execCtx: ExecutionContext, hydCtx: HydrationContext, qb: SelectQueryBuilder<any, TTable>): Promise<EntityInstance<TTable>[]>;
|
|
3729
|
+
|
|
3730
|
+
interface StandardDecoratorContext {
|
|
3731
|
+
kind: string;
|
|
3732
|
+
name?: string | symbol;
|
|
3733
|
+
metadata?: Record<PropertyKey, unknown>;
|
|
3734
|
+
addInitializer?(initializer: (this: unknown) => void): void;
|
|
3735
|
+
static?: boolean;
|
|
3736
|
+
private?: boolean;
|
|
3737
|
+
}
|
|
3738
|
+
interface DualModePropertyDecorator {
|
|
3739
|
+
(target: object, propertyKey: string | symbol): void;
|
|
3740
|
+
(value: unknown, context: StandardDecoratorContext): void;
|
|
3741
|
+
}
|
|
3742
|
+
interface DualModeClassDecorator {
|
|
3743
|
+
<TFunction extends Function>(value: TFunction): void | TFunction;
|
|
3744
|
+
<TFunction extends Function>(value: TFunction, context: StandardDecoratorContext): void | TFunction;
|
|
3745
|
+
}
|
|
3746
|
+
|
|
3747
|
+
interface EntityOptions {
|
|
3748
|
+
tableName?: string;
|
|
3749
|
+
hooks?: TableHooks;
|
|
3750
|
+
}
|
|
3751
|
+
declare function Entity(options?: EntityOptions): DualModeClassDecorator;
|
|
3752
|
+
|
|
3753
|
+
interface ColumnOptions {
|
|
3754
|
+
type: ColumnType;
|
|
3755
|
+
args?: ColumnDef['args'];
|
|
3756
|
+
notNull?: boolean;
|
|
3757
|
+
primary?: boolean;
|
|
3758
|
+
}
|
|
3759
|
+
type ColumnInput = ColumnOptions | ColumnDef;
|
|
3760
|
+
declare function Column(definition: ColumnInput): DualModePropertyDecorator;
|
|
3761
|
+
declare function PrimaryKey(definition: ColumnInput): DualModePropertyDecorator;
|
|
3762
|
+
|
|
3763
|
+
interface BaseRelationOptions {
|
|
3764
|
+
target: EntityOrTableTargetResolver;
|
|
3765
|
+
cascade?: CascadeMode;
|
|
3766
|
+
localKey?: string;
|
|
3767
|
+
}
|
|
3768
|
+
interface HasManyOptions extends BaseRelationOptions {
|
|
3769
|
+
foreignKey: string;
|
|
3770
|
+
}
|
|
3771
|
+
interface HasOneOptions extends BaseRelationOptions {
|
|
3772
|
+
foreignKey: string;
|
|
3773
|
+
}
|
|
3774
|
+
interface BelongsToOptions extends BaseRelationOptions {
|
|
3775
|
+
foreignKey: string;
|
|
3776
|
+
}
|
|
3777
|
+
interface BelongsToManyOptions {
|
|
3778
|
+
target: EntityOrTableTargetResolver;
|
|
3779
|
+
pivotTable: EntityOrTableTargetResolver;
|
|
3780
|
+
pivotForeignKeyToRoot: string;
|
|
3781
|
+
pivotForeignKeyToTarget: string;
|
|
3782
|
+
localKey?: string;
|
|
3783
|
+
targetKey?: string;
|
|
3784
|
+
pivotPrimaryKey?: string;
|
|
3785
|
+
defaultPivotColumns?: string[];
|
|
3786
|
+
cascade?: CascadeMode;
|
|
3787
|
+
}
|
|
3788
|
+
declare function HasMany(options: HasManyOptions): DualModePropertyDecorator;
|
|
3789
|
+
declare function HasOne(options: HasOneOptions): DualModePropertyDecorator;
|
|
3790
|
+
declare function BelongsTo(options: BelongsToOptions): DualModePropertyDecorator;
|
|
3791
|
+
declare function BelongsToMany(options: BelongsToManyOptions): DualModePropertyDecorator;
|
|
3792
|
+
|
|
3793
|
+
declare const bootstrapEntities: () => TableDef[];
|
|
3794
|
+
declare const getTableDefFromEntity: (ctor: EntityConstructor) => TableDef | undefined;
|
|
3795
|
+
declare const selectFromEntity: <TTable extends TableDef>(ctor: EntityConstructor) => SelectQueryBuilder<any, TTable>;
|
|
1307
3796
|
|
|
1308
3797
|
interface PostgresClientLike {
|
|
1309
3798
|
query(text: string, params?: unknown[]): Promise<{
|
|
@@ -1376,4 +3865,4 @@ interface CreateTediousClientOptions {
|
|
|
1376
3865
|
declare function createTediousMssqlClient(connection: TediousConnectionLike, { Request, TYPES }: TediousModule, options?: CreateTediousClientOptions): MssqlClientLike;
|
|
1377
3866
|
declare function createTediousExecutor(connection: TediousConnectionLike, module: TediousModule, options?: CreateTediousClientOptions): DbExecutor;
|
|
1378
3867
|
|
|
1379
|
-
export { AsyncLocalStorage, BelongsToManyRelation, BelongsToReference, BelongsToRelation, BetweenExpressionNode, BinaryExpressionNode, CaseExpressionNode, ColumnDef, type ColumnDiff, ColumnNode, ColumnRef, type CreateTediousClientOptions, type DatabaseCheck, type DatabaseColumn, type DatabaseIndex, type DatabaseSchema, type DatabaseTable, DbExecutor, DefaultBelongsToReference, DefaultHasManyCollection, DefaultManyToManyCollection, DeleteQueryBuilder, type DialectName, Entity, EntityContext, ExecutionContext, ExistsExpressionNode, ExpressionNode, type ExpressionVisitor, ForeignKeyReference, FunctionNode, type GroupConcatOptions, HasManyCollection, HasManyRelation, HasOneRelation, HydrationContext, HydrationPlan, InExpressionNode, IndexColumn, IndexDef, InsertQueryBuilder, type IntrospectOptions, JsonPathNode, LiteralNode, type LiteralValue, LogicalExpressionNode, ManyToManyCollection, type MssqlClientLike, MySqlDialect, type MysqlClientLike, NullExpressionNode, OperandNode, type OperandVisitor, OrmSession, type PostgresClientLike, PostgresDialect, RelationMap, type RenderColumnOptions, ScalarSubqueryNode, type SchemaChange, type SchemaChangeKind, type SchemaDiffOptions, type SchemaGenerateResult, type SchemaIntrospector, type SchemaPlan, SelectQueryBuilder, SqlServerDialect, type SqliteClientLike, SqliteDialect, type SynchronizeOptions, TableDef, TableRef, type TediousColumn, type TediousConnectionLike, type TediousModule, type TediousRequest, type TediousRequestCtor, type TediousTypes, TypeScriptGenerator, UpdateQueryBuilder, type ValueOperandInput, WindowFunctionNode, abs, acos, and, ascii, asin, atan, atan2, avg, between, caseWhen, ceil, ceiling, char, charLength, clearExpressionDispatchers, clearOperandDispatchers, columnOperand, concat, concatWs, correlateBy, cos, cot, count, createEntityFromRow, createEntityProxy, createMssqlExecutor, createMysqlExecutor, createPostgresExecutor, createSqliteExecutor, createTediousExecutor, createTediousMssqlClient, currentDate, currentTime, dateAdd, dateDiff, dateFormat, dateSub, dateTrunc, day, dayOfWeek, degrees, denseRank, diffSchema, endOfMonth, eq, esel, executeHydrated, executeHydratedWithContexts, exists, exp, extract, firstValue, floor, fromUnixTime, generateCreateTableSql, generateSchemaSql, getSchemaIntrospector, groupConcat, gt, gte, hydrateRows, inList, instr, introspectSchema, isNotNull, isNull, isValueOperandInput, jsonPath, lag, lastValue, lead, left, length, like, ln, loadBelongsToManyRelation, loadBelongsToRelation, loadHasManyRelation, loadHasOneRelation, locate, log, log10, logBase, lower, lpad, lt, lte, ltrim, max, min, mod, month, neq, notBetween, notExists, notInList, notLike, now, ntile, or, outerRef, pi, position, pow, power, radians, rand, random, rank, registerExpressionDispatcher, registerOperandDispatcher, registerSchemaIntrospector, renderColumnDefinition, repeat, replace, right, round, rowNumber, rpad, rtrim, sel, sign, sin, space, sqrt, substr, sum, synchronizeSchema, tan, toColumnRef, toTableRef, trim, trunc, truncate, unixTimestamp, upper, utcNow, valueToOperand, visitExpression, visitOperand, weekOfYear, windowFunction, year };
|
|
3868
|
+
export { type AnyDomainEvent, AsyncLocalStorage, BelongsTo, BelongsToMany, type BelongsToManyOptions, type BelongsToManyRelation, type BelongsToOptions, type BelongsToReference, type BelongsToRelation, type BetweenExpressionNode, type BinaryExpressionNode, type CascadeMode, type CaseExpressionNode, type CheckConstraint, Column, type ColumnDef, type ColumnDiff, type ColumnInput, type ColumnNode, type ColumnOptions, type ColumnRef, type ColumnToTs, type ColumnType, type CreateTediousClientOptions, type DatabaseCheck, type DatabaseColumn, type DatabaseIndex, type DatabaseSchema, type DatabaseTable, type DbExecutor, type DbExecutorFactory, DefaultBelongsToReference, DefaultHasManyCollection, DefaultManyToManyCollection, type DefaultValue, DeleteQueryBuilder, type DialectName, type DomainEvent, DomainEventBus, type DomainEventHandler, Entity, type EntityContext, type EntityInstance, type EntityOptions, EntityStatus, type ExecutionContext, type ExistsExpressionNode, type ExpressionNode, type ExpressionVisitor, type ExternalTransaction, type ForeignKeyReference, type FunctionNode, type GroupConcatOptions, type HasDomainEvents, HasMany, type HasManyCollection, type HasManyOptions, type HasManyRelation, HasOne, type HasOneOptions, type HasOneReference, type HasOneRelation, type HydrationContext, type HydrationMetadata, type HydrationPivotPlan, type HydrationPlan, type HydrationRelationPlan, type InExpressionNode, type IndexColumn, type IndexDef, type InferRow, type InitialHandlers, InsertQueryBuilder, type IntrospectOptions, type JsonPathNode, type LiteralNode, type LiteralValue, type LogicalExpressionNode, type ManyToManyCollection, type MssqlClientLike, MySqlDialect, type MysqlClientLike, type NullExpressionNode, type OperandNode, type OperandVisitor, Orm, type OrmDomainEvent, type OrmInterceptor, type OrmOptions, OrmSession, type OrmSessionOptions, type PostgresClientLike, PostgresDialect, PrimaryKey, type QueryLogEntry, type QueryLogger, type QueryResult, type RawDefaultValue, type ReferentialAction, type RelationChange, type RelationChangeEntry, type RelationDef, type RelationKey, RelationKinds, type RelationMap, type RelationTargetTable, type RelationType, type RenderColumnOptions, type ScalarSubqueryNode, type SchemaChange, type SchemaChangeKind, type SchemaDiffOptions, type SchemaGenerateResult, type SchemaIntrospector, type SchemaPlan, SelectQueryBuilder, type SimpleQueryRunner, SqlServerDialect, type SqliteClientLike, SqliteDialect, type SynchronizeOptions, type TableDef, type TableHooks, type TableOptions, type TableRef, type TediousColumn, type TediousConnectionLike, type TediousModule, type TediousRequest, type TediousRequestCtor, type TediousTypes, type TrackedEntity, TypeScriptGenerator, UpdateQueryBuilder, type ValueOperandInput, type WindowFunctionNode, abs, acos, addDomainEvent, and, ascii, asin, atan, atan2, avg, belongsTo, belongsToMany, between, bootstrapEntities, caseWhen, ceil, ceiling, char, charLength, clearExpressionDispatchers, clearOperandDispatchers, col, columnOperand, concat, concatWs, correlateBy, cos, cot, count, createColumn, createEntityFromRow, createEntityProxy, createExecutorFromQueryRunner, createLiteral, createMssqlExecutor, createMysqlExecutor, createPostgresExecutor, createQueryLoggingExecutor, createSqliteExecutor, createTediousExecutor, createTediousMssqlClient, currentDate, currentTime, dateAdd, dateDiff, dateFormat, dateSub, dateTrunc, day, dayOfWeek, defineTable, degrees, denseRank, diffSchema, endOfMonth, eq, esel, executeHydrated, executeHydratedWithContexts, exists, exp, extract, firstValue, floor, fromUnixTime, generateCreateTableSql, generateSchemaSql, getSchemaIntrospector, getTableDefFromEntity, groupConcat, gt, gte, hasMany, hasOne, hydrateRows, inList, instr, introspectSchema, isCaseExpressionNode, isExpressionSelectionNode, isFunctionNode, isNotNull, isNull, isOperandNode, isValueOperandInput, isWindowFunctionNode, jsonPath, lag, lastValue, lead, left, length, like, ln, loadBelongsToManyRelation, loadBelongsToRelation, loadHasManyRelation, loadHasOneRelation, locate, log, log10, logBase, lower, lpad, lt, lte, ltrim, max, min, mod, month, neq, notBetween, notExists, notInList, notLike, now, ntile, or, outerRef, pi, position, pow, power, radians, rand, random, rank, registerExpressionDispatcher, registerOperandDispatcher, registerSchemaIntrospector, renderColumnDefinition, repeat, replace, right, round, rowNumber, rowsToQueryResult, rpad, rtrim, sel, selectFromEntity, sign, sin, space, sqrt, substr, sum, synchronizeSchema, tan, toColumnRef, toTableRef, trim, trunc, truncate, unixTimestamp, upper, utcNow, valueToOperand, visitExpression, visitOperand, weekOfYear, windowFunction, year };
|