@simplysm/orm-common 13.0.83 → 13.0.85
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/dist/ddl/initialize.d.ts +2 -2
- package/dist/ddl/initialize.js +1 -1
- package/dist/ddl/initialize.js.map +1 -1
- package/dist/ddl/table-ddl.d.ts +1 -1
- package/dist/exec/queryable.d.ts +115 -115
- package/dist/exec/queryable.js +68 -68
- package/dist/exec/queryable.js.map +1 -1
- package/dist/expr/expr.d.ts +248 -248
- package/dist/expr/expr.js +250 -250
- package/dist/query-builder/base/expr-renderer-base.d.ts +7 -7
- package/dist/query-builder/mssql/mssql-expr-renderer.d.ts +3 -3
- package/dist/query-builder/mssql/mssql-expr-renderer.d.ts.map +1 -1
- package/dist/query-builder/mssql/mssql-expr-renderer.js +5 -5
- package/dist/query-builder/mssql/mssql-query-builder.d.ts +2 -2
- package/dist/query-builder/mssql/mssql-query-builder.d.ts.map +1 -1
- package/dist/query-builder/mssql/mssql-query-builder.js +7 -7
- package/dist/query-builder/mysql/mysql-expr-renderer.d.ts +2 -2
- package/dist/query-builder/mysql/mysql-expr-renderer.d.ts.map +1 -1
- package/dist/query-builder/mysql/mysql-expr-renderer.js +4 -4
- package/dist/query-builder/mysql/mysql-query-builder.d.ts +10 -10
- package/dist/query-builder/mysql/mysql-query-builder.d.ts.map +1 -1
- package/dist/query-builder/mysql/mysql-query-builder.js +4 -4
- package/dist/query-builder/postgresql/postgresql-expr-renderer.d.ts +2 -2
- package/dist/query-builder/postgresql/postgresql-expr-renderer.d.ts.map +1 -1
- package/dist/query-builder/postgresql/postgresql-expr-renderer.js +4 -4
- package/dist/query-builder/postgresql/postgresql-query-builder.d.ts +8 -8
- package/dist/query-builder/postgresql/postgresql-query-builder.d.ts.map +1 -1
- package/dist/query-builder/postgresql/postgresql-query-builder.js +7 -7
- package/dist/query-builder/query-builder.d.ts +1 -1
- package/dist/schema/factory/column-builder.d.ts +46 -46
- package/dist/schema/factory/column-builder.js +25 -25
- package/dist/schema/factory/index-builder.d.ts +22 -22
- package/dist/schema/factory/index-builder.js +14 -14
- package/dist/schema/factory/relation-builder.d.ts +93 -93
- package/dist/schema/factory/relation-builder.d.ts.map +1 -1
- package/dist/schema/factory/relation-builder.js +37 -37
- package/dist/schema/procedure-builder.d.ts +38 -38
- package/dist/schema/procedure-builder.d.ts.map +1 -1
- package/dist/schema/procedure-builder.js +26 -26
- package/dist/schema/table-builder.d.ts +38 -38
- package/dist/schema/table-builder.d.ts.map +1 -1
- package/dist/schema/table-builder.js +29 -29
- package/dist/schema/view-builder.d.ts +26 -26
- package/dist/schema/view-builder.d.ts.map +1 -1
- package/dist/schema/view-builder.js +18 -18
- package/dist/types/db.d.ts +40 -40
- package/dist/types/expr.d.ts +75 -75
- package/dist/types/expr.d.ts.map +1 -1
- package/dist/types/query-def.d.ts +32 -32
- package/dist/types/query-def.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/ddl/initialize.ts +16 -16
- package/src/ddl/table-ddl.ts +1 -1
- package/src/exec/queryable.ts +163 -163
- package/src/expr/expr.ts +257 -257
- package/src/query-builder/base/expr-renderer-base.ts +8 -8
- package/src/query-builder/mssql/mssql-expr-renderer.ts +20 -20
- package/src/query-builder/mssql/mssql-query-builder.ts +28 -28
- package/src/query-builder/mysql/mysql-expr-renderer.ts +22 -22
- package/src/query-builder/mysql/mysql-query-builder.ts +65 -65
- package/src/query-builder/postgresql/postgresql-expr-renderer.ts +15 -15
- package/src/query-builder/postgresql/postgresql-query-builder.ts +43 -43
- package/src/query-builder/query-builder.ts +1 -1
- package/src/schema/factory/column-builder.ts +48 -48
- package/src/schema/factory/index-builder.ts +22 -22
- package/src/schema/factory/relation-builder.ts +95 -95
- package/src/schema/procedure-builder.ts +38 -38
- package/src/schema/table-builder.ts +38 -38
- package/src/schema/view-builder.ts +28 -28
- package/src/types/db.ts +41 -41
- package/src/types/expr.ts +79 -79
- package/src/types/query-def.ts +37 -37
- package/tests/ddl/basic.expected.ts +8 -8
|
@@ -5,13 +5,13 @@ import { type ColumnBuilderRecord, createColumnFactory } from "./factory/column-
|
|
|
5
5
|
// ============================================
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* Stored Procedure definition builder
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
* DbContext
|
|
10
|
+
* Define Procedure parameters, return type, and body via Fluent API
|
|
11
|
+
* Use with DbContext's executable() for type-safe Procedure invocation
|
|
12
12
|
*
|
|
13
|
-
* @template TParams -
|
|
14
|
-
* @template TReturns -
|
|
13
|
+
* @template TParams - Parameter Column definition type
|
|
14
|
+
* @template TReturns - Return Column definition type
|
|
15
15
|
*
|
|
16
16
|
* @example
|
|
17
17
|
* ```typescript
|
|
@@ -28,12 +28,12 @@ import { type ColumnBuilderRecord, createColumnFactory } from "./factory/column-
|
|
|
28
28
|
* }))
|
|
29
29
|
* .body("SELECT id, name, email FROM User WHERE id = userId");
|
|
30
30
|
*
|
|
31
|
-
* //
|
|
31
|
+
* // Used in DbContext
|
|
32
32
|
* class MyDb extends DbContext {
|
|
33
33
|
* readonly getUserById = executable(this, GetUserById);
|
|
34
34
|
* }
|
|
35
35
|
*
|
|
36
|
-
* //
|
|
36
|
+
* // Invocation
|
|
37
37
|
* const users = await db.getUserById({ userId: 1n }).execute();
|
|
38
38
|
* ```
|
|
39
39
|
*
|
|
@@ -44,20 +44,20 @@ export class ProcedureBuilder<
|
|
|
44
44
|
TParams extends ColumnBuilderRecord,
|
|
45
45
|
TReturns extends ColumnBuilderRecord,
|
|
46
46
|
> {
|
|
47
|
-
/**
|
|
47
|
+
/** Parameter definition (type for inference) */
|
|
48
48
|
readonly $params!: TParams;
|
|
49
49
|
/** return type definition (type for inference) */
|
|
50
50
|
readonly $returns!: TReturns;
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* @param meta - Procedure Metadata
|
|
54
|
-
* @param meta.name - Procedure
|
|
55
|
-
* @param meta.description - Procedure description (
|
|
56
|
-
* @param meta.database - Database
|
|
57
|
-
* @param meta.schema - Schema
|
|
58
|
-
* @param meta.params -
|
|
59
|
-
* @param meta.returns -
|
|
60
|
-
* @param meta.query - Procedure
|
|
54
|
+
* @param meta.name - Procedure name
|
|
55
|
+
* @param meta.description - Procedure description (comment)
|
|
56
|
+
* @param meta.database - Database name
|
|
57
|
+
* @param meta.schema - Schema name (MSSQL/PostgreSQL)
|
|
58
|
+
* @param meta.params - Parameter definition
|
|
59
|
+
* @param meta.returns - Return type definition
|
|
60
|
+
* @param meta.query - Procedure body SQL
|
|
61
61
|
*/
|
|
62
62
|
constructor(
|
|
63
63
|
readonly meta: {
|
|
@@ -74,7 +74,7 @@ export class ProcedureBuilder<
|
|
|
74
74
|
/**
|
|
75
75
|
* Procedure set description
|
|
76
76
|
*
|
|
77
|
-
* @param desc - Procedure description (DDL Comment
|
|
77
|
+
* @param desc - Procedure description (used as DDL Comment)
|
|
78
78
|
* @returns new ProcedureBuilder instance
|
|
79
79
|
*/
|
|
80
80
|
description(desc: string): ProcedureBuilder<TParams, TReturns> {
|
|
@@ -84,7 +84,7 @@ export class ProcedureBuilder<
|
|
|
84
84
|
/**
|
|
85
85
|
* Database set name
|
|
86
86
|
*
|
|
87
|
-
* @param db - Database
|
|
87
|
+
* @param db - Database name
|
|
88
88
|
* @returns new ProcedureBuilder instance
|
|
89
89
|
*
|
|
90
90
|
* @example
|
|
@@ -101,7 +101,7 @@ export class ProcedureBuilder<
|
|
|
101
101
|
*
|
|
102
102
|
* MSSQL, PostgreSQLused in
|
|
103
103
|
*
|
|
104
|
-
* @param schema - Schema
|
|
104
|
+
* @param schema - Schema name (MSSQL: dbo, PostgreSQL: public)
|
|
105
105
|
* @returns new ProcedureBuilder instance
|
|
106
106
|
*/
|
|
107
107
|
schema(schema: string): ProcedureBuilder<TParams, TReturns> {
|
|
@@ -109,13 +109,13 @@ export class ProcedureBuilder<
|
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
/**
|
|
112
|
-
*
|
|
112
|
+
* Parameter definition
|
|
113
113
|
*
|
|
114
|
-
* Procedure
|
|
115
|
-
* DBMS
|
|
114
|
+
* Define Procedure input parameters
|
|
115
|
+
* Note DBMS-specific parameter syntax differences (MSSQL: @param, MySQL/PostgreSQL: param)
|
|
116
116
|
*
|
|
117
|
-
* @template T -
|
|
118
|
-
* @param fn - Column factory
|
|
117
|
+
* @template T - New parameter definition type
|
|
118
|
+
* @param fn - Function that receives a Column factory and returns parameter definitions
|
|
119
119
|
* @returns new ProcedureBuilder instance
|
|
120
120
|
*
|
|
121
121
|
* @example
|
|
@@ -134,12 +134,12 @@ export class ProcedureBuilder<
|
|
|
134
134
|
}
|
|
135
135
|
|
|
136
136
|
/**
|
|
137
|
-
*
|
|
137
|
+
* Return type definition
|
|
138
138
|
*
|
|
139
|
-
* Procedure return result
|
|
139
|
+
* Define Procedure return result columns
|
|
140
140
|
*
|
|
141
|
-
* @template T -
|
|
142
|
-
* @param fn - Column factory
|
|
141
|
+
* @template T - New return type definition
|
|
142
|
+
* @param fn - Function that receives a Column factory and returns Column definitions
|
|
143
143
|
* @returns new ProcedureBuilder instance
|
|
144
144
|
*
|
|
145
145
|
* @example
|
|
@@ -160,14 +160,14 @@ export class ProcedureBuilder<
|
|
|
160
160
|
}
|
|
161
161
|
|
|
162
162
|
/**
|
|
163
|
-
* Procedure
|
|
163
|
+
* Set Procedure body SQL
|
|
164
164
|
*
|
|
165
|
-
* DBMS
|
|
166
|
-
* - MySQL:
|
|
167
|
-
* - MSSQL: @
|
|
168
|
-
* - PostgreSQL: RETURN QUERY
|
|
165
|
+
* Note DBMS-specific SQL syntax differences:
|
|
166
|
+
* - MySQL: parameter name as-is (userId)
|
|
167
|
+
* - MSSQL: @ prefix (@userId)
|
|
168
|
+
* - PostgreSQL: RETURN QUERY required
|
|
169
169
|
*
|
|
170
|
-
* @param sql - Procedure
|
|
170
|
+
* @param sql - Procedure body SQL
|
|
171
171
|
* @returns new ProcedureBuilder instance
|
|
172
172
|
*
|
|
173
173
|
* @example
|
|
@@ -193,16 +193,16 @@ export class ProcedureBuilder<
|
|
|
193
193
|
// ============================================
|
|
194
194
|
|
|
195
195
|
/**
|
|
196
|
-
* Procedure builder
|
|
196
|
+
* Procedure builder factory function
|
|
197
197
|
*
|
|
198
|
-
* ProcedureBuilder
|
|
198
|
+
* Creates a ProcedureBuilder for defining stored Procedure schema via Fluent API
|
|
199
199
|
*
|
|
200
|
-
* @param name - Procedure
|
|
200
|
+
* @param name - Procedure name
|
|
201
201
|
* @returns ProcedureBuilder instance
|
|
202
202
|
*
|
|
203
203
|
* @example
|
|
204
204
|
* ```typescript
|
|
205
|
-
* // Basic
|
|
205
|
+
* // Basic usage
|
|
206
206
|
* const GetUserById = Procedure("GetUserById")
|
|
207
207
|
* .database("mydb")
|
|
208
208
|
* .params((c) => ({
|
|
@@ -215,7 +215,7 @@ export class ProcedureBuilder<
|
|
|
215
215
|
* }))
|
|
216
216
|
* .body("SELECT id, name, email FROM User WHERE id = userId");
|
|
217
217
|
*
|
|
218
|
-
* //
|
|
218
|
+
* // Procedure without parameters
|
|
219
219
|
* const GetAllActiveUsers = Procedure("GetAllActiveUsers")
|
|
220
220
|
* .database("mydb")
|
|
221
221
|
* .returns((c) => ({
|
|
@@ -19,11 +19,11 @@ import {
|
|
|
19
19
|
/**
|
|
20
20
|
* Database Table definition builder
|
|
21
21
|
*
|
|
22
|
-
*
|
|
23
|
-
* DbContext
|
|
22
|
+
* Define Table columns, PK, indexes, and relations via Fluent API
|
|
23
|
+
* Use with DbContext's queryable() for type-safe query composition
|
|
24
24
|
*
|
|
25
25
|
* @template TColumns - Column definition record type
|
|
26
|
-
* @template TRelations -
|
|
26
|
+
* @template TRelations - Relation definition record type
|
|
27
27
|
*
|
|
28
28
|
* @example
|
|
29
29
|
* ```typescript
|
|
@@ -39,7 +39,7 @@ import {
|
|
|
39
39
|
* .primaryKey("id")
|
|
40
40
|
* .indexes((i) => [i.index("email").unique()]);
|
|
41
41
|
*
|
|
42
|
-
* //
|
|
42
|
+
* // Used in DbContext
|
|
43
43
|
* class MyDb extends DbContext {
|
|
44
44
|
* readonly user = queryable(this, User);
|
|
45
45
|
* }
|
|
@@ -54,27 +54,27 @@ export class TableBuilder<
|
|
|
54
54
|
> {
|
|
55
55
|
/** Column definition (type for inference) */
|
|
56
56
|
readonly $columns!: TColumns;
|
|
57
|
-
/**
|
|
57
|
+
/** Relation definition (type for inference) */
|
|
58
58
|
readonly $relations!: TRelations;
|
|
59
59
|
|
|
60
|
-
/**
|
|
60
|
+
/** Full Type inference (column + relation) */
|
|
61
61
|
readonly $inferSelect!: InferColumns<TColumns> & InferDeepRelations<TRelations>;
|
|
62
|
-
/**
|
|
62
|
+
/** Column-only Type inference */
|
|
63
63
|
readonly $inferColumns!: InferColumns<TColumns>;
|
|
64
|
-
/** INSERT
|
|
64
|
+
/** INSERT Type inference (autoIncrement excluded, nullable/default are optional) */
|
|
65
65
|
readonly $inferInsert!: InferInsertColumns<TColumns>;
|
|
66
|
-
/** UPDATE
|
|
66
|
+
/** UPDATE Type inference (all fields optional) */
|
|
67
67
|
readonly $inferUpdate!: InferUpdateColumns<TColumns>;
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
70
|
* @param meta - Table Metadata
|
|
71
|
-
* @param meta.name - Table
|
|
72
|
-
* @param meta.description - Table description (
|
|
73
|
-
* @param meta.database - Database
|
|
74
|
-
* @param meta.schema - Schema
|
|
71
|
+
* @param meta.name - Table name
|
|
72
|
+
* @param meta.description - Table description (comment)
|
|
73
|
+
* @param meta.database - Database name
|
|
74
|
+
* @param meta.schema - Schema name (MSSQL/PostgreSQL)
|
|
75
75
|
* @param meta.columns - Column definition
|
|
76
76
|
* @param meta.primaryKey - PK column array
|
|
77
|
-
* @param meta.relations -
|
|
77
|
+
* @param meta.relations - Relation definition
|
|
78
78
|
* @param meta.indexes - Index definition
|
|
79
79
|
*/
|
|
80
80
|
constructor(
|
|
@@ -94,7 +94,7 @@ export class TableBuilder<
|
|
|
94
94
|
/**
|
|
95
95
|
* Table set description
|
|
96
96
|
*
|
|
97
|
-
* @param desc - Table description (DDL Comment
|
|
97
|
+
* @param desc - Table description (used as DDL Comment)
|
|
98
98
|
* @returns new TableBuilder instance
|
|
99
99
|
*/
|
|
100
100
|
description(desc: string) {
|
|
@@ -104,7 +104,7 @@ export class TableBuilder<
|
|
|
104
104
|
/**
|
|
105
105
|
* Database set name
|
|
106
106
|
*
|
|
107
|
-
* @param db - Database
|
|
107
|
+
* @param db - Database name
|
|
108
108
|
* @returns new TableBuilder instance
|
|
109
109
|
*
|
|
110
110
|
* @example
|
|
@@ -119,9 +119,9 @@ export class TableBuilder<
|
|
|
119
119
|
/**
|
|
120
120
|
* schema set name
|
|
121
121
|
*
|
|
122
|
-
* MSSQL,
|
|
122
|
+
* Used in MSSQL, PostgreSQL
|
|
123
123
|
*
|
|
124
|
-
* @param schema - Schema
|
|
124
|
+
* @param schema - Schema name (MSSQL: dbo, PostgreSQL: public)
|
|
125
125
|
* @returns new TableBuilder instance
|
|
126
126
|
*
|
|
127
127
|
* @example
|
|
@@ -138,10 +138,10 @@ export class TableBuilder<
|
|
|
138
138
|
/**
|
|
139
139
|
* Column definition
|
|
140
140
|
*
|
|
141
|
-
*
|
|
141
|
+
* Type-safe Column definition through column factory
|
|
142
142
|
*
|
|
143
|
-
* @template TNewColumnDefs -
|
|
144
|
-
* @param fn - Column factory
|
|
143
|
+
* @template TNewColumnDefs - New Column definition type
|
|
144
|
+
* @param fn - Function that receives a Column factory and returns Column definitions
|
|
145
145
|
* @returns new TableBuilder instance
|
|
146
146
|
*
|
|
147
147
|
* @example
|
|
@@ -165,19 +165,19 @@ export class TableBuilder<
|
|
|
165
165
|
}
|
|
166
166
|
|
|
167
167
|
/**
|
|
168
|
-
* Primary Key
|
|
168
|
+
* Primary Key configuration
|
|
169
169
|
*
|
|
170
|
-
* @param columns -
|
|
170
|
+
* @param columns - Column names composing the PK (composite PK supported)
|
|
171
171
|
* @returns new TableBuilder instance
|
|
172
172
|
*
|
|
173
173
|
* @example
|
|
174
174
|
* ```typescript
|
|
175
|
-
* //
|
|
175
|
+
* // Single PK
|
|
176
176
|
* const User = Table("User")
|
|
177
177
|
* .columns((c) => ({ id: c.bigint() }))
|
|
178
178
|
* .primaryKey("id");
|
|
179
179
|
*
|
|
180
|
-
* //
|
|
180
|
+
* // Composite PK
|
|
181
181
|
* const UserRole = Table("UserRole")
|
|
182
182
|
* .columns((c) => ({
|
|
183
183
|
* userId: c.bigint(),
|
|
@@ -196,7 +196,7 @@ export class TableBuilder<
|
|
|
196
196
|
/**
|
|
197
197
|
* Index definition
|
|
198
198
|
*
|
|
199
|
-
* @param fn - Index factory
|
|
199
|
+
* @param fn - Function that receives an Index factory and returns an Index array
|
|
200
200
|
* @returns new TableBuilder instance
|
|
201
201
|
*
|
|
202
202
|
* @example
|
|
@@ -225,12 +225,12 @@ export class TableBuilder<
|
|
|
225
225
|
}
|
|
226
226
|
|
|
227
227
|
/**
|
|
228
|
-
*
|
|
228
|
+
* Relation definition
|
|
229
229
|
*
|
|
230
|
-
* FK,
|
|
230
|
+
* Set up FK, reverse-references, and other inter-Table relations
|
|
231
231
|
*
|
|
232
|
-
* @template T -
|
|
233
|
-
* @param fn -
|
|
232
|
+
* @template T - Relation definition type
|
|
233
|
+
* @param fn - Function that receives a relation factory and returns relation definitions
|
|
234
234
|
* @returns new TableBuilder instance
|
|
235
235
|
*
|
|
236
236
|
* @example
|
|
@@ -243,7 +243,7 @@ export class TableBuilder<
|
|
|
243
243
|
* }))
|
|
244
244
|
* .primaryKey("id")
|
|
245
245
|
* .relations((r) => ({
|
|
246
|
-
* // FK
|
|
246
|
+
* // FK relation (N:1)
|
|
247
247
|
* author: r.foreignKey(["authorId"], () => User),
|
|
248
248
|
* }));
|
|
249
249
|
*
|
|
@@ -254,13 +254,13 @@ export class TableBuilder<
|
|
|
254
254
|
* }))
|
|
255
255
|
* .primaryKey("id")
|
|
256
256
|
* .relations((r) => ({
|
|
257
|
-
* //
|
|
257
|
+
* // Reverse-reference (1:N)
|
|
258
258
|
* posts: r.foreignKeyTarget(() => Post, "author"),
|
|
259
259
|
* }));
|
|
260
260
|
* ```
|
|
261
261
|
*
|
|
262
262
|
* @see {@link ForeignKeyBuilder} FK builder
|
|
263
|
-
* @see {@link ForeignKeyTargetBuilder} FK
|
|
263
|
+
* @see {@link ForeignKeyTargetBuilder} FK reverse-reference builder
|
|
264
264
|
*/
|
|
265
265
|
relations<T extends RelationBuilderRecord>(
|
|
266
266
|
fn: (r: ReturnType<typeof createRelationFactory<this, keyof TColumns & string>>) => T,
|
|
@@ -277,16 +277,16 @@ export class TableBuilder<
|
|
|
277
277
|
// ============================================
|
|
278
278
|
|
|
279
279
|
/**
|
|
280
|
-
* Table builder
|
|
280
|
+
* Table builder factory function
|
|
281
281
|
*
|
|
282
|
-
* TableBuilder
|
|
282
|
+
* Creates a TableBuilder for defining Table schema via Fluent API
|
|
283
283
|
*
|
|
284
|
-
* @param name - Table
|
|
284
|
+
* @param name - Table name
|
|
285
285
|
* @returns TableBuilder instance
|
|
286
286
|
*
|
|
287
287
|
* @example
|
|
288
288
|
* ```typescript
|
|
289
|
-
* // Basic
|
|
289
|
+
* // Basic usage
|
|
290
290
|
* const User = Table("User")
|
|
291
291
|
* .database("mydb")
|
|
292
292
|
* .columns((c) => ({
|
|
@@ -297,7 +297,7 @@ export class TableBuilder<
|
|
|
297
297
|
* .primaryKey("id")
|
|
298
298
|
* .indexes((i) => [i.index("email").unique()]);
|
|
299
299
|
*
|
|
300
|
-
* //
|
|
300
|
+
* // With relations
|
|
301
301
|
* const Post = Table("Post")
|
|
302
302
|
* .database("mydb")
|
|
303
303
|
* .columns((c) => ({
|
|
@@ -14,12 +14,12 @@ import {
|
|
|
14
14
|
/**
|
|
15
15
|
* Database View definition builder
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
* DbContext
|
|
17
|
+
* Define View query and relations via Fluent API
|
|
18
|
+
* Use with DbContext's queryable() for type-safe query composition
|
|
19
19
|
*
|
|
20
20
|
* @template TDbContext - DbContext type
|
|
21
21
|
* @template TData - View data record type
|
|
22
|
-
* @template TRelations -
|
|
22
|
+
* @template TRelations - Relation definition record type
|
|
23
23
|
*
|
|
24
24
|
* @example
|
|
25
25
|
* ```typescript
|
|
@@ -38,7 +38,7 @@ import {
|
|
|
38
38
|
* }))
|
|
39
39
|
* );
|
|
40
40
|
*
|
|
41
|
-
* //
|
|
41
|
+
* // Used in DbContext
|
|
42
42
|
* class MyDb extends DbContext {
|
|
43
43
|
* readonly userSummary = queryable(this, UserSummary);
|
|
44
44
|
* }
|
|
@@ -52,19 +52,19 @@ export class ViewBuilder<
|
|
|
52
52
|
TData extends DataRecord,
|
|
53
53
|
TRelations extends RelationBuilderRecord,
|
|
54
54
|
> {
|
|
55
|
-
/**
|
|
55
|
+
/** Relation definition (type for inference) */
|
|
56
56
|
readonly $relations!: TRelations;
|
|
57
|
-
/**
|
|
57
|
+
/** Full Type inference */
|
|
58
58
|
readonly $inferSelect!: TData;
|
|
59
59
|
|
|
60
60
|
/**
|
|
61
61
|
* @param meta - View Metadata
|
|
62
|
-
* @param meta.name - View
|
|
63
|
-
* @param meta.description - View description (
|
|
64
|
-
* @param meta.database - Database
|
|
65
|
-
* @param meta.schema - Schema
|
|
62
|
+
* @param meta.name - View name
|
|
63
|
+
* @param meta.description - View description (comment)
|
|
64
|
+
* @param meta.database - Database name
|
|
65
|
+
* @param meta.schema - Schema name (MSSQL/PostgreSQL)
|
|
66
66
|
* @param meta.viewFn - View Query definition function
|
|
67
|
-
* @param meta.relations -
|
|
67
|
+
* @param meta.relations - Relation definition
|
|
68
68
|
*/
|
|
69
69
|
constructor(
|
|
70
70
|
readonly meta: {
|
|
@@ -80,7 +80,7 @@ export class ViewBuilder<
|
|
|
80
80
|
/**
|
|
81
81
|
* View set description
|
|
82
82
|
*
|
|
83
|
-
* @param desc - View description (DDL Comment
|
|
83
|
+
* @param desc - View description (used as DDL Comment)
|
|
84
84
|
* @returns new ViewBuilder instance
|
|
85
85
|
*/
|
|
86
86
|
description(desc: string): ViewBuilder<TDbContext, TData, TRelations> {
|
|
@@ -90,7 +90,7 @@ export class ViewBuilder<
|
|
|
90
90
|
/**
|
|
91
91
|
* Database set name
|
|
92
92
|
*
|
|
93
|
-
* @param db - Database
|
|
93
|
+
* @param db - Database name
|
|
94
94
|
* @returns new ViewBuilder instance
|
|
95
95
|
*
|
|
96
96
|
* @example
|
|
@@ -105,9 +105,9 @@ export class ViewBuilder<
|
|
|
105
105
|
/**
|
|
106
106
|
* schema set name
|
|
107
107
|
*
|
|
108
|
-
* MSSQL,
|
|
108
|
+
* Used in MSSQL, PostgreSQL
|
|
109
109
|
*
|
|
110
|
-
* @param schema - Schema
|
|
110
|
+
* @param schema - Schema name (MSSQL: dbo, PostgreSQL: public)
|
|
111
111
|
* @returns new ViewBuilder instance
|
|
112
112
|
*/
|
|
113
113
|
schema(schema: string): ViewBuilder<TDbContext, TData, TRelations> {
|
|
@@ -117,11 +117,11 @@ export class ViewBuilder<
|
|
|
117
117
|
/**
|
|
118
118
|
* View Query definition
|
|
119
119
|
*
|
|
120
|
-
*
|
|
120
|
+
* Define the View's data source through a SELECT query
|
|
121
121
|
*
|
|
122
122
|
* @template TViewData - View data type
|
|
123
123
|
* @template TDb - DbContext type
|
|
124
|
-
* @param viewFn - DbContext
|
|
124
|
+
* @param viewFn - Function that receives a DbContext and returns a Queryable
|
|
125
125
|
* @returns new ViewBuilder instance
|
|
126
126
|
*
|
|
127
127
|
* @example
|
|
@@ -146,12 +146,12 @@ export class ViewBuilder<
|
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
/**
|
|
149
|
-
*
|
|
149
|
+
* Relation definition
|
|
150
150
|
*
|
|
151
|
-
*
|
|
151
|
+
* Set up relations with other Tables/Views
|
|
152
152
|
*
|
|
153
|
-
* @template T -
|
|
154
|
-
* @param fn -
|
|
153
|
+
* @template T - Relation definition type
|
|
154
|
+
* @param fn - Function that receives a relation factory and returns relation definitions
|
|
155
155
|
* @returns new ViewBuilder instance
|
|
156
156
|
*
|
|
157
157
|
* @example
|
|
@@ -164,13 +164,13 @@ export class ViewBuilder<
|
|
|
164
164
|
* ```
|
|
165
165
|
*
|
|
166
166
|
* @see {@link ForeignKeyBuilder} FK builder
|
|
167
|
-
* @see {@link ForeignKeyTargetBuilder} FK
|
|
167
|
+
* @see {@link ForeignKeyTargetBuilder} FK reverse-reference builder
|
|
168
168
|
*/
|
|
169
169
|
relations<T extends RelationBuilderRecord>(
|
|
170
170
|
fn: (r: ReturnType<typeof createRelationFactory<this, keyof TData & string>>) => T,
|
|
171
171
|
): ViewBuilder<TDbContext, TData & InferDeepRelations<T>, TRelations> {
|
|
172
|
-
// TypeScript
|
|
173
|
-
//
|
|
172
|
+
// Casting is unavoidable due to TypeScript generic type inference limitations
|
|
173
|
+
// Resolves type mismatch between TRelations type parameter and newly created relation type T
|
|
174
174
|
return new ViewBuilder({
|
|
175
175
|
...this.meta,
|
|
176
176
|
relations: fn(createRelationFactory<this, keyof TData & string>(() => this)),
|
|
@@ -183,16 +183,16 @@ export class ViewBuilder<
|
|
|
183
183
|
// ============================================
|
|
184
184
|
|
|
185
185
|
/**
|
|
186
|
-
* View builder
|
|
186
|
+
* View builder factory function
|
|
187
187
|
*
|
|
188
|
-
* ViewBuilder
|
|
188
|
+
* Creates a ViewBuilder for defining View schema via Fluent API
|
|
189
189
|
*
|
|
190
|
-
* @param name - View
|
|
190
|
+
* @param name - View name
|
|
191
191
|
* @returns ViewBuilder instance
|
|
192
192
|
*
|
|
193
193
|
* @example
|
|
194
194
|
* ```typescript
|
|
195
|
-
* // Basic
|
|
195
|
+
* // Basic usage
|
|
196
196
|
* const ActiveUsers = View("ActiveUsers")
|
|
197
197
|
* .database("mydb")
|
|
198
198
|
* .query((db: MyDb) =>
|