@simplysm/orm-common 14.0.4 → 14.0.5

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/docs/types.md CHANGED
@@ -2,35 +2,31 @@
2
2
 
3
3
  Core type definitions for the ORM.
4
4
 
5
- ## Dialect
5
+ ## Database Types
6
6
 
7
- Supported database dialects.
7
+ ### Dialect
8
8
 
9
9
  ```typescript
10
10
  type Dialect = "mysql" | "mssql" | "postgresql";
11
11
  ```
12
12
 
13
- **Constant:**
13
+ Supported database dialects. MySQL 8.0.14+, MSSQL 2012+, PostgreSQL 9.0+.
14
+
15
+ ### dialects
14
16
 
15
17
  ```typescript
16
18
  const dialects: Dialect[] = ["mysql", "mssql", "postgresql"];
17
19
  ```
18
20
 
19
- ## IsolationLevel
21
+ Array of all supported dialects. Useful for parameterized tests.
20
22
 
21
- Transaction isolation levels.
23
+ ### IsolationLevel
22
24
 
23
25
  ```typescript
24
- type IsolationLevel =
25
- | "READ_UNCOMMITTED"
26
- | "READ_COMMITTED"
27
- | "REPEATABLE_READ"
28
- | "SERIALIZABLE";
26
+ type IsolationLevel = "READ_UNCOMMITTED" | "READ_COMMITTED" | "REPEATABLE_READ" | "SERIALIZABLE";
29
27
  ```
30
28
 
31
- ## DataRecord
32
-
33
- Recursive data record type for query results. Supports nested relations.
29
+ ### DataRecord
34
30
 
35
31
  ```typescript
36
32
  type DataRecord = {
@@ -38,9 +34,9 @@ type DataRecord = {
38
34
  };
39
35
  ```
40
36
 
41
- ## DbContextExecutor
37
+ Recursive query result type. Supports nested relations via include/join.
42
38
 
43
- Interface for DB connection and query execution. Implemented by `NodeDbContextExecutor` (server) or service-based executors (client).
39
+ ### DbContextExecutor
44
40
 
45
41
  ```typescript
46
42
  interface DbContextExecutor {
@@ -56,9 +52,9 @@ interface DbContextExecutor {
56
52
  }
57
53
  ```
58
54
 
59
- ## ResultMeta
55
+ Query execution interface. Implemented by `NodeDbContextExecutor` (server) or service client executors.
60
56
 
61
- Metadata for transforming raw query results into typed TypeScript objects.
57
+ ### ResultMeta
62
58
 
63
59
  ```typescript
64
60
  interface ResultMeta {
@@ -67,14 +63,12 @@ interface ResultMeta {
67
63
  }
68
64
  ```
69
65
 
70
- | Field | Description |
71
- |---|---|
72
- | `columns` | Maps column name (dot-notation for nested) to TypeScript type name |
73
- | `joins` | Maps JOIN alias to single/array indicator |
74
-
75
- ## Migration
66
+ | Field | Type | Description |
67
+ |-------|------|-------------|
68
+ | `columns` | `Record<string, ColumnPrimitiveStr>` | Column name to type name mapping |
69
+ | `joins` | `Record<string, { isSingle: boolean }>` | JOIN alias to single/array flag |
76
70
 
77
- Database migration definition for schema versioning.
71
+ ### Migration
78
72
 
79
73
  ```typescript
80
74
  interface Migration {
@@ -84,13 +78,11 @@ interface Migration {
84
78
  ```
85
79
 
86
80
  | Field | Type | Description |
87
- |---|---|---|
81
+ |-------|------|-------------|
88
82
  | `name` | `string` | Unique migration name (timestamp recommended) |
89
- | `up` | `(db) => Promise<void>` | Migration function with DDL access |
90
-
91
- ## QueryBuildResult
83
+ | `up` | `(db) => Promise<void>` | Migration execution function |
92
84
 
93
- Return type of `QueryBuilderBase.build()`.
85
+ ### QueryBuildResult
94
86
 
95
87
  ```typescript
96
88
  interface QueryBuildResult {
@@ -101,29 +93,84 @@ interface QueryBuildResult {
101
93
  ```
102
94
 
103
95
  | Field | Type | Description |
104
- |---|---|---|
96
+ |-------|------|-------------|
105
97
  | `sql` | `string` | Generated SQL string |
106
- | `resultSetIndex` | `number` | Result set index to read (default: 0) |
107
- | `resultSetStride` | `number` | Read every Nth result set (for multi-INSERT) |
98
+ | `resultSetIndex` | `number?` | Index of result set to use |
99
+ | `resultSetStride` | `number?` | Extract every Nth result set |
108
100
 
109
- ## ColumnMeta
101
+ ## DbContext Types
110
102
 
111
- Column metadata generated by `ColumnBuilder`.
103
+ ### DbContextDef
112
104
 
113
105
  ```typescript
114
- interface ColumnMeta {
115
- type: ColumnPrimitiveStr;
116
- dataType: DataType;
117
- autoIncrement?: boolean;
118
- nullable?: boolean;
119
- default?: ColumnPrimitive;
120
- description?: string;
106
+ interface DbContextDef<TTables, TViews, TProcedures> {
107
+ readonly meta: {
108
+ readonly tables: TTables;
109
+ readonly views: TViews;
110
+ readonly procedures: TProcedures;
111
+ readonly migrations: Migration[];
112
+ };
113
+ }
114
+ ```
115
+
116
+ DbContext blueprint created by `defineDbContext()`. Contains only schema metadata, no runtime state.
117
+
118
+ ### DbContextBase
119
+
120
+ ```typescript
121
+ interface DbContextBase {
122
+ status: DbContextStatus;
123
+ readonly database: string | undefined;
124
+ readonly schema: string | undefined;
125
+ getNextAlias(): string;
126
+ resetAliasCounter(): void;
127
+ executeDefs<T = DataRecord>(defs: QueryDef[], resultMetas?: (ResultMeta | undefined)[]): Promise<T[][]>;
128
+ getQueryDefObjectName(tableOrView: TableBuilder | ViewBuilder): QueryDefObjectName;
129
+ switchFk(table: QueryDefObjectName, enabled: boolean): Promise<void>;
121
130
  }
122
131
  ```
123
132
 
124
- ## DataType
133
+ Core interface used internally by Queryable, Executable, and ViewBuilder.
134
+
135
+ ### DbContextStatus
136
+
137
+ ```typescript
138
+ type DbContextStatus = "ready" | "connect" | "transact";
139
+ ```
140
+
141
+ ### DbContextInstance
142
+
143
+ ```typescript
144
+ type DbContextInstance<TDef extends DbContextDef<any, any, any>> =
145
+ DbContextBase &
146
+ DbContextConnectionMethods &
147
+ DbContextDdlMethods &
148
+ { [K in keyof TDef["meta"]["tables"]]: () => Queryable<...> } &
149
+ { [K in keyof TDef["meta"]["views"]]: () => Queryable<...> } &
150
+ { [K in keyof TDef["meta"]["procedures"]]: () => Executable<...> } &
151
+ { _migration: () => Queryable<{ code: string }, any> } &
152
+ { initialize(options?: { dbs?: string[]; force?: boolean }): Promise<void> };
153
+ ```
154
+
155
+ Full DbContext instance type returned by `createDbContext()`. Includes queryable accessors for all tables/views, executable accessors for all procedures, connection methods, DDL methods, and `initialize()`.
156
+
157
+ ### DbContextConnectionMethods
158
+
159
+ ```typescript
160
+ interface DbContextConnectionMethods {
161
+ connect<TResult>(fn: () => Promise<TResult>, isolationLevel?: IsolationLevel): Promise<TResult>;
162
+ connectWithoutTransaction<TResult>(callback: () => Promise<TResult>): Promise<TResult>;
163
+ transaction<TResult>(fn: () => Promise<TResult>, isolationLevel?: IsolationLevel): Promise<TResult>;
164
+ }
165
+ ```
166
+
167
+ ### DbContextDdlMethods
168
+
169
+ See [Core - DDL Execution Methods](./core.md#ddl-execution-methods) and [Core - DDL QueryDef Generators](./core.md#ddl-querydef-generators) for the full list of 18 DDL execution methods and 22 QueryDef generator methods.
170
+
171
+ ## Column Types
125
172
 
126
- SQL data type definition. Discriminated union on `type` field.
173
+ ### DataType
127
174
 
128
175
  ```typescript
129
176
  type DataType =
@@ -143,30 +190,23 @@ type DataType =
143
190
  | { type: "uuid" };
144
191
  ```
145
192
 
146
- **DBMS Mapping:**
193
+ SQL data type definition. Used in ColumnBuilder metadata, CAST expressions, and DDL generation.
147
194
 
148
- | DataType | MySQL | MSSQL | PostgreSQL |
149
- |---|---|---|---|
150
- | `int` | INT | INT | INT |
151
- | `bigint` | BIGINT | BIGINT | BIGINT |
152
- | `float` | FLOAT | REAL | REAL |
153
- | `double` | DOUBLE | FLOAT | DOUBLE PRECISION |
154
- | `decimal` | DECIMAL(p,s) | DECIMAL(p,s) | DECIMAL(p,s) |
155
- | `varchar` | VARCHAR(n) | NVARCHAR(n) | VARCHAR(n) |
156
- | `char` | CHAR(n) | NCHAR(n) | CHAR(n) |
157
- | `text` | LONGTEXT | NVARCHAR(MAX) | TEXT |
158
- | `binary` | LONGBLOB | VARBINARY(MAX) | BYTEA |
159
- | `boolean` | TINYINT(1) | BIT | BOOLEAN |
160
- | `datetime` | DATETIME | DATETIME2 | TIMESTAMP |
161
- | `date` | DATE | DATE | DATE |
162
- | `time` | TIME | TIME | TIME |
163
- | `uuid` | BINARY(16) | UNIQUEIDENTIFIER | UUID |
195
+ ### ColumnPrimitive
164
196
 
165
- ## Column Primitive Types
197
+ ```typescript
198
+ type ColumnPrimitive = string | number | boolean | DateTime | DateOnly | Time | Uuid | Bytes | undefined;
199
+ ```
166
200
 
167
- ### ColumnPrimitiveMap
201
+ All storable column primitive types. `undefined` represents SQL NULL.
168
202
 
169
- Maps TypeScript type names to actual types.
203
+ ### ColumnPrimitiveStr
204
+
205
+ ```typescript
206
+ type ColumnPrimitiveStr = "string" | "number" | "boolean" | "DateTime" | "DateOnly" | "Time" | "Uuid" | "Bytes";
207
+ ```
208
+
209
+ ### ColumnPrimitiveMap
170
210
 
171
211
  ```typescript
172
212
  type ColumnPrimitiveMap = {
@@ -181,224 +221,143 @@ type ColumnPrimitiveMap = {
181
221
  };
182
222
  ```
183
223
 
184
- ### ColumnPrimitiveStr
224
+ ### ColumnMeta
185
225
 
186
226
  ```typescript
187
- type ColumnPrimitiveStr = keyof ColumnPrimitiveMap;
188
- // "string" | "number" | "boolean" | "DateTime" | "DateOnly" | "Time" | "Uuid" | "Bytes"
227
+ interface ColumnMeta {
228
+ type: ColumnPrimitiveStr;
229
+ dataType: DataType;
230
+ autoIncrement?: boolean;
231
+ nullable?: boolean;
232
+ default?: ColumnPrimitive;
233
+ description?: string;
234
+ }
189
235
  ```
190
236
 
191
- ### ColumnPrimitive
192
-
193
- All primitive types that can be stored in columns. `undefined` represents NULL.
237
+ ### dataTypeStrToColumnPrimitiveStr
194
238
 
195
239
  ```typescript
196
- type ColumnPrimitive = ColumnPrimitiveMap[ColumnPrimitiveStr] | undefined;
240
+ const dataTypeStrToColumnPrimitiveStr: Record<DataType["type"], ColumnPrimitiveStr>
197
241
  ```
198
242
 
199
- ### dataTypeStrToColumnPrimitiveStr
243
+ Maps SQL type names to TypeScript type names. For example: `"int"` -> `"number"`, `"datetime"` -> `"DateTime"`, `"varchar"` -> `"string"`.
200
244
 
201
- Constant mapping SQL DataType string to ColumnPrimitiveStr.
245
+ ### InferColumnPrimitiveFromDataType
202
246
 
203
247
  ```typescript
204
- const dataTypeStrToColumnPrimitiveStr: Record<DataType["type"], ColumnPrimitiveStr>;
205
- // { int: "number", bigint: "number", varchar: "string", datetime: "DateTime", ... }
248
+ type InferColumnPrimitiveFromDataType<TDataType extends DataType> = ColumnPrimitiveMap[...]
206
249
  ```
207
250
 
208
- ### InferColumnPrimitiveFromDataType
251
+ Infers the TypeScript type from a DataType definition.
209
252
 
210
- Infer TypeScript type from a `DataType`.
253
+ ### inferColumnPrimitiveStr
211
254
 
212
255
  ```typescript
213
- type InferColumnPrimitiveFromDataType<T extends DataType> = ColumnPrimitiveMap[...];
256
+ function inferColumnPrimitiveStr(value: ColumnPrimitive): ColumnPrimitiveStr
214
257
  ```
215
258
 
216
- ### inferColumnPrimitiveStr
259
+ Infers the ColumnPrimitiveStr from a runtime value. Throws for unknown types.
260
+
261
+ ## Column Builder Types
217
262
 
218
- Infer `ColumnPrimitiveStr` from a runtime value.
263
+ ### ColumnBuilderRecord
219
264
 
220
265
  ```typescript
221
- function inferColumnPrimitiveStr(value: ColumnPrimitive): ColumnPrimitiveStr;
266
+ type ColumnBuilderRecord = Record<string, ColumnBuilder<ColumnPrimitive, ColumnMeta>>;
222
267
  ```
223
268
 
224
- ## QueryDef Types
269
+ ### InferColumns
270
+
271
+ ```typescript
272
+ type InferColumns<TBuilders extends ColumnBuilderRecord> = {
273
+ [K in keyof TBuilders]: TBuilders[K] extends ColumnBuilder<infer V, any> ? V : never;
274
+ };
275
+ ```
225
276
 
226
- All query definition types used by the QueryBuilder.
277
+ Extracts runtime value types from a column builder record.
227
278
 
228
- ### QueryDefObjectName
279
+ ### InferColumnExprs
229
280
 
230
281
  ```typescript
231
- interface QueryDefObjectName {
232
- database?: string;
233
- schema?: string;
234
- name: string;
235
- }
282
+ type InferColumnExprs<TBuilders extends ColumnBuilderRecord> = {
283
+ [K in keyof TBuilders]: TBuilders[K] extends ColumnBuilder<infer V, any> ? ExprInput<V> : never;
284
+ };
236
285
  ```
237
286
 
238
- ### CudOutputDef
287
+ ### InferInsertColumns
239
288
 
240
289
  ```typescript
241
- interface CudOutputDef {
242
- columns: string[];
243
- pkColNames: string[];
244
- aiColName?: string;
245
- }
290
+ type InferInsertColumns<TBuilders> = Pick<InferColumns<TBuilders>, RequiredInsertKeys<TBuilders>> &
291
+ Partial<Pick<InferColumns<TBuilders>, OptionalInsertKeys<TBuilders>>>;
246
292
  ```
247
293
 
248
- ### SelectQueryDef
294
+ INSERT type: required columns are required, autoIncrement/nullable/default columns are optional.
295
+
296
+ ### InferUpdateColumns
249
297
 
250
298
  ```typescript
251
- interface SelectQueryDef {
252
- type: "select";
253
- from?: QueryDefObjectName | SelectQueryDef | SelectQueryDef[] | string;
254
- as: string;
255
- select?: Record<string, Expr>;
256
- distinct?: boolean;
257
- top?: number;
258
- lock?: boolean;
259
- where?: WhereExpr[];
260
- joins?: SelectQueryDefJoin[];
261
- orderBy?: [Expr, ("ASC" | "DESC")?][];
262
- limit?: [number, number];
263
- groupBy?: Expr[];
264
- having?: WhereExpr[];
265
- with?: { name: string; base: SelectQueryDef; recursive: SelectQueryDef };
266
- }
299
+ type InferUpdateColumns<TBuilders> = Partial<InferColumns<TBuilders>>;
267
300
  ```
268
301
 
269
- ### SelectQueryDefJoin
302
+ UPDATE type: all columns are optional.
303
+
304
+ ### RequiredInsertKeys
270
305
 
271
306
  ```typescript
272
- interface SelectQueryDefJoin extends SelectQueryDef {
273
- isSingle?: boolean;
274
- }
307
+ type RequiredInsertKeys<TBuilders> = /* columns without autoIncrement, nullable, or default */
275
308
  ```
276
309
 
277
- ### InsertQueryDef
310
+ ### OptionalInsertKeys
278
311
 
279
312
  ```typescript
280
- interface InsertQueryDef {
281
- type: "insert";
282
- table: QueryDefObjectName;
283
- records: Record<string, ColumnPrimitive>[];
284
- overrideIdentity?: boolean;
285
- output?: CudOutputDef;
286
- }
313
+ type OptionalInsertKeys<TBuilders> = Exclude<keyof TBuilders, RequiredInsertKeys<TBuilders>>;
287
314
  ```
288
315
 
289
- ### InsertIfNotExistsQueryDef
316
+ ### DataToColumnBuilderRecord
290
317
 
291
318
  ```typescript
292
- interface InsertIfNotExistsQueryDef {
293
- type: "insertIfNotExists";
294
- table: QueryDefObjectName;
295
- record: Record<string, ColumnPrimitive>;
296
- existsSelectQuery: Omit<SelectQueryDef, "select">;
297
- output?: CudOutputDef;
298
- }
319
+ type DataToColumnBuilderRecord<TData extends DataRecord> = {
320
+ [K in keyof TData as TData[K] extends ColumnPrimitive ? K : never]: ColumnBuilder<TData[K], any>;
321
+ };
299
322
  ```
300
323
 
301
- ### InsertIntoQueryDef
324
+ ## Relation Types
325
+
326
+ ### RelationBuilderRecord
302
327
 
303
328
  ```typescript
304
- interface InsertIntoQueryDef {
305
- type: "insertInto";
306
- table: QueryDefObjectName;
307
- recordsSelectQuery: SelectQueryDef;
308
- output?: CudOutputDef;
309
- }
329
+ type RelationBuilderRecord = Record<string,
330
+ ForeignKeyBuilder<any, any> | ForeignKeyTargetBuilder<any, any> |
331
+ RelationKeyBuilder<any, any> | RelationKeyTargetBuilder<any, any>>;
310
332
  ```
311
333
 
312
- ### UpdateQueryDef
334
+ ### InferDeepRelations
313
335
 
314
336
  ```typescript
315
- interface UpdateQueryDef {
316
- type: "update";
317
- table: QueryDefObjectName;
318
- as: string;
319
- record: Record<string, Expr>;
320
- top?: number;
321
- where?: WhereExpr[];
322
- joins?: SelectQueryDefJoin[];
323
- limit?: [number, number];
324
- output?: CudOutputDef;
325
- }
337
+ type InferDeepRelations<TRelations extends RelationBuilderRecord> = {
338
+ [K in keyof TRelations]?: ExtractRelationTarget<TRelations[K]> | ExtractRelationTargetResult<TRelations[K]>;
339
+ };
326
340
  ```
327
341
 
328
- ### DeleteQueryDef
342
+ All relations are optional (loaded only via `include()`).
343
+
344
+ ### ExtractRelationTarget
329
345
 
330
346
  ```typescript
331
- interface DeleteQueryDef {
332
- type: "delete";
333
- table: QueryDefObjectName;
334
- as: string;
335
- top?: number;
336
- where?: WhereExpr[];
337
- joins?: SelectQueryDefJoin[];
338
- limit?: [number, number];
339
- output?: CudOutputDef;
340
- }
347
+ type ExtractRelationTarget<TRelation> = /* Extracts N:1 target type (single object) */
341
348
  ```
342
349
 
343
- ### UpsertQueryDef
350
+ For FK/RelationKey relations, extracts `InferColumns<TCols> & InferDeepRelations<TRels>` of the target table.
351
+
352
+ ### ExtractRelationTargetResult
344
353
 
345
354
  ```typescript
346
- interface UpsertQueryDef {
347
- type: "upsert";
348
- table: QueryDefObjectName;
349
- existsSelectQuery: Omit<SelectQueryDef, "select">;
350
- updateRecord: Record<string, Expr>;
351
- insertRecord: Record<string, Expr>;
352
- output?: CudOutputDef;
353
- }
355
+ type ExtractRelationTargetResult<TRelation> = /* Extracts 1:N target type (array or single) */
354
356
  ```
355
357
 
356
- ### DDL QueryDef Types
357
-
358
- | Type | Description |
359
- |---|---|
360
- | `CreateTableQueryDef` | CREATE TABLE |
361
- | `DropTableQueryDef` | DROP TABLE |
362
- | `RenameTableQueryDef` | RENAME TABLE |
363
- | `TruncateQueryDef` | TRUNCATE TABLE |
364
- | `AddColumnQueryDef` | ADD COLUMN |
365
- | `DropColumnQueryDef` | DROP COLUMN |
366
- | `ModifyColumnQueryDef` | ALTER COLUMN |
367
- | `RenameColumnQueryDef` | RENAME COLUMN |
368
- | `AddPrimaryKeyQueryDef` | ADD PRIMARY KEY |
369
- | `DropPrimaryKeyQueryDef` | DROP PRIMARY KEY |
370
- | `AddForeignKeyQueryDef` | ADD FOREIGN KEY |
371
- | `DropForeignKeyQueryDef` | DROP FOREIGN KEY |
372
- | `AddIndexQueryDef` | CREATE INDEX |
373
- | `DropIndexQueryDef` | DROP INDEX |
374
- | `CreateViewQueryDef` | CREATE VIEW |
375
- | `DropViewQueryDef` | DROP VIEW |
376
- | `CreateProcQueryDef` | CREATE PROCEDURE |
377
- | `DropProcQueryDef` | DROP PROCEDURE |
378
- | `ExecProcQueryDef` | EXECUTE PROCEDURE |
379
- | `ClearSchemaQueryDef` | Clear all objects in schema |
380
- | `SchemaExistsQueryDef` | Check schema existence |
381
- | `SwitchFkQueryDef` | Enable/disable FK constraints |
382
-
383
- ### QueryDef (union)
384
-
385
- ```typescript
386
- type QueryDef = SelectQueryDef | InsertQueryDef | InsertIfNotExistsQueryDef
387
- | InsertIntoQueryDef | UpdateQueryDef | DeleteQueryDef | UpsertQueryDef
388
- | CreateTableQueryDef | DropTableQueryDef | RenameTableQueryDef
389
- | TruncateQueryDef | AddColumnQueryDef | DropColumnQueryDef
390
- | ModifyColumnQueryDef | RenameColumnQueryDef
391
- | AddPrimaryKeyQueryDef | DropPrimaryKeyQueryDef
392
- | AddForeignKeyQueryDef | DropForeignKeyQueryDef
393
- | AddIndexQueryDef | DropIndexQueryDef
394
- | CreateViewQueryDef | DropViewQueryDef
395
- | CreateProcQueryDef | DropProcQueryDef | ExecProcQueryDef
396
- | ClearSchemaQueryDef | SchemaExistsQueryDef | SwitchFkQueryDef;
397
- ```
398
-
399
- ## Expr Types
400
-
401
- All expression AST node types. See [expression.md](expression.md) for the `expr` builder API.
358
+ For FKTarget/RelationKeyTarget relations, extracts an array type (or single object if `isSingle: true`).
359
+
360
+ ## Expression Types
402
361
 
403
362
  ### DateUnit
404
363
 
@@ -408,18 +367,17 @@ type DateUnit = "year" | "month" | "day" | "hour" | "minute" | "second";
408
367
 
409
368
  ### WhereExpr
410
369
 
411
- Union of all WHERE-clause expression types.
412
-
413
370
  ```typescript
414
371
  type WhereExpr =
415
372
  | ExprEq | ExprGt | ExprLt | ExprGte | ExprLte | ExprBetween
416
- | ExprIsNull | ExprLike | ExprRegexp | ExprIn | ExprInQuery | ExprExists
373
+ | ExprIsNull | ExprLike | ExprRegexp
374
+ | ExprIn | ExprInQuery | ExprExists
417
375
  | ExprNot | ExprAnd | ExprOr;
418
376
  ```
419
377
 
420
- ### Expr
378
+ Union of all WHERE-clause expression types (comparison + logical).
421
379
 
422
- Union of all expression types (values, functions, aggregates, window, etc.).
380
+ ### Expr
423
381
 
424
382
  ```typescript
425
383
  type Expr =
@@ -436,6 +394,17 @@ type Expr =
436
394
  | ExprWindow | ExprSubquery;
437
395
  ```
438
396
 
397
+ Union of all expression types (value, string, number, date, conditional, aggregate, window, system).
398
+
399
+ ### WinFn
400
+
401
+ ```typescript
402
+ type WinFn =
403
+ | WinFnRowNumber | WinFnRank | WinFnDenseRank | WinFnNtile
404
+ | WinFnLag | WinFnLead | WinFnFirstValue | WinFnLastValue
405
+ | WinFnSum | WinFnAvg | WinFnCount | WinFnMin | WinFnMax;
406
+ ```
407
+
439
408
  ### WinSpec
440
409
 
441
410
  ```typescript
@@ -445,57 +414,107 @@ interface WinSpec {
445
414
  }
446
415
  ```
447
416
 
448
- ### WinFn
417
+ ## QueryDef Types
449
418
 
450
- Union of all window function types.
419
+ ### QueryDefObjectName
451
420
 
452
421
  ```typescript
453
- type WinFn =
454
- | WinFnRowNumber | WinFnRank | WinFnDenseRank | WinFnNtile
455
- | WinFnLag | WinFnLead | WinFnFirstValue | WinFnLastValue
456
- | WinFnSum | WinFnAvg | WinFnCount | WinFnMin | WinFnMax;
422
+ interface QueryDefObjectName {
423
+ database?: string;
424
+ schema?: string;
425
+ name: string;
426
+ }
457
427
  ```
458
428
 
459
- ## parseQueryResult
429
+ DB object name. MySQL: `database.name`, MSSQL: `database.schema.name`, PostgreSQL: `schema.name`.
460
430
 
461
- Transforms raw DB query results into typed TypeScript objects using `ResultMeta`. Handles type parsing, flat-to-nested conversion, and JOIN grouping.
431
+ ### QueryDef
462
432
 
463
433
  ```typescript
464
- async function parseQueryResult<TRecord>(
465
- rawResults: Record<string, unknown>[],
466
- meta: ResultMeta,
467
- ): Promise<TRecord[] | undefined>;
434
+ type QueryDef =
435
+ // DML
436
+ | SelectQueryDef | InsertQueryDef | InsertIfNotExistsQueryDef | InsertIntoQueryDef
437
+ | UpdateQueryDef | DeleteQueryDef | UpsertQueryDef
438
+ // DDL
439
+ | ClearSchemaQueryDef | CreateTableQueryDef | DropTableQueryDef | RenameTableQueryDef
440
+ | TruncateQueryDef | AddColumnQueryDef | DropColumnQueryDef | ModifyColumnQueryDef
441
+ | RenameColumnQueryDef | DropPrimaryKeyQueryDef | AddPrimaryKeyQueryDef
442
+ | AddForeignKeyQueryDef | DropForeignKeyQueryDef | AddIndexQueryDef | DropIndexQueryDef
443
+ | CreateViewQueryDef | DropViewQueryDef | CreateProcQueryDef | DropProcQueryDef
444
+ // Utils/Meta
445
+ | ExecProcQueryDef | SwitchFkQueryDef | SchemaExistsQueryDef;
468
446
  ```
469
447
 
470
- **Parameters:**
448
+ ### DDL_TYPES
471
449
 
472
- | Parameter | Type | Description |
473
- |---|---|---|
474
- | `rawResults` | `Record<string, unknown>[]` | Raw result rows from database |
475
- | `meta` | `ResultMeta` | Type and JOIN structure metadata |
450
+ ```typescript
451
+ const DDL_TYPES: readonly DdlType[]
452
+ ```
476
453
 
477
- **Returns:** Typed and nested result array, or `undefined` if empty.
454
+ Array of all DDL query type strings. Used for blocking DDL inside transactions.
478
455
 
479
- **Example:**
456
+ ### DdlType
480
457
 
481
458
  ```typescript
482
- const raw = [
483
- { id: "1", name: "User1", "posts.id": "10", "posts.title": "Post1" },
484
- { id: "1", name: "User1", "posts.id": "11", "posts.title": "Post2" },
485
- ];
486
- const meta = {
487
- columns: { id: "number", name: "string", "posts.id": "number", "posts.title": "string" },
488
- joins: { posts: { isSingle: false } },
489
- };
490
- const result = await parseQueryResult(raw, meta);
491
- // [{ id: 1, name: "User1", posts: [{ id: 10, title: "Post1" }, { id: 11, title: "Post2" }] }]
459
+ type DdlType = "clearSchema" | "createTable" | "dropTable" | "renameTable" | "truncate"
460
+ | "addColumn" | "dropColumn" | "modifyColumn" | "renameColumn"
461
+ | "dropPrimaryKey" | "addPrimaryKey" | "addForeignKey" | "dropForeignKey"
462
+ | "addIndex" | "dropIndex" | "createView" | "dropView" | "createProc" | "dropProc";
492
463
  ```
493
464
 
494
- ## _Migration
465
+ ### DML QueryDef Interfaces
466
+
467
+ | Interface | type field | Key fields |
468
+ |-----------|-----------|-------------|
469
+ | `SelectQueryDef` | `"select"` | `from`, `as`, `select`, `distinct`, `top`, `lock`, `where`, `joins`, `orderBy`, `limit`, `groupBy`, `having`, `with` |
470
+ | `SelectQueryDefJoin` | `"select"` | Extends SelectQueryDef + `isSingle` |
471
+ | `InsertQueryDef` | `"insert"` | `table`, `records`, `overrideIdentity`, `output` |
472
+ | `InsertIfNotExistsQueryDef` | `"insertIfNotExists"` | `table`, `record`, `existsSelectQuery`, `output` |
473
+ | `InsertIntoQueryDef` | `"insertInto"` | `table`, `recordsSelectQuery`, `output` |
474
+ | `UpdateQueryDef` | `"update"` | `table`, `as`, `record`, `top`, `where`, `joins`, `limit`, `output` |
475
+ | `DeleteQueryDef` | `"delete"` | `table`, `as`, `top`, `where`, `joins`, `limit`, `output` |
476
+ | `UpsertQueryDef` | `"upsert"` | `table`, `existsSelectQuery`, `insertRecord`, `updateRecord`, `output` |
477
+ | `ExecProcQueryDef` | `"execProc"` | `procedure`, `params` |
478
+
479
+ ### DDL QueryDef Interfaces
480
+
481
+ | Interface | type field | Key fields |
482
+ |-----------|-----------|-------------|
483
+ | `ClearSchemaQueryDef` | `"clearSchema"` | `database`, `schema` |
484
+ | `CreateTableQueryDef` | `"createTable"` | `table`, `columns`, `primaryKey` |
485
+ | `DropTableQueryDef` | `"dropTable"` | `table` |
486
+ | `RenameTableQueryDef` | `"renameTable"` | `table`, `newName` |
487
+ | `TruncateQueryDef` | `"truncate"` | `table` |
488
+ | `AddColumnQueryDef` | `"addColumn"` | `table`, `column` |
489
+ | `DropColumnQueryDef` | `"dropColumn"` | `table`, `column` |
490
+ | `ModifyColumnQueryDef` | `"modifyColumn"` | `table`, `column` |
491
+ | `RenameColumnQueryDef` | `"renameColumn"` | `table`, `column`, `newName` |
492
+ | `AddPrimaryKeyQueryDef` | `"addPrimaryKey"` | `table`, `columns` |
493
+ | `DropPrimaryKeyQueryDef` | `"dropPrimaryKey"` | `table` |
494
+ | `AddForeignKeyQueryDef` | `"addForeignKey"` | `table`, `foreignKey` |
495
+ | `DropForeignKeyQueryDef` | `"dropForeignKey"` | `table`, `foreignKey` |
496
+ | `AddIndexQueryDef` | `"addIndex"` | `table`, `index` |
497
+ | `DropIndexQueryDef` | `"dropIndex"` | `table`, `index` |
498
+ | `CreateViewQueryDef` | `"createView"` | `view`, `queryDef` |
499
+ | `DropViewQueryDef` | `"dropView"` | `view` |
500
+ | `CreateProcQueryDef` | `"createProc"` | `procedure`, `params`, `returns`, `query` |
501
+ | `DropProcQueryDef` | `"dropProc"` | `procedure` |
502
+
503
+ ### Utility QueryDef Interfaces
495
504
 
496
- System migration table definition. Automatically added to every `DbContextDef` by `defineDbContext()`.
505
+ | Interface | type field | Key fields |
506
+ |-----------|-----------|-------------|
507
+ | `SwitchFkQueryDef` | `"switchFk"` | `table`, `enabled` |
508
+ | `SchemaExistsQueryDef` | `"schemaExists"` | `database`, `schema` |
509
+
510
+ ### CudOutputDef
497
511
 
498
512
  ```typescript
499
- const _Migration: TableBuilder<{ code: ColumnBuilder<string, ...> }, {}>;
500
- // Table: "_migration", columns: { code: varchar(255) }, primaryKey: "code"
513
+ interface CudOutputDef {
514
+ columns: string[];
515
+ pkColNames: string[];
516
+ aiColName?: string;
517
+ }
501
518
  ```
519
+
520
+ OUTPUT clause definition for INSERT/UPDATE/DELETE.