@simplysm/orm-common 13.0.82 → 13.0.84
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 +106 -0
- 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/docs/db-context.md +238 -0
- package/docs/expressions.md +413 -0
- package/docs/query-builder.md +198 -0
- package/docs/queryable.md +420 -0
- package/docs/schema-builders.md +216 -0
- package/docs/types-and-utilities.md +353 -0
- package/package.json +4 -3
- 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,8 +5,8 @@
|
|
|
5
5
|
/**
|
|
6
6
|
* Index definition builder
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
* TableBuilder.indexes()
|
|
8
|
+
* Define Index columns, uniqueness, and sort order via Fluent API
|
|
9
|
+
* Used in TableBuilder.indexes()
|
|
10
10
|
*
|
|
11
11
|
* @template TKeys - Index column key array type
|
|
12
12
|
*
|
|
@@ -20,13 +20,13 @@
|
|
|
20
20
|
* createdAt: c.datetime(),
|
|
21
21
|
* }))
|
|
22
22
|
* .indexes((i) => [
|
|
23
|
-
* //
|
|
23
|
+
* // Unique index
|
|
24
24
|
* i.index("email").unique(),
|
|
25
25
|
*
|
|
26
|
-
* //
|
|
26
|
+
* // Composite index + sort order
|
|
27
27
|
* i.index("name", "createdAt").orderBy("ASC", "DESC"),
|
|
28
28
|
*
|
|
29
|
-
* //
|
|
29
|
+
* // Custom name
|
|
30
30
|
* i.index("createdAt").name("IX_User_CreatedAt"),
|
|
31
31
|
* ]);
|
|
32
32
|
* ```
|
|
@@ -38,9 +38,9 @@ export class IndexBuilder<TKeys extends string[]> {
|
|
|
38
38
|
/**
|
|
39
39
|
* @param meta - Index Metadata
|
|
40
40
|
* @param meta.columns - Index column array
|
|
41
|
-
* @param meta.name - Index
|
|
42
|
-
* @param meta.unique -
|
|
43
|
-
* @param meta.orderBy -
|
|
41
|
+
* @param meta.name - Index name (optional)
|
|
42
|
+
* @param meta.unique - Whether it is a unique index
|
|
43
|
+
* @param meta.orderBy - Sort order per column
|
|
44
44
|
* @param meta.description - Index description
|
|
45
45
|
*/
|
|
46
46
|
constructor(
|
|
@@ -56,7 +56,7 @@ export class IndexBuilder<TKeys extends string[]> {
|
|
|
56
56
|
/**
|
|
57
57
|
* Index set name
|
|
58
58
|
*
|
|
59
|
-
* @param name - Index
|
|
59
|
+
* @param name - Index name
|
|
60
60
|
* @returns new IndexBuilder instance
|
|
61
61
|
*
|
|
62
62
|
* @example
|
|
@@ -69,7 +69,7 @@ export class IndexBuilder<TKeys extends string[]> {
|
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
|
-
*
|
|
72
|
+
* Unique index configuration
|
|
73
73
|
*
|
|
74
74
|
* @returns new IndexBuilder instance
|
|
75
75
|
*
|
|
@@ -83,19 +83,19 @@ export class IndexBuilder<TKeys extends string[]> {
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
|
-
*
|
|
86
|
+
* Sort order configuration
|
|
87
87
|
*
|
|
88
|
-
*
|
|
88
|
+
* Specify ASC or DESC for each column
|
|
89
89
|
*
|
|
90
|
-
* @param orderBy -
|
|
90
|
+
* @param orderBy - Sort order per column (must match the number of columns)
|
|
91
91
|
* @returns new IndexBuilder instance
|
|
92
92
|
*
|
|
93
93
|
* @example
|
|
94
94
|
* ```typescript
|
|
95
|
-
* //
|
|
95
|
+
* // Single column
|
|
96
96
|
* i.index("createdAt").orderBy("DESC")
|
|
97
97
|
*
|
|
98
|
-
* //
|
|
98
|
+
* // Composite column
|
|
99
99
|
* i.index("status", "createdAt").orderBy("ASC", "DESC")
|
|
100
100
|
* ```
|
|
101
101
|
*/
|
|
@@ -106,7 +106,7 @@ export class IndexBuilder<TKeys extends string[]> {
|
|
|
106
106
|
/**
|
|
107
107
|
* Index set description
|
|
108
108
|
*
|
|
109
|
-
* @param description - Index description (DDL Comment
|
|
109
|
+
* @param description - Index description (used as DDL Comment)
|
|
110
110
|
* @returns new IndexBuilder instance
|
|
111
111
|
*/
|
|
112
112
|
description(description: string): IndexBuilder<TKeys> {
|
|
@@ -119,12 +119,12 @@ export class IndexBuilder<TKeys extends string[]> {
|
|
|
119
119
|
// ============================================
|
|
120
120
|
|
|
121
121
|
/**
|
|
122
|
-
* Index builder factory
|
|
122
|
+
* Index builder factory creation
|
|
123
123
|
*
|
|
124
|
-
* TableBuilder.indexes()
|
|
124
|
+
* Index factory used in TableBuilder.indexes()
|
|
125
125
|
*
|
|
126
126
|
* @template TColumnKey - Table column key type
|
|
127
|
-
* @returns
|
|
127
|
+
* @returns Object containing Index creation methods
|
|
128
128
|
*
|
|
129
129
|
* @example
|
|
130
130
|
* ```typescript
|
|
@@ -148,13 +148,13 @@ export function createIndexFactory<TColumnKey extends string>() {
|
|
|
148
148
|
* Index Generate
|
|
149
149
|
*
|
|
150
150
|
* @template TKeys - Index column key array type
|
|
151
|
-
* @param columns - Index column
|
|
151
|
+
* @param columns - Index column names
|
|
152
152
|
* @returns IndexBuilder instance
|
|
153
153
|
*
|
|
154
154
|
* @example
|
|
155
155
|
* ```typescript
|
|
156
|
-
* i.index("email") //
|
|
157
|
-
* i.index("name", "email") //
|
|
156
|
+
* i.index("email") // Single column
|
|
157
|
+
* i.index("name", "email") // Composite column
|
|
158
158
|
* ```
|
|
159
159
|
*/
|
|
160
160
|
index<TKeys extends TColumnKey[]>(...columns: [...TKeys]): IndexBuilder<TKeys> {
|
|
@@ -7,13 +7,13 @@ import type { ViewBuilder } from "../view-builder";
|
|
|
7
7
|
// ============================================
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
* Foreign Key
|
|
10
|
+
* Foreign Key relation builder (N:1)
|
|
11
11
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
12
|
+
* Define FK relation from current Table to target Table
|
|
13
|
+
* Creates actual FK constraint in the DB
|
|
14
14
|
*
|
|
15
|
-
* @template TOwner -
|
|
16
|
-
* @template TTargetFn -
|
|
15
|
+
* @template TOwner - Owner Table builder type
|
|
16
|
+
* @template TTargetFn - Target Table builder factory type
|
|
17
17
|
*
|
|
18
18
|
* @example
|
|
19
19
|
* ```typescript
|
|
@@ -29,8 +29,8 @@ import type { ViewBuilder } from "../view-builder";
|
|
|
29
29
|
* }));
|
|
30
30
|
* ```
|
|
31
31
|
*
|
|
32
|
-
* @see {@link ForeignKeyTargetBuilder}
|
|
33
|
-
* @see {@link RelationKeyBuilder} DB FK
|
|
32
|
+
* @see {@link ForeignKeyTargetBuilder} Reverse-reference builder
|
|
33
|
+
* @see {@link RelationKeyBuilder} Relation without DB FK
|
|
34
34
|
*/
|
|
35
35
|
export class ForeignKeyBuilder<
|
|
36
36
|
TOwner extends TableBuilder<any, any>,
|
|
@@ -38,10 +38,10 @@ export class ForeignKeyBuilder<
|
|
|
38
38
|
> {
|
|
39
39
|
/**
|
|
40
40
|
* @param meta - FK Metadata
|
|
41
|
-
* @param meta.ownerFn -
|
|
42
|
-
* @param meta.columns - FK column
|
|
43
|
-
* @param meta.targetFn -
|
|
44
|
-
* @param meta.description -
|
|
41
|
+
* @param meta.ownerFn - Owner Table factory
|
|
42
|
+
* @param meta.columns - FK column name array
|
|
43
|
+
* @param meta.targetFn - Target Table factory
|
|
44
|
+
* @param meta.description - Relation description
|
|
45
45
|
*/
|
|
46
46
|
constructor(
|
|
47
47
|
readonly meta: {
|
|
@@ -53,10 +53,10 @@ export class ForeignKeyBuilder<
|
|
|
53
53
|
) {}
|
|
54
54
|
|
|
55
55
|
/**
|
|
56
|
-
*
|
|
56
|
+
* Set relation description
|
|
57
57
|
*
|
|
58
|
-
* @param desc -
|
|
59
|
-
* @returns
|
|
58
|
+
* @param desc - Relation description
|
|
59
|
+
* @returns New ForeignKeyBuilder instance
|
|
60
60
|
*/
|
|
61
61
|
description(desc: string): ForeignKeyBuilder<TOwner, TTargetFn> {
|
|
62
62
|
return new ForeignKeyBuilder({ ...this.meta, description: desc });
|
|
@@ -64,13 +64,13 @@ export class ForeignKeyBuilder<
|
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
|
-
* Foreign Key
|
|
67
|
+
* Foreign Key reverse-reference builder (1:N)
|
|
68
68
|
*
|
|
69
|
-
*
|
|
70
|
-
* include()
|
|
69
|
+
* Define reverse-reference for FK from another Table referencing current Table
|
|
70
|
+
* Loaded as array on include() (single object when single() is called)
|
|
71
71
|
*
|
|
72
|
-
* @template TTargetTableFn -
|
|
73
|
-
* @template TIsSingle -
|
|
72
|
+
* @template TTargetTableFn - Referencing Table builder factory type
|
|
73
|
+
* @template TIsSingle - Whether it is a single object
|
|
74
74
|
*
|
|
75
75
|
* @example
|
|
76
76
|
* ```typescript
|
|
@@ -84,7 +84,7 @@ export class ForeignKeyBuilder<
|
|
|
84
84
|
* // 1:N relationship - User ← Post.author
|
|
85
85
|
* posts: r.foreignKeyTarget(() => Post, "author"),
|
|
86
86
|
*
|
|
87
|
-
* // 1:1
|
|
87
|
+
* // 1:1 relation (single object)
|
|
88
88
|
* profile: r.foreignKeyTarget(() => Profile, "user").single(),
|
|
89
89
|
* }));
|
|
90
90
|
* ```
|
|
@@ -96,11 +96,11 @@ export class ForeignKeyTargetBuilder<
|
|
|
96
96
|
TIsSingle extends boolean,
|
|
97
97
|
> {
|
|
98
98
|
/**
|
|
99
|
-
* @param meta - FK
|
|
100
|
-
* @param meta.targetTableFn -
|
|
101
|
-
* @param meta.relationName -
|
|
102
|
-
* @param meta.description -
|
|
103
|
-
* @param meta.isSingle -
|
|
99
|
+
* @param meta - FK reverse-reference Metadata
|
|
100
|
+
* @param meta.targetTableFn - Referencing Table factory
|
|
101
|
+
* @param meta.relationName - FK relation name of the referencing Table
|
|
102
|
+
* @param meta.description - Relation description
|
|
103
|
+
* @param meta.isSingle - Whether it is a single object
|
|
104
104
|
*/
|
|
105
105
|
constructor(
|
|
106
106
|
readonly meta: {
|
|
@@ -112,21 +112,21 @@ export class ForeignKeyTargetBuilder<
|
|
|
112
112
|
) {}
|
|
113
113
|
|
|
114
114
|
/**
|
|
115
|
-
*
|
|
115
|
+
* Set relation description
|
|
116
116
|
*
|
|
117
|
-
* @param desc -
|
|
118
|
-
* @returns
|
|
117
|
+
* @param desc - Relation description
|
|
118
|
+
* @returns New ForeignKeyTargetBuilder instance
|
|
119
119
|
*/
|
|
120
120
|
description(desc: string): ForeignKeyTargetBuilder<TTargetTableFn, TIsSingle> {
|
|
121
121
|
return new ForeignKeyTargetBuilder({ ...this.meta, description: desc });
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
/**
|
|
125
|
-
*
|
|
125
|
+
* Set as single object relation (1:1)
|
|
126
126
|
*
|
|
127
|
-
*
|
|
127
|
+
* Default is array (1:N), single object when single() is called
|
|
128
128
|
*
|
|
129
|
-
* @returns
|
|
129
|
+
* @returns New ForeignKeyTargetBuilder instance (isSingle=true)
|
|
130
130
|
*
|
|
131
131
|
* @example
|
|
132
132
|
* ```typescript
|
|
@@ -139,29 +139,29 @@ export class ForeignKeyTargetBuilder<
|
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
// ============================================
|
|
142
|
-
// RelationKeyBuilder (FK
|
|
142
|
+
// RelationKeyBuilder (same as FK but does not register FK in DB)
|
|
143
143
|
// ============================================
|
|
144
144
|
|
|
145
145
|
/**
|
|
146
|
-
*
|
|
146
|
+
* Logical relation builder (N:1) - No DB FK creation
|
|
147
147
|
*
|
|
148
|
-
* ForeignKeyBuilder
|
|
149
|
-
*
|
|
148
|
+
* Same as ForeignKeyBuilder but does not create FK constraints in the DB
|
|
149
|
+
* Can also be used in Views
|
|
150
150
|
*
|
|
151
|
-
* @template TOwner -
|
|
152
|
-
* @template TTargetFn -
|
|
151
|
+
* @template TOwner - Owner Table/View builder type
|
|
152
|
+
* @template TTargetFn - Target Table/View builder factory type
|
|
153
153
|
*
|
|
154
154
|
* @example
|
|
155
155
|
* ```typescript
|
|
156
|
-
* // View
|
|
156
|
+
* // Relation definition from View to Table
|
|
157
157
|
* const UserSummary = View("UserSummary")
|
|
158
158
|
* .query((db: MyDb) => db.user().select(...))
|
|
159
159
|
* .relations((r) => ({
|
|
160
|
-
* // View → Table (FK
|
|
160
|
+
* // View → Table (no FK creation)
|
|
161
161
|
* company: r.relationKey(["companyId"], () => Company),
|
|
162
162
|
* }));
|
|
163
163
|
*
|
|
164
|
-
* //
|
|
164
|
+
* // Relation definition from Table without FK
|
|
165
165
|
* const Report = Table("Report")
|
|
166
166
|
* .columns((c) => ({ userId: c.bigint() }))
|
|
167
167
|
* .relations((r) => ({
|
|
@@ -169,18 +169,18 @@ export class ForeignKeyTargetBuilder<
|
|
|
169
169
|
* }));
|
|
170
170
|
* ```
|
|
171
171
|
*
|
|
172
|
-
* @see {@link ForeignKeyBuilder} DB FK
|
|
172
|
+
* @see {@link ForeignKeyBuilder} DB FK creation version
|
|
173
173
|
*/
|
|
174
174
|
export class RelationKeyBuilder<
|
|
175
175
|
TOwner extends TableBuilder<any, any> | ViewBuilder<any, any, any>,
|
|
176
176
|
TTargetFn extends () => TableBuilder<any, any> | ViewBuilder<any, any, any>,
|
|
177
177
|
> {
|
|
178
178
|
/**
|
|
179
|
-
* @param meta -
|
|
180
|
-
* @param meta.ownerFn -
|
|
181
|
-
* @param meta.columns -
|
|
182
|
-
* @param meta.targetFn -
|
|
183
|
-
* @param meta.description -
|
|
179
|
+
* @param meta - Relation Metadata
|
|
180
|
+
* @param meta.ownerFn - Owner Table/View factory
|
|
181
|
+
* @param meta.columns - Relation column name array
|
|
182
|
+
* @param meta.targetFn - Target Table/View factory
|
|
183
|
+
* @param meta.description - Relation description
|
|
184
184
|
*/
|
|
185
185
|
constructor(
|
|
186
186
|
readonly meta: {
|
|
@@ -192,10 +192,10 @@ export class RelationKeyBuilder<
|
|
|
192
192
|
) {}
|
|
193
193
|
|
|
194
194
|
/**
|
|
195
|
-
*
|
|
195
|
+
* Set relation description
|
|
196
196
|
*
|
|
197
|
-
* @param desc -
|
|
198
|
-
* @returns
|
|
197
|
+
* @param desc - Relation description
|
|
198
|
+
* @returns New RelationKeyBuilder instance
|
|
199
199
|
*/
|
|
200
200
|
description(desc: string): RelationKeyBuilder<TOwner, TTargetFn> {
|
|
201
201
|
return new RelationKeyBuilder({ ...this.meta, description: desc });
|
|
@@ -203,36 +203,36 @@ export class RelationKeyBuilder<
|
|
|
203
203
|
}
|
|
204
204
|
|
|
205
205
|
/**
|
|
206
|
-
*
|
|
206
|
+
* Logical relation reverse-reference builder (1:N) - No DB FK creation
|
|
207
207
|
*
|
|
208
|
-
* ForeignKeyTargetBuilder
|
|
209
|
-
*
|
|
208
|
+
* Same as ForeignKeyTargetBuilder but does not create FK constraints in the DB
|
|
209
|
+
* Can also be used in Views
|
|
210
210
|
*
|
|
211
|
-
* @template TTargetTableFn -
|
|
212
|
-
* @template TIsSingle -
|
|
211
|
+
* @template TTargetTableFn - Referencing Table/View builder factory type
|
|
212
|
+
* @template TIsSingle - Whether it is a single object
|
|
213
213
|
*
|
|
214
214
|
* @example
|
|
215
215
|
* ```typescript
|
|
216
216
|
* const Company = Table("Company")
|
|
217
217
|
* .columns((c) => ({ id: c.bigint() }))
|
|
218
218
|
* .relations((r) => ({
|
|
219
|
-
* //
|
|
219
|
+
* // Reverse-reference (no FK creation)
|
|
220
220
|
* employees: r.relationKeyTarget(() => UserSummary, "company"),
|
|
221
221
|
* }));
|
|
222
222
|
* ```
|
|
223
223
|
*
|
|
224
|
-
* @see {@link ForeignKeyTargetBuilder} DB FK
|
|
224
|
+
* @see {@link ForeignKeyTargetBuilder} DB FK creation version
|
|
225
225
|
*/
|
|
226
226
|
export class RelationKeyTargetBuilder<
|
|
227
227
|
TTargetTableFn extends () => TableBuilder<any, any> | ViewBuilder<any, any, any>,
|
|
228
228
|
TIsSingle extends boolean,
|
|
229
229
|
> {
|
|
230
230
|
/**
|
|
231
|
-
* @param meta -
|
|
232
|
-
* @param meta.targetTableFn -
|
|
233
|
-
* @param meta.relationName -
|
|
234
|
-
* @param meta.description -
|
|
235
|
-
* @param meta.isSingle -
|
|
231
|
+
* @param meta - Relation reverse-reference Metadata
|
|
232
|
+
* @param meta.targetTableFn - Referencing Table/View factory
|
|
233
|
+
* @param meta.relationName - Relation name of the referencing Table/View
|
|
234
|
+
* @param meta.description - Relation description
|
|
235
|
+
* @param meta.isSingle - Whether it is a single object
|
|
236
236
|
*/
|
|
237
237
|
constructor(
|
|
238
238
|
readonly meta: {
|
|
@@ -244,21 +244,21 @@ export class RelationKeyTargetBuilder<
|
|
|
244
244
|
) {}
|
|
245
245
|
|
|
246
246
|
/**
|
|
247
|
-
*
|
|
247
|
+
* Set relation description
|
|
248
248
|
*
|
|
249
|
-
* @param desc -
|
|
250
|
-
* @returns
|
|
249
|
+
* @param desc - Relation description
|
|
250
|
+
* @returns New RelationKeyTargetBuilder instance
|
|
251
251
|
*/
|
|
252
252
|
description(desc: string): RelationKeyTargetBuilder<TTargetTableFn, TIsSingle> {
|
|
253
253
|
return new RelationKeyTargetBuilder({ ...this.meta, description: desc });
|
|
254
254
|
}
|
|
255
255
|
|
|
256
256
|
/**
|
|
257
|
-
*
|
|
257
|
+
* Set as single object relation (1:1)
|
|
258
258
|
*
|
|
259
|
-
*
|
|
259
|
+
* Default is array (1:N), single object when single() is called
|
|
260
260
|
*
|
|
261
|
-
* @returns
|
|
261
|
+
* @returns New RelationKeyTargetBuilder instance (isSingle=true)
|
|
262
262
|
*/
|
|
263
263
|
single(): RelationKeyTargetBuilder<TTargetTableFn, true> {
|
|
264
264
|
return new RelationKeyTargetBuilder({ ...this.meta, isSingle: true });
|
|
@@ -266,9 +266,9 @@ export class RelationKeyTargetBuilder<
|
|
|
266
266
|
}
|
|
267
267
|
|
|
268
268
|
/**
|
|
269
|
-
* FK
|
|
269
|
+
* FK relation factory type (table only)
|
|
270
270
|
*
|
|
271
|
-
* @template TOwner -
|
|
271
|
+
* @template TOwner - Owner Table builder type
|
|
272
272
|
* @template TColumnKey - Column key type
|
|
273
273
|
*/
|
|
274
274
|
type RelationFkFactory<TOwner extends TableBuilder<any, any>, TColumnKey extends string> = {
|
|
@@ -277,7 +277,7 @@ type RelationFkFactory<TOwner extends TableBuilder<any, any>, TColumnKey extends
|
|
|
277
277
|
columns: TColumnKey[],
|
|
278
278
|
targetFn: TTargetFn,
|
|
279
279
|
): ForeignKeyBuilder<TOwner, TTargetFn>;
|
|
280
|
-
/** 1:N FK
|
|
280
|
+
/** 1:N FK reverse-reference definition */
|
|
281
281
|
foreignKeyTarget<TTargetTableFn extends () => TableBuilder<any, any>>(
|
|
282
282
|
targetTableFn: TTargetTableFn,
|
|
283
283
|
relationName: string,
|
|
@@ -285,21 +285,21 @@ type RelationFkFactory<TOwner extends TableBuilder<any, any>, TColumnKey extends
|
|
|
285
285
|
};
|
|
286
286
|
|
|
287
287
|
/**
|
|
288
|
-
*
|
|
288
|
+
* Logical relation factory type (shared for table/View)
|
|
289
289
|
*
|
|
290
|
-
* @template TOwner -
|
|
290
|
+
* @template TOwner - Owner Table/View builder type
|
|
291
291
|
* @template TColumnKey - Column key type
|
|
292
292
|
*/
|
|
293
293
|
type RelationRkFactory<
|
|
294
294
|
TOwner extends TableBuilder<any, any> | ViewBuilder<any, any, any>,
|
|
295
295
|
TColumnKey extends string,
|
|
296
296
|
> = {
|
|
297
|
-
/** N:1
|
|
297
|
+
/** N:1 logical relation definition (no DB FK creation) */
|
|
298
298
|
relationKey<TTargetFn extends () => TableBuilder<any, any> | ViewBuilder<any, any, any>>(
|
|
299
299
|
columns: TColumnKey[],
|
|
300
300
|
targetFn: TTargetFn,
|
|
301
301
|
): RelationKeyBuilder<TOwner, TTargetFn>;
|
|
302
|
-
/** 1:N
|
|
302
|
+
/** 1:N logical reverse-reference definition */
|
|
303
303
|
relationKeyTarget<
|
|
304
304
|
TTargetTableFn extends () => TableBuilder<any, any> | ViewBuilder<any, any, any>,
|
|
305
305
|
>(
|
|
@@ -309,19 +309,19 @@ type RelationRkFactory<
|
|
|
309
309
|
};
|
|
310
310
|
|
|
311
311
|
/**
|
|
312
|
-
*
|
|
312
|
+
* Relation builder factory creation
|
|
313
313
|
*
|
|
314
|
-
* TableBuilder.relations()
|
|
315
|
-
* Table
|
|
314
|
+
* Used in TableBuilder.relations() and ViewBuilder.relations()
|
|
315
|
+
* Table can use both FK + RelationKey, View can only use RelationKey
|
|
316
316
|
*
|
|
317
|
-
* @template TOwner -
|
|
317
|
+
* @template TOwner - Owner Table/View builder type
|
|
318
318
|
* @template TColumnKey - Column key type
|
|
319
|
-
* @param ownerFn -
|
|
320
|
-
* @returns
|
|
319
|
+
* @param ownerFn - Owner Table/View factory function
|
|
320
|
+
* @returns Relation builder factory
|
|
321
321
|
*
|
|
322
322
|
* @example
|
|
323
323
|
* ```typescript
|
|
324
|
-
* // Table - FK
|
|
324
|
+
* // Table - both FK and RelationKey available
|
|
325
325
|
* const Post = Table("Post")
|
|
326
326
|
* .columns((c) => ({
|
|
327
327
|
* id: c.bigint(),
|
|
@@ -331,11 +331,11 @@ type RelationRkFactory<
|
|
|
331
331
|
* author: r.foreignKey(["authorId"], () => User), // FK Generate
|
|
332
332
|
* }));
|
|
333
333
|
*
|
|
334
|
-
* // View - RelationKey
|
|
334
|
+
* // View - only RelationKey available
|
|
335
335
|
* const UserSummary = View("UserSummary")
|
|
336
336
|
* .query(...)
|
|
337
337
|
* .relations((r) => ({
|
|
338
|
-
* posts: r.relationKeyTarget(() => Post, "author"), // FK
|
|
338
|
+
* posts: r.relationKeyTarget(() => Post, "author"), // No FK creation
|
|
339
339
|
* }));
|
|
340
340
|
* ```
|
|
341
341
|
*/
|
|
@@ -374,13 +374,13 @@ export function createRelationFactory<
|
|
|
374
374
|
}
|
|
375
375
|
|
|
376
376
|
// ============================================
|
|
377
|
-
// builder
|
|
377
|
+
// builder record
|
|
378
378
|
// ============================================
|
|
379
379
|
|
|
380
380
|
/**
|
|
381
381
|
* relationship builder record type
|
|
382
382
|
*
|
|
383
|
-
* TableBuilder.relations()
|
|
383
|
+
* Return type of TableBuilder.relations() and ViewBuilder.relations()
|
|
384
384
|
*/
|
|
385
385
|
export type RelationBuilderRecord = Record<
|
|
386
386
|
string,
|
|
@@ -395,11 +395,11 @@ export type RelationBuilderRecord = Record<
|
|
|
395
395
|
// ============================================
|
|
396
396
|
|
|
397
397
|
/**
|
|
398
|
-
*
|
|
398
|
+
* Extract target type from FK/RelationKey (single object)
|
|
399
399
|
*
|
|
400
|
-
* N:1
|
|
400
|
+
* Target type of N:1 relation
|
|
401
401
|
*
|
|
402
|
-
* @template T - FK
|
|
402
|
+
* @template T - FK or RelationKey builder type
|
|
403
403
|
*/
|
|
404
404
|
export type ExtractRelationTarget<TRelation> = TRelation extends
|
|
405
405
|
| ForeignKeyBuilder<any, infer TTargetFn>
|
|
@@ -412,12 +412,12 @@ export type ExtractRelationTarget<TRelation> = TRelation extends
|
|
|
412
412
|
: never;
|
|
413
413
|
|
|
414
414
|
/**
|
|
415
|
-
*
|
|
415
|
+
* Extract target type from FKTarget/RelationKeyTarget (array or single object)
|
|
416
416
|
*
|
|
417
|
-
* 1:N
|
|
418
|
-
* TTargetTableFn: () => Post
|
|
417
|
+
* Target type of 1:N relation (single object when single() is called)
|
|
418
|
+
* TTargetTableFn: () => Post form for lazy evaluation to prevent circular references
|
|
419
419
|
*
|
|
420
|
-
* @template T - FKTarget
|
|
420
|
+
* @template T - FKTarget or RelationKeyTarget builder type
|
|
421
421
|
*/
|
|
422
422
|
export type ExtractRelationTargetResult<TRelation> = TRelation extends
|
|
423
423
|
| ForeignKeyTargetBuilder<infer TTargetTableFn, infer TIsSingle>
|
|
@@ -434,11 +434,11 @@ export type ExtractRelationTargetResult<TRelation> = TRelation extends
|
|
|
434
434
|
: never;
|
|
435
435
|
|
|
436
436
|
/**
|
|
437
|
-
*
|
|
437
|
+
* Deep relation Type inference from relation definitions
|
|
438
438
|
*
|
|
439
|
-
*
|
|
439
|
+
* Makes all relations optional so accessing without include() results in undefined
|
|
440
440
|
*
|
|
441
|
-
* @template TRelations -
|
|
441
|
+
* @template TRelations - Relation builder record type
|
|
442
442
|
*
|
|
443
443
|
* @example
|
|
444
444
|
* ```typescript
|