@simplysm/orm-common 13.0.98 → 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.
- package/README.md +109 -184
- package/docs/core.md +125 -116
- package/docs/expression.md +168 -230
- package/docs/query-builder.md +46 -149
- package/docs/queryable.md +144 -524
- package/docs/schema-builders.md +245 -197
- package/docs/types.md +190 -190
- package/docs/utilities.md +13 -108
- package/package.json +2 -2
package/docs/schema-builders.md
CHANGED
|
@@ -1,27 +1,30 @@
|
|
|
1
1
|
# Schema Builders
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## `Table`
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
## Table / TableBuilder
|
|
8
|
-
|
|
9
|
-
### Table (factory function)
|
|
5
|
+
Table builder factory function. Creates a `TableBuilder` for defining table schema via fluent API.
|
|
10
6
|
|
|
11
7
|
```typescript
|
|
12
8
|
function Table(name: string): TableBuilder<{}, {}>;
|
|
13
9
|
```
|
|
14
10
|
|
|
15
|
-
|
|
11
|
+
## `TableBuilder`
|
|
16
12
|
|
|
17
|
-
|
|
13
|
+
Database table definition builder. Define columns, PK, indexes, and relations via fluent API.
|
|
18
14
|
|
|
19
15
|
```typescript
|
|
20
16
|
class TableBuilder<
|
|
21
17
|
TColumns extends ColumnBuilderRecord,
|
|
22
18
|
TRelations extends RelationBuilderRecord,
|
|
23
19
|
> {
|
|
24
|
-
readonly
|
|
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: {
|
|
25
28
|
name: string;
|
|
26
29
|
description?: string;
|
|
27
30
|
database?: string;
|
|
@@ -30,60 +33,41 @@ class TableBuilder<
|
|
|
30
33
|
primaryKey?: (keyof TColumns & string)[];
|
|
31
34
|
relations?: TRelations;
|
|
32
35
|
indexes?: IndexBuilder<(keyof TColumns & string)[]>[];
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
// Type inference helpers
|
|
36
|
-
readonly $inferSelect: InferColumns<TColumns> & InferDeepRelations<TRelations>;
|
|
37
|
-
readonly $inferColumns: InferColumns<TColumns>;
|
|
38
|
-
readonly $inferInsert: InferInsertColumns<TColumns>;
|
|
39
|
-
readonly $inferUpdate: InferUpdateColumns<TColumns>;
|
|
36
|
+
});
|
|
40
37
|
|
|
41
38
|
description(desc: string): TableBuilder<TColumns, TRelations>;
|
|
42
39
|
database(db: string): TableBuilder<TColumns, TRelations>;
|
|
43
40
|
schema(schema: string): TableBuilder<TColumns, TRelations>;
|
|
44
|
-
columns<
|
|
45
|
-
fn: (c: ReturnType<typeof createColumnFactory>) =>
|
|
46
|
-
): TableBuilder<
|
|
41
|
+
columns<TNewColumnDefs extends ColumnBuilderRecord>(
|
|
42
|
+
fn: (c: ReturnType<typeof createColumnFactory>) => TNewColumnDefs,
|
|
43
|
+
): TableBuilder<TNewColumnDefs, TRelations>;
|
|
47
44
|
primaryKey(...columns: (keyof TColumns & string)[]): TableBuilder<TColumns, TRelations>;
|
|
48
|
-
indexes(
|
|
49
|
-
|
|
50
|
-
): TableBuilder<TColumns, TRelations>;
|
|
51
|
-
relations<T extends RelationBuilderRecord>(
|
|
52
|
-
fn: (r: RelationFactory) => T,
|
|
53
|
-
): TableBuilder<TColumns, T>;
|
|
45
|
+
indexes(fn: (i: ...) => IndexBuilder<string[]>[]): TableBuilder<TColumns, TRelations>;
|
|
46
|
+
relations<T extends RelationBuilderRecord>(fn: (r: ...) => T): TableBuilder<TColumns, T>;
|
|
54
47
|
}
|
|
55
48
|
```
|
|
56
49
|
|
|
57
|
-
|
|
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 |
|
|
58
59
|
|
|
59
|
-
|
|
60
|
-
const User = Table("User")
|
|
61
|
-
.database("mydb")
|
|
62
|
-
.columns((c) => ({
|
|
63
|
-
id: c.bigint().autoIncrement(),
|
|
64
|
-
name: c.varchar(100),
|
|
65
|
-
email: c.varchar(200).nullable(),
|
|
66
|
-
status: c.varchar(20).default("active"),
|
|
67
|
-
createdAt: c.datetime(),
|
|
68
|
-
}))
|
|
69
|
-
.primaryKey("id")
|
|
70
|
-
.indexes((i) => [i.index("email").unique()])
|
|
71
|
-
.relations((r) => ({
|
|
72
|
-
posts: r.foreignKeyTarget(() => Post, "author"),
|
|
73
|
-
}));
|
|
74
|
-
```
|
|
60
|
+
## `View`
|
|
75
61
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
## View / ViewBuilder
|
|
79
|
-
|
|
80
|
-
### View (factory function)
|
|
62
|
+
View builder factory function. Creates a `ViewBuilder` for defining view schema via fluent API.
|
|
81
63
|
|
|
82
64
|
```typescript
|
|
83
65
|
function View(name: string): ViewBuilder<any, any, {}>;
|
|
84
66
|
```
|
|
85
67
|
|
|
86
|
-
|
|
68
|
+
## `ViewBuilder`
|
|
69
|
+
|
|
70
|
+
Database view definition builder. Define view query and relations via fluent API.
|
|
87
71
|
|
|
88
72
|
```typescript
|
|
89
73
|
class ViewBuilder<
|
|
@@ -91,16 +75,17 @@ class ViewBuilder<
|
|
|
91
75
|
TData extends DataRecord,
|
|
92
76
|
TRelations extends RelationBuilderRecord,
|
|
93
77
|
> {
|
|
94
|
-
readonly
|
|
78
|
+
readonly $relations!: TRelations;
|
|
79
|
+
readonly $inferSelect!: TData;
|
|
80
|
+
|
|
81
|
+
constructor(readonly meta: {
|
|
95
82
|
name: string;
|
|
96
83
|
description?: string;
|
|
97
84
|
database?: string;
|
|
98
85
|
schema?: string;
|
|
99
86
|
viewFn?: (db: TDbContext) => Queryable<TData, any>;
|
|
100
87
|
relations?: TRelations;
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
readonly $inferSelect: TData;
|
|
88
|
+
});
|
|
104
89
|
|
|
105
90
|
description(desc: string): ViewBuilder<TDbContext, TData, TRelations>;
|
|
106
91
|
database(db: string): ViewBuilder<TDbContext, TData, TRelations>;
|
|
@@ -108,42 +93,39 @@ class ViewBuilder<
|
|
|
108
93
|
query<TViewData extends DataRecord, TDb extends DbContextBase>(
|
|
109
94
|
viewFn: (db: TDb) => Queryable<TViewData, any>,
|
|
110
95
|
): ViewBuilder<TDb, TViewData, TRelations>;
|
|
111
|
-
relations<T extends RelationBuilderRecord>(
|
|
112
|
-
fn: (r: RelationFactory) => T,
|
|
113
|
-
): ViewBuilder<TDbContext, TData & InferDeepRelations<T>, TRelations>;
|
|
96
|
+
relations<T extends RelationBuilderRecord>(fn: (r: ...) => T): ViewBuilder<TDbContext, TData & InferDeepRelations<T>, TRelations>;
|
|
114
97
|
}
|
|
115
98
|
```
|
|
116
99
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
.where((u) => [expr.eq(u.status, "active")])
|
|
125
|
-
.select((u) => ({ id: u.id, name: u.name, email: u.email }))
|
|
126
|
-
);
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
---
|
|
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 |
|
|
130
107
|
|
|
131
|
-
## Procedure
|
|
108
|
+
## `Procedure`
|
|
132
109
|
|
|
133
|
-
|
|
110
|
+
Procedure builder factory function. Creates a `ProcedureBuilder` for defining stored procedure schema via fluent API.
|
|
134
111
|
|
|
135
112
|
```typescript
|
|
136
113
|
function Procedure(name: string): ProcedureBuilder<never, never>;
|
|
137
114
|
```
|
|
138
115
|
|
|
139
|
-
|
|
116
|
+
## `ProcedureBuilder`
|
|
117
|
+
|
|
118
|
+
Stored procedure definition builder. Define parameters, return type, and body via fluent API.
|
|
140
119
|
|
|
141
120
|
```typescript
|
|
142
121
|
class ProcedureBuilder<
|
|
143
122
|
TParams extends ColumnBuilderRecord,
|
|
144
123
|
TReturns extends ColumnBuilderRecord,
|
|
145
124
|
> {
|
|
146
|
-
readonly
|
|
125
|
+
readonly $params!: TParams;
|
|
126
|
+
readonly $returns!: TReturns;
|
|
127
|
+
|
|
128
|
+
constructor(readonly meta: {
|
|
147
129
|
name: string;
|
|
148
130
|
description?: string;
|
|
149
131
|
database?: string;
|
|
@@ -151,51 +133,51 @@ class ProcedureBuilder<
|
|
|
151
133
|
params?: TParams;
|
|
152
134
|
returns?: TReturns;
|
|
153
135
|
query?: string;
|
|
154
|
-
};
|
|
136
|
+
});
|
|
155
137
|
|
|
156
138
|
description(desc: string): ProcedureBuilder<TParams, TReturns>;
|
|
157
139
|
database(db: string): ProcedureBuilder<TParams, TReturns>;
|
|
158
140
|
schema(schema: string): ProcedureBuilder<TParams, TReturns>;
|
|
159
|
-
params<T extends ColumnBuilderRecord>(
|
|
160
|
-
|
|
161
|
-
): ProcedureBuilder<T, TReturns>;
|
|
162
|
-
returns<T extends ColumnBuilderRecord>(
|
|
163
|
-
fn: (c: ReturnType<typeof createColumnFactory>) => T,
|
|
164
|
-
): ProcedureBuilder<TParams, T>;
|
|
141
|
+
params<T extends ColumnBuilderRecord>(fn: (c: ...) => T): ProcedureBuilder<T, TReturns>;
|
|
142
|
+
returns<T extends ColumnBuilderRecord>(fn: (c: ...) => T): ProcedureBuilder<TParams, T>;
|
|
165
143
|
body(sql: string): ProcedureBuilder<TParams, TReturns>;
|
|
166
144
|
}
|
|
167
145
|
```
|
|
168
146
|
|
|
169
|
-
|
|
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 |
|
|
170
155
|
|
|
171
|
-
|
|
172
|
-
const GetUserById = Procedure("GetUserById")
|
|
173
|
-
.database("mydb")
|
|
174
|
-
.params((c) => ({ userId: c.bigint() }))
|
|
175
|
-
.returns((c) => ({ id: c.bigint(), name: c.varchar(100) }))
|
|
176
|
-
.body("SELECT id, name FROM User WHERE id = userId");
|
|
177
|
-
```
|
|
156
|
+
## `ColumnBuilder`
|
|
178
157
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
## ColumnBuilder
|
|
182
|
-
|
|
183
|
-
Column definition builder used within `TableBuilder.columns()`.
|
|
158
|
+
Column definition builder. Define column type, nullable, autoIncrement, default, and description via fluent API.
|
|
184
159
|
|
|
185
160
|
```typescript
|
|
186
161
|
class ColumnBuilder<TValue extends ColumnPrimitive, TMeta extends ColumnMeta> {
|
|
187
|
-
readonly meta: TMeta;
|
|
162
|
+
constructor(readonly meta: TMeta);
|
|
188
163
|
|
|
189
|
-
autoIncrement(): ColumnBuilder<TValue, TMeta & { autoIncrement: true }>;
|
|
190
|
-
nullable(): ColumnBuilder<TValue | undefined, TMeta & { nullable: true }>;
|
|
191
|
-
default(value: TValue): ColumnBuilder<TValue, TMeta & { default: typeof value }>;
|
|
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 }>;
|
|
192
167
|
description(desc: string): ColumnBuilder<TValue, TMeta & { description: string }>;
|
|
193
168
|
}
|
|
194
169
|
```
|
|
195
170
|
|
|
196
|
-
|
|
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`
|
|
197
179
|
|
|
198
|
-
|
|
180
|
+
Column builder factory. Provides builder creation methods for all basic data types.
|
|
199
181
|
|
|
200
182
|
```typescript
|
|
201
183
|
function createColumnFactory(): {
|
|
@@ -216,34 +198,36 @@ function createColumnFactory(): {
|
|
|
216
198
|
};
|
|
217
199
|
```
|
|
218
200
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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.
|
|
237
221
|
|
|
238
222
|
```typescript
|
|
239
223
|
class IndexBuilder<TKeys extends string[]> {
|
|
240
|
-
readonly meta: {
|
|
224
|
+
constructor(readonly meta: {
|
|
241
225
|
columns: TKeys;
|
|
242
226
|
name?: string;
|
|
243
227
|
unique?: boolean;
|
|
244
228
|
orderBy?: { [K in keyof TKeys]: "ASC" | "DESC" };
|
|
245
229
|
description?: string;
|
|
246
|
-
};
|
|
230
|
+
});
|
|
247
231
|
|
|
248
232
|
name(name: string): IndexBuilder<TKeys>;
|
|
249
233
|
unique(): IndexBuilder<TKeys>;
|
|
@@ -252,7 +236,9 @@ class IndexBuilder<TKeys extends string[]> {
|
|
|
252
236
|
}
|
|
253
237
|
```
|
|
254
238
|
|
|
255
|
-
## createIndexFactory
|
|
239
|
+
## `createIndexFactory`
|
|
240
|
+
|
|
241
|
+
Index builder factory. Used in `TableBuilder.indexes()`.
|
|
256
242
|
|
|
257
243
|
```typescript
|
|
258
244
|
function createIndexFactory<TColumnKey extends string>(): {
|
|
@@ -260,156 +246,218 @@ function createIndexFactory<TColumnKey extends string>(): {
|
|
|
260
246
|
};
|
|
261
247
|
```
|
|
262
248
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
```typescript
|
|
266
|
-
Table("User")
|
|
267
|
-
.columns((c) => ({ id: c.bigint(), email: c.varchar(200), name: c.varchar(100) }))
|
|
268
|
-
.indexes((i) => [
|
|
269
|
-
i.index("email").unique(),
|
|
270
|
-
i.index("name", "email").orderBy("ASC", "DESC"),
|
|
271
|
-
i.index("email").name("IX_User_Email"),
|
|
272
|
-
]);
|
|
273
|
-
```
|
|
249
|
+
## `ForeignKeyBuilder`
|
|
274
250
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
## Relation Builders
|
|
278
|
-
|
|
279
|
-
### ForeignKeyBuilder
|
|
280
|
-
|
|
281
|
-
N:1 FK relation that creates a DB constraint.
|
|
251
|
+
Foreign key relation builder (N:1). Creates actual FK constraints in the DB.
|
|
282
252
|
|
|
283
253
|
```typescript
|
|
284
|
-
class ForeignKeyBuilder<
|
|
285
|
-
|
|
254
|
+
class ForeignKeyBuilder<
|
|
255
|
+
TOwner extends TableBuilder<any, any>,
|
|
256
|
+
TTargetFn extends () => TableBuilder<any, any>,
|
|
257
|
+
> {
|
|
258
|
+
constructor(readonly meta: {
|
|
286
259
|
ownerFn: () => TOwner;
|
|
287
260
|
columns: string[];
|
|
288
261
|
targetFn: TTargetFn;
|
|
289
262
|
description?: string;
|
|
290
|
-
};
|
|
263
|
+
});
|
|
291
264
|
|
|
292
265
|
description(desc: string): ForeignKeyBuilder<TOwner, TTargetFn>;
|
|
293
266
|
}
|
|
294
267
|
```
|
|
295
268
|
|
|
296
|
-
|
|
269
|
+
## `ForeignKeyTargetBuilder`
|
|
297
270
|
|
|
298
|
-
|
|
271
|
+
Foreign key reverse-reference builder (1:N). Loaded as array on `include()` (single object when `.single()` is called).
|
|
299
272
|
|
|
300
273
|
```typescript
|
|
301
|
-
class ForeignKeyTargetBuilder<
|
|
302
|
-
|
|
274
|
+
class ForeignKeyTargetBuilder<
|
|
275
|
+
TTargetTableFn extends () => TableBuilder<any, any>,
|
|
276
|
+
TIsSingle extends boolean,
|
|
277
|
+
> {
|
|
278
|
+
constructor(readonly meta: {
|
|
303
279
|
targetTableFn: TTargetTableFn;
|
|
304
280
|
relationName: string;
|
|
305
281
|
description?: string;
|
|
306
282
|
isSingle?: TIsSingle;
|
|
307
|
-
};
|
|
283
|
+
});
|
|
308
284
|
|
|
309
285
|
description(desc: string): ForeignKeyTargetBuilder<TTargetTableFn, TIsSingle>;
|
|
310
286
|
single(): ForeignKeyTargetBuilder<TTargetTableFn, true>;
|
|
311
287
|
}
|
|
312
288
|
```
|
|
313
289
|
|
|
314
|
-
|
|
290
|
+
## `RelationKeyBuilder`
|
|
315
291
|
|
|
316
|
-
N:1
|
|
292
|
+
Logical relation builder (N:1). Same as `ForeignKeyBuilder` but does not create FK constraints in the DB. Can be used in views.
|
|
317
293
|
|
|
318
294
|
```typescript
|
|
319
295
|
class RelationKeyBuilder<
|
|
320
296
|
TOwner extends TableBuilder<any, any> | ViewBuilder<any, any, any>,
|
|
321
297
|
TTargetFn extends () => TableBuilder<any, any> | ViewBuilder<any, any, any>,
|
|
322
298
|
> {
|
|
323
|
-
readonly meta: {
|
|
299
|
+
constructor(readonly meta: {
|
|
324
300
|
ownerFn: () => TOwner;
|
|
325
301
|
columns: string[];
|
|
326
302
|
targetFn: TTargetFn;
|
|
327
303
|
description?: string;
|
|
328
|
-
};
|
|
304
|
+
});
|
|
329
305
|
|
|
330
306
|
description(desc: string): RelationKeyBuilder<TOwner, TTargetFn>;
|
|
331
307
|
}
|
|
332
308
|
```
|
|
333
309
|
|
|
334
|
-
|
|
310
|
+
## `RelationKeyTargetBuilder`
|
|
335
311
|
|
|
336
|
-
|
|
312
|
+
Logical relation reverse-reference builder (1:N). Same as `ForeignKeyTargetBuilder` but without DB FK constraints. Can be used in views.
|
|
337
313
|
|
|
338
314
|
```typescript
|
|
339
315
|
class RelationKeyTargetBuilder<
|
|
340
316
|
TTargetTableFn extends () => TableBuilder<any, any> | ViewBuilder<any, any, any>,
|
|
341
317
|
TIsSingle extends boolean,
|
|
342
318
|
> {
|
|
343
|
-
readonly meta: {
|
|
319
|
+
constructor(readonly meta: {
|
|
344
320
|
targetTableFn: TTargetTableFn;
|
|
345
321
|
relationName: string;
|
|
346
322
|
description?: string;
|
|
347
323
|
isSingle?: TIsSingle;
|
|
348
|
-
};
|
|
324
|
+
});
|
|
349
325
|
|
|
350
326
|
description(desc: string): RelationKeyTargetBuilder<TTargetTableFn, TIsSingle>;
|
|
351
327
|
single(): RelationKeyTargetBuilder<TTargetTableFn, true>;
|
|
352
328
|
}
|
|
353
329
|
```
|
|
354
330
|
|
|
355
|
-
|
|
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.
|
|
356
334
|
|
|
357
335
|
```typescript
|
|
358
|
-
function createRelationFactory<
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
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>;
|
|
363
342
|
```
|
|
364
343
|
|
|
365
|
-
|
|
366
|
-
- `foreignKey(columns, targetFn)` -- N:1 FK (table only)
|
|
367
|
-
- `foreignKeyTarget(targetTableFn, relationName)` -- 1:N FK reverse-reference (table only)
|
|
368
|
-
- `relationKey(columns, targetFn)` -- N:1 logical relation
|
|
369
|
-
- `relationKeyTarget(targetTableFn, relationName)` -- 1:N logical reverse-reference
|
|
344
|
+
## `ColumnBuilderRecord`
|
|
370
345
|
|
|
371
|
-
|
|
346
|
+
Column builder record type. Used as the return type of `TableBuilder.columns()`.
|
|
372
347
|
|
|
373
348
|
```typescript
|
|
374
|
-
|
|
375
|
-
.columns((c) => ({ id: c.bigint().autoIncrement(), authorId: c.bigint() }))
|
|
376
|
-
.primaryKey("id")
|
|
377
|
-
.relations((r) => ({
|
|
378
|
-
author: r.foreignKey(["authorId"], () => User),
|
|
379
|
-
}));
|
|
380
|
-
|
|
381
|
-
const User = Table("User")
|
|
382
|
-
.columns((c) => ({ id: c.bigint().autoIncrement(), name: c.varchar(100) }))
|
|
383
|
-
.primaryKey("id")
|
|
384
|
-
.relations((r) => ({
|
|
385
|
-
posts: r.foreignKeyTarget(() => Post, "author"),
|
|
386
|
-
profile: r.foreignKeyTarget(() => Profile, "user").single(),
|
|
387
|
-
}));
|
|
349
|
+
type ColumnBuilderRecord = Record<string, ColumnBuilder<ColumnPrimitive, ColumnMeta>>;
|
|
388
350
|
```
|
|
389
351
|
|
|
390
|
-
|
|
352
|
+
## `RelationBuilderRecord`
|
|
391
353
|
|
|
392
|
-
|
|
354
|
+
Relation builder record type. Return type of `TableBuilder.relations()` and `ViewBuilder.relations()`.
|
|
393
355
|
|
|
394
|
-
|
|
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.
|
|
395
366
|
|
|
396
367
|
```typescript
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
.primaryKey("code");
|
|
368
|
+
type InferColumns<TBuilders extends ColumnBuilderRecord> = {
|
|
369
|
+
[K in keyof TBuilders]: TBuilders[K] extends ColumnBuilder<infer V, any> ? V : never;
|
|
370
|
+
};
|
|
401
371
|
```
|
|
402
372
|
|
|
403
|
-
|
|
373
|
+
## `InferColumnExprs`
|
|
404
374
|
|
|
405
|
-
|
|
375
|
+
Infer expression input types from a column builder record.
|
|
406
376
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
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
|
+
```
|