@simplysm/orm-common 13.0.97 → 13.0.99

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.
@@ -0,0 +1,463 @@
1
+ # Schema Builders
2
+
3
+ ## `Table`
4
+
5
+ Table builder factory function. Creates a `TableBuilder` for defining table schema via fluent API.
6
+
7
+ ```typescript
8
+ function Table(name: string): TableBuilder<{}, {}>;
9
+ ```
10
+
11
+ ## `TableBuilder`
12
+
13
+ Database table definition builder. Define columns, PK, indexes, and relations via fluent API.
14
+
15
+ ```typescript
16
+ class TableBuilder<
17
+ TColumns extends ColumnBuilderRecord,
18
+ TRelations extends RelationBuilderRecord,
19
+ > {
20
+ readonly $columns!: TColumns;
21
+ readonly $relations!: TRelations;
22
+ readonly $inferSelect!: InferColumns<TColumns> & InferDeepRelations<TRelations>;
23
+ readonly $inferColumns!: InferColumns<TColumns>;
24
+ readonly $inferInsert!: InferInsertColumns<TColumns>;
25
+ readonly $inferUpdate!: InferUpdateColumns<TColumns>;
26
+
27
+ constructor(readonly meta: {
28
+ name: string;
29
+ description?: string;
30
+ database?: string;
31
+ schema?: string;
32
+ columns?: TColumns;
33
+ primaryKey?: (keyof TColumns & string)[];
34
+ relations?: TRelations;
35
+ indexes?: IndexBuilder<(keyof TColumns & string)[]>[];
36
+ });
37
+
38
+ description(desc: string): TableBuilder<TColumns, TRelations>;
39
+ database(db: string): TableBuilder<TColumns, TRelations>;
40
+ schema(schema: string): TableBuilder<TColumns, TRelations>;
41
+ columns<TNewColumnDefs extends ColumnBuilderRecord>(
42
+ fn: (c: ReturnType<typeof createColumnFactory>) => TNewColumnDefs,
43
+ ): TableBuilder<TNewColumnDefs, TRelations>;
44
+ primaryKey(...columns: (keyof TColumns & string)[]): TableBuilder<TColumns, TRelations>;
45
+ indexes(fn: (i: ...) => IndexBuilder<string[]>[]): TableBuilder<TColumns, TRelations>;
46
+ relations<T extends RelationBuilderRecord>(fn: (r: ...) => T): TableBuilder<TColumns, T>;
47
+ }
48
+ ```
49
+
50
+ | Method | Description |
51
+ |--------|-------------|
52
+ | `description()` | Set table description (DDL comment) |
53
+ | `database()` | Set database name |
54
+ | `schema()` | Set schema name (MSSQL/PostgreSQL) |
55
+ | `columns()` | Define columns using the column factory |
56
+ | `primaryKey()` | Set primary key columns (composite PK supported) |
57
+ | `indexes()` | Define indexes using the index factory |
58
+ | `relations()` | Define relations using the relation factory |
59
+
60
+ ## `View`
61
+
62
+ View builder factory function. Creates a `ViewBuilder` for defining view schema via fluent API.
63
+
64
+ ```typescript
65
+ function View(name: string): ViewBuilder<any, any, {}>;
66
+ ```
67
+
68
+ ## `ViewBuilder`
69
+
70
+ Database view definition builder. Define view query and relations via fluent API.
71
+
72
+ ```typescript
73
+ class ViewBuilder<
74
+ TDbContext extends DbContextBase,
75
+ TData extends DataRecord,
76
+ TRelations extends RelationBuilderRecord,
77
+ > {
78
+ readonly $relations!: TRelations;
79
+ readonly $inferSelect!: TData;
80
+
81
+ constructor(readonly meta: {
82
+ name: string;
83
+ description?: string;
84
+ database?: string;
85
+ schema?: string;
86
+ viewFn?: (db: TDbContext) => Queryable<TData, any>;
87
+ relations?: TRelations;
88
+ });
89
+
90
+ description(desc: string): ViewBuilder<TDbContext, TData, TRelations>;
91
+ database(db: string): ViewBuilder<TDbContext, TData, TRelations>;
92
+ schema(schema: string): ViewBuilder<TDbContext, TData, TRelations>;
93
+ query<TViewData extends DataRecord, TDb extends DbContextBase>(
94
+ viewFn: (db: TDb) => Queryable<TViewData, any>,
95
+ ): ViewBuilder<TDb, TViewData, TRelations>;
96
+ relations<T extends RelationBuilderRecord>(fn: (r: ...) => T): ViewBuilder<TDbContext, TData & InferDeepRelations<T>, TRelations>;
97
+ }
98
+ ```
99
+
100
+ | Method | Description |
101
+ |--------|-------------|
102
+ | `description()` | Set view description (DDL comment) |
103
+ | `database()` | Set database name |
104
+ | `schema()` | Set schema name |
105
+ | `query()` | Define the view's SELECT query |
106
+ | `relations()` | Define relations with other tables/views |
107
+
108
+ ## `Procedure`
109
+
110
+ Procedure builder factory function. Creates a `ProcedureBuilder` for defining stored procedure schema via fluent API.
111
+
112
+ ```typescript
113
+ function Procedure(name: string): ProcedureBuilder<never, never>;
114
+ ```
115
+
116
+ ## `ProcedureBuilder`
117
+
118
+ Stored procedure definition builder. Define parameters, return type, and body via fluent API.
119
+
120
+ ```typescript
121
+ class ProcedureBuilder<
122
+ TParams extends ColumnBuilderRecord,
123
+ TReturns extends ColumnBuilderRecord,
124
+ > {
125
+ readonly $params!: TParams;
126
+ readonly $returns!: TReturns;
127
+
128
+ constructor(readonly meta: {
129
+ name: string;
130
+ description?: string;
131
+ database?: string;
132
+ schema?: string;
133
+ params?: TParams;
134
+ returns?: TReturns;
135
+ query?: string;
136
+ });
137
+
138
+ description(desc: string): ProcedureBuilder<TParams, TReturns>;
139
+ database(db: string): ProcedureBuilder<TParams, TReturns>;
140
+ schema(schema: string): ProcedureBuilder<TParams, TReturns>;
141
+ params<T extends ColumnBuilderRecord>(fn: (c: ...) => T): ProcedureBuilder<T, TReturns>;
142
+ returns<T extends ColumnBuilderRecord>(fn: (c: ...) => T): ProcedureBuilder<TParams, T>;
143
+ body(sql: string): ProcedureBuilder<TParams, TReturns>;
144
+ }
145
+ ```
146
+
147
+ | Method | Description |
148
+ |--------|-------------|
149
+ | `description()` | Set procedure description |
150
+ | `database()` | Set database name |
151
+ | `schema()` | Set schema name |
152
+ | `params()` | Define input parameters |
153
+ | `returns()` | Define return type columns |
154
+ | `body()` | Set procedure body SQL |
155
+
156
+ ## `ColumnBuilder`
157
+
158
+ Column definition builder. Define column type, nullable, autoIncrement, default, and description via fluent API.
159
+
160
+ ```typescript
161
+ class ColumnBuilder<TValue extends ColumnPrimitive, TMeta extends ColumnMeta> {
162
+ constructor(readonly meta: TMeta);
163
+
164
+ autoIncrement(): ColumnBuilder<TValue, Omit<TMeta, "autoIncrement"> & { autoIncrement: true }>;
165
+ nullable(): ColumnBuilder<TValue | undefined, Omit<TMeta, "nullable"> & { nullable: true }>;
166
+ default(value: TValue): ColumnBuilder<TValue, Omit<TMeta, "default"> & { default: typeof value }>;
167
+ description(desc: string): ColumnBuilder<TValue, TMeta & { description: string }>;
168
+ }
169
+ ```
170
+
171
+ | Method | Description |
172
+ |--------|-------------|
173
+ | `autoIncrement()` | Set auto increment (optional in INSERT) |
174
+ | `nullable()` | Allow NULL (adds `undefined` to type) |
175
+ | `default()` | Set default value (optional in INSERT) |
176
+ | `description()` | Set column description (DDL comment) |
177
+
178
+ ## `createColumnFactory`
179
+
180
+ Column builder factory. Provides builder creation methods for all basic data types.
181
+
182
+ ```typescript
183
+ function createColumnFactory(): {
184
+ int(): ColumnBuilder<number, ...>;
185
+ bigint(): ColumnBuilder<number, ...>;
186
+ float(): ColumnBuilder<number, ...>;
187
+ double(): ColumnBuilder<number, ...>;
188
+ decimal(precision: number, scale?: number): ColumnBuilder<number, ...>;
189
+ varchar(length: number): ColumnBuilder<string, ...>;
190
+ char(length: number): ColumnBuilder<string, ...>;
191
+ text(): ColumnBuilder<string, ...>;
192
+ binary(): ColumnBuilder<Bytes, ...>;
193
+ boolean(): ColumnBuilder<boolean, ...>;
194
+ datetime(): ColumnBuilder<DateTime, ...>;
195
+ date(): ColumnBuilder<DateOnly, ...>;
196
+ time(): ColumnBuilder<Time, ...>;
197
+ uuid(): ColumnBuilder<Uuid, ...>;
198
+ };
199
+ ```
200
+
201
+ | Method | SQL Type | TypeScript Type |
202
+ |--------|----------|-----------------|
203
+ | `int()` | INT (4 bytes) | `number` |
204
+ | `bigint()` | BIGINT (8 bytes) | `number` |
205
+ | `float()` | FLOAT (4 bytes) | `number` |
206
+ | `double()` | DOUBLE (8 bytes) | `number` |
207
+ | `decimal()` | DECIMAL(p, s) | `number` |
208
+ | `varchar()` | VARCHAR(n) | `string` |
209
+ | `char()` | CHAR(n) | `string` |
210
+ | `text()` | TEXT | `string` |
211
+ | `binary()` | LONGBLOB/VARBINARY(MAX)/BYTEA | `Bytes` |
212
+ | `boolean()` | TINYINT(1)/BIT/BOOLEAN | `boolean` |
213
+ | `datetime()` | DATETIME | `DateTime` |
214
+ | `date()` | DATE | `DateOnly` |
215
+ | `time()` | TIME | `Time` |
216
+ | `uuid()` | BINARY(16)/UNIQUEIDENTIFIER/UUID | `Uuid` |
217
+
218
+ ## `IndexBuilder`
219
+
220
+ Index definition builder. Define index columns, uniqueness, and sort order via fluent API.
221
+
222
+ ```typescript
223
+ class IndexBuilder<TKeys extends string[]> {
224
+ constructor(readonly meta: {
225
+ columns: TKeys;
226
+ name?: string;
227
+ unique?: boolean;
228
+ orderBy?: { [K in keyof TKeys]: "ASC" | "DESC" };
229
+ description?: string;
230
+ });
231
+
232
+ name(name: string): IndexBuilder<TKeys>;
233
+ unique(): IndexBuilder<TKeys>;
234
+ orderBy(...orderBy: { [K in keyof TKeys]: "ASC" | "DESC" }): IndexBuilder<TKeys>;
235
+ description(description: string): IndexBuilder<TKeys>;
236
+ }
237
+ ```
238
+
239
+ ## `createIndexFactory`
240
+
241
+ Index builder factory. Used in `TableBuilder.indexes()`.
242
+
243
+ ```typescript
244
+ function createIndexFactory<TColumnKey extends string>(): {
245
+ index<TKeys extends TColumnKey[]>(...columns: [...TKeys]): IndexBuilder<TKeys>;
246
+ };
247
+ ```
248
+
249
+ ## `ForeignKeyBuilder`
250
+
251
+ Foreign key relation builder (N:1). Creates actual FK constraints in the DB.
252
+
253
+ ```typescript
254
+ class ForeignKeyBuilder<
255
+ TOwner extends TableBuilder<any, any>,
256
+ TTargetFn extends () => TableBuilder<any, any>,
257
+ > {
258
+ constructor(readonly meta: {
259
+ ownerFn: () => TOwner;
260
+ columns: string[];
261
+ targetFn: TTargetFn;
262
+ description?: string;
263
+ });
264
+
265
+ description(desc: string): ForeignKeyBuilder<TOwner, TTargetFn>;
266
+ }
267
+ ```
268
+
269
+ ## `ForeignKeyTargetBuilder`
270
+
271
+ Foreign key reverse-reference builder (1:N). Loaded as array on `include()` (single object when `.single()` is called).
272
+
273
+ ```typescript
274
+ class ForeignKeyTargetBuilder<
275
+ TTargetTableFn extends () => TableBuilder<any, any>,
276
+ TIsSingle extends boolean,
277
+ > {
278
+ constructor(readonly meta: {
279
+ targetTableFn: TTargetTableFn;
280
+ relationName: string;
281
+ description?: string;
282
+ isSingle?: TIsSingle;
283
+ });
284
+
285
+ description(desc: string): ForeignKeyTargetBuilder<TTargetTableFn, TIsSingle>;
286
+ single(): ForeignKeyTargetBuilder<TTargetTableFn, true>;
287
+ }
288
+ ```
289
+
290
+ ## `RelationKeyBuilder`
291
+
292
+ Logical relation builder (N:1). Same as `ForeignKeyBuilder` but does not create FK constraints in the DB. Can be used in views.
293
+
294
+ ```typescript
295
+ class RelationKeyBuilder<
296
+ TOwner extends TableBuilder<any, any> | ViewBuilder<any, any, any>,
297
+ TTargetFn extends () => TableBuilder<any, any> | ViewBuilder<any, any, any>,
298
+ > {
299
+ constructor(readonly meta: {
300
+ ownerFn: () => TOwner;
301
+ columns: string[];
302
+ targetFn: TTargetFn;
303
+ description?: string;
304
+ });
305
+
306
+ description(desc: string): RelationKeyBuilder<TOwner, TTargetFn>;
307
+ }
308
+ ```
309
+
310
+ ## `RelationKeyTargetBuilder`
311
+
312
+ Logical relation reverse-reference builder (1:N). Same as `ForeignKeyTargetBuilder` but without DB FK constraints. Can be used in views.
313
+
314
+ ```typescript
315
+ class RelationKeyTargetBuilder<
316
+ TTargetTableFn extends () => TableBuilder<any, any> | ViewBuilder<any, any, any>,
317
+ TIsSingle extends boolean,
318
+ > {
319
+ constructor(readonly meta: {
320
+ targetTableFn: TTargetTableFn;
321
+ relationName: string;
322
+ description?: string;
323
+ isSingle?: TIsSingle;
324
+ });
325
+
326
+ description(desc: string): RelationKeyTargetBuilder<TTargetTableFn, TIsSingle>;
327
+ single(): RelationKeyTargetBuilder<TTargetTableFn, true>;
328
+ }
329
+ ```
330
+
331
+ ## `createRelationFactory`
332
+
333
+ Relation builder factory. Used in `TableBuilder.relations()` and `ViewBuilder.relations()`. Tables can use both FK + RelationKey; views can only use RelationKey.
334
+
335
+ ```typescript
336
+ function createRelationFactory<
337
+ TOwner extends TableBuilder<any, any> | ViewBuilder<any, any, any>,
338
+ TColumnKey extends string,
339
+ >(ownerFn: () => TOwner): TOwner extends TableBuilder<any, any>
340
+ ? RelationFkFactory<TOwner, TColumnKey> & RelationRkFactory<TOwner, TColumnKey>
341
+ : RelationRkFactory<TOwner, TColumnKey>;
342
+ ```
343
+
344
+ ## `ColumnBuilderRecord`
345
+
346
+ Column builder record type. Used as the return type of `TableBuilder.columns()`.
347
+
348
+ ```typescript
349
+ type ColumnBuilderRecord = Record<string, ColumnBuilder<ColumnPrimitive, ColumnMeta>>;
350
+ ```
351
+
352
+ ## `RelationBuilderRecord`
353
+
354
+ Relation builder record type. Return type of `TableBuilder.relations()` and `ViewBuilder.relations()`.
355
+
356
+ ```typescript
357
+ type RelationBuilderRecord = Record<
358
+ string,
359
+ ForeignKeyBuilder<any, any> | ForeignKeyTargetBuilder<any, any> | RelationKeyBuilder<any, any> | RelationKeyTargetBuilder<any, any>
360
+ >;
361
+ ```
362
+
363
+ ## `InferColumns`
364
+
365
+ Infer actual value types from a column builder record.
366
+
367
+ ```typescript
368
+ type InferColumns<TBuilders extends ColumnBuilderRecord> = {
369
+ [K in keyof TBuilders]: TBuilders[K] extends ColumnBuilder<infer V, any> ? V : never;
370
+ };
371
+ ```
372
+
373
+ ## `InferColumnExprs`
374
+
375
+ Infer expression input types from a column builder record.
376
+
377
+ ```typescript
378
+ type InferColumnExprs<TBuilders extends ColumnBuilderRecord> = {
379
+ [K in keyof TBuilders]: TBuilders[K] extends ColumnBuilder<infer V, any> ? ExprInput<V> : never;
380
+ };
381
+ ```
382
+
383
+ ## `InferInsertColumns`
384
+
385
+ INSERT type inference. Required columns are required, optional columns (autoIncrement, nullable, default) are `Partial`.
386
+
387
+ ```typescript
388
+ type InferInsertColumns<TBuilders extends ColumnBuilderRecord> = Pick<
389
+ InferColumns<TBuilders>,
390
+ RequiredInsertKeys<TBuilders>
391
+ > & Partial<Pick<InferColumns<TBuilders>, OptionalInsertKeys<TBuilders>>>;
392
+ ```
393
+
394
+ ## `InferUpdateColumns`
395
+
396
+ UPDATE type inference. All columns are optional.
397
+
398
+ ```typescript
399
+ type InferUpdateColumns<TBuilders extends ColumnBuilderRecord> = Partial<InferColumns<TBuilders>>;
400
+ ```
401
+
402
+ ## `RequiredInsertKeys`
403
+
404
+ Extract required column keys for INSERT (columns without autoIncrement, nullable, or default).
405
+
406
+ ```typescript
407
+ type RequiredInsertKeys<TBuilders extends ColumnBuilderRecord> = { ... }[keyof TBuilders];
408
+ ```
409
+
410
+ ## `OptionalInsertKeys`
411
+
412
+ Extract optional column keys for INSERT.
413
+
414
+ ```typescript
415
+ type OptionalInsertKeys<TBuilders extends ColumnBuilderRecord> = Exclude<keyof TBuilders, RequiredInsertKeys<TBuilders>>;
416
+ ```
417
+
418
+ ## `DataToColumnBuilderRecord`
419
+
420
+ Transform from data record to column builder record.
421
+
422
+ ```typescript
423
+ type DataToColumnBuilderRecord<TData extends DataRecord> = { ... };
424
+ ```
425
+
426
+ ## `InferDeepRelations`
427
+
428
+ Deep relation type inference from relation definitions. Makes all relations optional.
429
+
430
+ ```typescript
431
+ type InferDeepRelations<TRelations extends RelationBuilderRecord> = {
432
+ [K in keyof TRelations]?: ExtractRelationTarget<TRelations[K]> | ExtractRelationTargetResult<TRelations[K]>;
433
+ };
434
+ ```
435
+
436
+ ## `ExtractRelationTarget`
437
+
438
+ Extract target type from FK/RelationKey (single object, N:1 relation).
439
+
440
+ ```typescript
441
+ type ExtractRelationTarget<TRelation> = ...;
442
+ ```
443
+
444
+ ## `ExtractRelationTargetResult`
445
+
446
+ Extract target type from FKTarget/RelationKeyTarget (array or single object, 1:N relation).
447
+
448
+ ```typescript
449
+ type ExtractRelationTargetResult<TRelation> = ...;
450
+ ```
451
+
452
+ ## `_Migration`
453
+
454
+ Built-in system migration table. Automatically included in every `DbContextDef`.
455
+
456
+ ```typescript
457
+ const _Migration = Table("_migration")
458
+ .columns((c) => ({
459
+ code: c.varchar(255),
460
+ }))
461
+ .description("System Migration Table")
462
+ .primaryKey("code");
463
+ ```