@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,401 +1,319 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const { createModifier } = require('../utils/createModifier');
|
|
14
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Relation = void 0;
|
|
4
|
+
const whereScopes_1 = require("../queryBuilder/whereScopes");
|
|
5
|
+
const RelationProperty_1 = require("./RelationProperty");
|
|
6
|
+
const RelationFindOperation_1 = require("./RelationFindOperation");
|
|
7
|
+
const RelationUpdateOperation_1 = require("./RelationUpdateOperation");
|
|
8
|
+
const RelationDeleteOperation_1 = require("./RelationDeleteOperation");
|
|
9
|
+
const resolveModel_1 = require("../utils/resolveModel");
|
|
10
|
+
const objectUtils_1 = require("../utils/objectUtils");
|
|
11
|
+
const promiseUtils_1 = require("../utils/promiseUtils");
|
|
12
|
+
const createModifier_1 = require("../utils/createModifier");
|
|
15
13
|
class Relation {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
return new RelationUpdateOperation('patch', {
|
|
192
|
-
relation: this,
|
|
193
|
-
owner,
|
|
194
|
-
modelOptions: { patch: true },
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
delete(_, owner) {
|
|
199
|
-
return new RelationDeleteOperation('delete', {
|
|
200
|
-
relation: this,
|
|
201
|
-
owner,
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
relate() {
|
|
206
|
-
/* istanbul ignore next */
|
|
207
|
-
throw this.createError('not implemented');
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
unrelate() {
|
|
211
|
-
/* istanbul ignore next */
|
|
212
|
-
throw this.createError('not implemented');
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
hasRelateProp(model) {
|
|
216
|
-
return model.$hasProps(this.relatedProp.props);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
setRelateProp(model, values) {
|
|
220
|
-
this.relatedProp.forEach((i) => {
|
|
221
|
-
this.relatedProp.setProp(model, i, values[i]);
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
executeBeforeInsert(models, queryContext, result) {
|
|
226
|
-
return mapAfterAllReturn(models, (model) => this.beforeInsert(model, queryContext), result);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
createError(message) {
|
|
230
|
-
if (this.ownerModelClass && this.ownerModelClass.name && this.name) {
|
|
231
|
-
return new Error(`${this.ownerModelClass.name}.relationMappings.${this.name}: ${message}`);
|
|
232
|
-
} else {
|
|
233
|
-
return new Error(`${this.constructor.name}: ${message}`);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
14
|
+
constructor(relationName, OwnerClass) {
|
|
15
|
+
this.name = relationName;
|
|
16
|
+
this.ownerModelClass = OwnerClass;
|
|
17
|
+
this.relatedModelClass = null;
|
|
18
|
+
this.ownerProp = null;
|
|
19
|
+
this.relatedProp = null;
|
|
20
|
+
this.joinTableModelClass = null;
|
|
21
|
+
this.joinTableOwnerProp = null;
|
|
22
|
+
this.joinTableRelatedProp = null;
|
|
23
|
+
this.joinTableBeforeInsert = null;
|
|
24
|
+
this.joinTableExtras = [];
|
|
25
|
+
this.modify = null;
|
|
26
|
+
this.beforeInsert = null;
|
|
27
|
+
}
|
|
28
|
+
setMapping(mapping) {
|
|
29
|
+
let ctx = {
|
|
30
|
+
name: this.name,
|
|
31
|
+
mapping,
|
|
32
|
+
ownerModelClass: this.ownerModelClass,
|
|
33
|
+
relatedModelClass: null,
|
|
34
|
+
relatedProp: null,
|
|
35
|
+
ownerProp: null,
|
|
36
|
+
modify: null,
|
|
37
|
+
beforeInsert: null,
|
|
38
|
+
forbiddenMappingProperties: this.forbiddenMappingProperties,
|
|
39
|
+
createError: (msg) => this.createError(msg),
|
|
40
|
+
};
|
|
41
|
+
ctx = checkForbiddenProperties(ctx);
|
|
42
|
+
ctx = checkOwnerModelClass(ctx);
|
|
43
|
+
ctx = checkRelatedModelClass(ctx);
|
|
44
|
+
ctx = resolveRelatedModelClass(ctx);
|
|
45
|
+
ctx = checkRelation(ctx);
|
|
46
|
+
ctx = createJoinProperties(ctx);
|
|
47
|
+
ctx = parseModify(ctx);
|
|
48
|
+
ctx = parseBeforeInsert(ctx);
|
|
49
|
+
this.relatedModelClass = ctx.relatedModelClass;
|
|
50
|
+
this.ownerProp = ctx.ownerProp;
|
|
51
|
+
this.relatedProp = ctx.relatedProp;
|
|
52
|
+
this.modify = ctx.modify;
|
|
53
|
+
this.beforeInsert = ctx.beforeInsert;
|
|
54
|
+
}
|
|
55
|
+
get forbiddenMappingProperties() {
|
|
56
|
+
return ['join.through'];
|
|
57
|
+
}
|
|
58
|
+
get joinTable() {
|
|
59
|
+
return this.joinTableModelClass ? this.joinTableModelClass.getTableName() : null;
|
|
60
|
+
}
|
|
61
|
+
get joinModelClass() {
|
|
62
|
+
return this.getJoinModelClass(this.ownerModelClass.knex());
|
|
63
|
+
}
|
|
64
|
+
getJoinModelClass(knex) {
|
|
65
|
+
return this.joinTableModelClass && knex !== this.joinTableModelClass.knex()
|
|
66
|
+
? this.joinTableModelClass.bindKnex(knex)
|
|
67
|
+
: this.joinTableModelClass;
|
|
68
|
+
}
|
|
69
|
+
isOneToOne() {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
clone() {
|
|
73
|
+
const relation = new this.constructor(this.name, this.ownerModelClass);
|
|
74
|
+
relation.relatedModelClass = this.relatedModelClass;
|
|
75
|
+
relation.ownerProp = this.ownerProp;
|
|
76
|
+
relation.relatedProp = this.relatedProp;
|
|
77
|
+
relation.modify = this.modify;
|
|
78
|
+
relation.beforeInsert = this.beforeInsert;
|
|
79
|
+
relation.joinTableModelClass = this.joinTableModelClass;
|
|
80
|
+
relation.joinTableOwnerProp = this.joinTableOwnerProp;
|
|
81
|
+
relation.joinTableRelatedProp = this.joinTableRelatedProp;
|
|
82
|
+
relation.joinTableBeforeInsert = this.joinTableBeforeInsert;
|
|
83
|
+
relation.joinTableExtras = this.joinTableExtras;
|
|
84
|
+
return relation;
|
|
85
|
+
}
|
|
86
|
+
bindKnex(knex) {
|
|
87
|
+
const bound = this.clone();
|
|
88
|
+
bound.relatedModelClass = this.relatedModelClass.bindKnex(knex);
|
|
89
|
+
bound.ownerModelClass = this.ownerModelClass.bindKnex(knex);
|
|
90
|
+
if (this.joinTableModelClass) {
|
|
91
|
+
bound.joinTableModelClass = this.joinTableModelClass.bindKnex(knex);
|
|
92
|
+
}
|
|
93
|
+
return bound;
|
|
94
|
+
}
|
|
95
|
+
findQuery(builder, owner) {
|
|
96
|
+
const relatedRefs = this.relatedProp.refs(builder);
|
|
97
|
+
(0, whereScopes_1.withWhereScope)(builder, () => {
|
|
98
|
+
owner.buildFindQuery(builder, this, relatedRefs);
|
|
99
|
+
});
|
|
100
|
+
return this.applyModify(builder);
|
|
101
|
+
}
|
|
102
|
+
applyModify(builder) {
|
|
103
|
+
try {
|
|
104
|
+
// The relation's `modify` gets its own scope so that an `orWhere`
|
|
105
|
+
// in it can't escape the owner condition.
|
|
106
|
+
return (0, whereScopes_1.withWhereScope)(builder, () => builder.modify(this.modify));
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
if (err instanceof this.relatedModelClass.ModifierNotFoundError) {
|
|
110
|
+
throw this.createError(err.message);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
throw err;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
join(builder, { joinOperation = 'join', relatedTableAlias = builder.tableRefFor(this.relatedModelClass), relatedJoinSelectQuery = this.relatedModelClass.query().childQueryOf(builder), relatedTable = builder.tableNameFor(this.relatedModelClass), ownerTable = builder.tableRefFor(this.ownerModelClass), } = {}) {
|
|
118
|
+
let relatedJoinSelect = this.applyModify(relatedJoinSelectQuery).as(relatedTableAlias);
|
|
119
|
+
if (relatedJoinSelect.isSelectAll()) {
|
|
120
|
+
// No need to join a subquery if the query is `select * from "RelatedTable"`.
|
|
121
|
+
relatedJoinSelect = aliasedTableName(relatedTable, relatedTableAlias);
|
|
122
|
+
}
|
|
123
|
+
return builder[joinOperation](relatedJoinSelect, (join) => {
|
|
124
|
+
const relatedProp = this.relatedProp;
|
|
125
|
+
const ownerProp = this.ownerProp;
|
|
126
|
+
relatedProp.forEach((i) => {
|
|
127
|
+
const relatedRef = relatedProp.ref(builder, i).table(relatedTableAlias);
|
|
128
|
+
const ownerRef = ownerProp.ref(builder, i).table(ownerTable);
|
|
129
|
+
join.on(relatedRef, ownerRef);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
find(_, owner) {
|
|
134
|
+
return new RelationFindOperation_1.RelationFindOperation('find', {
|
|
135
|
+
relation: this,
|
|
136
|
+
owner,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
insert(_, owner) {
|
|
140
|
+
/* istanbul ignore next */
|
|
141
|
+
throw this.createError('not implemented');
|
|
142
|
+
}
|
|
143
|
+
update(_, owner) {
|
|
144
|
+
return new RelationUpdateOperation_1.RelationUpdateOperation('update', {
|
|
145
|
+
relation: this,
|
|
146
|
+
owner,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
patch(_, owner) {
|
|
150
|
+
return new RelationUpdateOperation_1.RelationUpdateOperation('patch', {
|
|
151
|
+
relation: this,
|
|
152
|
+
owner,
|
|
153
|
+
modelOptions: { patch: true },
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
delete(_, owner) {
|
|
157
|
+
return new RelationDeleteOperation_1.RelationDeleteOperation('delete', {
|
|
158
|
+
relation: this,
|
|
159
|
+
owner,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
relate() {
|
|
163
|
+
/* istanbul ignore next */
|
|
164
|
+
throw this.createError('not implemented');
|
|
165
|
+
}
|
|
166
|
+
unrelate() {
|
|
167
|
+
/* istanbul ignore next */
|
|
168
|
+
throw this.createError('not implemented');
|
|
169
|
+
}
|
|
170
|
+
hasRelateProp(model) {
|
|
171
|
+
return model.$hasProps(this.relatedProp.props);
|
|
172
|
+
}
|
|
173
|
+
setRelateProp(model, values) {
|
|
174
|
+
this.relatedProp.forEach((i) => {
|
|
175
|
+
this.relatedProp.setProp(model, i, values[i]);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
executeBeforeInsert(models, queryContext, result) {
|
|
179
|
+
return (0, promiseUtils_1.mapAfterAllReturn)(models, (model) => this.beforeInsert(model, queryContext), result);
|
|
180
|
+
}
|
|
181
|
+
createError(message) {
|
|
182
|
+
if (this.ownerModelClass && this.ownerModelClass.name && this.name) {
|
|
183
|
+
return new Error(`${this.ownerModelClass.name}.relationMappings.${this.name}: ${message}`);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
return new Error(`${this.constructor.name}: ${message}`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
236
189
|
}
|
|
237
|
-
|
|
190
|
+
exports.Relation = Relation;
|
|
238
191
|
Object.defineProperties(Relation, {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
192
|
+
isObjectionRelationClass: {
|
|
193
|
+
enumerable: false,
|
|
194
|
+
writable: false,
|
|
195
|
+
value: true,
|
|
196
|
+
},
|
|
244
197
|
});
|
|
245
|
-
|
|
246
198
|
Object.defineProperties(Relation.prototype, {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
199
|
+
isObjectionRelation: {
|
|
200
|
+
enumerable: false,
|
|
201
|
+
writable: false,
|
|
202
|
+
value: true,
|
|
203
|
+
},
|
|
252
204
|
});
|
|
253
|
-
|
|
254
205
|
function checkForbiddenProperties(ctx) {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
return ctx;
|
|
206
|
+
ctx.forbiddenMappingProperties.forEach((prop) => {
|
|
207
|
+
if ((0, objectUtils_1.get)(ctx.mapping, prop.split('.')) !== undefined) {
|
|
208
|
+
throw ctx.createError(`Property ${prop} is not supported for this relation type.`);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
return ctx;
|
|
262
212
|
}
|
|
263
|
-
|
|
264
213
|
function checkOwnerModelClass(ctx) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
return ctx;
|
|
214
|
+
if (!(0, objectUtils_1.isFunction)(ctx.ownerModelClass) || !ctx.ownerModelClass.isObjectionModelClass) {
|
|
215
|
+
throw ctx.createError(`Relation's owner is not a subclass of Model`);
|
|
216
|
+
}
|
|
217
|
+
return ctx;
|
|
270
218
|
}
|
|
271
|
-
|
|
272
219
|
function checkRelatedModelClass(ctx) {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
return ctx;
|
|
220
|
+
if (!ctx.mapping.modelClass) {
|
|
221
|
+
throw ctx.createError('modelClass is not defined');
|
|
222
|
+
}
|
|
223
|
+
return ctx;
|
|
278
224
|
}
|
|
279
|
-
|
|
280
225
|
function resolveRelatedModelClass(ctx) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
return Object.assign(ctx, { relatedModelClass });
|
|
226
|
+
let relatedModelClass;
|
|
227
|
+
try {
|
|
228
|
+
// undefined only for a relative path without `modelPaths`, which then
|
|
229
|
+
// fails further on, as it did in the JavaScript code.
|
|
230
|
+
relatedModelClass = (0, resolveModel_1.resolveModel)(ctx.mapping.modelClass, ctx.ownerModelClass.modelPaths, 'modelClass');
|
|
231
|
+
}
|
|
232
|
+
catch (err) {
|
|
233
|
+
throw ctx.createError(err.message);
|
|
234
|
+
}
|
|
235
|
+
return Object.assign(ctx, { relatedModelClass });
|
|
294
236
|
}
|
|
295
|
-
|
|
296
237
|
function checkRelation(ctx) {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
return ctx;
|
|
238
|
+
if (!ctx.mapping.relation) {
|
|
239
|
+
throw ctx.createError('relation is not defined');
|
|
240
|
+
}
|
|
241
|
+
if (!(0, objectUtils_1.isFunction)(ctx.mapping.relation) || !ctx.mapping.relation.isObjectionRelationClass) {
|
|
242
|
+
throw ctx.createError('relation is not a subclass of Relation');
|
|
243
|
+
}
|
|
244
|
+
return ctx;
|
|
306
245
|
}
|
|
307
|
-
|
|
308
246
|
function createJoinProperties(ctx) {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
);
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
`join: relation name and join property '${ctx.name}' cannot have the same name. If you cannot change one or the other, you can use $parseDatabaseJson and $formatDatabaseJson methods to convert the column name.`,
|
|
336
|
-
);
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
if (relatedProp.modelClass.getTableName() !== ctx.relatedModelClass.getTableName()) {
|
|
340
|
-
throw ctx.createError('join: either `from` or `to` must point to the related model table.');
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
return Object.assign(ctx, { ownerProp, relatedProp });
|
|
247
|
+
const mapping = ctx.mapping;
|
|
248
|
+
if (!mapping.join || !mapping.join.from || !mapping.join.to) {
|
|
249
|
+
throw ctx.createError('join must be an object that maps the columns of the related models together. For example: {from: "SomeTable.id", to: "SomeOtherTable.someModelId"}');
|
|
250
|
+
}
|
|
251
|
+
const fromProp = createRelationProperty(ctx, mapping.join.from, 'join.from');
|
|
252
|
+
const toProp = createRelationProperty(ctx, mapping.join.to, 'join.to');
|
|
253
|
+
let ownerProp;
|
|
254
|
+
let relatedProp;
|
|
255
|
+
if (fromProp.modelClass.getTableName() === ctx.ownerModelClass.getTableName()) {
|
|
256
|
+
ownerProp = fromProp;
|
|
257
|
+
relatedProp = toProp;
|
|
258
|
+
}
|
|
259
|
+
else if (toProp.modelClass.getTableName() === ctx.ownerModelClass.getTableName()) {
|
|
260
|
+
ownerProp = toProp;
|
|
261
|
+
relatedProp = fromProp;
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
throw ctx.createError('join: either `from` or `to` must point to the owner model table.');
|
|
265
|
+
}
|
|
266
|
+
if (ownerProp.props.some((it) => it === ctx.name)) {
|
|
267
|
+
throw ctx.createError(`join: relation name and join property '${ctx.name}' cannot have the same name. If you cannot change one or the other, you can use $parseDatabaseJson and $formatDatabaseJson methods to convert the column name.`);
|
|
268
|
+
}
|
|
269
|
+
if (relatedProp.modelClass.getTableName() !== ctx.relatedModelClass.getTableName()) {
|
|
270
|
+
throw ctx.createError('join: either `from` or `to` must point to the related model table.');
|
|
271
|
+
}
|
|
272
|
+
return Object.assign(ctx, { ownerProp, relatedProp });
|
|
344
273
|
}
|
|
345
|
-
|
|
346
274
|
function createRelationProperty(ctx, refString, propName) {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
}
|
|
363
|
-
}
|
|
275
|
+
try {
|
|
276
|
+
return new RelationProperty_1.RelationProperty(refString, (table) => {
|
|
277
|
+
return [ctx.ownerModelClass, ctx.relatedModelClass].find((it) => it.getTableName() === table);
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
catch (err) {
|
|
281
|
+
if (err instanceof RelationProperty_1.RelationProperty.ModelNotFoundError) {
|
|
282
|
+
throw ctx.createError(`join: either \`from\` or \`to\` must point to the owner model table and the other one to the related table. It might be that specified table '${err.tableName}' is not correct`);
|
|
283
|
+
}
|
|
284
|
+
else if (err instanceof RelationProperty_1.RelationProperty.InvalidReferenceError) {
|
|
285
|
+
throw ctx.createError(`${propName} must have format TableName.columnName. For example "SomeTable.id" or in case of composite key ["SomeTable.a", "SomeTable.b"].`);
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
throw err;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
364
291
|
}
|
|
365
|
-
|
|
366
292
|
function parseModify(ctx) {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
});
|
|
375
|
-
|
|
376
|
-
return Object.assign(ctx, { modify });
|
|
293
|
+
const mapping = ctx.mapping;
|
|
294
|
+
const modifier = mapping.modify || mapping.filter;
|
|
295
|
+
const modify = modifier &&
|
|
296
|
+
(0, createModifier_1.createModifier)({
|
|
297
|
+
modifier,
|
|
298
|
+
modelClass: ctx.relatedModelClass,
|
|
299
|
+
});
|
|
300
|
+
return Object.assign(ctx, { modify });
|
|
377
301
|
}
|
|
378
|
-
|
|
379
302
|
function parseBeforeInsert(ctx) {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
return Object.assign(ctx, { beforeInsert });
|
|
303
|
+
let beforeInsert;
|
|
304
|
+
if ((0, objectUtils_1.isFunction)(ctx.mapping.beforeInsert)) {
|
|
305
|
+
beforeInsert = ctx.mapping.beforeInsert;
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
beforeInsert = (model) => model;
|
|
309
|
+
}
|
|
310
|
+
return Object.assign(ctx, { beforeInsert });
|
|
389
311
|
}
|
|
390
|
-
|
|
391
312
|
function aliasedTableName(tableName, alias) {
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
313
|
+
if (tableName === alias) {
|
|
314
|
+
return tableName;
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
return `${tableName} as ${alias}`;
|
|
318
|
+
}
|
|
397
319
|
}
|
|
398
|
-
|
|
399
|
-
module.exports = {
|
|
400
|
-
Relation,
|
|
401
|
-
};
|