metal-orm 1.0.32 → 1.0.34
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 +2585 -98
- package/dist/index.d.ts +2585 -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/types.ts +39 -39
- 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
|
@@ -1,2424 +0,0 @@
|
|
|
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> {
|
|
28
|
-
/** Column name (filled at runtime by defineTable) */
|
|
29
|
-
name: string;
|
|
30
|
-
/** Data type of the column */
|
|
31
|
-
type: T;
|
|
32
|
-
/** Whether this column is a primary key */
|
|
33
|
-
primary?: boolean;
|
|
34
|
-
/** Whether this column cannot be null */
|
|
35
|
-
notNull?: boolean;
|
|
36
|
-
/** Whether this column must be unique (or name of the unique constraint) */
|
|
37
|
-
unique?: boolean | string;
|
|
38
|
-
/** Default value for the column */
|
|
39
|
-
default?: DefaultValue;
|
|
40
|
-
/** Whether the column auto-increments / identity */
|
|
41
|
-
autoIncrement?: boolean;
|
|
42
|
-
/** Identity strategy where supported */
|
|
43
|
-
generated?: 'always' | 'byDefault';
|
|
44
|
-
/** Inline check constraint expression */
|
|
45
|
-
check?: string;
|
|
46
|
-
/** Foreign key reference */
|
|
47
|
-
references?: ForeignKeyReference;
|
|
48
|
-
/** Column comment/description */
|
|
49
|
-
comment?: string;
|
|
50
|
-
/** Additional arguments for the column type (e.g., VARCHAR length) */
|
|
51
|
-
args?: any[];
|
|
52
|
-
/** Table name this column belongs to (filled at runtime by defineTable) */
|
|
53
|
-
table?: string;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Factory for creating column definitions with common data types
|
|
57
|
-
*/
|
|
58
|
-
declare const col: {
|
|
59
|
-
/**
|
|
60
|
-
* Creates an integer column definition
|
|
61
|
-
* @returns ColumnDef with INT type
|
|
62
|
-
*/
|
|
63
|
-
int: () => ColumnDef<"INT">;
|
|
64
|
-
/**
|
|
65
|
-
* Creates a big integer column definition
|
|
66
|
-
*/
|
|
67
|
-
bigint: () => ColumnDef<"BIGINT">;
|
|
68
|
-
/**
|
|
69
|
-
* Creates a variable character column definition
|
|
70
|
-
* @param length - Maximum length of the string
|
|
71
|
-
* @returns ColumnDef with VARCHAR type
|
|
72
|
-
*/
|
|
73
|
-
varchar: (length: number) => ColumnDef<"VARCHAR">;
|
|
74
|
-
/**
|
|
75
|
-
* Creates a fixed precision decimal column definition
|
|
76
|
-
*/
|
|
77
|
-
decimal: (precision: number, scale?: number) => ColumnDef<"DECIMAL">;
|
|
78
|
-
/**
|
|
79
|
-
* Creates a floating point column definition
|
|
80
|
-
*/
|
|
81
|
-
float: (precision?: number) => ColumnDef<"FLOAT">;
|
|
82
|
-
/**
|
|
83
|
-
* Creates a UUID column definition
|
|
84
|
-
*/
|
|
85
|
-
uuid: () => ColumnDef<"UUID">;
|
|
86
|
-
/**
|
|
87
|
-
* Creates a binary large object column definition
|
|
88
|
-
*/
|
|
89
|
-
blob: () => ColumnDef<"BLOB">;
|
|
90
|
-
/**
|
|
91
|
-
* Creates a fixed-length binary column definition
|
|
92
|
-
*/
|
|
93
|
-
binary: (length?: number) => ColumnDef<"BINARY">;
|
|
94
|
-
/**
|
|
95
|
-
* Creates a variable-length binary column definition
|
|
96
|
-
*/
|
|
97
|
-
varbinary: (length?: number) => ColumnDef<"VARBINARY">;
|
|
98
|
-
/**
|
|
99
|
-
* Creates a Postgres bytea column definition
|
|
100
|
-
*/
|
|
101
|
-
bytea: () => ColumnDef<"BYTEA">;
|
|
102
|
-
/**
|
|
103
|
-
* Creates a timestamp column definition
|
|
104
|
-
*/
|
|
105
|
-
timestamp: () => ColumnDef<"TIMESTAMP">;
|
|
106
|
-
/**
|
|
107
|
-
* Creates a timestamptz column definition
|
|
108
|
-
*/
|
|
109
|
-
timestamptz: () => ColumnDef<"TIMESTAMPTZ">;
|
|
110
|
-
/**
|
|
111
|
-
* Creates a date column definition
|
|
112
|
-
*/
|
|
113
|
-
date: () => ColumnDef<"DATE">;
|
|
114
|
-
/**
|
|
115
|
-
* Creates a datetime column definition
|
|
116
|
-
*/
|
|
117
|
-
datetime: () => ColumnDef<"DATETIME">;
|
|
118
|
-
/**
|
|
119
|
-
* Creates a JSON column definition
|
|
120
|
-
* @returns ColumnDef with JSON type
|
|
121
|
-
*/
|
|
122
|
-
json: () => ColumnDef<"JSON">;
|
|
123
|
-
/**
|
|
124
|
-
* Creates a boolean column definition
|
|
125
|
-
* @returns ColumnDef with BOOLEAN type
|
|
126
|
-
*/
|
|
127
|
-
boolean: () => ColumnDef<"BOOLEAN">;
|
|
128
|
-
/**
|
|
129
|
-
* Creates an enum column definition
|
|
130
|
-
* @param values - Enum values
|
|
131
|
-
*/
|
|
132
|
-
enum: (values: string[]) => ColumnDef<"ENUM">;
|
|
133
|
-
/**
|
|
134
|
-
* Marks a column definition as a primary key
|
|
135
|
-
* @param def - Column definition to modify
|
|
136
|
-
* @returns Modified ColumnDef with primary: true
|
|
137
|
-
*/
|
|
138
|
-
primaryKey: <T extends ColumnType>(def: ColumnDef<T>) => ColumnDef<T>;
|
|
139
|
-
/**
|
|
140
|
-
* Marks a column as NOT NULL
|
|
141
|
-
*/
|
|
142
|
-
notNull: <T extends ColumnType>(def: ColumnDef<T>) => ColumnDef<T>;
|
|
143
|
-
/**
|
|
144
|
-
* Marks a column as UNIQUE
|
|
145
|
-
*/
|
|
146
|
-
unique: <T extends ColumnType>(def: ColumnDef<T>, name?: string) => ColumnDef<T>;
|
|
147
|
-
/**
|
|
148
|
-
* Sets a default value for the column
|
|
149
|
-
*/
|
|
150
|
-
default: <T extends ColumnType>(def: ColumnDef<T>, value: unknown) => ColumnDef<T>;
|
|
151
|
-
/**
|
|
152
|
-
* Sets a raw SQL default value for the column
|
|
153
|
-
*/
|
|
154
|
-
defaultRaw: <T extends ColumnType>(def: ColumnDef<T>, expression: string) => ColumnDef<T>;
|
|
155
|
-
/**
|
|
156
|
-
* Marks a column as auto-increment / identity
|
|
157
|
-
*/
|
|
158
|
-
autoIncrement: <T extends ColumnType>(def: ColumnDef<T>, strategy?: ColumnDef["generated"]) => ColumnDef<T>;
|
|
159
|
-
/**
|
|
160
|
-
* Adds a foreign key reference
|
|
161
|
-
*/
|
|
162
|
-
references: <T extends ColumnType>(def: ColumnDef<T>, ref: ForeignKeyReference) => ColumnDef<T>;
|
|
163
|
-
/**
|
|
164
|
-
* Adds a check constraint to the column
|
|
165
|
-
*/
|
|
166
|
-
check: <T extends ColumnType>(def: ColumnDef<T>, expression: string) => ColumnDef<T>;
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Types of relationships supported between tables
|
|
171
|
-
*/
|
|
172
|
-
declare const RelationKinds: {
|
|
173
|
-
/** One-to-one relationship */
|
|
174
|
-
readonly HasOne: "HAS_ONE";
|
|
175
|
-
/** One-to-many relationship */
|
|
176
|
-
readonly HasMany: "HAS_MANY";
|
|
177
|
-
/** Many-to-one relationship */
|
|
178
|
-
readonly BelongsTo: "BELONGS_TO";
|
|
179
|
-
/** Many-to-many relationship with pivot metadata */
|
|
180
|
-
readonly BelongsToMany: "BELONGS_TO_MANY";
|
|
181
|
-
};
|
|
182
|
-
/**
|
|
183
|
-
* Type representing the supported relationship kinds
|
|
184
|
-
*/
|
|
185
|
-
type RelationType = (typeof RelationKinds)[keyof typeof RelationKinds];
|
|
186
|
-
type CascadeMode = 'none' | 'all' | 'persist' | 'remove' | 'link';
|
|
187
|
-
/**
|
|
188
|
-
* One-to-many relationship definition
|
|
189
|
-
*/
|
|
190
|
-
interface HasManyRelation<TTarget extends TableDef = TableDef> {
|
|
191
|
-
type: typeof RelationKinds.HasMany;
|
|
192
|
-
target: TTarget;
|
|
193
|
-
foreignKey: string;
|
|
194
|
-
localKey?: string;
|
|
195
|
-
cascade?: CascadeMode;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* One-to-one relationship definition
|
|
199
|
-
*/
|
|
200
|
-
interface HasOneRelation<TTarget extends TableDef = TableDef> {
|
|
201
|
-
type: typeof RelationKinds.HasOne;
|
|
202
|
-
target: TTarget;
|
|
203
|
-
foreignKey: string;
|
|
204
|
-
localKey?: string;
|
|
205
|
-
cascade?: CascadeMode;
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* Many-to-one relationship definition
|
|
209
|
-
*/
|
|
210
|
-
interface BelongsToRelation<TTarget extends TableDef = TableDef> {
|
|
211
|
-
type: typeof RelationKinds.BelongsTo;
|
|
212
|
-
target: TTarget;
|
|
213
|
-
foreignKey: string;
|
|
214
|
-
localKey?: string;
|
|
215
|
-
cascade?: CascadeMode;
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Many-to-many relationship definition with rich pivot metadata
|
|
219
|
-
*/
|
|
220
|
-
interface BelongsToManyRelation<TTarget extends TableDef = TableDef> {
|
|
221
|
-
type: typeof RelationKinds.BelongsToMany;
|
|
222
|
-
target: TTarget;
|
|
223
|
-
pivotTable: TableDef;
|
|
224
|
-
pivotForeignKeyToRoot: string;
|
|
225
|
-
pivotForeignKeyToTarget: string;
|
|
226
|
-
localKey?: string;
|
|
227
|
-
targetKey?: string;
|
|
228
|
-
pivotPrimaryKey?: string;
|
|
229
|
-
defaultPivotColumns?: string[];
|
|
230
|
-
cascade?: CascadeMode;
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* Union type representing any supported relationship definition
|
|
234
|
-
*/
|
|
235
|
-
type RelationDef = HasManyRelation | HasOneRelation | BelongsToRelation | BelongsToManyRelation;
|
|
236
|
-
/**
|
|
237
|
-
* Creates a one-to-many relationship definition
|
|
238
|
-
* @param target - Target table of the relationship
|
|
239
|
-
* @param foreignKey - Foreign key column name on the child table
|
|
240
|
-
* @param localKey - Local key column name (optional)
|
|
241
|
-
* @returns HasManyRelation definition
|
|
242
|
-
*
|
|
243
|
-
* @example
|
|
244
|
-
* ```typescript
|
|
245
|
-
* hasMany(usersTable, 'user_id')
|
|
246
|
-
* ```
|
|
247
|
-
*/
|
|
248
|
-
declare const hasMany: <TTarget extends TableDef>(target: TTarget, foreignKey: string, localKey?: string, cascade?: CascadeMode) => HasManyRelation<TTarget>;
|
|
249
|
-
/**
|
|
250
|
-
* Creates a one-to-one relationship definition
|
|
251
|
-
* @param target - Target table of the relationship
|
|
252
|
-
* @param foreignKey - Foreign key column name on the child table
|
|
253
|
-
* @param localKey - Local key column name (optional)
|
|
254
|
-
* @returns HasOneRelation definition
|
|
255
|
-
*/
|
|
256
|
-
declare const hasOne: <TTarget extends TableDef>(target: TTarget, foreignKey: string, localKey?: string, cascade?: CascadeMode) => HasOneRelation<TTarget>;
|
|
257
|
-
/**
|
|
258
|
-
* Creates a many-to-one relationship definition
|
|
259
|
-
* @param target - Target table of the relationship
|
|
260
|
-
* @param foreignKey - Foreign key column name on the child table
|
|
261
|
-
* @param localKey - Local key column name (optional)
|
|
262
|
-
* @returns BelongsToRelation definition
|
|
263
|
-
*
|
|
264
|
-
* @example
|
|
265
|
-
* ```typescript
|
|
266
|
-
* belongsTo(usersTable, 'user_id')
|
|
267
|
-
* ```
|
|
268
|
-
*/
|
|
269
|
-
declare const belongsTo: <TTarget extends TableDef>(target: TTarget, foreignKey: string, localKey?: string, cascade?: CascadeMode) => BelongsToRelation<TTarget>;
|
|
270
|
-
/**
|
|
271
|
-
* Creates a many-to-many relationship definition with pivot metadata
|
|
272
|
-
* @param target - Target table
|
|
273
|
-
* @param pivotTable - Intermediate pivot table definition
|
|
274
|
-
* @param options - Pivot metadata configuration
|
|
275
|
-
* @returns BelongsToManyRelation definition
|
|
276
|
-
*/
|
|
277
|
-
declare const belongsToMany: <TTarget extends TableDef>(target: TTarget, pivotTable: TableDef, options: {
|
|
278
|
-
pivotForeignKeyToRoot: string;
|
|
279
|
-
pivotForeignKeyToTarget: string;
|
|
280
|
-
localKey?: string;
|
|
281
|
-
targetKey?: string;
|
|
282
|
-
pivotPrimaryKey?: string;
|
|
283
|
-
defaultPivotColumns?: string[];
|
|
284
|
-
cascade?: CascadeMode;
|
|
285
|
-
}) => BelongsToManyRelation<TTarget>;
|
|
286
|
-
|
|
287
|
-
interface IndexColumn {
|
|
288
|
-
column: string;
|
|
289
|
-
order?: 'ASC' | 'DESC';
|
|
290
|
-
nulls?: 'FIRST' | 'LAST';
|
|
291
|
-
}
|
|
292
|
-
interface IndexDef {
|
|
293
|
-
name?: string;
|
|
294
|
-
columns: (string | IndexColumn)[];
|
|
295
|
-
unique?: boolean;
|
|
296
|
-
where?: string;
|
|
297
|
-
}
|
|
298
|
-
interface CheckConstraint {
|
|
299
|
-
name?: string;
|
|
300
|
-
expression: string;
|
|
301
|
-
}
|
|
302
|
-
interface TableOptions {
|
|
303
|
-
schema?: string;
|
|
304
|
-
primaryKey?: string[];
|
|
305
|
-
indexes?: IndexDef[];
|
|
306
|
-
checks?: CheckConstraint[];
|
|
307
|
-
comment?: string;
|
|
308
|
-
engine?: string;
|
|
309
|
-
charset?: string;
|
|
310
|
-
collation?: string;
|
|
311
|
-
}
|
|
312
|
-
interface TableHooks {
|
|
313
|
-
beforeInsert?(ctx: unknown, entity: any): Promise<void> | void;
|
|
314
|
-
afterInsert?(ctx: unknown, entity: any): Promise<void> | void;
|
|
315
|
-
beforeUpdate?(ctx: unknown, entity: any): Promise<void> | void;
|
|
316
|
-
afterUpdate?(ctx: unknown, entity: any): Promise<void> | void;
|
|
317
|
-
beforeDelete?(ctx: unknown, entity: any): Promise<void> | void;
|
|
318
|
-
afterDelete?(ctx: unknown, entity: any): Promise<void> | void;
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
* Definition of a database table with its columns and relationships
|
|
322
|
-
* @typeParam T - Type of the columns record
|
|
323
|
-
*/
|
|
324
|
-
interface TableDef<T extends Record<string, ColumnDef> = Record<string, ColumnDef>> {
|
|
325
|
-
/** Name of the table */
|
|
326
|
-
name: string;
|
|
327
|
-
/** Optional schema/catalog name */
|
|
328
|
-
schema?: string;
|
|
329
|
-
/** Record of column definitions keyed by column name */
|
|
330
|
-
columns: T;
|
|
331
|
-
/** Record of relationship definitions keyed by relation name */
|
|
332
|
-
relations: Record<string, RelationDef>;
|
|
333
|
-
/** Optional lifecycle hooks */
|
|
334
|
-
hooks?: TableHooks;
|
|
335
|
-
/** Composite primary key definition (falls back to column.primary flags) */
|
|
336
|
-
primaryKey?: string[];
|
|
337
|
-
/** Secondary indexes */
|
|
338
|
-
indexes?: IndexDef[];
|
|
339
|
-
/** Table-level check constraints */
|
|
340
|
-
checks?: CheckConstraint[];
|
|
341
|
-
/** Table comment/description */
|
|
342
|
-
comment?: string;
|
|
343
|
-
/** Dialect-specific options */
|
|
344
|
-
engine?: string;
|
|
345
|
-
charset?: string;
|
|
346
|
-
collation?: string;
|
|
347
|
-
}
|
|
348
|
-
/**
|
|
349
|
-
* Creates a table definition with columns and relationships
|
|
350
|
-
* @typeParam T - Type of the columns record
|
|
351
|
-
* @param name - Name of the table
|
|
352
|
-
* @param columns - Record of column definitions
|
|
353
|
-
* @param relations - Record of relationship definitions (optional)
|
|
354
|
-
* @returns Complete table definition with runtime-filled column metadata
|
|
355
|
-
*
|
|
356
|
-
* @example
|
|
357
|
-
* ```typescript
|
|
358
|
-
* const usersTable = defineTable('users', {
|
|
359
|
-
* id: col.primaryKey(col.int()),
|
|
360
|
-
* name: col.varchar(255),
|
|
361
|
-
* email: col.varchar(255)
|
|
362
|
-
* });
|
|
363
|
-
* ```
|
|
364
|
-
*/
|
|
365
|
-
declare const defineTable: <T extends Record<string, ColumnDef>>(name: string, columns: T, relations?: Record<string, RelationDef>, hooks?: TableHooks, options?: TableOptions) => TableDef<T>;
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* Resolves a relation definition to its target table type.
|
|
369
|
-
*/
|
|
370
|
-
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;
|
|
371
|
-
/**
|
|
372
|
-
* Maps a ColumnDef to its TypeScript type representation
|
|
373
|
-
*/
|
|
374
|
-
type ColumnToTs<T extends ColumnDef> = 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;
|
|
375
|
-
/**
|
|
376
|
-
* Infers a row shape from a table definition
|
|
377
|
-
*/
|
|
378
|
-
type InferRow<TTable extends TableDef> = {
|
|
379
|
-
[K in keyof TTable['columns']]: ColumnToTs<TTable['columns'][K]>;
|
|
380
|
-
};
|
|
381
|
-
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> & {
|
|
382
|
-
_pivot?: any;
|
|
383
|
-
})[] : never;
|
|
384
|
-
/**
|
|
385
|
-
* Maps relation names to the expected row results
|
|
386
|
-
*/
|
|
387
|
-
type RelationMap<TTable extends TableDef> = {
|
|
388
|
-
[K in keyof TTable['relations']]: RelationResult$1<TTable['relations'][K]>;
|
|
389
|
-
};
|
|
390
|
-
interface HasManyCollection<TChild> {
|
|
391
|
-
load(): Promise<TChild[]>;
|
|
392
|
-
getItems(): TChild[];
|
|
393
|
-
add(data: Partial<TChild>): TChild;
|
|
394
|
-
attach(entity: TChild): void;
|
|
395
|
-
remove(entity: TChild): void;
|
|
396
|
-
clear(): void;
|
|
397
|
-
}
|
|
398
|
-
interface BelongsToReference<TParent> {
|
|
399
|
-
load(): Promise<TParent | null>;
|
|
400
|
-
get(): TParent | null;
|
|
401
|
-
set(data: Partial<TParent> | TParent | null): TParent | null;
|
|
402
|
-
}
|
|
403
|
-
interface HasOneReference<TChild> {
|
|
404
|
-
load(): Promise<TChild | null>;
|
|
405
|
-
get(): TChild | null;
|
|
406
|
-
set(data: Partial<TChild> | TChild | null): TChild | null;
|
|
407
|
-
}
|
|
408
|
-
interface ManyToManyCollection<TTarget> {
|
|
409
|
-
load(): Promise<TTarget[]>;
|
|
410
|
-
getItems(): TTarget[];
|
|
411
|
-
attach(target: TTarget | number | string): void;
|
|
412
|
-
detach(target: TTarget | number | string): void;
|
|
413
|
-
syncByIds(ids: (number | string)[]): Promise<void>;
|
|
414
|
-
}
|
|
415
|
-
type Entity<TTable extends TableDef, TRow = InferRow<TTable>> = TRow & {
|
|
416
|
-
[K in keyof RelationMap<TTable>]: TTable['relations'][K] extends HasManyRelation<infer TTarget> ? HasManyCollection<Entity<TTarget>> : TTable['relations'][K] extends HasOneRelation<infer TTarget> ? HasOneReference<Entity<TTarget>> : TTable['relations'][K] extends BelongsToManyRelation<infer TTarget> ? ManyToManyCollection<Entity<TTarget>> : TTable['relations'][K] extends BelongsToRelation<infer TTarget> ? BelongsToReference<Entity<TTarget>> : never;
|
|
417
|
-
} & {
|
|
418
|
-
$load<K extends keyof RelationMap<TTable>>(relation: K): Promise<RelationMap<TTable>[K]>;
|
|
419
|
-
};
|
|
420
|
-
|
|
421
|
-
/**
|
|
422
|
-
* SQL operators used in query conditions
|
|
423
|
-
*/
|
|
424
|
-
declare const SQL_OPERATORS: {
|
|
425
|
-
/** Equality operator */
|
|
426
|
-
readonly EQUALS: "=";
|
|
427
|
-
/** Not equals operator */
|
|
428
|
-
readonly NOT_EQUALS: "!=";
|
|
429
|
-
/** Greater than operator */
|
|
430
|
-
readonly GREATER_THAN: ">";
|
|
431
|
-
/** Greater than or equal operator */
|
|
432
|
-
readonly GREATER_OR_EQUAL: ">=";
|
|
433
|
-
/** Less than operator */
|
|
434
|
-
readonly LESS_THAN: "<";
|
|
435
|
-
/** Less than or equal operator */
|
|
436
|
-
readonly LESS_OR_EQUAL: "<=";
|
|
437
|
-
/** LIKE pattern matching operator */
|
|
438
|
-
readonly LIKE: "LIKE";
|
|
439
|
-
/** NOT LIKE pattern matching operator */
|
|
440
|
-
readonly NOT_LIKE: "NOT LIKE";
|
|
441
|
-
/** IN membership operator */
|
|
442
|
-
readonly IN: "IN";
|
|
443
|
-
/** NOT IN membership operator */
|
|
444
|
-
readonly NOT_IN: "NOT IN";
|
|
445
|
-
/** BETWEEN range operator */
|
|
446
|
-
readonly BETWEEN: "BETWEEN";
|
|
447
|
-
/** NOT BETWEEN range operator */
|
|
448
|
-
readonly NOT_BETWEEN: "NOT BETWEEN";
|
|
449
|
-
/** IS NULL null check operator */
|
|
450
|
-
readonly IS_NULL: "IS NULL";
|
|
451
|
-
/** IS NOT NULL null check operator */
|
|
452
|
-
readonly IS_NOT_NULL: "IS NOT NULL";
|
|
453
|
-
/** Logical AND operator */
|
|
454
|
-
readonly AND: "AND";
|
|
455
|
-
/** Logical OR operator */
|
|
456
|
-
readonly OR: "OR";
|
|
457
|
-
/** EXISTS operator */
|
|
458
|
-
readonly EXISTS: "EXISTS";
|
|
459
|
-
/** NOT EXISTS operator */
|
|
460
|
-
readonly NOT_EXISTS: "NOT EXISTS";
|
|
461
|
-
};
|
|
462
|
-
/**
|
|
463
|
-
* Type representing any supported SQL operator
|
|
464
|
-
*/
|
|
465
|
-
type SqlOperator = (typeof SQL_OPERATORS)[keyof typeof SQL_OPERATORS];
|
|
466
|
-
/**
|
|
467
|
-
* Types of SQL joins supported
|
|
468
|
-
*/
|
|
469
|
-
declare const JOIN_KINDS: {
|
|
470
|
-
/** INNER JOIN type */
|
|
471
|
-
readonly INNER: "INNER";
|
|
472
|
-
/** LEFT JOIN type */
|
|
473
|
-
readonly LEFT: "LEFT";
|
|
474
|
-
/** RIGHT JOIN type */
|
|
475
|
-
readonly RIGHT: "RIGHT";
|
|
476
|
-
/** CROSS JOIN type */
|
|
477
|
-
readonly CROSS: "CROSS";
|
|
478
|
-
};
|
|
479
|
-
/**
|
|
480
|
-
* Type representing any supported join kind
|
|
481
|
-
*/
|
|
482
|
-
type JoinKind = (typeof JOIN_KINDS)[keyof typeof JOIN_KINDS];
|
|
483
|
-
/**
|
|
484
|
-
* Ordering directions for result sorting
|
|
485
|
-
*/
|
|
486
|
-
declare const ORDER_DIRECTIONS: {
|
|
487
|
-
/** Ascending order */
|
|
488
|
-
readonly ASC: "ASC";
|
|
489
|
-
/** Descending order */
|
|
490
|
-
readonly DESC: "DESC";
|
|
491
|
-
};
|
|
492
|
-
/**
|
|
493
|
-
* Type representing any supported order direction
|
|
494
|
-
*/
|
|
495
|
-
type OrderDirection = (typeof ORDER_DIRECTIONS)[keyof typeof ORDER_DIRECTIONS];
|
|
496
|
-
/**
|
|
497
|
-
* Supported database dialects
|
|
498
|
-
*/
|
|
499
|
-
declare const SUPPORTED_DIALECTS: {
|
|
500
|
-
/** MySQL database dialect */
|
|
501
|
-
readonly MYSQL: "mysql";
|
|
502
|
-
/** SQLite database dialect */
|
|
503
|
-
readonly SQLITE: "sqlite";
|
|
504
|
-
/** Microsoft SQL Server dialect */
|
|
505
|
-
readonly MSSQL: "mssql";
|
|
506
|
-
/** PostgreSQL database dialect */
|
|
507
|
-
readonly POSTGRES: "postgres";
|
|
508
|
-
};
|
|
509
|
-
/**
|
|
510
|
-
* Type representing any supported database dialect
|
|
511
|
-
*/
|
|
512
|
-
type DialectName = (typeof SUPPORTED_DIALECTS)[keyof typeof SUPPORTED_DIALECTS];
|
|
513
|
-
|
|
514
|
-
/**
|
|
515
|
-
* Minimal column reference used by AST builders.
|
|
516
|
-
* Accepts any object with a name and optional table/alias fields
|
|
517
|
-
* (schema ColumnDef/TableDef remain structurally compatible).
|
|
518
|
-
*/
|
|
519
|
-
interface ColumnRef {
|
|
520
|
-
name: string;
|
|
521
|
-
table?: string;
|
|
522
|
-
alias?: string;
|
|
523
|
-
}
|
|
524
|
-
/**
|
|
525
|
-
* Minimal table reference used by AST builders.
|
|
526
|
-
* Keeps AST decoupled from full schema TableDef shape.
|
|
527
|
-
*/
|
|
528
|
-
interface TableRef {
|
|
529
|
-
name: string;
|
|
530
|
-
schema?: string;
|
|
531
|
-
alias?: string;
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
/**
|
|
535
|
-
* AST node representing a literal value
|
|
536
|
-
*/
|
|
537
|
-
interface LiteralNode {
|
|
538
|
-
type: 'Literal';
|
|
539
|
-
/** The literal value (string, number, boolean, or null) */
|
|
540
|
-
value: string | number | boolean | null;
|
|
541
|
-
}
|
|
542
|
-
/**
|
|
543
|
-
* AST node representing a column reference
|
|
544
|
-
*/
|
|
545
|
-
interface ColumnNode {
|
|
546
|
-
type: 'Column';
|
|
547
|
-
/** Table name the column belongs to */
|
|
548
|
-
table: string;
|
|
549
|
-
/** Column name */
|
|
550
|
-
name: string;
|
|
551
|
-
/** Optional alias for the column */
|
|
552
|
-
alias?: string;
|
|
553
|
-
/** Optional scope marker (e.g., 'outer' for correlated references) */
|
|
554
|
-
scope?: 'outer' | 'default';
|
|
555
|
-
}
|
|
556
|
-
/**
|
|
557
|
-
* AST node representing a function call
|
|
558
|
-
*/
|
|
559
|
-
interface FunctionNode {
|
|
560
|
-
type: 'Function';
|
|
561
|
-
/** Function name (e.g., COUNT, SUM) */
|
|
562
|
-
name: string;
|
|
563
|
-
/** Optional canonical function key for dialect-aware rendering */
|
|
564
|
-
fn?: string;
|
|
565
|
-
/** Function arguments */
|
|
566
|
-
args: OperandNode[];
|
|
567
|
-
/** Optional alias for the function result */
|
|
568
|
-
alias?: string;
|
|
569
|
-
/** Optional ORDER BY clause used by aggregations like GROUP_CONCAT */
|
|
570
|
-
orderBy?: OrderByNode[];
|
|
571
|
-
/** Optional separator argument used by GROUP_CONCAT-like functions */
|
|
572
|
-
separator?: OperandNode;
|
|
573
|
-
/** Optional DISTINCT modifier */
|
|
574
|
-
distinct?: boolean;
|
|
575
|
-
}
|
|
576
|
-
/**
|
|
577
|
-
* AST node representing a JSON path expression
|
|
578
|
-
*/
|
|
579
|
-
interface JsonPathNode {
|
|
580
|
-
type: 'JsonPath';
|
|
581
|
-
/** Source column */
|
|
582
|
-
column: ColumnNode;
|
|
583
|
-
/** JSON path expression */
|
|
584
|
-
path: string;
|
|
585
|
-
/** Optional alias for the result */
|
|
586
|
-
alias?: string;
|
|
587
|
-
}
|
|
588
|
-
/**
|
|
589
|
-
* AST node representing a scalar subquery
|
|
590
|
-
*/
|
|
591
|
-
interface ScalarSubqueryNode {
|
|
592
|
-
type: 'ScalarSubquery';
|
|
593
|
-
/** Subquery to execute */
|
|
594
|
-
query: SelectQueryNode;
|
|
595
|
-
/** Optional alias for the subquery result */
|
|
596
|
-
alias?: string;
|
|
597
|
-
}
|
|
598
|
-
/**
|
|
599
|
-
* AST node representing a CASE expression
|
|
600
|
-
*/
|
|
601
|
-
interface CaseExpressionNode {
|
|
602
|
-
type: 'CaseExpression';
|
|
603
|
-
/** WHEN-THEN conditions */
|
|
604
|
-
conditions: {
|
|
605
|
-
when: ExpressionNode;
|
|
606
|
-
then: OperandNode;
|
|
607
|
-
}[];
|
|
608
|
-
/** Optional ELSE clause */
|
|
609
|
-
else?: OperandNode;
|
|
610
|
-
/** Optional alias for the result */
|
|
611
|
-
alias?: string;
|
|
612
|
-
}
|
|
613
|
-
/**
|
|
614
|
-
* AST node representing a window function
|
|
615
|
-
*/
|
|
616
|
-
interface WindowFunctionNode {
|
|
617
|
-
type: 'WindowFunction';
|
|
618
|
-
/** Window function name (e.g., ROW_NUMBER, RANK) */
|
|
619
|
-
name: string;
|
|
620
|
-
/** Function arguments */
|
|
621
|
-
args: (ColumnNode | LiteralNode | JsonPathNode)[];
|
|
622
|
-
/** Optional PARTITION BY clause */
|
|
623
|
-
partitionBy?: ColumnNode[];
|
|
624
|
-
/** Optional ORDER BY clause */
|
|
625
|
-
orderBy?: OrderByNode[];
|
|
626
|
-
/** Optional alias for the result */
|
|
627
|
-
alias?: string;
|
|
628
|
-
}
|
|
629
|
-
/**
|
|
630
|
-
* Union type representing any operand that can be used in expressions
|
|
631
|
-
*/
|
|
632
|
-
type OperandNode = ColumnNode | LiteralNode | FunctionNode | JsonPathNode | ScalarSubqueryNode | CaseExpressionNode | WindowFunctionNode;
|
|
633
|
-
declare const isOperandNode: (node: any) => node is OperandNode;
|
|
634
|
-
declare const isFunctionNode: (node: any) => node is FunctionNode;
|
|
635
|
-
declare const isCaseExpressionNode: (node: any) => node is CaseExpressionNode;
|
|
636
|
-
declare const isWindowFunctionNode: (node: any) => node is WindowFunctionNode;
|
|
637
|
-
declare const isExpressionSelectionNode: (node: ColumnRef | FunctionNode | CaseExpressionNode | WindowFunctionNode) => node is FunctionNode | CaseExpressionNode | WindowFunctionNode;
|
|
638
|
-
/**
|
|
639
|
-
* AST node representing a binary expression (e.g., column = value)
|
|
640
|
-
*/
|
|
641
|
-
interface BinaryExpressionNode {
|
|
642
|
-
type: 'BinaryExpression';
|
|
643
|
-
/** Left operand */
|
|
644
|
-
left: OperandNode;
|
|
645
|
-
/** Comparison operator */
|
|
646
|
-
operator: SqlOperator;
|
|
647
|
-
/** Right operand */
|
|
648
|
-
right: OperandNode;
|
|
649
|
-
/** Optional escape character for LIKE expressions */
|
|
650
|
-
escape?: LiteralNode;
|
|
651
|
-
}
|
|
652
|
-
/**
|
|
653
|
-
* AST node representing a logical expression (AND/OR)
|
|
654
|
-
*/
|
|
655
|
-
interface LogicalExpressionNode {
|
|
656
|
-
type: 'LogicalExpression';
|
|
657
|
-
/** Logical operator (AND or OR) */
|
|
658
|
-
operator: 'AND' | 'OR';
|
|
659
|
-
/** Operands to combine */
|
|
660
|
-
operands: ExpressionNode[];
|
|
661
|
-
}
|
|
662
|
-
/**
|
|
663
|
-
* AST node representing a null check expression
|
|
664
|
-
*/
|
|
665
|
-
interface NullExpressionNode {
|
|
666
|
-
type: 'NullExpression';
|
|
667
|
-
/** Operand to check for null */
|
|
668
|
-
left: OperandNode;
|
|
669
|
-
/** Null check operator */
|
|
670
|
-
operator: 'IS NULL' | 'IS NOT NULL';
|
|
671
|
-
}
|
|
672
|
-
/**
|
|
673
|
-
* AST node representing an IN/NOT IN expression
|
|
674
|
-
*/
|
|
675
|
-
interface InExpressionNode {
|
|
676
|
-
type: 'InExpression';
|
|
677
|
-
/** Left operand to check */
|
|
678
|
-
left: OperandNode;
|
|
679
|
-
/** IN/NOT IN operator */
|
|
680
|
-
operator: 'IN' | 'NOT IN';
|
|
681
|
-
/** Values to check against */
|
|
682
|
-
right: OperandNode[];
|
|
683
|
-
}
|
|
684
|
-
/**
|
|
685
|
-
* AST node representing an EXISTS/NOT EXISTS expression
|
|
686
|
-
*/
|
|
687
|
-
interface ExistsExpressionNode {
|
|
688
|
-
type: 'ExistsExpression';
|
|
689
|
-
/** EXISTS/NOT EXISTS operator */
|
|
690
|
-
operator: SqlOperator;
|
|
691
|
-
/** Subquery to check */
|
|
692
|
-
subquery: SelectQueryNode;
|
|
693
|
-
}
|
|
694
|
-
/**
|
|
695
|
-
* AST node representing a BETWEEN/NOT BETWEEN expression
|
|
696
|
-
*/
|
|
697
|
-
interface BetweenExpressionNode {
|
|
698
|
-
type: 'BetweenExpression';
|
|
699
|
-
/** Operand to check */
|
|
700
|
-
left: OperandNode;
|
|
701
|
-
/** BETWEEN/NOT BETWEEN operator */
|
|
702
|
-
operator: 'BETWEEN' | 'NOT BETWEEN';
|
|
703
|
-
/** Lower bound */
|
|
704
|
-
lower: OperandNode;
|
|
705
|
-
/** Upper bound */
|
|
706
|
-
upper: OperandNode;
|
|
707
|
-
}
|
|
708
|
-
/**
|
|
709
|
-
* Union type representing any supported expression node
|
|
710
|
-
*/
|
|
711
|
-
type ExpressionNode = BinaryExpressionNode | LogicalExpressionNode | NullExpressionNode | InExpressionNode | ExistsExpressionNode | BetweenExpressionNode;
|
|
712
|
-
|
|
713
|
-
/**
|
|
714
|
-
* AST node representing a JOIN clause
|
|
715
|
-
*/
|
|
716
|
-
interface JoinNode {
|
|
717
|
-
type: 'Join';
|
|
718
|
-
/** Type of join (INNER, LEFT, RIGHT, etc.) */
|
|
719
|
-
kind: JoinKind;
|
|
720
|
-
/** Table to join */
|
|
721
|
-
table: TableSourceNode;
|
|
722
|
-
/** Join condition expression */
|
|
723
|
-
condition: ExpressionNode;
|
|
724
|
-
/** Optional metadata for non-SQL concerns (e.g., relation name) */
|
|
725
|
-
meta?: Record<string, unknown>;
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
/**
|
|
729
|
-
* AST node representing a table reference in a query
|
|
730
|
-
*/
|
|
731
|
-
interface TableNode {
|
|
732
|
-
type: 'Table';
|
|
733
|
-
/** Table name */
|
|
734
|
-
name: string;
|
|
735
|
-
/** Optional schema name */
|
|
736
|
-
schema?: string;
|
|
737
|
-
/** Optional table alias */
|
|
738
|
-
alias?: string;
|
|
739
|
-
}
|
|
740
|
-
/**
|
|
741
|
-
* AST node representing a function used as a table source (table-valued function)
|
|
742
|
-
*/
|
|
743
|
-
interface FunctionTableNode {
|
|
744
|
-
type: 'FunctionTable';
|
|
745
|
-
/** Function name */
|
|
746
|
-
name: string;
|
|
747
|
-
/** Optional schema for the function (some dialects) */
|
|
748
|
-
schema?: string;
|
|
749
|
-
/** Function arguments as operand nodes */
|
|
750
|
-
args?: any[];
|
|
751
|
-
/** Optional alias for the function table */
|
|
752
|
-
alias?: string;
|
|
753
|
-
/** LATERAL flag */
|
|
754
|
-
lateral?: boolean;
|
|
755
|
-
/** WITH ORDINALITY flag */
|
|
756
|
-
withOrdinality?: boolean;
|
|
757
|
-
/** Optional column aliases */
|
|
758
|
-
columnAliases?: string[];
|
|
759
|
-
}
|
|
760
|
-
/**
|
|
761
|
-
* AST node representing a derived table (subquery with an alias)
|
|
762
|
-
*/
|
|
763
|
-
interface DerivedTableNode {
|
|
764
|
-
type: 'DerivedTable';
|
|
765
|
-
/** Subquery providing the rows */
|
|
766
|
-
query: SelectQueryNode;
|
|
767
|
-
/** Required alias for the derived table */
|
|
768
|
-
alias: string;
|
|
769
|
-
/** Optional column aliases */
|
|
770
|
-
columnAliases?: string[];
|
|
771
|
-
}
|
|
772
|
-
type TableSourceNode = TableNode | FunctionTableNode | DerivedTableNode;
|
|
773
|
-
/**
|
|
774
|
-
* AST node representing an ORDER BY clause
|
|
775
|
-
*/
|
|
776
|
-
interface OrderByNode {
|
|
777
|
-
type: 'OrderBy';
|
|
778
|
-
/** Column to order by */
|
|
779
|
-
column: ColumnNode;
|
|
780
|
-
/** Order direction (ASC or DESC) */
|
|
781
|
-
direction: OrderDirection;
|
|
782
|
-
}
|
|
783
|
-
/**
|
|
784
|
-
* AST node representing a Common Table Expression (CTE)
|
|
785
|
-
*/
|
|
786
|
-
interface CommonTableExpressionNode {
|
|
787
|
-
type: 'CommonTableExpression';
|
|
788
|
-
/** CTE name */
|
|
789
|
-
name: string;
|
|
790
|
-
/** Optional column names */
|
|
791
|
-
columns?: string[];
|
|
792
|
-
/** CTE query */
|
|
793
|
-
query: SelectQueryNode;
|
|
794
|
-
/** Whether the CTE is recursive */
|
|
795
|
-
recursive: boolean;
|
|
796
|
-
}
|
|
797
|
-
/**
|
|
798
|
-
* Supported set operation kinds for compound SELECT queries
|
|
799
|
-
*/
|
|
800
|
-
type SetOperationKind = 'UNION' | 'UNION ALL' | 'INTERSECT' | 'EXCEPT';
|
|
801
|
-
/**
|
|
802
|
-
* AST node representing a set operation (UNION, INTERSECT, etc.)
|
|
803
|
-
*/
|
|
804
|
-
interface SetOperationNode {
|
|
805
|
-
type: 'SetOperation';
|
|
806
|
-
/** Operator to combine queries */
|
|
807
|
-
operator: SetOperationKind;
|
|
808
|
-
/** Right-hand query in the compound expression */
|
|
809
|
-
query: SelectQueryNode;
|
|
810
|
-
}
|
|
811
|
-
/**
|
|
812
|
-
* AST node representing a complete SELECT query
|
|
813
|
-
*/
|
|
814
|
-
interface SelectQueryNode {
|
|
815
|
-
type: 'SelectQuery';
|
|
816
|
-
/** Optional CTEs (WITH clauses) */
|
|
817
|
-
ctes?: CommonTableExpressionNode[];
|
|
818
|
-
/** FROM clause table (either a Table or a FunctionTable) */
|
|
819
|
-
from: TableSourceNode;
|
|
820
|
-
/** SELECT clause columns */
|
|
821
|
-
columns: (ColumnNode | FunctionNode | ScalarSubqueryNode | CaseExpressionNode | WindowFunctionNode)[];
|
|
822
|
-
/** JOIN clauses */
|
|
823
|
-
joins: JoinNode[];
|
|
824
|
-
/** Optional WHERE clause */
|
|
825
|
-
where?: ExpressionNode;
|
|
826
|
-
/** Optional GROUP BY clause */
|
|
827
|
-
groupBy?: ColumnNode[];
|
|
828
|
-
/** Optional HAVING clause */
|
|
829
|
-
having?: ExpressionNode;
|
|
830
|
-
/** Optional ORDER BY clause */
|
|
831
|
-
orderBy?: OrderByNode[];
|
|
832
|
-
/** Optional LIMIT clause */
|
|
833
|
-
limit?: number;
|
|
834
|
-
/** Optional OFFSET clause */
|
|
835
|
-
offset?: number;
|
|
836
|
-
/** Optional query metadata */
|
|
837
|
-
meta?: Record<string, unknown>;
|
|
838
|
-
/** Optional DISTINCT clause */
|
|
839
|
-
distinct?: ColumnNode[];
|
|
840
|
-
/** Optional set operations chaining this query with others */
|
|
841
|
-
setOps?: SetOperationNode[];
|
|
842
|
-
}
|
|
843
|
-
interface InsertQueryNode {
|
|
844
|
-
type: 'InsertQuery';
|
|
845
|
-
/** Target table */
|
|
846
|
-
into: TableNode;
|
|
847
|
-
/** Column order for inserted values */
|
|
848
|
-
columns: ColumnNode[];
|
|
849
|
-
/** Rows of values to insert */
|
|
850
|
-
values: OperandNode[][];
|
|
851
|
-
/** Optional RETURNING clause */
|
|
852
|
-
returning?: ColumnNode[];
|
|
853
|
-
}
|
|
854
|
-
interface UpdateAssignmentNode {
|
|
855
|
-
/** Column to update */
|
|
856
|
-
column: ColumnNode;
|
|
857
|
-
/** Value to set */
|
|
858
|
-
value: OperandNode;
|
|
859
|
-
}
|
|
860
|
-
interface UpdateQueryNode {
|
|
861
|
-
type: 'UpdateQuery';
|
|
862
|
-
/** Table being updated */
|
|
863
|
-
table: TableNode;
|
|
864
|
-
/** Assignments for SET clause */
|
|
865
|
-
set: UpdateAssignmentNode[];
|
|
866
|
-
/** Optional WHERE clause */
|
|
867
|
-
where?: ExpressionNode;
|
|
868
|
-
/** Optional RETURNING clause */
|
|
869
|
-
returning?: ColumnNode[];
|
|
870
|
-
}
|
|
871
|
-
interface DeleteQueryNode {
|
|
872
|
-
type: 'DeleteQuery';
|
|
873
|
-
/** Table to delete from */
|
|
874
|
-
from: TableNode;
|
|
875
|
-
/** Optional WHERE clause */
|
|
876
|
-
where?: ExpressionNode;
|
|
877
|
-
/** Optional RETURNING clause */
|
|
878
|
-
returning?: ColumnNode[];
|
|
879
|
-
}
|
|
880
|
-
|
|
881
|
-
/**
|
|
882
|
-
* Plan describing pivot columns needed for hydration
|
|
883
|
-
*/
|
|
884
|
-
interface HydrationPivotPlan {
|
|
885
|
-
table: string;
|
|
886
|
-
primaryKey: string;
|
|
887
|
-
aliasPrefix: string;
|
|
888
|
-
columns: string[];
|
|
889
|
-
}
|
|
890
|
-
/**
|
|
891
|
-
* Plan for hydrating relationship data
|
|
892
|
-
*/
|
|
893
|
-
interface HydrationRelationPlan {
|
|
894
|
-
/** Name of the relationship */
|
|
895
|
-
name: string;
|
|
896
|
-
/** Alias prefix for the relationship */
|
|
897
|
-
aliasPrefix: string;
|
|
898
|
-
/** Type of relationship */
|
|
899
|
-
type: RelationType;
|
|
900
|
-
/** Target table name */
|
|
901
|
-
targetTable: string;
|
|
902
|
-
/** Target table primary key */
|
|
903
|
-
targetPrimaryKey: string;
|
|
904
|
-
/** Foreign key column */
|
|
905
|
-
foreignKey: string;
|
|
906
|
-
/** Local key column */
|
|
907
|
-
localKey: string;
|
|
908
|
-
/** Columns to include */
|
|
909
|
-
columns: string[];
|
|
910
|
-
/** Optional pivot plan for many-to-many relationships */
|
|
911
|
-
pivot?: HydrationPivotPlan;
|
|
912
|
-
}
|
|
913
|
-
/**
|
|
914
|
-
* Complete hydration plan for a query
|
|
915
|
-
*/
|
|
916
|
-
interface HydrationPlan {
|
|
917
|
-
/** Root table name */
|
|
918
|
-
rootTable: string;
|
|
919
|
-
/** Root table primary key */
|
|
920
|
-
rootPrimaryKey: string;
|
|
921
|
-
/** Root table columns */
|
|
922
|
-
rootColumns: string[];
|
|
923
|
-
/** Relationship hydration plans */
|
|
924
|
-
relations: HydrationRelationPlan[];
|
|
925
|
-
}
|
|
926
|
-
/**
|
|
927
|
-
* Metadata bag for attaching hydration to query ASTs without coupling AST types.
|
|
928
|
-
*/
|
|
929
|
-
interface HydrationMetadata {
|
|
930
|
-
hydration?: HydrationPlan;
|
|
931
|
-
[key: string]: unknown;
|
|
932
|
-
}
|
|
933
|
-
|
|
934
|
-
interface FunctionRenderContext {
|
|
935
|
-
node: FunctionNode;
|
|
936
|
-
compiledArgs: string[];
|
|
937
|
-
/** Helper to compile additional operands (e.g., separators or ORDER BY columns) */
|
|
938
|
-
compileOperand: (operand: OperandNode) => string;
|
|
939
|
-
}
|
|
940
|
-
type FunctionRenderer = (ctx: FunctionRenderContext) => string;
|
|
941
|
-
interface FunctionStrategy {
|
|
942
|
-
/**
|
|
943
|
-
* Returns a renderer for a specific function name (e.g. "DATE_ADD").
|
|
944
|
-
* Returns undefined if this dialect doesn't support the function.
|
|
945
|
-
*/
|
|
946
|
-
getRenderer(functionName: string): FunctionRenderer | undefined;
|
|
947
|
-
}
|
|
948
|
-
|
|
949
|
-
/**
|
|
950
|
-
* Context for SQL compilation with parameter management
|
|
951
|
-
*/
|
|
952
|
-
interface CompilerContext {
|
|
953
|
-
/** Array of parameters */
|
|
954
|
-
params: unknown[];
|
|
955
|
-
/** Function to add a parameter and get its placeholder */
|
|
956
|
-
addParameter(value: unknown): string;
|
|
957
|
-
}
|
|
958
|
-
/**
|
|
959
|
-
* Result of SQL compilation
|
|
960
|
-
*/
|
|
961
|
-
interface CompiledQuery {
|
|
962
|
-
/** Generated SQL string */
|
|
963
|
-
sql: string;
|
|
964
|
-
/** Parameters for the query */
|
|
965
|
-
params: unknown[];
|
|
966
|
-
}
|
|
967
|
-
interface SelectCompiler {
|
|
968
|
-
compileSelect(ast: SelectQueryNode): CompiledQuery;
|
|
969
|
-
}
|
|
970
|
-
interface InsertCompiler {
|
|
971
|
-
compileInsert(ast: InsertQueryNode): CompiledQuery;
|
|
972
|
-
}
|
|
973
|
-
interface UpdateCompiler {
|
|
974
|
-
compileUpdate(ast: UpdateQueryNode): CompiledQuery;
|
|
975
|
-
}
|
|
976
|
-
interface DeleteCompiler {
|
|
977
|
-
compileDelete(ast: DeleteQueryNode): CompiledQuery;
|
|
978
|
-
}
|
|
979
|
-
/**
|
|
980
|
-
* Abstract base class for SQL dialect implementations
|
|
981
|
-
*/
|
|
982
|
-
declare abstract class Dialect implements SelectCompiler, InsertCompiler, UpdateCompiler, DeleteCompiler {
|
|
983
|
-
/** Dialect identifier used for function rendering and formatting */
|
|
984
|
-
protected abstract readonly dialect: DialectName;
|
|
985
|
-
/**
|
|
986
|
-
* Compiles a SELECT query AST to SQL
|
|
987
|
-
* @param ast - Query AST to compile
|
|
988
|
-
* @returns Compiled query with SQL and parameters
|
|
989
|
-
*/
|
|
990
|
-
compileSelect(ast: SelectQueryNode): CompiledQuery;
|
|
991
|
-
compileInsert(ast: InsertQueryNode): CompiledQuery;
|
|
992
|
-
compileUpdate(ast: UpdateQueryNode): CompiledQuery;
|
|
993
|
-
compileDelete(ast: DeleteQueryNode): CompiledQuery;
|
|
994
|
-
supportsReturning(): boolean;
|
|
995
|
-
/**
|
|
996
|
-
* Compiles SELECT query AST to SQL (to be implemented by concrete dialects)
|
|
997
|
-
* @param ast - Query AST
|
|
998
|
-
* @param ctx - Compiler context
|
|
999
|
-
* @returns SQL string
|
|
1000
|
-
*/
|
|
1001
|
-
protected abstract compileSelectAst(ast: SelectQueryNode, ctx: CompilerContext): string;
|
|
1002
|
-
protected abstract compileInsertAst(ast: InsertQueryNode, ctx: CompilerContext): string;
|
|
1003
|
-
protected abstract compileUpdateAst(ast: UpdateQueryNode, ctx: CompilerContext): string;
|
|
1004
|
-
protected abstract compileDeleteAst(ast: DeleteQueryNode, ctx: CompilerContext): string;
|
|
1005
|
-
/**
|
|
1006
|
-
* Quotes an SQL identifier (to be implemented by concrete dialects)
|
|
1007
|
-
* @param id - Identifier to quote
|
|
1008
|
-
* @returns Quoted identifier
|
|
1009
|
-
*/
|
|
1010
|
-
abstract quoteIdentifier(id: string): string;
|
|
1011
|
-
/**
|
|
1012
|
-
* Compiles a WHERE clause
|
|
1013
|
-
* @param where - WHERE expression
|
|
1014
|
-
* @param ctx - Compiler context
|
|
1015
|
-
* @returns SQL WHERE clause or empty string
|
|
1016
|
-
*/
|
|
1017
|
-
protected compileWhere(where: ExpressionNode | undefined, ctx: CompilerContext): string;
|
|
1018
|
-
protected compileReturning(returning: ColumnNode[] | undefined, ctx: CompilerContext): string;
|
|
1019
|
-
/**
|
|
1020
|
-
* Generates subquery for EXISTS expressions
|
|
1021
|
-
* Rule: Always forces SELECT 1, ignoring column list
|
|
1022
|
-
* Maintains FROM, JOINs, WHERE, GROUP BY, ORDER BY, LIMIT/OFFSET
|
|
1023
|
-
* Does not add ';' at the end
|
|
1024
|
-
* @param ast - Query AST
|
|
1025
|
-
* @param ctx - Compiler context
|
|
1026
|
-
* @returns SQL for EXISTS subquery
|
|
1027
|
-
*/
|
|
1028
|
-
protected compileSelectForExists(ast: SelectQueryNode, ctx: CompilerContext): string;
|
|
1029
|
-
/**
|
|
1030
|
-
* Creates a new compiler context
|
|
1031
|
-
* @returns Compiler context with parameter management
|
|
1032
|
-
*/
|
|
1033
|
-
protected createCompilerContext(): CompilerContext;
|
|
1034
|
-
/**
|
|
1035
|
-
* Formats a parameter placeholder
|
|
1036
|
-
* @param index - Parameter index
|
|
1037
|
-
* @returns Formatted placeholder string
|
|
1038
|
-
*/
|
|
1039
|
-
protected formatPlaceholder(index: number): string;
|
|
1040
|
-
/**
|
|
1041
|
-
* Whether the current dialect supports a given set operation.
|
|
1042
|
-
* Override in concrete dialects to restrict support.
|
|
1043
|
-
*/
|
|
1044
|
-
protected supportsSetOperation(kind: SetOperationKind): boolean;
|
|
1045
|
-
/**
|
|
1046
|
-
* Validates set-operation semantics:
|
|
1047
|
-
* - Ensures the dialect supports requested operators.
|
|
1048
|
-
* - Enforces that only the outermost compound query may have ORDER/LIMIT/OFFSET.
|
|
1049
|
-
* @param ast - Query to validate
|
|
1050
|
-
* @param isOutermost - Whether this node is the outermost compound query
|
|
1051
|
-
*/
|
|
1052
|
-
protected validateSetOperations(ast: SelectQueryNode, isOutermost?: boolean): void;
|
|
1053
|
-
/**
|
|
1054
|
-
* Hoists CTEs from set-operation operands to the outermost query so WITH appears once.
|
|
1055
|
-
* @param ast - Query AST
|
|
1056
|
-
* @returns Normalized AST without inner CTEs and a list of hoisted CTEs
|
|
1057
|
-
*/
|
|
1058
|
-
private hoistCtes;
|
|
1059
|
-
/**
|
|
1060
|
-
* Normalizes a SELECT AST before compilation (validation + CTE hoisting).
|
|
1061
|
-
* @param ast - Query AST
|
|
1062
|
-
* @returns Normalized query AST
|
|
1063
|
-
*/
|
|
1064
|
-
protected normalizeSelectAst(ast: SelectQueryNode): SelectQueryNode;
|
|
1065
|
-
private readonly expressionCompilers;
|
|
1066
|
-
private readonly operandCompilers;
|
|
1067
|
-
protected readonly functionStrategy: FunctionStrategy;
|
|
1068
|
-
protected constructor(functionStrategy?: FunctionStrategy);
|
|
1069
|
-
/**
|
|
1070
|
-
* Creates a new Dialect instance (for testing purposes)
|
|
1071
|
-
* @param functionStrategy - Optional function strategy
|
|
1072
|
-
* @returns New Dialect instance
|
|
1073
|
-
*/
|
|
1074
|
-
static create(functionStrategy?: FunctionStrategy): Dialect;
|
|
1075
|
-
/**
|
|
1076
|
-
* Registers an expression compiler for a specific node type
|
|
1077
|
-
* @param type - Expression node type
|
|
1078
|
-
* @param compiler - Compiler function
|
|
1079
|
-
*/
|
|
1080
|
-
protected registerExpressionCompiler<T extends ExpressionNode>(type: T['type'], compiler: (node: T, ctx: CompilerContext) => string): void;
|
|
1081
|
-
/**
|
|
1082
|
-
* Registers an operand compiler for a specific node type
|
|
1083
|
-
* @param type - Operand node type
|
|
1084
|
-
* @param compiler - Compiler function
|
|
1085
|
-
*/
|
|
1086
|
-
protected registerOperandCompiler<T extends OperandNode>(type: T['type'], compiler: (node: T, ctx: CompilerContext) => string): void;
|
|
1087
|
-
/**
|
|
1088
|
-
* Compiles an expression node
|
|
1089
|
-
* @param node - Expression node to compile
|
|
1090
|
-
* @param ctx - Compiler context
|
|
1091
|
-
* @returns Compiled SQL expression
|
|
1092
|
-
*/
|
|
1093
|
-
protected compileExpression(node: ExpressionNode, ctx: CompilerContext): string;
|
|
1094
|
-
/**
|
|
1095
|
-
* Compiles an operand node
|
|
1096
|
-
* @param node - Operand node to compile
|
|
1097
|
-
* @param ctx - Compiler context
|
|
1098
|
-
* @returns Compiled SQL operand
|
|
1099
|
-
*/
|
|
1100
|
-
protected compileOperand(node: OperandNode, ctx: CompilerContext): string;
|
|
1101
|
-
private registerDefaultExpressionCompilers;
|
|
1102
|
-
private registerDefaultOperandCompilers;
|
|
1103
|
-
protected compileJsonPath(node: JsonPathNode): string;
|
|
1104
|
-
/**
|
|
1105
|
-
* Compiles a function operand, using the dialect's function strategy.
|
|
1106
|
-
*/
|
|
1107
|
-
protected compileFunctionOperand(fnNode: FunctionNode, ctx: CompilerContext): string;
|
|
1108
|
-
}
|
|
1109
|
-
|
|
1110
|
-
type DialectKey = 'postgres' | 'mysql' | 'sqlite' | 'mssql' | (string & {});
|
|
1111
|
-
|
|
1112
|
-
/**
|
|
1113
|
-
* Node types that can be used in query projections
|
|
1114
|
-
*/
|
|
1115
|
-
type ProjectionNode = ColumnNode | FunctionNode | ScalarSubqueryNode | CaseExpressionNode | WindowFunctionNode;
|
|
1116
|
-
/**
|
|
1117
|
-
* Manages the state of a SELECT query being built
|
|
1118
|
-
*/
|
|
1119
|
-
declare class SelectQueryState {
|
|
1120
|
-
/**
|
|
1121
|
-
* Table definition for the query
|
|
1122
|
-
*/
|
|
1123
|
-
readonly table: TableDef;
|
|
1124
|
-
/**
|
|
1125
|
-
* Abstract Syntax Tree (AST) representation of the query
|
|
1126
|
-
*/
|
|
1127
|
-
readonly ast: SelectQueryNode;
|
|
1128
|
-
/**
|
|
1129
|
-
* Creates a new SelectQueryState instance
|
|
1130
|
-
* @param table - Table definition
|
|
1131
|
-
* @param ast - Optional existing AST
|
|
1132
|
-
*/
|
|
1133
|
-
constructor(table: TableDef, ast?: SelectQueryNode);
|
|
1134
|
-
/**
|
|
1135
|
-
* Creates a new SelectQueryState with updated AST
|
|
1136
|
-
* @param nextAst - Updated AST
|
|
1137
|
-
* @returns New SelectQueryState instance
|
|
1138
|
-
*/
|
|
1139
|
-
private clone;
|
|
1140
|
-
/**
|
|
1141
|
-
* Adds columns to the query
|
|
1142
|
-
* @param newCols - Columns to add
|
|
1143
|
-
* @returns New SelectQueryState with added columns
|
|
1144
|
-
*/
|
|
1145
|
-
withColumns(newCols: ProjectionNode[]): SelectQueryState;
|
|
1146
|
-
/**
|
|
1147
|
-
* Adds a join to the query
|
|
1148
|
-
* @param join - Join node to add
|
|
1149
|
-
* @returns New SelectQueryState with added join
|
|
1150
|
-
*/
|
|
1151
|
-
withJoin(join: JoinNode): SelectQueryState;
|
|
1152
|
-
/**
|
|
1153
|
-
* Replaces the FROM clause.
|
|
1154
|
-
* @param from - Table source for the FROM clause
|
|
1155
|
-
* @returns New SelectQueryState with updated FROM
|
|
1156
|
-
*/
|
|
1157
|
-
withFrom(from: TableSourceNode): SelectQueryState;
|
|
1158
|
-
/**
|
|
1159
|
-
* Adds a WHERE clause to the query
|
|
1160
|
-
* @param predicate - WHERE predicate expression
|
|
1161
|
-
* @returns New SelectQueryState with WHERE clause
|
|
1162
|
-
*/
|
|
1163
|
-
withWhere(predicate: ExpressionNode): SelectQueryState;
|
|
1164
|
-
/**
|
|
1165
|
-
* Adds a HAVING clause to the query
|
|
1166
|
-
* @param predicate - HAVING predicate expression
|
|
1167
|
-
* @returns New SelectQueryState with HAVING clause
|
|
1168
|
-
*/
|
|
1169
|
-
withHaving(predicate: ExpressionNode): SelectQueryState;
|
|
1170
|
-
/**
|
|
1171
|
-
* Adds GROUP BY columns to the query
|
|
1172
|
-
* @param columns - Columns to group by
|
|
1173
|
-
* @returns New SelectQueryState with GROUP BY clause
|
|
1174
|
-
*/
|
|
1175
|
-
withGroupBy(columns: ColumnNode[]): SelectQueryState;
|
|
1176
|
-
/**
|
|
1177
|
-
* Adds ORDER BY clauses to the query
|
|
1178
|
-
* @param orderBy - ORDER BY nodes
|
|
1179
|
-
* @returns New SelectQueryState with ORDER BY clause
|
|
1180
|
-
*/
|
|
1181
|
-
withOrderBy(orderBy: OrderByNode[]): SelectQueryState;
|
|
1182
|
-
/**
|
|
1183
|
-
* Adds DISTINCT columns to the query
|
|
1184
|
-
* @param columns - Columns to make distinct
|
|
1185
|
-
* @returns New SelectQueryState with DISTINCT clause
|
|
1186
|
-
*/
|
|
1187
|
-
withDistinct(columns: ColumnNode[]): SelectQueryState;
|
|
1188
|
-
/**
|
|
1189
|
-
* Adds a LIMIT clause to the query
|
|
1190
|
-
* @param limit - Maximum number of rows to return
|
|
1191
|
-
* @returns New SelectQueryState with LIMIT clause
|
|
1192
|
-
*/
|
|
1193
|
-
withLimit(limit: number): SelectQueryState;
|
|
1194
|
-
/**
|
|
1195
|
-
* Adds an OFFSET clause to the query
|
|
1196
|
-
* @param offset - Number of rows to skip
|
|
1197
|
-
* @returns New SelectQueryState with OFFSET clause
|
|
1198
|
-
*/
|
|
1199
|
-
withOffset(offset: number): SelectQueryState;
|
|
1200
|
-
/**
|
|
1201
|
-
* Adds a Common Table Expression (CTE) to the query
|
|
1202
|
-
* @param cte - CTE node to add
|
|
1203
|
-
* @returns New SelectQueryState with CTE
|
|
1204
|
-
*/
|
|
1205
|
-
withCte(cte: CommonTableExpressionNode): SelectQueryState;
|
|
1206
|
-
/**
|
|
1207
|
-
* Adds a set operation (UNION/INTERSECT/EXCEPT) to the query
|
|
1208
|
-
* @param op - Set operation node to add
|
|
1209
|
-
* @returns New SelectQueryState with set operation
|
|
1210
|
-
*/
|
|
1211
|
-
withSetOperation(op: SetOperationNode): SelectQueryState;
|
|
1212
|
-
}
|
|
1213
|
-
|
|
1214
|
-
/**
|
|
1215
|
-
* Manages hydration planning for query results
|
|
1216
|
-
*/
|
|
1217
|
-
declare class HydrationPlanner {
|
|
1218
|
-
private readonly table;
|
|
1219
|
-
private readonly plan?;
|
|
1220
|
-
/**
|
|
1221
|
-
* Creates a new HydrationPlanner instance
|
|
1222
|
-
* @param table - Table definition
|
|
1223
|
-
* @param plan - Optional existing hydration plan
|
|
1224
|
-
*/
|
|
1225
|
-
constructor(table: TableDef, plan?: HydrationPlan);
|
|
1226
|
-
/**
|
|
1227
|
-
* Captures root table columns for hydration planning
|
|
1228
|
-
* @param columns - Columns to capture
|
|
1229
|
-
* @returns Updated HydrationPlanner with captured columns
|
|
1230
|
-
*/
|
|
1231
|
-
captureRootColumns(columns: ProjectionNode[]): HydrationPlanner;
|
|
1232
|
-
/**
|
|
1233
|
-
* Includes a relation in the hydration plan
|
|
1234
|
-
* @param rel - Relation definition
|
|
1235
|
-
* @param relationName - Name of the relation
|
|
1236
|
-
* @param aliasPrefix - Alias prefix for relation columns
|
|
1237
|
-
* @param columns - Columns to include from the relation
|
|
1238
|
-
* @returns Updated HydrationPlanner with included relation
|
|
1239
|
-
*/
|
|
1240
|
-
includeRelation(rel: RelationDef, relationName: string, aliasPrefix: string, columns: string[], pivot?: {
|
|
1241
|
-
aliasPrefix: string;
|
|
1242
|
-
columns: string[];
|
|
1243
|
-
}): HydrationPlanner;
|
|
1244
|
-
/**
|
|
1245
|
-
* Gets the current hydration plan
|
|
1246
|
-
* @returns Current hydration plan or undefined
|
|
1247
|
-
*/
|
|
1248
|
-
getPlan(): HydrationPlan | undefined;
|
|
1249
|
-
/**
|
|
1250
|
-
* Gets the current hydration plan or creates a default one
|
|
1251
|
-
* @returns Current hydration plan or default plan
|
|
1252
|
-
*/
|
|
1253
|
-
private getPlanOrDefault;
|
|
1254
|
-
/**
|
|
1255
|
-
* Builds a relation plan for hydration
|
|
1256
|
-
* @param rel - Relation definition
|
|
1257
|
-
* @param relationName - Name of the relation
|
|
1258
|
-
* @param aliasPrefix - Alias prefix for relation columns
|
|
1259
|
-
* @param columns - Columns to include from the relation
|
|
1260
|
-
* @returns Hydration relation plan
|
|
1261
|
-
*/
|
|
1262
|
-
private buildRelationPlan;
|
|
1263
|
-
}
|
|
1264
|
-
|
|
1265
|
-
/**
|
|
1266
|
-
* Manages hydration planning for query results
|
|
1267
|
-
*/
|
|
1268
|
-
declare class HydrationManager {
|
|
1269
|
-
private readonly table;
|
|
1270
|
-
private readonly planner;
|
|
1271
|
-
/**
|
|
1272
|
-
* Creates a new HydrationManager instance
|
|
1273
|
-
* @param table - Table definition
|
|
1274
|
-
* @param planner - Hydration planner
|
|
1275
|
-
*/
|
|
1276
|
-
constructor(table: TableDef, planner: HydrationPlanner);
|
|
1277
|
-
/**
|
|
1278
|
-
* Creates a new HydrationManager with updated planner
|
|
1279
|
-
* @param nextPlanner - Updated hydration planner
|
|
1280
|
-
* @returns New HydrationManager instance
|
|
1281
|
-
*/
|
|
1282
|
-
private clone;
|
|
1283
|
-
/**
|
|
1284
|
-
* Handles column selection for hydration planning
|
|
1285
|
-
* @param state - Current query state
|
|
1286
|
-
* @param newColumns - Newly selected columns
|
|
1287
|
-
* @returns Updated HydrationManager with captured columns
|
|
1288
|
-
*/
|
|
1289
|
-
onColumnsSelected(state: SelectQueryState, newColumns: ProjectionNode[]): HydrationManager;
|
|
1290
|
-
/**
|
|
1291
|
-
* Handles relation inclusion for hydration planning
|
|
1292
|
-
* @param state - Current query state
|
|
1293
|
-
* @param relation - Relation definition
|
|
1294
|
-
* @param relationName - Name of the relation
|
|
1295
|
-
* @param aliasPrefix - Alias prefix for the relation
|
|
1296
|
-
* @param targetColumns - Target columns to include
|
|
1297
|
-
* @returns Updated HydrationManager with included relation
|
|
1298
|
-
*/
|
|
1299
|
-
onRelationIncluded(state: SelectQueryState, relation: RelationDef, relationName: string, aliasPrefix: string, targetColumns: string[], pivot?: {
|
|
1300
|
-
aliasPrefix: string;
|
|
1301
|
-
columns: string[];
|
|
1302
|
-
}): HydrationManager;
|
|
1303
|
-
/**
|
|
1304
|
-
* Applies hydration plan to the AST
|
|
1305
|
-
* @param ast - Query AST to modify
|
|
1306
|
-
* @returns AST with hydration metadata
|
|
1307
|
-
*/
|
|
1308
|
-
applyToAst(ast: SelectQueryNode): SelectQueryNode;
|
|
1309
|
-
/**
|
|
1310
|
-
* Gets the current hydration plan
|
|
1311
|
-
* @returns Hydration plan or undefined if none exists
|
|
1312
|
-
*/
|
|
1313
|
-
getPlan(): HydrationPlan | undefined;
|
|
1314
|
-
/**
|
|
1315
|
-
* Attaches hydration metadata to a query AST node.
|
|
1316
|
-
*/
|
|
1317
|
-
private attachHydrationMeta;
|
|
1318
|
-
/**
|
|
1319
|
-
* Determines whether the query needs pagination rewriting to keep LIMIT/OFFSET
|
|
1320
|
-
* applied to parent rows when eager-loading multiplicative relations.
|
|
1321
|
-
*/
|
|
1322
|
-
private requiresParentPagination;
|
|
1323
|
-
private hasMultiplyingRelations;
|
|
1324
|
-
/**
|
|
1325
|
-
* Rewrites the query using CTEs so LIMIT/OFFSET target distinct parent rows
|
|
1326
|
-
* instead of the joined result set.
|
|
1327
|
-
*
|
|
1328
|
-
* The strategy:
|
|
1329
|
-
* - Hoist the original query (minus limit/offset) into a base CTE.
|
|
1330
|
-
* - Select distinct parent ids from that base CTE with the original ordering and pagination.
|
|
1331
|
-
* - Join the base CTE against the paged ids to retrieve the joined rows for just that page.
|
|
1332
|
-
*/
|
|
1333
|
-
private wrapForParentPagination;
|
|
1334
|
-
private nextCteName;
|
|
1335
|
-
private getProjectionNames;
|
|
1336
|
-
private buildProjectionAliasMap;
|
|
1337
|
-
private mapOrderBy;
|
|
1338
|
-
private buildPagingColumns;
|
|
1339
|
-
}
|
|
1340
|
-
|
|
1341
|
-
/**
|
|
1342
|
-
* Result of column selection operation
|
|
1343
|
-
*/
|
|
1344
|
-
interface ColumnSelectionResult {
|
|
1345
|
-
/**
|
|
1346
|
-
* Updated query state
|
|
1347
|
-
*/
|
|
1348
|
-
state: SelectQueryState;
|
|
1349
|
-
/**
|
|
1350
|
-
* Columns that were added
|
|
1351
|
-
*/
|
|
1352
|
-
addedColumns: ProjectionNode[];
|
|
1353
|
-
}
|
|
1354
|
-
/**
|
|
1355
|
-
* Service for manipulating query AST (Abstract Syntax Tree)
|
|
1356
|
-
*/
|
|
1357
|
-
declare class QueryAstService {
|
|
1358
|
-
private readonly table;
|
|
1359
|
-
private readonly state;
|
|
1360
|
-
/**
|
|
1361
|
-
* Creates a new QueryAstService instance
|
|
1362
|
-
* @param table - Table definition
|
|
1363
|
-
* @param state - Current query state
|
|
1364
|
-
*/
|
|
1365
|
-
constructor(table: TableDef, state: SelectQueryState);
|
|
1366
|
-
/**
|
|
1367
|
-
* Selects columns for the query
|
|
1368
|
-
* @param columns - Columns to select (key: alias, value: column definition or expression)
|
|
1369
|
-
* @returns Column selection result with updated state and added columns
|
|
1370
|
-
*/
|
|
1371
|
-
select(columns: Record<string, ColumnDef | FunctionNode | CaseExpressionNode | WindowFunctionNode>): ColumnSelectionResult;
|
|
1372
|
-
/**
|
|
1373
|
-
* Selects raw column expressions (best-effort parser for simple references/functions)
|
|
1374
|
-
* @param cols - Raw column expressions
|
|
1375
|
-
* @returns Column selection result with updated state and added columns
|
|
1376
|
-
*/
|
|
1377
|
-
selectRaw(cols: string[]): ColumnSelectionResult;
|
|
1378
|
-
/**
|
|
1379
|
-
* Adds a Common Table Expression (CTE) to the query
|
|
1380
|
-
* @param name - Name of the CTE
|
|
1381
|
-
* @param query - Query for the CTE
|
|
1382
|
-
* @param columns - Optional column names for the CTE
|
|
1383
|
-
* @param recursive - Whether the CTE is recursive
|
|
1384
|
-
* @returns Updated query state with CTE
|
|
1385
|
-
*/
|
|
1386
|
-
withCte(name: string, query: SelectQueryNode, columns?: string[], recursive?: boolean): SelectQueryState;
|
|
1387
|
-
/**
|
|
1388
|
-
* Adds a set operation (UNION/UNION ALL/INTERSECT/EXCEPT) to the query
|
|
1389
|
-
* @param operator - Set operator
|
|
1390
|
-
* @param query - Right-hand side query
|
|
1391
|
-
* @returns Updated query state with set operation
|
|
1392
|
-
*/
|
|
1393
|
-
withSetOperation(operator: SetOperationKind, query: SelectQueryNode): SelectQueryState;
|
|
1394
|
-
/**
|
|
1395
|
-
* Replaces the FROM clause for the current query.
|
|
1396
|
-
* @param from - Table source to use in the FROM clause
|
|
1397
|
-
* @returns Updated query state with new FROM
|
|
1398
|
-
*/
|
|
1399
|
-
withFrom(from: TableSourceNode): SelectQueryState;
|
|
1400
|
-
/**
|
|
1401
|
-
* Selects a subquery as a column
|
|
1402
|
-
* @param alias - Alias for the subquery
|
|
1403
|
-
* @param query - Subquery to select
|
|
1404
|
-
* @returns Updated query state with subquery selection
|
|
1405
|
-
*/
|
|
1406
|
-
selectSubquery(alias: string, query: SelectQueryNode): SelectQueryState;
|
|
1407
|
-
/**
|
|
1408
|
-
* Adds a JOIN clause to the query
|
|
1409
|
-
* @param join - Join node to add
|
|
1410
|
-
* @returns Updated query state with JOIN
|
|
1411
|
-
*/
|
|
1412
|
-
withJoin(join: JoinNode): SelectQueryState;
|
|
1413
|
-
/**
|
|
1414
|
-
* Adds a WHERE clause to the query
|
|
1415
|
-
* @param expr - Expression for the WHERE clause
|
|
1416
|
-
* @returns Updated query state with WHERE clause
|
|
1417
|
-
*/
|
|
1418
|
-
withWhere(expr: ExpressionNode): SelectQueryState;
|
|
1419
|
-
/**
|
|
1420
|
-
* Adds a GROUP BY clause to the query
|
|
1421
|
-
* @param col - Column to group by
|
|
1422
|
-
* @returns Updated query state with GROUP BY clause
|
|
1423
|
-
*/
|
|
1424
|
-
withGroupBy(col: ColumnDef | ColumnNode): SelectQueryState;
|
|
1425
|
-
/**
|
|
1426
|
-
* Adds a HAVING clause to the query
|
|
1427
|
-
* @param expr - Expression for the HAVING clause
|
|
1428
|
-
* @returns Updated query state with HAVING clause
|
|
1429
|
-
*/
|
|
1430
|
-
withHaving(expr: ExpressionNode): SelectQueryState;
|
|
1431
|
-
/**
|
|
1432
|
-
* Adds an ORDER BY clause to the query
|
|
1433
|
-
* @param col - Column to order by
|
|
1434
|
-
* @param direction - Order direction (ASC/DESC)
|
|
1435
|
-
* @returns Updated query state with ORDER BY clause
|
|
1436
|
-
*/
|
|
1437
|
-
withOrderBy(col: ColumnDef | ColumnNode, direction: OrderDirection): SelectQueryState;
|
|
1438
|
-
/**
|
|
1439
|
-
* Adds a DISTINCT clause to the query
|
|
1440
|
-
* @param cols - Columns to make distinct
|
|
1441
|
-
* @returns Updated query state with DISTINCT clause
|
|
1442
|
-
*/
|
|
1443
|
-
withDistinct(cols: ColumnNode[]): SelectQueryState;
|
|
1444
|
-
/**
|
|
1445
|
-
* Adds a LIMIT clause to the query
|
|
1446
|
-
* @param limit - Maximum number of rows to return
|
|
1447
|
-
* @returns Updated query state with LIMIT clause
|
|
1448
|
-
*/
|
|
1449
|
-
withLimit(limit: number): SelectQueryState;
|
|
1450
|
-
/**
|
|
1451
|
-
* Adds an OFFSET clause to the query
|
|
1452
|
-
* @param offset - Number of rows to skip
|
|
1453
|
-
* @returns Updated query state with OFFSET clause
|
|
1454
|
-
*/
|
|
1455
|
-
withOffset(offset: number): SelectQueryState;
|
|
1456
|
-
/**
|
|
1457
|
-
* Combines expressions with AND operator
|
|
1458
|
-
* @param existing - Existing expression
|
|
1459
|
-
* @param next - New expression to combine
|
|
1460
|
-
* @returns Combined expression
|
|
1461
|
-
*/
|
|
1462
|
-
private combineExpressions;
|
|
1463
|
-
}
|
|
1464
|
-
|
|
1465
|
-
/**
|
|
1466
|
-
* Result of a relation operation
|
|
1467
|
-
*/
|
|
1468
|
-
interface RelationResult {
|
|
1469
|
-
/**
|
|
1470
|
-
* Updated query state
|
|
1471
|
-
*/
|
|
1472
|
-
state: SelectQueryState;
|
|
1473
|
-
/**
|
|
1474
|
-
* Updated hydration manager
|
|
1475
|
-
*/
|
|
1476
|
-
hydration: HydrationManager;
|
|
1477
|
-
}
|
|
1478
|
-
|
|
1479
|
-
/**
|
|
1480
|
-
* Join kinds allowed when including a relation using `.include(...)`.
|
|
1481
|
-
*/
|
|
1482
|
-
type RelationIncludeJoinKind = typeof JOIN_KINDS.LEFT | typeof JOIN_KINDS.INNER;
|
|
1483
|
-
/**
|
|
1484
|
-
* Options for including a relation in a query
|
|
1485
|
-
*/
|
|
1486
|
-
interface RelationIncludeOptions {
|
|
1487
|
-
columns?: string[];
|
|
1488
|
-
aliasPrefix?: string;
|
|
1489
|
-
filter?: ExpressionNode;
|
|
1490
|
-
joinKind?: RelationIncludeJoinKind;
|
|
1491
|
-
pivot?: {
|
|
1492
|
-
columns?: string[];
|
|
1493
|
-
aliasPrefix?: string;
|
|
1494
|
-
};
|
|
1495
|
-
}
|
|
1496
|
-
|
|
1497
|
-
/**
|
|
1498
|
-
* Service for handling relation operations (joins, includes, etc.)
|
|
1499
|
-
*/
|
|
1500
|
-
declare class RelationService {
|
|
1501
|
-
private readonly table;
|
|
1502
|
-
private readonly state;
|
|
1503
|
-
private readonly hydration;
|
|
1504
|
-
private readonly createQueryAstService;
|
|
1505
|
-
private readonly projectionHelper;
|
|
1506
|
-
/**
|
|
1507
|
-
* Creates a new RelationService instance
|
|
1508
|
-
* @param table - Table definition
|
|
1509
|
-
* @param state - Current query state
|
|
1510
|
-
* @param hydration - Hydration manager
|
|
1511
|
-
*/
|
|
1512
|
-
constructor(table: TableDef, state: SelectQueryState, hydration: HydrationManager, createQueryAstService: (table: TableDef, state: SelectQueryState) => QueryAstService);
|
|
1513
|
-
/**
|
|
1514
|
-
* Joins a relation to the query
|
|
1515
|
-
* @param relationName - Name of the relation to join
|
|
1516
|
-
* @param joinKind - Type of join to use
|
|
1517
|
-
* @param extraCondition - Additional join condition
|
|
1518
|
-
* @returns Relation result with updated state and hydration
|
|
1519
|
-
*/
|
|
1520
|
-
joinRelation(relationName: string, joinKind: JoinKind, extraCondition?: ExpressionNode): RelationResult;
|
|
1521
|
-
/**
|
|
1522
|
-
* Matches records based on a relation with an optional predicate
|
|
1523
|
-
* @param relationName - Name of the relation to match
|
|
1524
|
-
* @param predicate - Optional predicate expression
|
|
1525
|
-
* @returns Relation result with updated state and hydration
|
|
1526
|
-
*/
|
|
1527
|
-
match(relationName: string, predicate?: ExpressionNode): RelationResult;
|
|
1528
|
-
/**
|
|
1529
|
-
* Includes a relation in the query result
|
|
1530
|
-
* @param relationName - Name of the relation to include
|
|
1531
|
-
* @param options - Options for relation inclusion
|
|
1532
|
-
* @returns Relation result with updated state and hydration
|
|
1533
|
-
*/
|
|
1534
|
-
include(relationName: string, options?: RelationIncludeOptions): RelationResult;
|
|
1535
|
-
/**
|
|
1536
|
-
* Applies relation correlation to a query AST
|
|
1537
|
-
* @param relationName - Name of the relation
|
|
1538
|
-
* @param ast - Query AST to modify
|
|
1539
|
-
* @returns Modified query AST with relation correlation
|
|
1540
|
-
*/
|
|
1541
|
-
applyRelationCorrelation(relationName: string, ast: SelectQueryNode, additionalCorrelation?: ExpressionNode): SelectQueryNode;
|
|
1542
|
-
/**
|
|
1543
|
-
* Creates a join node for a relation
|
|
1544
|
-
* @param state - Current query state
|
|
1545
|
-
* @param relationName - Name of the relation
|
|
1546
|
-
* @param joinKind - Type of join to use
|
|
1547
|
-
* @param extraCondition - Additional join condition
|
|
1548
|
-
* @returns Updated query state with join
|
|
1549
|
-
*/
|
|
1550
|
-
private withJoin;
|
|
1551
|
-
/**
|
|
1552
|
-
* Selects columns for a relation
|
|
1553
|
-
* @param state - Current query state
|
|
1554
|
-
* @param hydration - Hydration manager
|
|
1555
|
-
* @param columns - Columns to select
|
|
1556
|
-
* @returns Relation result with updated state and hydration
|
|
1557
|
-
*/
|
|
1558
|
-
private selectColumns;
|
|
1559
|
-
/**
|
|
1560
|
-
* Gets a relation definition by name
|
|
1561
|
-
* @param relationName - Name of the relation
|
|
1562
|
-
* @returns Relation definition
|
|
1563
|
-
* @throws Error if relation is not found
|
|
1564
|
-
*/
|
|
1565
|
-
private getRelation;
|
|
1566
|
-
/**
|
|
1567
|
-
* Creates a QueryAstService instance
|
|
1568
|
-
* @param state - Current query state
|
|
1569
|
-
* @returns QueryAstService instance
|
|
1570
|
-
*/
|
|
1571
|
-
private astService;
|
|
1572
|
-
private rootTableName;
|
|
1573
|
-
}
|
|
1574
|
-
|
|
1575
|
-
/**
|
|
1576
|
-
* Dependencies for query builder operations
|
|
1577
|
-
*/
|
|
1578
|
-
interface SelectQueryBuilderDependencies {
|
|
1579
|
-
/**
|
|
1580
|
-
* Creates a new query state
|
|
1581
|
-
* @param table - Table definition
|
|
1582
|
-
* @returns New query state
|
|
1583
|
-
*/
|
|
1584
|
-
createState: (table: TableDef) => SelectQueryState;
|
|
1585
|
-
/**
|
|
1586
|
-
* Creates a new hydration manager
|
|
1587
|
-
* @param table - Table definition
|
|
1588
|
-
* @returns New hydration manager
|
|
1589
|
-
*/
|
|
1590
|
-
createHydration: (table: TableDef) => HydrationManager;
|
|
1591
|
-
/**
|
|
1592
|
-
* Creates a new hydration planner
|
|
1593
|
-
* @param table - Table definition
|
|
1594
|
-
* @returns Hydration planner
|
|
1595
|
-
*/
|
|
1596
|
-
createHydrationPlanner: (table: TableDef) => HydrationPlanner;
|
|
1597
|
-
/**
|
|
1598
|
-
* Creates a new query AST service
|
|
1599
|
-
* @param table - Table definition
|
|
1600
|
-
* @param state - Query state
|
|
1601
|
-
* @returns New query AST service
|
|
1602
|
-
*/
|
|
1603
|
-
createQueryAstService: (table: TableDef, state: SelectQueryState) => QueryAstService;
|
|
1604
|
-
/**
|
|
1605
|
-
* Creates a new relation service
|
|
1606
|
-
* @param table - Table definition
|
|
1607
|
-
* @param state - Query state
|
|
1608
|
-
* @param hydration - Hydration manager
|
|
1609
|
-
* @returns New relation service
|
|
1610
|
-
*/
|
|
1611
|
-
createRelationService: (table: TableDef, state: SelectQueryState, hydration: HydrationManager) => RelationService;
|
|
1612
|
-
}
|
|
1613
|
-
|
|
1614
|
-
type QueryResult = {
|
|
1615
|
-
columns: string[];
|
|
1616
|
-
values: unknown[][];
|
|
1617
|
-
};
|
|
1618
|
-
interface DbExecutor {
|
|
1619
|
-
executeSql(sql: string, params?: unknown[]): Promise<QueryResult[]>;
|
|
1620
|
-
beginTransaction?(): Promise<void>;
|
|
1621
|
-
commitTransaction?(): Promise<void>;
|
|
1622
|
-
rollbackTransaction?(): Promise<void>;
|
|
1623
|
-
}
|
|
1624
|
-
/**
|
|
1625
|
-
* Convert an array of row objects into a QueryResult.
|
|
1626
|
-
*/
|
|
1627
|
-
declare function rowsToQueryResult(rows: Array<Record<string, unknown>>): QueryResult;
|
|
1628
|
-
/**
|
|
1629
|
-
* Minimal contract that most SQL clients can implement.
|
|
1630
|
-
*/
|
|
1631
|
-
interface SimpleQueryRunner {
|
|
1632
|
-
query(sql: string, params?: unknown[]): Promise<Array<Record<string, unknown>>>;
|
|
1633
|
-
beginTransaction?(): Promise<void>;
|
|
1634
|
-
commitTransaction?(): Promise<void>;
|
|
1635
|
-
rollbackTransaction?(): Promise<void>;
|
|
1636
|
-
}
|
|
1637
|
-
/**
|
|
1638
|
-
* Generic factory: turn any SimpleQueryRunner into a DbExecutor.
|
|
1639
|
-
*/
|
|
1640
|
-
declare function createExecutorFromQueryRunner(runner: SimpleQueryRunner): DbExecutor;
|
|
1641
|
-
|
|
1642
|
-
type EntityConstructor = new (...args: any[]) => any;
|
|
1643
|
-
type EntityOrTableTarget = EntityConstructor | TableDef;
|
|
1644
|
-
type EntityOrTableTargetResolver = EntityOrTableTarget | (() => EntityOrTableTarget);
|
|
1645
|
-
|
|
1646
|
-
/**
|
|
1647
|
-
* Entity status enum representing the lifecycle state of an entity
|
|
1648
|
-
*/
|
|
1649
|
-
declare enum EntityStatus {
|
|
1650
|
-
/** Entity is newly created and not yet persisted */
|
|
1651
|
-
New = "new",
|
|
1652
|
-
/** Entity is managed by the ORM and synchronized with the database */
|
|
1653
|
-
Managed = "managed",
|
|
1654
|
-
/** Entity has been modified but not yet persisted */
|
|
1655
|
-
Dirty = "dirty",
|
|
1656
|
-
/** Entity has been marked for removal */
|
|
1657
|
-
Removed = "removed",
|
|
1658
|
-
/** Entity is detached from the ORM context */
|
|
1659
|
-
Detached = "detached"
|
|
1660
|
-
}
|
|
1661
|
-
/**
|
|
1662
|
-
* Represents an entity being tracked by the ORM
|
|
1663
|
-
*/
|
|
1664
|
-
interface TrackedEntity {
|
|
1665
|
-
/** The table definition this entity belongs to */
|
|
1666
|
-
table: TableDef;
|
|
1667
|
-
/** The actual entity instance */
|
|
1668
|
-
entity: any;
|
|
1669
|
-
/** Primary key value of the entity */
|
|
1670
|
-
pk: string | number | null;
|
|
1671
|
-
/** Current status of the entity */
|
|
1672
|
-
status: EntityStatus;
|
|
1673
|
-
/** Original values of the entity when it was loaded */
|
|
1674
|
-
original: Record<string, any> | null;
|
|
1675
|
-
}
|
|
1676
|
-
/**
|
|
1677
|
-
* Type representing a key for relation navigation
|
|
1678
|
-
*/
|
|
1679
|
-
type RelationKey = string;
|
|
1680
|
-
/**
|
|
1681
|
-
* Represents a change operation on a relation
|
|
1682
|
-
* @typeParam T - Type of the related entity
|
|
1683
|
-
*/
|
|
1684
|
-
type RelationChange<T> = {
|
|
1685
|
-
kind: 'add';
|
|
1686
|
-
entity: T;
|
|
1687
|
-
} | {
|
|
1688
|
-
kind: 'attach';
|
|
1689
|
-
entity: T;
|
|
1690
|
-
} | {
|
|
1691
|
-
kind: 'remove';
|
|
1692
|
-
entity: T;
|
|
1693
|
-
} | {
|
|
1694
|
-
kind: 'detach';
|
|
1695
|
-
entity: T;
|
|
1696
|
-
};
|
|
1697
|
-
/**
|
|
1698
|
-
* Represents a relation change entry in the unit of work
|
|
1699
|
-
*/
|
|
1700
|
-
interface RelationChangeEntry {
|
|
1701
|
-
/** Root entity that owns the relation */
|
|
1702
|
-
root: any;
|
|
1703
|
-
/** Key of the relation being changed */
|
|
1704
|
-
relationKey: RelationKey;
|
|
1705
|
-
/** Table definition of the root entity */
|
|
1706
|
-
rootTable: TableDef;
|
|
1707
|
-
/** Name of the relation */
|
|
1708
|
-
relationName: string;
|
|
1709
|
-
/** Relation definition */
|
|
1710
|
-
relation: RelationDef;
|
|
1711
|
-
/** The change being applied */
|
|
1712
|
-
change: RelationChange<any>;
|
|
1713
|
-
}
|
|
1714
|
-
/**
|
|
1715
|
-
* Represents a domain event that can be emitted by entities
|
|
1716
|
-
* @typeParam TType - Type of the event (string literal)
|
|
1717
|
-
*/
|
|
1718
|
-
interface DomainEvent<TType extends string = string> {
|
|
1719
|
-
/** Type identifier for the event */
|
|
1720
|
-
readonly type: TType;
|
|
1721
|
-
/** Timestamp when the event occurred */
|
|
1722
|
-
readonly occurredAt?: Date;
|
|
1723
|
-
}
|
|
1724
|
-
/**
|
|
1725
|
-
* Type representing any domain event
|
|
1726
|
-
*/
|
|
1727
|
-
type AnyDomainEvent = DomainEvent<string>;
|
|
1728
|
-
/**
|
|
1729
|
-
* Type representing ORM-specific domain events
|
|
1730
|
-
*/
|
|
1731
|
-
type OrmDomainEvent = AnyDomainEvent;
|
|
1732
|
-
/**
|
|
1733
|
-
* Interface for entities that can emit domain events
|
|
1734
|
-
* @typeParam E - Type of domain events this entity can emit
|
|
1735
|
-
*/
|
|
1736
|
-
interface HasDomainEvents<E extends DomainEvent = AnyDomainEvent> {
|
|
1737
|
-
/** Array of domain events emitted by this entity */
|
|
1738
|
-
domainEvents?: E[];
|
|
1739
|
-
}
|
|
1740
|
-
|
|
1741
|
-
/**
|
|
1742
|
-
* Strategy interface for converting database names to TypeScript identifiers
|
|
1743
|
-
*/
|
|
1744
|
-
interface NamingStrategy {
|
|
1745
|
-
/**
|
|
1746
|
-
* Converts a table name to a TypeScript symbol name
|
|
1747
|
-
* @param table - Table node, function table node, or name
|
|
1748
|
-
* @returns Valid TypeScript identifier
|
|
1749
|
-
*/
|
|
1750
|
-
tableToSymbol(table: TableSourceNode | string): string;
|
|
1751
|
-
/**
|
|
1752
|
-
* Converts a column reference to a property name
|
|
1753
|
-
* @param column - Column node
|
|
1754
|
-
* @returns Valid TypeScript property name
|
|
1755
|
-
*/
|
|
1756
|
-
columnToProperty(column: ColumnNode): string;
|
|
1757
|
-
}
|
|
1758
|
-
|
|
1759
|
-
interface QueryContext {
|
|
1760
|
-
sql: string;
|
|
1761
|
-
params: unknown[];
|
|
1762
|
-
}
|
|
1763
|
-
type QueryInterceptor = (ctx: QueryContext, next: () => Promise<QueryResult[]>) => Promise<QueryResult[]>;
|
|
1764
|
-
declare class InterceptorPipeline {
|
|
1765
|
-
private interceptors;
|
|
1766
|
-
use(interceptor: QueryInterceptor): void;
|
|
1767
|
-
run(ctx: QueryContext, executor: DbExecutor): Promise<QueryResult[]>;
|
|
1768
|
-
}
|
|
1769
|
-
|
|
1770
|
-
interface OrmOptions<E extends DomainEvent = OrmDomainEvent> {
|
|
1771
|
-
dialect: Dialect;
|
|
1772
|
-
executorFactory: DbExecutorFactory;
|
|
1773
|
-
interceptors?: InterceptorPipeline;
|
|
1774
|
-
namingStrategy?: NamingStrategy;
|
|
1775
|
-
}
|
|
1776
|
-
interface DbExecutorFactory {
|
|
1777
|
-
createExecutor(options?: {
|
|
1778
|
-
tx?: ExternalTransaction;
|
|
1779
|
-
}): DbExecutor;
|
|
1780
|
-
createTransactionalExecutor(): DbExecutor;
|
|
1781
|
-
}
|
|
1782
|
-
interface ExternalTransaction {
|
|
1783
|
-
}
|
|
1784
|
-
declare class Orm<E extends DomainEvent = OrmDomainEvent> {
|
|
1785
|
-
readonly dialect: Dialect;
|
|
1786
|
-
readonly interceptors: InterceptorPipeline;
|
|
1787
|
-
readonly namingStrategy: NamingStrategy;
|
|
1788
|
-
private readonly executorFactory;
|
|
1789
|
-
constructor(opts: OrmOptions<E>);
|
|
1790
|
-
createSession(options?: {
|
|
1791
|
-
tx?: ExternalTransaction;
|
|
1792
|
-
}): OrmSession<E>;
|
|
1793
|
-
transaction<T>(fn: (session: OrmSession<E>) => Promise<T>): Promise<T>;
|
|
1794
|
-
}
|
|
1795
|
-
|
|
1796
|
-
declare class IdentityMap {
|
|
1797
|
-
private readonly buckets;
|
|
1798
|
-
get bucketsMap(): Map<string, Map<string, TrackedEntity>>;
|
|
1799
|
-
getEntity(table: TableDef, pk: string | number): any | undefined;
|
|
1800
|
-
register(tracked: TrackedEntity): void;
|
|
1801
|
-
remove(tracked: TrackedEntity): void;
|
|
1802
|
-
getEntitiesForTable(table: TableDef): TrackedEntity[];
|
|
1803
|
-
clear(): void;
|
|
1804
|
-
private toIdentityKey;
|
|
1805
|
-
}
|
|
1806
|
-
|
|
1807
|
-
declare class UnitOfWork {
|
|
1808
|
-
private readonly dialect;
|
|
1809
|
-
private readonly executor;
|
|
1810
|
-
private readonly identityMap;
|
|
1811
|
-
private readonly hookContext;
|
|
1812
|
-
private readonly trackedEntities;
|
|
1813
|
-
constructor(dialect: Dialect, executor: DbExecutor, identityMap: IdentityMap, hookContext: () => unknown);
|
|
1814
|
-
get identityBuckets(): Map<string, Map<string, TrackedEntity>>;
|
|
1815
|
-
getTracked(): TrackedEntity[];
|
|
1816
|
-
getEntity(table: TableDef, pk: string | number): any | undefined;
|
|
1817
|
-
getEntitiesForTable(table: TableDef): TrackedEntity[];
|
|
1818
|
-
findTracked(entity: any): TrackedEntity | undefined;
|
|
1819
|
-
setEntity(table: TableDef, pk: string | number, entity: any): void;
|
|
1820
|
-
trackNew(table: TableDef, entity: any, pk?: string | number): void;
|
|
1821
|
-
trackManaged(table: TableDef, pk: string | number, entity: any): void;
|
|
1822
|
-
markDirty(entity: any): void;
|
|
1823
|
-
markRemoved(entity: any): void;
|
|
1824
|
-
flush(): Promise<void>;
|
|
1825
|
-
reset(): void;
|
|
1826
|
-
private flushInsert;
|
|
1827
|
-
private flushUpdate;
|
|
1828
|
-
private flushDelete;
|
|
1829
|
-
private runHook;
|
|
1830
|
-
private computeChanges;
|
|
1831
|
-
private extractColumns;
|
|
1832
|
-
private executeCompiled;
|
|
1833
|
-
private getReturningColumns;
|
|
1834
|
-
private applyReturningResults;
|
|
1835
|
-
private normalizeColumnName;
|
|
1836
|
-
private registerIdentity;
|
|
1837
|
-
private createSnapshot;
|
|
1838
|
-
private getPrimaryKeyValue;
|
|
1839
|
-
}
|
|
1840
|
-
|
|
1841
|
-
type EventOfType<E extends DomainEvent, TType extends E['type']> = Extract<E, {
|
|
1842
|
-
type: TType;
|
|
1843
|
-
}>;
|
|
1844
|
-
type DomainEventHandler<E extends DomainEvent, Context> = (event: E, ctx: Context) => Promise<void> | void;
|
|
1845
|
-
type InitialHandlers<E extends DomainEvent, Context> = {
|
|
1846
|
-
[K in E['type']]?: DomainEventHandler<EventOfType<E, K>, Context>[];
|
|
1847
|
-
};
|
|
1848
|
-
declare class DomainEventBus<E extends DomainEvent, Context> {
|
|
1849
|
-
private readonly handlers;
|
|
1850
|
-
constructor(initialHandlers?: InitialHandlers<E, Context>);
|
|
1851
|
-
on<TType extends E['type']>(type: TType, handler: DomainEventHandler<EventOfType<E, TType>, Context>): void;
|
|
1852
|
-
register<TType extends E['type']>(type: TType, handler: DomainEventHandler<EventOfType<E, TType>, Context>): void;
|
|
1853
|
-
dispatch(trackedEntities: Iterable<TrackedEntity>, ctx: Context): Promise<void>;
|
|
1854
|
-
}
|
|
1855
|
-
declare const addDomainEvent: <E extends DomainEvent>(entity: HasDomainEvents<E>, event: E) => void;
|
|
1856
|
-
|
|
1857
|
-
declare class RelationChangeProcessor {
|
|
1858
|
-
private readonly unitOfWork;
|
|
1859
|
-
private readonly dialect;
|
|
1860
|
-
private readonly executor;
|
|
1861
|
-
private readonly relationChanges;
|
|
1862
|
-
constructor(unitOfWork: UnitOfWork, dialect: Dialect, executor: DbExecutor);
|
|
1863
|
-
registerChange(entry: RelationChangeEntry): void;
|
|
1864
|
-
reset(): void;
|
|
1865
|
-
process(): Promise<void>;
|
|
1866
|
-
private handleHasManyChange;
|
|
1867
|
-
private handleHasOneChange;
|
|
1868
|
-
private handleBelongsToChange;
|
|
1869
|
-
private handleBelongsToManyChange;
|
|
1870
|
-
private assignHasManyForeignKey;
|
|
1871
|
-
private detachHasManyChild;
|
|
1872
|
-
private assignHasOneForeignKey;
|
|
1873
|
-
private detachHasOneChild;
|
|
1874
|
-
private insertPivotRow;
|
|
1875
|
-
private deletePivotRow;
|
|
1876
|
-
private resolvePrimaryKeyValue;
|
|
1877
|
-
}
|
|
1878
|
-
|
|
1879
|
-
/**
|
|
1880
|
-
* Represents a single SQL query log entry
|
|
1881
|
-
*/
|
|
1882
|
-
interface QueryLogEntry {
|
|
1883
|
-
/** The SQL query that was executed */
|
|
1884
|
-
sql: string;
|
|
1885
|
-
/** Parameters used in the query */
|
|
1886
|
-
params?: unknown[];
|
|
1887
|
-
}
|
|
1888
|
-
/**
|
|
1889
|
-
* Function type for query logging callbacks
|
|
1890
|
-
* @param entry - The query log entry to process
|
|
1891
|
-
*/
|
|
1892
|
-
type QueryLogger = (entry: QueryLogEntry) => void;
|
|
1893
|
-
/**
|
|
1894
|
-
* Creates a wrapped database executor that logs all SQL queries
|
|
1895
|
-
* @param executor - Original database executor to wrap
|
|
1896
|
-
* @param logger - Optional logger function to receive query log entries
|
|
1897
|
-
* @returns Wrapped executor that logs queries before execution
|
|
1898
|
-
*/
|
|
1899
|
-
declare const createQueryLoggingExecutor: (executor: DbExecutor, logger?: QueryLogger) => DbExecutor;
|
|
1900
|
-
|
|
1901
|
-
/**
|
|
1902
|
-
* Context for SQL query execution
|
|
1903
|
-
*/
|
|
1904
|
-
interface ExecutionContext {
|
|
1905
|
-
/** Database dialect to use for SQL generation */
|
|
1906
|
-
dialect: Dialect;
|
|
1907
|
-
/** Database executor for running SQL queries */
|
|
1908
|
-
executor: DbExecutor;
|
|
1909
|
-
/** Interceptor pipeline for query processing */
|
|
1910
|
-
interceptors: InterceptorPipeline;
|
|
1911
|
-
}
|
|
1912
|
-
|
|
1913
|
-
interface EntityContext {
|
|
1914
|
-
dialect: Dialect;
|
|
1915
|
-
executor: DbExecutor;
|
|
1916
|
-
getEntity(table: TableDef, pk: any): any;
|
|
1917
|
-
setEntity(table: TableDef, pk: any, entity: any): void;
|
|
1918
|
-
trackNew(table: TableDef, entity: any, pk?: any): void;
|
|
1919
|
-
trackManaged(table: TableDef, pk: any, entity: any): void;
|
|
1920
|
-
markDirty(entity: any): void;
|
|
1921
|
-
markRemoved(entity: any): void;
|
|
1922
|
-
getEntitiesForTable(table: TableDef): TrackedEntity[];
|
|
1923
|
-
registerRelationChange(root: any, relationKey: RelationKey, rootTable: TableDef, relationName: string, relation: RelationDef, change: RelationChange<any>): void;
|
|
1924
|
-
}
|
|
1925
|
-
|
|
1926
|
-
interface HydrationContext<E extends DomainEvent = AnyDomainEvent> {
|
|
1927
|
-
identityMap: IdentityMap;
|
|
1928
|
-
unitOfWork: UnitOfWork;
|
|
1929
|
-
domainEvents: DomainEventBus<E, OrmSession<E>>;
|
|
1930
|
-
relationChanges: RelationChangeProcessor;
|
|
1931
|
-
entityContext: EntityContext;
|
|
1932
|
-
}
|
|
1933
|
-
|
|
1934
|
-
interface OrmInterceptor {
|
|
1935
|
-
beforeFlush?(ctx: EntityContext): Promise<void> | void;
|
|
1936
|
-
afterFlush?(ctx: EntityContext): Promise<void> | void;
|
|
1937
|
-
}
|
|
1938
|
-
interface OrmSessionOptions<E extends DomainEvent = OrmDomainEvent> {
|
|
1939
|
-
orm: Orm<E>;
|
|
1940
|
-
executor: DbExecutor;
|
|
1941
|
-
queryLogger?: QueryLogger;
|
|
1942
|
-
interceptors?: OrmInterceptor[];
|
|
1943
|
-
domainEventHandlers?: InitialHandlers<E, OrmSession<E>>;
|
|
1944
|
-
}
|
|
1945
|
-
declare class OrmSession<E extends DomainEvent = OrmDomainEvent> implements EntityContext {
|
|
1946
|
-
readonly orm: Orm<E>;
|
|
1947
|
-
readonly executor: DbExecutor;
|
|
1948
|
-
readonly identityMap: IdentityMap;
|
|
1949
|
-
readonly unitOfWork: UnitOfWork;
|
|
1950
|
-
readonly domainEvents: DomainEventBus<E, OrmSession<E>>;
|
|
1951
|
-
readonly relationChanges: RelationChangeProcessor;
|
|
1952
|
-
private readonly interceptors;
|
|
1953
|
-
constructor(opts: OrmSessionOptions<E>);
|
|
1954
|
-
get dialect(): Dialect;
|
|
1955
|
-
get identityBuckets(): Map<string, Map<string, TrackedEntity>>;
|
|
1956
|
-
get tracked(): TrackedEntity[];
|
|
1957
|
-
getEntity(table: TableDef, pk: any): any | undefined;
|
|
1958
|
-
setEntity(table: TableDef, pk: any, entity: any): void;
|
|
1959
|
-
trackNew(table: TableDef, entity: any, pk?: any): void;
|
|
1960
|
-
trackManaged(table: TableDef, pk: any, entity: any): void;
|
|
1961
|
-
markDirty(entity: any): void;
|
|
1962
|
-
markRemoved(entity: any): void;
|
|
1963
|
-
registerRelationChange: (root: any, relationKey: RelationKey, rootTable: TableDef, relationName: string, relation: RelationDef, change: RelationChange<any>) => void;
|
|
1964
|
-
getEntitiesForTable(table: TableDef): TrackedEntity[];
|
|
1965
|
-
registerInterceptor(interceptor: OrmInterceptor): void;
|
|
1966
|
-
registerDomainEventHandler<TType extends E['type']>(type: TType, handler: DomainEventHandler<Extract<E, {
|
|
1967
|
-
type: TType;
|
|
1968
|
-
}>, OrmSession<E>>): void;
|
|
1969
|
-
find<TTable extends TableDef>(entityClass: EntityConstructor, id: any): Promise<Entity<TTable> | null>;
|
|
1970
|
-
findOne<TTable extends TableDef>(qb: SelectQueryBuilder<any, TTable>): Promise<Entity<TTable> | null>;
|
|
1971
|
-
findMany<TTable extends TableDef>(qb: SelectQueryBuilder<any, TTable>): Promise<Entity<TTable>[]>;
|
|
1972
|
-
persist(entity: object): Promise<void>;
|
|
1973
|
-
remove(entity: object): Promise<void>;
|
|
1974
|
-
flush(): Promise<void>;
|
|
1975
|
-
commit(): Promise<void>;
|
|
1976
|
-
rollback(): Promise<void>;
|
|
1977
|
-
getExecutionContext(): ExecutionContext;
|
|
1978
|
-
getHydrationContext(): HydrationContext<E>;
|
|
1979
|
-
}
|
|
1980
|
-
|
|
1981
|
-
type SelectDialectInput = Dialect | DialectKey;
|
|
1982
|
-
|
|
1983
|
-
type ColumnSelectionValue = ColumnDef | FunctionNode | CaseExpressionNode | WindowFunctionNode;
|
|
1984
|
-
type DeepSelectConfig<TTable extends TableDef> = {
|
|
1985
|
-
root?: (keyof TTable['columns'] & string)[];
|
|
1986
|
-
} & {
|
|
1987
|
-
[K in keyof TTable['relations'] & string]?: (keyof RelationTargetTable<TTable['relations'][K]>['columns'] & string)[];
|
|
1988
|
-
};
|
|
1989
|
-
type WhereHasOptions = {
|
|
1990
|
-
correlate?: ExpressionNode;
|
|
1991
|
-
};
|
|
1992
|
-
type RelationCallback = <TChildTable extends TableDef>(qb: SelectQueryBuilder<any, TChildTable>) => SelectQueryBuilder<any, TChildTable>;
|
|
1993
|
-
/**
|
|
1994
|
-
|
|
1995
|
-
* Main query builder class for constructing SQL SELECT queries
|
|
1996
|
-
|
|
1997
|
-
* @typeParam T - Result type for projections (unused)
|
|
1998
|
-
|
|
1999
|
-
* @typeParam TTable - Table definition being queried
|
|
2000
|
-
|
|
2001
|
-
*/
|
|
2002
|
-
declare class SelectQueryBuilder<T = any, TTable extends TableDef = TableDef> {
|
|
2003
|
-
private readonly env;
|
|
2004
|
-
private readonly context;
|
|
2005
|
-
private readonly columnSelector;
|
|
2006
|
-
private readonly relationManager;
|
|
2007
|
-
private readonly lazyRelations;
|
|
2008
|
-
/**
|
|
2009
|
-
|
|
2010
|
-
* Creates a new SelectQueryBuilder instance
|
|
2011
|
-
|
|
2012
|
-
* @param table - Table definition to query
|
|
2013
|
-
|
|
2014
|
-
* @param state - Optional initial query state
|
|
2015
|
-
|
|
2016
|
-
* @param hydration - Optional hydration manager
|
|
2017
|
-
|
|
2018
|
-
* @param dependencies - Optional query builder dependencies
|
|
2019
|
-
|
|
2020
|
-
*/
|
|
2021
|
-
constructor(table: TTable, state?: SelectQueryState, hydration?: HydrationManager, dependencies?: Partial<SelectQueryBuilderDependencies>, lazyRelations?: Set<string>);
|
|
2022
|
-
private clone;
|
|
2023
|
-
/**
|
|
2024
|
-
* Applies an alias to the root FROM table.
|
|
2025
|
-
* @param alias - Alias to apply
|
|
2026
|
-
*/
|
|
2027
|
-
as(alias: string): SelectQueryBuilder<T, TTable>;
|
|
2028
|
-
private resolveQueryNode;
|
|
2029
|
-
private applyCorrelation;
|
|
2030
|
-
private createChildBuilder;
|
|
2031
|
-
private applyAst;
|
|
2032
|
-
private applyJoin;
|
|
2033
|
-
private applySetOperation;
|
|
2034
|
-
/**
|
|
2035
|
-
|
|
2036
|
-
* Selects specific columns for the query
|
|
2037
|
-
|
|
2038
|
-
* @param columns - Record of column definitions, function nodes, case expressions, or window functions
|
|
2039
|
-
|
|
2040
|
-
* @returns New query builder instance with selected columns
|
|
2041
|
-
|
|
2042
|
-
*/
|
|
2043
|
-
select(columns: Record<string, ColumnSelectionValue>): SelectQueryBuilder<T, TTable>;
|
|
2044
|
-
/**
|
|
2045
|
-
* Selects columns from the root table by name (typed).
|
|
2046
|
-
* @param cols - Column names on the root table
|
|
2047
|
-
*/
|
|
2048
|
-
selectColumns<K extends keyof TTable['columns'] & string>(...cols: K[]): SelectQueryBuilder<T, TTable>;
|
|
2049
|
-
/**
|
|
2050
|
-
|
|
2051
|
-
* Selects raw column expressions
|
|
2052
|
-
|
|
2053
|
-
* @param cols - Column expressions as strings
|
|
2054
|
-
|
|
2055
|
-
* @returns New query builder instance with raw column selections
|
|
2056
|
-
|
|
2057
|
-
*/
|
|
2058
|
-
selectRaw(...cols: string[]): SelectQueryBuilder<T, TTable>;
|
|
2059
|
-
/**
|
|
2060
|
-
|
|
2061
|
-
* Adds a Common Table Expression (CTE) to the query
|
|
2062
|
-
|
|
2063
|
-
* @param name - Name of the CTE
|
|
2064
|
-
|
|
2065
|
-
* @param query - Query builder or query node for the CTE
|
|
2066
|
-
|
|
2067
|
-
* @param columns - Optional column names for the CTE
|
|
2068
|
-
|
|
2069
|
-
* @returns New query builder instance with the CTE
|
|
2070
|
-
|
|
2071
|
-
*/
|
|
2072
|
-
with(name: string, query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, columns?: string[]): SelectQueryBuilder<T, TTable>;
|
|
2073
|
-
/**
|
|
2074
|
-
|
|
2075
|
-
* Adds a recursive Common Table Expression (CTE) to the query
|
|
2076
|
-
|
|
2077
|
-
* @param name - Name of the CTE
|
|
2078
|
-
|
|
2079
|
-
* @param query - Query builder or query node for the CTE
|
|
2080
|
-
|
|
2081
|
-
* @param columns - Optional column names for the CTE
|
|
2082
|
-
|
|
2083
|
-
* @returns New query builder instance with the recursive CTE
|
|
2084
|
-
|
|
2085
|
-
*/
|
|
2086
|
-
withRecursive(name: string, query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, columns?: string[]): SelectQueryBuilder<T, TTable>;
|
|
2087
|
-
/**
|
|
2088
|
-
* Replaces the FROM clause with a derived table (subquery with alias)
|
|
2089
|
-
* @param subquery - Subquery to use as the FROM source
|
|
2090
|
-
* @param alias - Alias for the derived table
|
|
2091
|
-
* @param columnAliases - Optional column alias list
|
|
2092
|
-
* @returns New query builder instance with updated FROM
|
|
2093
|
-
*/
|
|
2094
|
-
fromSubquery(subquery: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, alias: string, columnAliases?: string[]): SelectQueryBuilder<T, TTable>;
|
|
2095
|
-
/**
|
|
2096
|
-
|
|
2097
|
-
* Selects a subquery as a column
|
|
2098
|
-
|
|
2099
|
-
* @param alias - Alias for the subquery column
|
|
2100
|
-
|
|
2101
|
-
* @param sub - Query builder or query node for the subquery
|
|
2102
|
-
|
|
2103
|
-
* @returns New query builder instance with the subquery selection
|
|
2104
|
-
|
|
2105
|
-
*/
|
|
2106
|
-
selectSubquery(alias: string, sub: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode): SelectQueryBuilder<T, TTable>;
|
|
2107
|
-
/**
|
|
2108
|
-
* Adds a JOIN against a derived table (subquery with alias)
|
|
2109
|
-
* @param subquery - Subquery to join
|
|
2110
|
-
* @param alias - Alias for the derived table
|
|
2111
|
-
* @param condition - Join condition expression
|
|
2112
|
-
* @param joinKind - Join kind (defaults to INNER)
|
|
2113
|
-
* @param columnAliases - Optional column alias list for the derived table
|
|
2114
|
-
* @returns New query builder instance with the derived-table join
|
|
2115
|
-
*/
|
|
2116
|
-
joinSubquery(subquery: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, alias: string, condition: BinaryExpressionNode, joinKind?: JoinKind, columnAliases?: string[]): SelectQueryBuilder<T, TTable>;
|
|
2117
|
-
/**
|
|
2118
|
-
|
|
2119
|
-
* Adds an INNER JOIN to the query
|
|
2120
|
-
|
|
2121
|
-
* @param table - Table to join
|
|
2122
|
-
|
|
2123
|
-
* @param condition - Join condition expression
|
|
2124
|
-
|
|
2125
|
-
* @returns New query builder instance with the INNER JOIN
|
|
2126
|
-
|
|
2127
|
-
*/
|
|
2128
|
-
innerJoin(table: TableDef, condition: BinaryExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2129
|
-
/**
|
|
2130
|
-
|
|
2131
|
-
* Adds a LEFT JOIN to the query
|
|
2132
|
-
|
|
2133
|
-
* @param table - Table to join
|
|
2134
|
-
|
|
2135
|
-
* @param condition - Join condition expression
|
|
2136
|
-
|
|
2137
|
-
* @returns New query builder instance with the LEFT JOIN
|
|
2138
|
-
|
|
2139
|
-
*/
|
|
2140
|
-
leftJoin(table: TableDef, condition: BinaryExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2141
|
-
/**
|
|
2142
|
-
|
|
2143
|
-
* Adds a RIGHT JOIN to the query
|
|
2144
|
-
|
|
2145
|
-
* @param table - Table to join
|
|
2146
|
-
|
|
2147
|
-
* @param condition - Join condition expression
|
|
2148
|
-
|
|
2149
|
-
* @returns New query builder instance with the RIGHT JOIN
|
|
2150
|
-
|
|
2151
|
-
*/
|
|
2152
|
-
rightJoin(table: TableDef, condition: BinaryExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2153
|
-
/**
|
|
2154
|
-
|
|
2155
|
-
* Matches records based on a relationship
|
|
2156
|
-
|
|
2157
|
-
* @param relationName - Name of the relationship to match
|
|
2158
|
-
|
|
2159
|
-
* @param predicate - Optional predicate expression
|
|
2160
|
-
|
|
2161
|
-
* @returns New query builder instance with the relationship match
|
|
2162
|
-
|
|
2163
|
-
*/
|
|
2164
|
-
match(relationName: string, predicate?: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2165
|
-
/**
|
|
2166
|
-
|
|
2167
|
-
* Joins a related table
|
|
2168
|
-
|
|
2169
|
-
* @param relationName - Name of the relationship to join
|
|
2170
|
-
|
|
2171
|
-
* @param joinKind - Type of join (defaults to INNER)
|
|
2172
|
-
|
|
2173
|
-
* @param extraCondition - Optional additional join condition
|
|
2174
|
-
|
|
2175
|
-
* @returns New query builder instance with the relationship join
|
|
2176
|
-
|
|
2177
|
-
*/
|
|
2178
|
-
joinRelation(relationName: string, joinKind?: JoinKind, extraCondition?: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2179
|
-
/**
|
|
2180
|
-
|
|
2181
|
-
* Includes related data in the query results
|
|
2182
|
-
|
|
2183
|
-
* @param relationName - Name of the relationship to include
|
|
2184
|
-
|
|
2185
|
-
* @param options - Optional include options
|
|
2186
|
-
|
|
2187
|
-
* @returns New query builder instance with the relationship inclusion
|
|
2188
|
-
|
|
2189
|
-
*/
|
|
2190
|
-
include(relationName: string, options?: RelationIncludeOptions): SelectQueryBuilder<T, TTable>;
|
|
2191
|
-
includeLazy<K extends keyof RelationMap<TTable>>(relationName: K): SelectQueryBuilder<T, TTable>;
|
|
2192
|
-
/**
|
|
2193
|
-
* Selects columns for a related table in a single hop.
|
|
2194
|
-
*/
|
|
2195
|
-
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>;
|
|
2196
|
-
/**
|
|
2197
|
-
* Convenience alias for selecting specific columns from a relation.
|
|
2198
|
-
*/
|
|
2199
|
-
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>;
|
|
2200
|
-
/**
|
|
2201
|
-
* Selects columns for the root table and relations from a single config object.
|
|
2202
|
-
*/
|
|
2203
|
-
selectColumnsDeep(config: DeepSelectConfig<TTable>): SelectQueryBuilder<T, TTable>;
|
|
2204
|
-
getLazyRelations(): (keyof RelationMap<TTable>)[];
|
|
2205
|
-
getTable(): TTable;
|
|
2206
|
-
execute(ctx: OrmSession): Promise<Entity<TTable>[]>;
|
|
2207
|
-
executeWithContexts(execCtx: ExecutionContext, hydCtx: HydrationContext): Promise<Entity<TTable>[]>;
|
|
2208
|
-
/**
|
|
2209
|
-
|
|
2210
|
-
* Adds a WHERE condition to the query
|
|
2211
|
-
|
|
2212
|
-
* @param expr - Expression for the WHERE clause
|
|
2213
|
-
|
|
2214
|
-
* @returns New query builder instance with the WHERE condition
|
|
2215
|
-
|
|
2216
|
-
*/
|
|
2217
|
-
where(expr: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2218
|
-
/**
|
|
2219
|
-
|
|
2220
|
-
* Adds a GROUP BY clause to the query
|
|
2221
|
-
|
|
2222
|
-
* @param col - Column definition or column node to group by
|
|
2223
|
-
|
|
2224
|
-
* @returns New query builder instance with the GROUP BY clause
|
|
2225
|
-
|
|
2226
|
-
*/
|
|
2227
|
-
groupBy(col: ColumnDef | ColumnNode): SelectQueryBuilder<T, TTable>;
|
|
2228
|
-
/**
|
|
2229
|
-
|
|
2230
|
-
* Adds a HAVING condition to the query
|
|
2231
|
-
|
|
2232
|
-
* @param expr - Expression for the HAVING clause
|
|
2233
|
-
|
|
2234
|
-
* @returns New query builder instance with the HAVING condition
|
|
2235
|
-
|
|
2236
|
-
*/
|
|
2237
|
-
having(expr: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2238
|
-
/**
|
|
2239
|
-
|
|
2240
|
-
* Adds an ORDER BY clause to the query
|
|
2241
|
-
|
|
2242
|
-
* @param col - Column definition or column node to order by
|
|
2243
|
-
|
|
2244
|
-
* @param direction - Order direction (defaults to ASC)
|
|
2245
|
-
|
|
2246
|
-
* @returns New query builder instance with the ORDER BY clause
|
|
2247
|
-
|
|
2248
|
-
*/
|
|
2249
|
-
orderBy(col: ColumnDef | ColumnNode, direction?: OrderDirection): SelectQueryBuilder<T, TTable>;
|
|
2250
|
-
/**
|
|
2251
|
-
|
|
2252
|
-
* Adds a DISTINCT clause to the query
|
|
2253
|
-
|
|
2254
|
-
* @param cols - Columns to make distinct
|
|
2255
|
-
|
|
2256
|
-
* @returns New query builder instance with the DISTINCT clause
|
|
2257
|
-
|
|
2258
|
-
*/
|
|
2259
|
-
distinct(...cols: (ColumnDef | ColumnNode)[]): SelectQueryBuilder<T, TTable>;
|
|
2260
|
-
/**
|
|
2261
|
-
|
|
2262
|
-
* Adds a LIMIT clause to the query
|
|
2263
|
-
|
|
2264
|
-
* @param n - Maximum number of rows to return
|
|
2265
|
-
|
|
2266
|
-
* @returns New query builder instance with the LIMIT clause
|
|
2267
|
-
|
|
2268
|
-
*/
|
|
2269
|
-
limit(n: number): SelectQueryBuilder<T, TTable>;
|
|
2270
|
-
/**
|
|
2271
|
-
|
|
2272
|
-
* Adds an OFFSET clause to the query
|
|
2273
|
-
|
|
2274
|
-
* @param n - Number of rows to skip
|
|
2275
|
-
|
|
2276
|
-
* @returns New query builder instance with the OFFSET clause
|
|
2277
|
-
|
|
2278
|
-
*/
|
|
2279
|
-
offset(n: number): SelectQueryBuilder<T, TTable>;
|
|
2280
|
-
/**
|
|
2281
|
-
|
|
2282
|
-
* Combines this query with another using UNION
|
|
2283
|
-
|
|
2284
|
-
* @param query - Query to union with
|
|
2285
|
-
|
|
2286
|
-
* @returns New query builder instance with the set operation
|
|
2287
|
-
|
|
2288
|
-
*/
|
|
2289
|
-
union(query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode): SelectQueryBuilder<T, TTable>;
|
|
2290
|
-
/**
|
|
2291
|
-
|
|
2292
|
-
* Combines this query with another using UNION ALL
|
|
2293
|
-
|
|
2294
|
-
* @param query - Query to union with
|
|
2295
|
-
|
|
2296
|
-
* @returns New query builder instance with the set operation
|
|
2297
|
-
|
|
2298
|
-
*/
|
|
2299
|
-
unionAll(query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode): SelectQueryBuilder<T, TTable>;
|
|
2300
|
-
/**
|
|
2301
|
-
|
|
2302
|
-
* Combines this query with another using INTERSECT
|
|
2303
|
-
|
|
2304
|
-
* @param query - Query to intersect with
|
|
2305
|
-
|
|
2306
|
-
* @returns New query builder instance with the set operation
|
|
2307
|
-
|
|
2308
|
-
*/
|
|
2309
|
-
intersect(query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode): SelectQueryBuilder<T, TTable>;
|
|
2310
|
-
/**
|
|
2311
|
-
|
|
2312
|
-
* Combines this query with another using EXCEPT
|
|
2313
|
-
|
|
2314
|
-
* @param query - Query to subtract
|
|
2315
|
-
|
|
2316
|
-
* @returns New query builder instance with the set operation
|
|
2317
|
-
|
|
2318
|
-
*/
|
|
2319
|
-
except(query: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode): SelectQueryBuilder<T, TTable>;
|
|
2320
|
-
/**
|
|
2321
|
-
|
|
2322
|
-
* Adds a WHERE EXISTS condition to the query
|
|
2323
|
-
|
|
2324
|
-
* @param subquery - Subquery to check for existence
|
|
2325
|
-
|
|
2326
|
-
* @returns New query builder instance with the WHERE EXISTS condition
|
|
2327
|
-
|
|
2328
|
-
*/
|
|
2329
|
-
whereExists(subquery: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, correlate?: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2330
|
-
/**
|
|
2331
|
-
|
|
2332
|
-
* Adds a WHERE NOT EXISTS condition to the query
|
|
2333
|
-
|
|
2334
|
-
* @param subquery - Subquery to check for non-existence
|
|
2335
|
-
|
|
2336
|
-
* @returns New query builder instance with the WHERE NOT EXISTS condition
|
|
2337
|
-
|
|
2338
|
-
*/
|
|
2339
|
-
whereNotExists(subquery: SelectQueryBuilder<any, TableDef<any>> | SelectQueryNode, correlate?: ExpressionNode): SelectQueryBuilder<T, TTable>;
|
|
2340
|
-
/**
|
|
2341
|
-
|
|
2342
|
-
* Adds a WHERE EXISTS condition based on a relationship
|
|
2343
|
-
|
|
2344
|
-
* @param relationName - Name of the relationship to check
|
|
2345
|
-
|
|
2346
|
-
* @param callback - Optional callback to modify the relationship query
|
|
2347
|
-
|
|
2348
|
-
* @returns New query builder instance with the relationship existence check
|
|
2349
|
-
|
|
2350
|
-
*/
|
|
2351
|
-
whereHas(relationName: string, callbackOrOptions?: RelationCallback | WhereHasOptions, maybeOptions?: WhereHasOptions): SelectQueryBuilder<T, TTable>;
|
|
2352
|
-
/**
|
|
2353
|
-
|
|
2354
|
-
* Adds a WHERE NOT EXISTS condition based on a relationship
|
|
2355
|
-
|
|
2356
|
-
* @param relationName - Name of the relationship to check
|
|
2357
|
-
|
|
2358
|
-
* @param callback - Optional callback to modify the relationship query
|
|
2359
|
-
|
|
2360
|
-
* @returns New query builder instance with the relationship non-existence check
|
|
2361
|
-
|
|
2362
|
-
*/
|
|
2363
|
-
whereHasNot(relationName: string, callbackOrOptions?: RelationCallback | WhereHasOptions, maybeOptions?: WhereHasOptions): SelectQueryBuilder<T, TTable>;
|
|
2364
|
-
/**
|
|
2365
|
-
|
|
2366
|
-
* Compiles the query to SQL for a specific dialect
|
|
2367
|
-
|
|
2368
|
-
* @param dialect - Database dialect to compile for
|
|
2369
|
-
|
|
2370
|
-
* @returns Compiled query with SQL and parameters
|
|
2371
|
-
|
|
2372
|
-
*/
|
|
2373
|
-
compile(dialect: SelectDialectInput): CompiledQuery;
|
|
2374
|
-
/**
|
|
2375
|
-
|
|
2376
|
-
* Converts the query to SQL string for a specific dialect
|
|
2377
|
-
|
|
2378
|
-
* @param dialect - Database dialect to generate SQL for
|
|
2379
|
-
|
|
2380
|
-
* @returns SQL string representation of the query
|
|
2381
|
-
|
|
2382
|
-
*/
|
|
2383
|
-
toSql(dialect: SelectDialectInput): string;
|
|
2384
|
-
/**
|
|
2385
|
-
|
|
2386
|
-
* Gets the hydration plan for the query
|
|
2387
|
-
|
|
2388
|
-
* @returns Hydration plan or undefined if none exists
|
|
2389
|
-
|
|
2390
|
-
*/
|
|
2391
|
-
getHydrationPlan(): HydrationPlan | undefined;
|
|
2392
|
-
/**
|
|
2393
|
-
|
|
2394
|
-
* Gets the Abstract Syntax Tree (AST) representation of the query
|
|
2395
|
-
|
|
2396
|
-
* @returns Query AST with hydration applied
|
|
2397
|
-
|
|
2398
|
-
*/
|
|
2399
|
-
getAST(): SelectQueryNode;
|
|
2400
|
-
}
|
|
2401
|
-
/**
|
|
2402
|
-
|
|
2403
|
-
* Creates a column node for use in expressions
|
|
2404
|
-
|
|
2405
|
-
* @param table - Table name
|
|
2406
|
-
|
|
2407
|
-
* @param name - Column name
|
|
2408
|
-
|
|
2409
|
-
* @returns ColumnNode with the specified table and name
|
|
2410
|
-
|
|
2411
|
-
*/
|
|
2412
|
-
declare const createColumn: (table: string, name: string) => ColumnNode;
|
|
2413
|
-
/**
|
|
2414
|
-
|
|
2415
|
-
* Creates a literal value node for use in expressions
|
|
2416
|
-
|
|
2417
|
-
* @param val - Literal value (string or number)
|
|
2418
|
-
|
|
2419
|
-
* @returns LiteralNode with the specified value
|
|
2420
|
-
|
|
2421
|
-
*/
|
|
2422
|
-
declare const createLiteral: (val: string | number) => LiteralNode;
|
|
2423
|
-
|
|
2424
|
-
export { type HydrationContext as $, type Entity as A, type BinaryExpressionNode as B, type ColumnRef as C, Dialect as D, type ExpressionNode as E, type FunctionNode as F, type HasManyRelation as G, type HydrationPlan as H, type InExpressionNode as I, type JsonPathNode as J, type HasOneRelation as K, type LiteralNode as L, type BelongsToRelation as M, type NullExpressionNode as N, type OperandNode as O, type BelongsToManyRelation as P, type HasManyCollection as Q, type RelationMap as R, type SelectQueryNode as S, type TableRef as T, type UpdateQueryNode as U, type BelongsToReference as V, type WindowFunctionNode as W, type ManyToManyCollection as X, OrmSession as Y, SelectQueryBuilder as Z, type ExecutionContext as _, type ColumnNode as a, type CheckConstraint as a0, type TableOptions as a1, type TableHooks as a2, defineTable as a3, type ColumnType as a4, type ReferentialAction as a5, type RawDefaultValue as a6, type DefaultValue as a7, col as a8, RelationKinds as a9, Orm as aA, type DomainEventHandler as aB, type InitialHandlers as aC, DomainEventBus as aD, addDomainEvent as aE, EntityStatus as aF, type TrackedEntity as aG, type RelationKey as aH, type RelationChange as aI, type RelationChangeEntry as aJ, type DomainEvent as aK, type AnyDomainEvent as aL, type OrmDomainEvent as aM, type HasDomainEvents as aN, type QueryLogEntry as aO, type QueryLogger as aP, createQueryLoggingExecutor as aQ, type QueryResult as aR, rowsToQueryResult as aS, type SimpleQueryRunner as aT, createExecutorFromQueryRunner as aU, type EntityOrTableTargetResolver as aV, type EntityConstructor as aW, type RelationType as aa, type CascadeMode as ab, type RelationDef as ac, hasMany as ad, hasOne as ae, belongsTo as af, belongsToMany as ag, type RelationTargetTable as ah, type ColumnToTs as ai, type InferRow as aj, type HasOneReference as ak, createColumn as al, createLiteral as am, isOperandNode as an, isFunctionNode as ao, isCaseExpressionNode as ap, isWindowFunctionNode as aq, isExpressionSelectionNode as ar, type HydrationPivotPlan as as, type HydrationRelationPlan as at, type HydrationMetadata as au, type OrmInterceptor as av, type OrmSessionOptions as aw, type OrmOptions as ax, type DbExecutorFactory as ay, type ExternalTransaction as az, type LogicalExpressionNode as b, type BetweenExpressionNode as c, type CaseExpressionNode as d, type ExistsExpressionNode as e, type OrderDirection as f, type ScalarSubqueryNode as g, type ColumnDef as h, type TableDef as i, type InsertQueryNode as j, type InsertCompiler as k, type CompiledQuery as l, type DialectKey as m, type UpdateCompiler as n, type DeleteQueryNode as o, type DeleteCompiler as p, type CompilerContext as q, type FunctionTableNode as r, type DerivedTableNode as s, type TableSourceNode as t, type ForeignKeyReference as u, type IndexColumn as v, type IndexDef as w, type DbExecutor as x, type NamingStrategy as y, type EntityContext as z };
|