@abedmurrar/objection 4.0.0-alpha.0 → 4.0.0-beta.1
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 +103 -67
- package/lib/index.mjs +6 -0
- package/lib/initialize.js +6 -9
- package/lib/model/AjvValidator.js +235 -282
- package/lib/model/Model.js +653 -823
- package/lib/model/ModifierNotFoundError.js +5 -7
- package/lib/model/NotFoundError.js +14 -18
- package/lib/model/RelationDoesNotExistError.js +6 -9
- package/lib/model/ValidationError.js +33 -41
- package/lib/model/Validator.js +15 -21
- package/lib/model/getModel.js +1 -3
- package/lib/model/graph/ModelGraph.js +44 -64
- package/lib/model/graph/ModelGraphBuilder.js +164 -217
- package/lib/model/graph/ModelGraphEdge.js +31 -43
- package/lib/model/graph/ModelGraphNode.js +192 -231
- package/lib/model/inheritModel.js +9 -18
- package/lib/model/modelBindKnex.js +48 -62
- package/lib/model/modelClone.js +79 -98
- package/lib/model/modelColPropMap.js +16 -26
- package/lib/model/modelConcurrency.js +24 -0
- package/lib/model/modelId.js +44 -49
- package/lib/model/modelJsonAttributes.js +56 -76
- package/lib/model/modelParseRelations.js +68 -67
- package/lib/model/modelQueryProps.js +76 -107
- package/lib/model/modelSet.js +92 -112
- package/lib/model/modelTableMetadata.js +60 -72
- package/lib/model/modelToJson.js +120 -150
- package/lib/model/modelUtils.js +30 -37
- package/lib/model/modelValidate.js +34 -42
- package/lib/model/modelValues.js +78 -91
- package/lib/model/modelVisitor.js +22 -29
- package/lib/objection.js +42 -67
- package/lib/queryBuilder/FunctionBuilder.js +231 -279
- package/lib/queryBuilder/InternalOptions.js +19 -24
- package/lib/queryBuilder/JoinBuilder.js +116 -149
- package/lib/queryBuilder/QueryBuilder.js +1009 -1271
- package/lib/queryBuilder/QueryBuilderBase.js +647 -865
- package/lib/queryBuilder/QueryBuilderContext.js +14 -21
- package/lib/queryBuilder/QueryBuilderContextBase.js +20 -29
- package/lib/queryBuilder/QueryBuilderOperationSupport.js +462 -536
- package/lib/queryBuilder/QueryBuilderUserContext.js +19 -25
- package/lib/queryBuilder/RawBuilder.js +58 -73
- package/lib/queryBuilder/ReferenceBuilder.js +232 -270
- package/lib/queryBuilder/RelationExpression.js +322 -380
- package/lib/queryBuilder/StaticHookArguments.js +94 -106
- package/lib/queryBuilder/ValueBuilder.js +100 -112
- package/lib/queryBuilder/expressions.js +86 -116
- package/lib/queryBuilder/functions/mysql.js +642 -697
- package/lib/queryBuilder/graph/GraphAction.js +44 -59
- package/lib/queryBuilder/graph/GraphData.js +7 -9
- package/lib/queryBuilder/graph/GraphFetcher.js +115 -151
- package/lib/queryBuilder/graph/GraphNodeDbExistence.js +114 -152
- package/lib/queryBuilder/graph/GraphOperation.js +16 -22
- package/lib/queryBuilder/graph/GraphOptions.js +143 -185
- package/lib/queryBuilder/graph/GraphUpsert.js +167 -241
- package/lib/queryBuilder/graph/delete/GraphDelete.js +8 -13
- package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +44 -62
- package/lib/queryBuilder/graph/insert/GraphInsert.js +99 -128
- package/lib/queryBuilder/graph/insert/GraphInsertAction.js +151 -193
- package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +57 -79
- package/lib/queryBuilder/graph/patch/GraphPatch.js +8 -13
- package/lib/queryBuilder/graph/patch/GraphPatchAction.js +181 -234
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +16 -21
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +80 -110
- package/lib/queryBuilder/join/JoinResultColumn.js +14 -18
- package/lib/queryBuilder/join/JoinResultParser.js +88 -101
- package/lib/queryBuilder/join/RelationJoiner.js +205 -288
- package/lib/queryBuilder/join/TableNode.js +117 -144
- package/lib/queryBuilder/join/TableTree.js +60 -76
- package/lib/queryBuilder/join/utils.js +10 -16
- package/lib/queryBuilder/operations/DelegateOperation.js +93 -133
- package/lib/queryBuilder/operations/DeleteOperation.js +33 -37
- package/lib/queryBuilder/operations/FindByIdOperation.js +20 -28
- package/lib/queryBuilder/operations/FindByIdsOperation.js +17 -23
- package/lib/queryBuilder/operations/FindOperation.js +114 -131
- package/lib/queryBuilder/operations/FirstOperation.js +14 -19
- package/lib/queryBuilder/operations/FromOperation.js +69 -88
- package/lib/queryBuilder/operations/InsertAndFetchOperation.js +28 -39
- package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +25 -35
- package/lib/queryBuilder/operations/InsertGraphOperation.js +77 -98
- package/lib/queryBuilder/operations/InsertOperation.js +132 -165
- package/lib/queryBuilder/operations/InstanceDeleteOperation.js +27 -36
- package/lib/queryBuilder/operations/InstanceFindOperation.js +9 -13
- package/lib/queryBuilder/operations/InstanceInsertOperation.js +13 -17
- package/lib/queryBuilder/operations/InstanceUpdateOperation.js +32 -46
- package/lib/queryBuilder/operations/JoinRelatedOperation.js +66 -86
- package/lib/queryBuilder/operations/KnexOperation.js +4 -7
- package/lib/queryBuilder/operations/MergeOperation.js +32 -45
- package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +131 -150
- package/lib/queryBuilder/operations/OnBuildKnexOperation.js +17 -23
- package/lib/queryBuilder/operations/OnBuildOperation.js +17 -23
- package/lib/queryBuilder/operations/OnErrorOperation.js +17 -23
- package/lib/queryBuilder/operations/QueryBuilderOperation.js +231 -270
- package/lib/queryBuilder/operations/RangeOperation.js +30 -40
- package/lib/queryBuilder/operations/RelateOperation.js +14 -21
- package/lib/queryBuilder/operations/ReturningOperation.js +12 -18
- package/lib/queryBuilder/operations/RunAfterOperation.js +17 -23
- package/lib/queryBuilder/operations/RunBeforeOperation.js +17 -23
- package/lib/queryBuilder/operations/UnrelateOperation.js +12 -17
- package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +48 -65
- package/lib/queryBuilder/operations/UpdateOperation.js +104 -129
- package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +25 -35
- package/lib/queryBuilder/operations/UpsertGraphOperation.js +36 -55
- package/lib/queryBuilder/operations/WhereCompositeOperation.js +30 -39
- package/lib/queryBuilder/operations/WhereInOperation.js +34 -36
- package/lib/queryBuilder/operations/eager/EagerOperation.js +37 -52
- package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +34 -44
- package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +4 -7
- package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +193 -251
- package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +18 -25
- package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +25 -41
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +19 -20
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +23 -21
- package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +106 -118
- package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +79 -108
- package/lib/queryBuilder/operations/select/SelectOperation.js +37 -53
- package/lib/queryBuilder/operations/select/Selection.js +94 -105
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +68 -101
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +65 -75
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +75 -84
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +760 -859
- package/lib/queryBuilder/parsers/relationExpressionParser.js +840 -952
- package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +10 -15
- package/lib/queryBuilder/transformations/QueryTransformation.js +4 -6
- package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +35 -49
- package/lib/queryBuilder/transformations/index.js +3 -9
- package/lib/queryBuilder/whereScopes.js +158 -186
- package/lib/relations/Relation.js +293 -375
- package/lib/relations/RelationDeleteOperation.js +24 -28
- package/lib/relations/RelationFindOperation.js +108 -138
- package/lib/relations/RelationInsertOperation.js +24 -32
- package/lib/relations/RelationOwner.js +175 -209
- package/lib/relations/RelationProperty.js +186 -223
- package/lib/relations/RelationUpdateOperation.js +23 -29
- package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +16 -21
- package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +30 -41
- package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +36 -47
- package/lib/relations/belongsToOne/BelongsToOneRelation.js +42 -50
- package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +43 -58
- package/lib/relations/hasMany/HasManyInsertOperation.js +40 -56
- package/lib/relations/hasMany/HasManyRelateOperation.js +45 -61
- package/lib/relations/hasMany/HasManyRelation.js +38 -46
- package/lib/relations/hasMany/HasManyUnrelateOperation.js +25 -32
- package/lib/relations/hasOne/HasOneRelation.js +9 -12
- package/lib/relations/hasOneThrough/HasOneThroughRelation.js +9 -12
- package/lib/relations/manyToMany/ManyToManyModifyMixin.js +73 -95
- package/lib/relations/manyToMany/ManyToManyRelation.js +374 -473
- package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +23 -30
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +8 -10
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +20 -25
- package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +8 -12
- package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +77 -100
- package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +37 -49
- package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +58 -74
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +10 -13
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +26 -33
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +10 -15
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +8 -10
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +61 -78
- package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +8 -12
- package/lib/transaction.js +53 -83
- package/lib/utils/assert.js +15 -21
- package/lib/utils/buildUtils.js +17 -19
- package/lib/utils/classUtils.js +7 -11
- package/lib/utils/cloneDeep.js +365 -0
- package/lib/utils/createModifier.js +36 -41
- package/lib/utils/deprecate.js +8 -12
- package/lib/utils/identifierMapping.js +153 -192
- package/lib/utils/internalPropUtils.js +4 -8
- package/lib/utils/knexUtils.js +61 -56
- package/lib/utils/mixin.js +16 -23
- package/lib/utils/mysqlJsonPath.js +55 -40
- package/lib/utils/normalizeIds.js +110 -127
- package/lib/utils/objectUtils.js +236 -338
- package/lib/utils/parseFieldExpression.js +66 -44
- package/lib/utils/postgresJsonPath.js +48 -62
- package/lib/utils/promiseUtils/after.js +10 -13
- package/lib/utils/promiseUtils/afterReturn.js +10 -13
- package/lib/utils/promiseUtils/index.js +15 -17
- package/lib/utils/promiseUtils/isPromise.js +6 -9
- package/lib/utils/promiseUtils/map.js +75 -89
- package/lib/utils/promiseUtils/mapAfterAllReturn.js +17 -23
- package/lib/utils/promiseUtils/try.js +15 -18
- package/lib/utils/resolveModel.js +91 -94
- package/lib/utils/tmpColumnUtils.js +4 -13
- package/package.json +50 -23
- package/typings/objection/index.d.mts +362 -0
- package/typings/objection/index.d.ts +36 -9
- package/lib/.eslintrc.json +0 -8
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.pegjs +0 -97
- package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +0 -141
- package/lib/utils/clone.js +0 -2329
|
@@ -1,507 +1,408 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
createJoinModels(ownerId, related) {
|
|
232
|
-
return related.map((related) => this.createJoinModel(ownerId, related));
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
createJoinModel(ownerId, rel) {
|
|
236
|
-
let joinModel = {};
|
|
237
|
-
|
|
238
|
-
for (let j = 0, lp = this.joinTableOwnerProp.size; j < lp; ++j) {
|
|
239
|
-
this.joinTableOwnerProp.setProp(joinModel, j, ownerId[j]);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
for (let j = 0, lp = this.joinTableRelatedProp.size; j < lp; ++j) {
|
|
243
|
-
const value = this.relatedProp.getProp(rel, j);
|
|
244
|
-
|
|
245
|
-
if (value !== undefined) {
|
|
246
|
-
this.joinTableRelatedProp.setProp(joinModel, j, value);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
for (const extra of this.joinTableExtras) {
|
|
251
|
-
let extraValue = rel[extra.aliasProp];
|
|
252
|
-
|
|
253
|
-
if (extraValue === undefined && rel.$$queryProps) {
|
|
254
|
-
extraValue = rel.$$queryProps[extra.aliasProp];
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
if (extraValue !== undefined) {
|
|
258
|
-
joinModel[extra.joinTableProp] = extraValue;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return joinModel;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
omitExtraProps(models) {
|
|
266
|
-
if (this.joinTableExtras && this.joinTableExtras.length) {
|
|
267
|
-
const props = this.joinTableExtras.map((extra) => extra.aliasProp);
|
|
268
|
-
|
|
269
|
-
for (const model of models) {
|
|
270
|
-
// Omit extra properties instead of deleting them from the models so that they can
|
|
271
|
-
// be used in the `$before` and `$after` hooks.
|
|
272
|
-
model.$omitFromDatabaseJson(props);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
executeJoinTableBeforeInsert(models, queryContext, result) {
|
|
278
|
-
return mapAfterAllReturn(
|
|
279
|
-
models,
|
|
280
|
-
(model) => this.joinTableBeforeInsert(model, queryContext),
|
|
281
|
-
result,
|
|
282
|
-
);
|
|
283
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ManyToManyRelation = void 0;
|
|
4
|
+
const whereScopes_1 = require("../../queryBuilder/whereScopes");
|
|
5
|
+
const getModel_1 = require("../../model/getModel");
|
|
6
|
+
const Relation_1 = require("../Relation");
|
|
7
|
+
const RelationProperty_1 = require("../RelationProperty");
|
|
8
|
+
const knexUtils_1 = require("../../utils/knexUtils");
|
|
9
|
+
const inheritModel_1 = require("../../model/inheritModel");
|
|
10
|
+
const resolveModel_1 = require("../../utils/resolveModel");
|
|
11
|
+
const promiseUtils_1 = require("../../utils/promiseUtils");
|
|
12
|
+
const objectUtils_1 = require("../../utils/objectUtils");
|
|
13
|
+
const createModifier_1 = require("../../utils/createModifier");
|
|
14
|
+
const ManyToManyFindOperation_1 = require("./find/ManyToManyFindOperation");
|
|
15
|
+
const ManyToManyInsertOperation_1 = require("./insert/ManyToManyInsertOperation");
|
|
16
|
+
const ManyToManyRelateOperation_1 = require("./relate/ManyToManyRelateOperation");
|
|
17
|
+
const ManyToManyUnrelateOperation_1 = require("./unrelate/ManyToManyUnrelateOperation");
|
|
18
|
+
const ManyToManyUnrelateSqliteOperation_1 = require("./unrelate/ManyToManyUnrelateSqliteOperation");
|
|
19
|
+
const ManyToManyUpdateOperation_1 = require("./update/ManyToManyUpdateOperation");
|
|
20
|
+
const ManyToManyUpdateSqliteOperation_1 = require("./update/ManyToManyUpdateSqliteOperation");
|
|
21
|
+
const ManyToManyDeleteOperation_1 = require("./delete/ManyToManyDeleteOperation");
|
|
22
|
+
const ManyToManyDeleteSqliteOperation_1 = require("./delete/ManyToManyDeleteSqliteOperation");
|
|
23
|
+
class ManyToManyRelation extends Relation_1.Relation {
|
|
24
|
+
setMapping(mapping) {
|
|
25
|
+
const retVal = super.setMapping(mapping);
|
|
26
|
+
let ctx = {
|
|
27
|
+
mapping,
|
|
28
|
+
ownerModelClass: this.ownerModelClass,
|
|
29
|
+
relatedModelClass: this.relatedModelClass,
|
|
30
|
+
ownerProp: this.ownerProp,
|
|
31
|
+
relatedProp: this.relatedProp,
|
|
32
|
+
joinTableModelClass: null,
|
|
33
|
+
joinTableOwnerProp: null,
|
|
34
|
+
joinTableRelatedProp: null,
|
|
35
|
+
joinTableBeforeInsert: null,
|
|
36
|
+
joinTableExtras: [],
|
|
37
|
+
createError: (msg) => this.createError(msg),
|
|
38
|
+
};
|
|
39
|
+
ctx = checkThroughObject(ctx);
|
|
40
|
+
ctx = resolveJoinModelClassIfDefined(ctx);
|
|
41
|
+
ctx = createJoinProperties(ctx);
|
|
42
|
+
ctx = parseExtras(ctx);
|
|
43
|
+
ctx = parseModify(ctx);
|
|
44
|
+
ctx = parseBeforeInsert(ctx);
|
|
45
|
+
ctx = finalizeJoinModelClass(ctx);
|
|
46
|
+
this.joinTableExtras = ctx.joinTableExtras;
|
|
47
|
+
this.joinTableModify = ctx.joinTableModify;
|
|
48
|
+
this.joinTableModelClass = ctx.joinTableModelClass;
|
|
49
|
+
this.joinTableOwnerProp = ctx.joinTableOwnerProp;
|
|
50
|
+
this.joinTableRelatedProp = ctx.joinTableRelatedProp;
|
|
51
|
+
this.joinTableBeforeInsert = ctx.joinTableBeforeInsert;
|
|
52
|
+
return retVal;
|
|
53
|
+
}
|
|
54
|
+
get forbiddenMappingProperties() {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
findQuery(builder, owner) {
|
|
58
|
+
const joinTableOwnerRefs = this.joinTableOwnerProp.refs(builder);
|
|
59
|
+
const joinTable = builder.tableNameFor(this.joinTable);
|
|
60
|
+
const joinTableAlias = builder.tableRefFor(this.joinTable);
|
|
61
|
+
let joinTableSelect = this.joinTableModelClass
|
|
62
|
+
.query()
|
|
63
|
+
.childQueryOf(builder)
|
|
64
|
+
.modify(this.joinTableModify)
|
|
65
|
+
.as(joinTableAlias);
|
|
66
|
+
if (joinTableSelect.isSelectAll()) {
|
|
67
|
+
joinTableSelect = aliasedTableName(joinTable, joinTableAlias);
|
|
68
|
+
}
|
|
69
|
+
builder.join(joinTableSelect, (join) => {
|
|
70
|
+
this.relatedProp.forEach((i) => {
|
|
71
|
+
const relatedRef = this.relatedProp.ref(builder, i);
|
|
72
|
+
const joinTableRelatedRef = this.joinTableRelatedProp.ref(builder, i);
|
|
73
|
+
join.on(relatedRef, joinTableRelatedRef);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
(0, whereScopes_1.withWhereScope)(builder, () => {
|
|
77
|
+
owner.buildFindQuery(builder, this, joinTableOwnerRefs);
|
|
78
|
+
});
|
|
79
|
+
return this.applyModify(builder);
|
|
80
|
+
}
|
|
81
|
+
join(builder, { joinOperation = defaultJoinOperation(this, builder), relatedTableAlias = defaultRelatedTableAlias(this, builder), relatedJoinSelectQuery = defaultRelatedJoinSelectQuery(this, builder), relatedTable = defaultRelatedTable(this, builder), ownerTable = defaultOwnerTable(this, builder), joinTableAlias = defaultJoinTableAlias(this, relatedTableAlias, builder), } = {}) {
|
|
82
|
+
let relatedJoinSelect = this.applyModify(relatedJoinSelectQuery).as(relatedTableAlias);
|
|
83
|
+
if (relatedJoinSelect.isSelectAll()) {
|
|
84
|
+
// No need to join a subquery if the query is `select * from "RelatedTable"`.
|
|
85
|
+
relatedJoinSelect = aliasedTableName(relatedTable, relatedTableAlias);
|
|
86
|
+
}
|
|
87
|
+
let joinTableSelect = this.joinTableModelClass
|
|
88
|
+
.query()
|
|
89
|
+
.childQueryOf(builder)
|
|
90
|
+
.modify(this.joinTableModify)
|
|
91
|
+
.as(joinTableAlias);
|
|
92
|
+
if (joinTableSelect.isSelectAll()) {
|
|
93
|
+
joinTableSelect = aliasedTableName(this.joinTable, joinTableAlias);
|
|
94
|
+
}
|
|
95
|
+
return builder[joinOperation](joinTableSelect, (join) => {
|
|
96
|
+
const ownerProp = this.ownerProp;
|
|
97
|
+
const joinTableOwnerProp = this.joinTableOwnerProp;
|
|
98
|
+
ownerProp.forEach((i) => {
|
|
99
|
+
const joinTableOwnerRef = joinTableOwnerProp.ref(builder, i).table(joinTableAlias);
|
|
100
|
+
const ownerRef = ownerProp.ref(builder, i).table(ownerTable);
|
|
101
|
+
join.on(joinTableOwnerRef, ownerRef);
|
|
102
|
+
});
|
|
103
|
+
})[joinOperation](relatedJoinSelect, (join) => {
|
|
104
|
+
const relatedProp = this.relatedProp;
|
|
105
|
+
const joinTableRelatedProp = this.joinTableRelatedProp;
|
|
106
|
+
relatedProp.forEach((i) => {
|
|
107
|
+
const joinTableRelatedRef = joinTableRelatedProp.ref(builder, i).table(joinTableAlias);
|
|
108
|
+
const relatedRef = relatedProp.ref(builder, i).table(relatedTableAlias);
|
|
109
|
+
join.on(joinTableRelatedRef, relatedRef);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
find(_, owner) {
|
|
114
|
+
return new ManyToManyFindOperation_1.ManyToManyFindOperation('find', {
|
|
115
|
+
relation: this,
|
|
116
|
+
owner,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
insert(_, owner) {
|
|
120
|
+
return new ManyToManyInsertOperation_1.ManyToManyInsertOperation('insert', {
|
|
121
|
+
relation: this,
|
|
122
|
+
owner,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
update(builder, owner) {
|
|
126
|
+
if ((0, knexUtils_1.isSqlite)(builder.knex())) {
|
|
127
|
+
return new ManyToManyUpdateSqliteOperation_1.ManyToManyUpdateSqliteOperation('update', {
|
|
128
|
+
relation: this,
|
|
129
|
+
owner,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
return new ManyToManyUpdateOperation_1.ManyToManyUpdateOperation('update', {
|
|
134
|
+
relation: this,
|
|
135
|
+
owner,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
patch(builder, owner) {
|
|
140
|
+
if ((0, knexUtils_1.isSqlite)(builder.knex())) {
|
|
141
|
+
return new ManyToManyUpdateSqliteOperation_1.ManyToManyUpdateSqliteOperation('patch', {
|
|
142
|
+
modelOptions: { patch: true },
|
|
143
|
+
relation: this,
|
|
144
|
+
owner,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
return new ManyToManyUpdateOperation_1.ManyToManyUpdateOperation('patch', {
|
|
149
|
+
modelOptions: { patch: true },
|
|
150
|
+
relation: this,
|
|
151
|
+
owner,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
delete(builder, owner) {
|
|
156
|
+
if ((0, knexUtils_1.isSqlite)(builder.knex())) {
|
|
157
|
+
return new ManyToManyDeleteSqliteOperation_1.ManyToManyDeleteSqliteOperation('delete', {
|
|
158
|
+
relation: this,
|
|
159
|
+
owner,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
return new ManyToManyDeleteOperation_1.ManyToManyDeleteOperation('delete', {
|
|
164
|
+
relation: this,
|
|
165
|
+
owner,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
relate(builder, owner) {
|
|
170
|
+
return new ManyToManyRelateOperation_1.ManyToManyRelateOperation('relate', {
|
|
171
|
+
relation: this,
|
|
172
|
+
owner,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
unrelate(builder, owner) {
|
|
176
|
+
if ((0, knexUtils_1.isSqlite)(builder.knex())) {
|
|
177
|
+
return new ManyToManyUnrelateSqliteOperation_1.ManyToManyUnrelateSqliteOperation('unrelate', {
|
|
178
|
+
relation: this,
|
|
179
|
+
owner,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
return new ManyToManyUnrelateOperation_1.ManyToManyUnrelateOperation('unrelate', {
|
|
184
|
+
relation: this,
|
|
185
|
+
owner,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
createJoinModels(ownerId, related) {
|
|
190
|
+
return related.map((related) => this.createJoinModel(ownerId, related));
|
|
191
|
+
}
|
|
192
|
+
// `rel` is a related model, or an id object (see ManyToManyRelateOperation).
|
|
193
|
+
createJoinModel(ownerId, rel) {
|
|
194
|
+
const joinModel = {};
|
|
195
|
+
for (let j = 0, lp = this.joinTableOwnerProp.size; j < lp; ++j) {
|
|
196
|
+
this.joinTableOwnerProp.setProp(joinModel, j, ownerId[j]);
|
|
197
|
+
}
|
|
198
|
+
for (let j = 0, lp = this.joinTableRelatedProp.size; j < lp; ++j) {
|
|
199
|
+
const value = this.relatedProp.getProp(rel, j);
|
|
200
|
+
if (value !== undefined) {
|
|
201
|
+
this.joinTableRelatedProp.setProp(joinModel, j, value);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
for (const extra of this.joinTableExtras) {
|
|
205
|
+
let extraValue = rel[extra.aliasProp];
|
|
206
|
+
if (extraValue === undefined && rel.$$queryProps) {
|
|
207
|
+
extraValue = rel.$$queryProps[extra.aliasProp];
|
|
208
|
+
}
|
|
209
|
+
if (extraValue !== undefined) {
|
|
210
|
+
joinModel[extra.joinTableProp] = extraValue;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return joinModel;
|
|
214
|
+
}
|
|
215
|
+
omitExtraProps(models) {
|
|
216
|
+
if (this.joinTableExtras && this.joinTableExtras.length) {
|
|
217
|
+
const props = this.joinTableExtras.map((extra) => extra.aliasProp);
|
|
218
|
+
for (const model of models) {
|
|
219
|
+
// Omit extra properties instead of deleting them from the models so that they can
|
|
220
|
+
// be used in the `$before` and `$after` hooks.
|
|
221
|
+
model.$omitFromDatabaseJson(props);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
executeJoinTableBeforeInsert(models, queryContext, result) {
|
|
226
|
+
return (0, promiseUtils_1.mapAfterAllReturn)(models, (model) => this.joinTableBeforeInsert(model, queryContext), result);
|
|
227
|
+
}
|
|
284
228
|
}
|
|
285
|
-
|
|
229
|
+
exports.ManyToManyRelation = ManyToManyRelation;
|
|
286
230
|
Object.defineProperties(ManyToManyRelation.prototype, {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
231
|
+
isObjectionManyToManyRelation: {
|
|
232
|
+
enumerable: false,
|
|
233
|
+
writable: false,
|
|
234
|
+
value: true,
|
|
235
|
+
},
|
|
292
236
|
});
|
|
293
|
-
|
|
294
237
|
function defaultJoinOperation() {
|
|
295
|
-
|
|
238
|
+
return 'join';
|
|
296
239
|
}
|
|
297
|
-
|
|
298
240
|
function defaultRelatedTableAlias(relation, builder) {
|
|
299
|
-
|
|
241
|
+
return builder.tableRefFor(relation.relatedModelClass);
|
|
300
242
|
}
|
|
301
|
-
|
|
302
243
|
function defaultRelatedJoinSelectQuery(relation, builder) {
|
|
303
|
-
|
|
244
|
+
return relation.relatedModelClass.query().childQueryOf(builder);
|
|
304
245
|
}
|
|
305
|
-
|
|
306
246
|
function defaultRelatedTable(relation, builder) {
|
|
307
|
-
|
|
247
|
+
return builder.tableNameFor(relation.relatedModelClass);
|
|
308
248
|
}
|
|
309
|
-
|
|
310
249
|
function defaultOwnerTable(relation, builder) {
|
|
311
|
-
|
|
250
|
+
return builder.tableRefFor(relation.ownerModelClass);
|
|
312
251
|
}
|
|
313
|
-
|
|
314
252
|
function defaultJoinTableAlias(relation, relatedTableAlias, builder) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
253
|
+
const alias = builder.tableRefFor(relation.joinTable);
|
|
254
|
+
if (alias === relation.joinTable) {
|
|
255
|
+
return relation.ownerModelClass.joinTableAlias(relatedTableAlias);
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
return alias;
|
|
259
|
+
}
|
|
322
260
|
}
|
|
323
|
-
|
|
324
261
|
function aliasedTableName(tableName, alias) {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
262
|
+
if (tableName === alias) {
|
|
263
|
+
return tableName;
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
return `${tableName} as ${alias}`;
|
|
267
|
+
}
|
|
330
268
|
}
|
|
331
|
-
|
|
332
269
|
function checkThroughObject(ctx) {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
'join.through must be an object that describes the join table. For example: {from: "JoinTable.someId", to: "JoinTable.someOtherId"}',
|
|
342
|
-
);
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
return ctx;
|
|
270
|
+
const mapping = ctx.mapping;
|
|
271
|
+
if (!(0, objectUtils_1.isObject)(mapping.join.through)) {
|
|
272
|
+
throw ctx.createError('join must have a `through` object that describes the join table.');
|
|
273
|
+
}
|
|
274
|
+
if (!mapping.join.through.from || !mapping.join.through.to) {
|
|
275
|
+
throw ctx.createError('join.through must be an object that describes the join table. For example: {from: "JoinTable.someId", to: "JoinTable.someOtherId"}');
|
|
276
|
+
}
|
|
277
|
+
return ctx;
|
|
346
278
|
}
|
|
347
|
-
|
|
348
279
|
function resolveJoinModelClassIfDefined(ctx) {
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
return Object.assign(ctx, { joinTableModelClass });
|
|
280
|
+
let joinTableModelClass = null;
|
|
281
|
+
if (ctx.mapping.join.through.modelClass) {
|
|
282
|
+
try {
|
|
283
|
+
// undefined only for a relative path without `modelPaths`, which then
|
|
284
|
+
// fails further on, as it did in the JavaScript code.
|
|
285
|
+
joinTableModelClass = (0, resolveModel_1.resolveModel)(ctx.mapping.join.through.modelClass, ctx.ownerModelClass.modelPaths, 'join.through.modelClass');
|
|
286
|
+
}
|
|
287
|
+
catch (err) {
|
|
288
|
+
throw ctx.createError(err.message);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return Object.assign(ctx, { joinTableModelClass });
|
|
364
292
|
}
|
|
365
|
-
|
|
366
293
|
function createJoinProperties(ctx) {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
return Object.assign(ctx, {
|
|
396
|
-
joinTableOwnerProp: ownerProp,
|
|
397
|
-
joinTableRelatedProp: relatedProp,
|
|
398
|
-
});
|
|
294
|
+
let ret;
|
|
295
|
+
let fromProp;
|
|
296
|
+
let toProp;
|
|
297
|
+
let relatedProp;
|
|
298
|
+
let ownerProp;
|
|
299
|
+
ret = createRelationProperty(ctx, ctx.mapping.join.through.from, 'join.through.from');
|
|
300
|
+
fromProp = ret.prop;
|
|
301
|
+
ctx = ret.ctx;
|
|
302
|
+
ret = createRelationProperty(ctx, ctx.mapping.join.through.to, 'join.through.to');
|
|
303
|
+
toProp = ret.prop;
|
|
304
|
+
ctx = ret.ctx;
|
|
305
|
+
if (fromProp.modelClass.getTableName() !== toProp.modelClass.getTableName()) {
|
|
306
|
+
throw ctx.createError('join.through `from` and `to` must point to the same join table.');
|
|
307
|
+
}
|
|
308
|
+
if (ctx.relatedProp.modelClass.getTableName() === fromProp.modelClass.getTableName()) {
|
|
309
|
+
relatedProp = fromProp;
|
|
310
|
+
ownerProp = toProp;
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
relatedProp = toProp;
|
|
314
|
+
ownerProp = fromProp;
|
|
315
|
+
}
|
|
316
|
+
return Object.assign(ctx, {
|
|
317
|
+
joinTableOwnerProp: ownerProp,
|
|
318
|
+
joinTableRelatedProp: relatedProp,
|
|
319
|
+
});
|
|
399
320
|
}
|
|
400
|
-
|
|
401
321
|
function createRelationProperty(ctx, refString, messagePrefix) {
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
}
|
|
418
|
-
};
|
|
419
|
-
|
|
420
|
-
try {
|
|
421
|
-
prop = new RelationProperty(refString, resolveModelClass);
|
|
422
|
-
} catch (err) {
|
|
423
|
-
if (err instanceof RelationProperty.ModelNotFoundError) {
|
|
424
|
-
throw ctx.createError('join.through `from` and `to` must point to the same join table.');
|
|
425
|
-
} else {
|
|
426
|
-
throw ctx.createError(
|
|
427
|
-
`${messagePrefix} must have format JoinTable.columnName. For example "JoinTable.someId" or in case of composite key ["JoinTable.a", "JoinTable.b"].`,
|
|
428
|
-
);
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
return {
|
|
433
|
-
ctx: Object.assign(ctx, { joinTableModelClass }),
|
|
434
|
-
prop,
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
function parseExtras(ctx) {
|
|
439
|
-
let extraDef = ctx.mapping.join.through.extra;
|
|
440
|
-
|
|
441
|
-
if (!extraDef) {
|
|
442
|
-
return ctx;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
if (isString(extraDef)) {
|
|
446
|
-
extraDef = {
|
|
447
|
-
[extraDef]: extraDef,
|
|
322
|
+
let prop = null;
|
|
323
|
+
let joinTableModelClass = ctx.joinTableModelClass;
|
|
324
|
+
const resolveModelClass = (table) => {
|
|
325
|
+
if (joinTableModelClass === null) {
|
|
326
|
+
joinTableModelClass = (0, inheritModel_1.inheritModel)((0, getModel_1.getModel)());
|
|
327
|
+
joinTableModelClass.tableName = table;
|
|
328
|
+
joinTableModelClass.idColumn = null;
|
|
329
|
+
joinTableModelClass.concurrency = ctx.ownerModelClass.concurrency;
|
|
330
|
+
}
|
|
331
|
+
if (joinTableModelClass.getTableName() === table) {
|
|
332
|
+
return joinTableModelClass;
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
448
337
|
};
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
338
|
+
try {
|
|
339
|
+
prop = new RelationProperty_1.RelationProperty(refString, resolveModelClass);
|
|
340
|
+
}
|
|
341
|
+
catch (err) {
|
|
342
|
+
if (err instanceof RelationProperty_1.RelationProperty.ModelNotFoundError) {
|
|
343
|
+
throw ctx.createError('join.through `from` and `to` must point to the same join table.');
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
throw ctx.createError(`${messagePrefix} must have format JoinTable.columnName. For example "JoinTable.someId" or in case of composite key ["JoinTable.a", "JoinTable.b"].`);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
459
349
|
return {
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
aliasCol: key,
|
|
463
|
-
aliasProp: ctx.joinTableModelClass.columnNameToPropertyName(key),
|
|
350
|
+
ctx: Object.assign(ctx, { joinTableModelClass }),
|
|
351
|
+
prop,
|
|
464
352
|
};
|
|
465
|
-
});
|
|
466
|
-
|
|
467
|
-
return Object.assign(ctx, { joinTableExtras });
|
|
468
353
|
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
354
|
+
function parseExtras(ctx) {
|
|
355
|
+
let extraDef = ctx.mapping.join.through.extra;
|
|
356
|
+
if (!extraDef) {
|
|
357
|
+
return ctx;
|
|
358
|
+
}
|
|
359
|
+
if ((0, objectUtils_1.isString)(extraDef)) {
|
|
360
|
+
extraDef = {
|
|
361
|
+
[extraDef]: extraDef,
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
else if (Array.isArray(extraDef)) {
|
|
365
|
+
extraDef = extraDef.reduce((extraDef, col) => {
|
|
366
|
+
extraDef[col] = col;
|
|
367
|
+
return extraDef;
|
|
368
|
+
}, {});
|
|
369
|
+
}
|
|
370
|
+
const joinTableExtras = Object.keys(extraDef).map((key) => {
|
|
371
|
+
const val = extraDef[key];
|
|
372
|
+
return {
|
|
373
|
+
joinTableCol: val,
|
|
374
|
+
joinTableProp: ctx.joinTableModelClass.columnNameToPropertyName(val),
|
|
375
|
+
aliasCol: key,
|
|
376
|
+
aliasProp: ctx.joinTableModelClass.columnNameToPropertyName(key),
|
|
377
|
+
};
|
|
478
378
|
});
|
|
479
|
-
|
|
480
|
-
|
|
379
|
+
return Object.assign(ctx, { joinTableExtras });
|
|
380
|
+
}
|
|
381
|
+
function parseModify(ctx) {
|
|
382
|
+
const mapping = ctx.mapping.join.through;
|
|
383
|
+
const modifier = mapping.modify || mapping.filter;
|
|
384
|
+
const joinTableModify = modifier &&
|
|
385
|
+
(0, createModifier_1.createModifier)({
|
|
386
|
+
modifier,
|
|
387
|
+
modelClass: ctx.relatedModelClass,
|
|
388
|
+
});
|
|
389
|
+
return Object.assign(ctx, { joinTableModify });
|
|
481
390
|
}
|
|
482
|
-
|
|
483
391
|
function parseBeforeInsert(ctx) {
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
return Object.assign(ctx, { joinTableBeforeInsert });
|
|
392
|
+
let joinTableBeforeInsert;
|
|
393
|
+
if ((0, objectUtils_1.isFunction)(ctx.mapping.join.through.beforeInsert)) {
|
|
394
|
+
joinTableBeforeInsert = ctx.mapping.join.through.beforeInsert;
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
joinTableBeforeInsert = (model) => model;
|
|
398
|
+
}
|
|
399
|
+
return Object.assign(ctx, { joinTableBeforeInsert });
|
|
493
400
|
}
|
|
494
|
-
|
|
495
401
|
function finalizeJoinModelClass(ctx) {
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
return ctx;
|
|
402
|
+
if (ctx.joinTableModelClass.getIdColumn() === null) {
|
|
403
|
+
// We cannot know if the join table has a primary key. Therefore we set some
|
|
404
|
+
// known column as the idColumn so that inserts will work.
|
|
405
|
+
ctx.joinTableModelClass.idColumn = ctx.joinTableRelatedProp.cols;
|
|
406
|
+
}
|
|
407
|
+
return ctx;
|
|
503
408
|
}
|
|
504
|
-
|
|
505
|
-
module.exports = {
|
|
506
|
-
ManyToManyRelation,
|
|
507
|
-
};
|