@abedmurrar/objection 4.0.0-alpha.0 → 4.0.0-beta.0
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 +279 -363
- package/lib/relations/RelationDeleteOperation.js +16 -22
- package/lib/relations/RelationFindOperation.js +100 -133
- package/lib/relations/RelationInsertOperation.js +19 -27
- package/lib/relations/RelationOwner.js +167 -204
- package/lib/relations/RelationProperty.js +177 -217
- package/lib/relations/RelationUpdateOperation.js +16 -23
- package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +11 -16
- package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +24 -35
- package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +30 -41
- package/lib/relations/belongsToOne/BelongsToOneRelation.js +33 -41
- package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +38 -53
- package/lib/relations/hasMany/HasManyInsertOperation.js +35 -51
- package/lib/relations/hasMany/HasManyRelateOperation.js +38 -54
- package/lib/relations/hasMany/HasManyRelation.js +30 -38
- package/lib/relations/hasMany/HasManyUnrelateOperation.js +19 -26
- package/lib/relations/hasOne/HasOneRelation.js +4 -7
- package/lib/relations/hasOneThrough/HasOneThroughRelation.js +4 -7
- package/lib/relations/manyToMany/ManyToManyModifyMixin.js +67 -90
- package/lib/relations/manyToMany/ManyToManyRelation.js +349 -451
- package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +19 -26
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +3 -5
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +14 -20
- package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +3 -7
- package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +71 -94
- package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +31 -43
- package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +50 -67
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +4 -7
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +20 -27
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +5 -10
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +3 -5
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +55 -72
- package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +3 -7
- 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
package/lib/model/Model.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { clone } = require('./modelClone');
|
|
4
3
|
const { bindKnex } = require('./modelBindKnex');
|
|
5
4
|
const { validate } = require('./modelValidate');
|
|
@@ -14,716 +13,583 @@ const { parseRelationsIntoModelInstances } = require('./modelParseRelations');
|
|
|
14
13
|
const { fetchTableMetadata, tableMetadata } = require('./modelTableMetadata');
|
|
15
14
|
const { asArray, isFunction, isString, asSingle } = require('../utils/objectUtils');
|
|
16
15
|
const { setJson, setFast, setRelated, appendRelated, setDatabaseJson } = require('./modelSet');
|
|
17
|
-
const {
|
|
18
|
-
getJsonAttributes,
|
|
19
|
-
parseJsonAttributes,
|
|
20
|
-
formatJsonAttributes,
|
|
21
|
-
} = require('./modelJsonAttributes');
|
|
16
|
+
const { getJsonAttributes, parseJsonAttributes, formatJsonAttributes, } = require('./modelJsonAttributes');
|
|
22
17
|
const { columnNameToPropertyName, propertyNameToColumnName } = require('./modelColPropMap');
|
|
23
|
-
|
|
24
18
|
const { raw } = require('../queryBuilder/RawBuilder');
|
|
25
19
|
const { ref } = require('../queryBuilder/ReferenceBuilder');
|
|
26
20
|
const { fn } = require('../queryBuilder/FunctionBuilder');
|
|
27
|
-
|
|
28
21
|
const { AjvValidator } = require('./AjvValidator');
|
|
29
22
|
const { QueryBuilder } = require('../queryBuilder/QueryBuilder');
|
|
23
|
+
const { markDefaultStaticHook } = require('../queryBuilder/StaticHookArguments');
|
|
24
|
+
const { setDefaultGetConcurrency } = require('./modelConcurrency');
|
|
30
25
|
const { NotFoundError } = require('./NotFoundError');
|
|
31
26
|
const { ValidationError } = require('./ValidationError');
|
|
32
27
|
const { ModifierNotFoundError } = require('./ModifierNotFoundError');
|
|
33
28
|
const { RelationProperty } = require('../relations/RelationProperty');
|
|
34
29
|
const { RelationOwner } = require('../relations/RelationOwner');
|
|
35
|
-
|
|
36
30
|
const { HasOneRelation } = require('../relations/hasOne/HasOneRelation');
|
|
37
31
|
const { HasManyRelation } = require('../relations/hasMany/HasManyRelation');
|
|
38
32
|
const { ManyToManyRelation } = require('../relations/manyToMany/ManyToManyRelation');
|
|
39
33
|
const { BelongsToOneRelation } = require('../relations/belongsToOne/BelongsToOneRelation');
|
|
40
34
|
const { HasOneThroughRelation } = require('../relations/hasOneThrough/HasOneThroughRelation');
|
|
41
|
-
|
|
42
35
|
const { InstanceFindOperation } = require('../queryBuilder/operations/InstanceFindOperation');
|
|
43
36
|
const { InstanceInsertOperation } = require('../queryBuilder/operations/InstanceInsertOperation');
|
|
44
37
|
const { InstanceUpdateOperation } = require('../queryBuilder/operations/InstanceUpdateOperation');
|
|
45
38
|
const { InstanceDeleteOperation } = require('../queryBuilder/operations/InstanceDeleteOperation');
|
|
46
|
-
|
|
47
39
|
class Model {
|
|
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
|
-
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
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
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
return
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
static get fn() {
|
|
551
|
-
return fn;
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
static knexQuery() {
|
|
555
|
-
return this.knex().table(this.getTableName());
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
static uniqueTag() {
|
|
559
|
-
if (this.name) {
|
|
560
|
-
return `${this.getTableName()}_${this.name}`;
|
|
561
|
-
} else {
|
|
562
|
-
return this.getTableName();
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
static bindKnex(knex) {
|
|
567
|
-
return bindKnex(this, knex);
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
static bindTransaction(trx) {
|
|
571
|
-
return bindKnex(this, trx);
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
static ensureModel(model, options) {
|
|
575
|
-
const modelClass = this;
|
|
576
|
-
|
|
577
|
-
if (!model) {
|
|
578
|
-
return null;
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
if (model instanceof modelClass) {
|
|
582
|
-
return parseRelationsIntoModelInstances(model, model, options);
|
|
583
|
-
} else {
|
|
584
|
-
return modelClass.fromJson(model, options);
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
static ensureModelArray(input, options) {
|
|
589
|
-
if (!input) {
|
|
590
|
-
return [];
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
if (Array.isArray(input)) {
|
|
594
|
-
const models = new Array(input.length);
|
|
595
|
-
|
|
596
|
-
for (let i = 0, l = input.length; i < l; ++i) {
|
|
597
|
-
models[i] = this.ensureModel(input[i], options);
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
return models;
|
|
601
|
-
} else {
|
|
602
|
-
return [this.ensureModel(input, options)];
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
static getRelationUnsafe(name) {
|
|
607
|
-
const mapping = this.getRelationMappings()[name];
|
|
608
|
-
|
|
609
|
-
if (!mapping) {
|
|
610
|
-
return null;
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
if (!this.hasOwnProperty('$$relations')) {
|
|
614
|
-
defineNonEnumerableProperty(this, '$$relations', Object.create(null));
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
if (!this.$$relations[name]) {
|
|
618
|
-
this.$$relations[name] = new mapping.relation(name, this);
|
|
619
|
-
this.$$relations[name].setMapping(mapping);
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
return this.$$relations[name];
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
static getRelation(name) {
|
|
626
|
-
const relation = this.getRelationUnsafe(name);
|
|
627
|
-
|
|
628
|
-
if (!relation) {
|
|
629
|
-
throw new Error(`A model class ${this.name} doesn't have relation ${name}`);
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
return relation;
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
static fetchGraph($models, expression, options = {}) {
|
|
636
|
-
return this.query(options.transaction)
|
|
637
|
-
.resolve(this.ensureModelArray($models))
|
|
638
|
-
.findOptions({ dontCallFindHooks: true })
|
|
639
|
-
.withGraphFetched(expression, options)
|
|
640
|
-
.runAfter((models) => (Array.isArray($models) ? models : models[0]));
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
static traverse(...args) {
|
|
644
|
-
const { traverser, models, filterConstructor } = getTraverseArgs(...args);
|
|
645
|
-
|
|
646
|
-
if (!asSingle(models)) {
|
|
647
|
-
return;
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
const modelClass = asSingle(models).constructor;
|
|
651
|
-
|
|
652
|
-
visitModels(models, modelClass, (model, _, parent, relation) => {
|
|
653
|
-
if (!filterConstructor || model instanceof filterConstructor) {
|
|
654
|
-
traverser(model, parent, relation && relation.name);
|
|
655
|
-
}
|
|
656
|
-
});
|
|
657
|
-
|
|
658
|
-
return this;
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
static traverseAsync(...args) {
|
|
662
|
-
const { traverser, models, filterConstructor } = getTraverseArgs(...args);
|
|
663
|
-
|
|
664
|
-
if (!asSingle(models)) {
|
|
665
|
-
return Promise.resolve();
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
const modelClass = asSingle(models).constructor;
|
|
669
|
-
const promises = [];
|
|
670
|
-
|
|
671
|
-
visitModels(models, modelClass, (model, _, parent, relation) => {
|
|
672
|
-
if (!filterConstructor || model instanceof filterConstructor) {
|
|
673
|
-
const maybePromise = traverser(model, parent, relation && relation.name);
|
|
674
|
-
promises.push(maybePromise);
|
|
675
|
-
}
|
|
676
|
-
});
|
|
677
|
-
|
|
678
|
-
return promiseMap(promises, (it) => it, { concurrency: this.getConcurrency(this.knex()) });
|
|
679
|
-
}
|
|
40
|
+
get $modelClass() {
|
|
41
|
+
return this.constructor;
|
|
42
|
+
}
|
|
43
|
+
$id(maybeId) {
|
|
44
|
+
return getSetId(this, maybeId);
|
|
45
|
+
}
|
|
46
|
+
$hasId() {
|
|
47
|
+
return hasId(this);
|
|
48
|
+
}
|
|
49
|
+
$hasProps(props) {
|
|
50
|
+
return hasProps(this, props);
|
|
51
|
+
}
|
|
52
|
+
$query(trx) {
|
|
53
|
+
return instanceQuery({
|
|
54
|
+
instance: this,
|
|
55
|
+
transaction: trx,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
$relatedQuery(relationName, trx) {
|
|
59
|
+
return relatedQuery({
|
|
60
|
+
modelClass: this.constructor,
|
|
61
|
+
relationName,
|
|
62
|
+
transaction: trx,
|
|
63
|
+
alwaysReturnArray: false,
|
|
64
|
+
}).for(this);
|
|
65
|
+
}
|
|
66
|
+
$fetchGraph(relationExpression, options) {
|
|
67
|
+
return this.constructor.fetchGraph(this, relationExpression, options);
|
|
68
|
+
}
|
|
69
|
+
$beforeValidate(jsonSchema, json, options) {
|
|
70
|
+
/* istanbul ignore next */
|
|
71
|
+
return jsonSchema;
|
|
72
|
+
}
|
|
73
|
+
$validate(json, options) {
|
|
74
|
+
return validate(this, json, options);
|
|
75
|
+
}
|
|
76
|
+
$afterValidate(json, options) {
|
|
77
|
+
// Do nothing by default.
|
|
78
|
+
}
|
|
79
|
+
$parseDatabaseJson(json) {
|
|
80
|
+
const columnNameMappers = this.constructor.getColumnNameMappers();
|
|
81
|
+
if (columnNameMappers) {
|
|
82
|
+
json = columnNameMappers.parse(json);
|
|
83
|
+
}
|
|
84
|
+
return parseJsonAttributes(json, this.constructor);
|
|
85
|
+
}
|
|
86
|
+
$formatDatabaseJson(json) {
|
|
87
|
+
const columnNameMappers = this.constructor.getColumnNameMappers();
|
|
88
|
+
json = formatJsonAttributes(json, this.constructor);
|
|
89
|
+
if (columnNameMappers) {
|
|
90
|
+
json = columnNameMappers.format(json);
|
|
91
|
+
}
|
|
92
|
+
return json;
|
|
93
|
+
}
|
|
94
|
+
$parseJson(json, options) {
|
|
95
|
+
return json;
|
|
96
|
+
}
|
|
97
|
+
$formatJson(json) {
|
|
98
|
+
return json;
|
|
99
|
+
}
|
|
100
|
+
$setJson(json, options) {
|
|
101
|
+
return setJson(this, json, options);
|
|
102
|
+
}
|
|
103
|
+
$setDatabaseJson(json) {
|
|
104
|
+
return setDatabaseJson(this, json);
|
|
105
|
+
}
|
|
106
|
+
$set(obj) {
|
|
107
|
+
return setFast(this, obj);
|
|
108
|
+
}
|
|
109
|
+
$setRelated(relation, models) {
|
|
110
|
+
return setRelated(this, relation, models);
|
|
111
|
+
}
|
|
112
|
+
$appendRelated(relation, models) {
|
|
113
|
+
return appendRelated(this, relation, models);
|
|
114
|
+
}
|
|
115
|
+
$toJson(opt) {
|
|
116
|
+
return toJson(this, opt);
|
|
117
|
+
}
|
|
118
|
+
toJSON(opt) {
|
|
119
|
+
return this.$toJson(opt);
|
|
120
|
+
}
|
|
121
|
+
$toDatabaseJson(builder) {
|
|
122
|
+
return toDatabaseJson(this, builder);
|
|
123
|
+
}
|
|
124
|
+
$beforeInsert(queryContext) {
|
|
125
|
+
// Do nothing by default.
|
|
126
|
+
}
|
|
127
|
+
$afterInsert(queryContext) {
|
|
128
|
+
// Do nothing by default.
|
|
129
|
+
}
|
|
130
|
+
$beforeUpdate(opt, queryContext) {
|
|
131
|
+
// Do nothing by default.
|
|
132
|
+
}
|
|
133
|
+
$afterUpdate(opt, queryContext) {
|
|
134
|
+
// Do nothing by default.
|
|
135
|
+
}
|
|
136
|
+
$afterFind(queryContext) {
|
|
137
|
+
// Do nothing by default.
|
|
138
|
+
}
|
|
139
|
+
$beforeDelete(queryContext) {
|
|
140
|
+
// Do nothing by default.
|
|
141
|
+
}
|
|
142
|
+
$afterDelete(queryContext) {
|
|
143
|
+
// Do nothing by default.
|
|
144
|
+
}
|
|
145
|
+
$values(props) {
|
|
146
|
+
return values(this, props);
|
|
147
|
+
}
|
|
148
|
+
$propKey(props) {
|
|
149
|
+
return propKey(this, props);
|
|
150
|
+
}
|
|
151
|
+
$idKey() {
|
|
152
|
+
return this.$propKey(this.constructor.getIdPropertyArray());
|
|
153
|
+
}
|
|
154
|
+
$clone(opt) {
|
|
155
|
+
return clone(this, !!(opt && opt.shallow), false);
|
|
156
|
+
}
|
|
157
|
+
$traverse(filterConstructor, callback) {
|
|
158
|
+
if (callback === undefined) {
|
|
159
|
+
callback = filterConstructor;
|
|
160
|
+
filterConstructor = null;
|
|
161
|
+
}
|
|
162
|
+
this.constructor.traverse(filterConstructor, this, callback);
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
$traverseAsync(filterConstructor, callback) {
|
|
166
|
+
if (callback === undefined) {
|
|
167
|
+
callback = filterConstructor;
|
|
168
|
+
filterConstructor = null;
|
|
169
|
+
}
|
|
170
|
+
return this.constructor.traverseAsync(filterConstructor, this, callback);
|
|
171
|
+
}
|
|
172
|
+
$omitFromJson(...props) {
|
|
173
|
+
if (arguments.length === 0) {
|
|
174
|
+
return this.$$omitFromJson;
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
if (!Object.hasOwn(this, '$$omitFromJson')) {
|
|
178
|
+
defineNonEnumerableProperty(this, '$$omitFromJson', []);
|
|
179
|
+
}
|
|
180
|
+
this.$$omitFromJson = this.$$omitFromJson.concat(asPropsArray(props));
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
$omitFromDatabaseJson(...props) {
|
|
185
|
+
if (arguments.length === 0) {
|
|
186
|
+
return this.$$omitFromDatabaseJson;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
if (!Object.hasOwn(this, '$$omitFromDatabaseJson')) {
|
|
190
|
+
defineNonEnumerableProperty(this, '$$omitFromDatabaseJson', []);
|
|
191
|
+
}
|
|
192
|
+
this.$$omitFromDatabaseJson = this.$$omitFromDatabaseJson.concat(asPropsArray(props));
|
|
193
|
+
return this;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
$knex() {
|
|
197
|
+
return this.constructor.knex();
|
|
198
|
+
}
|
|
199
|
+
$transaction(...args) {
|
|
200
|
+
return this.constructor.transaction(...args);
|
|
201
|
+
}
|
|
202
|
+
get $ref() {
|
|
203
|
+
return this.constructor.ref;
|
|
204
|
+
}
|
|
205
|
+
static get objectionModelClass() {
|
|
206
|
+
return Model;
|
|
207
|
+
}
|
|
208
|
+
static fromJson(json, options) {
|
|
209
|
+
const model = new this();
|
|
210
|
+
model.$setJson(json || {}, options);
|
|
211
|
+
return model;
|
|
212
|
+
}
|
|
213
|
+
static fromDatabaseJson(json) {
|
|
214
|
+
const model = new this();
|
|
215
|
+
model.$setDatabaseJson(json || {});
|
|
216
|
+
return model;
|
|
217
|
+
}
|
|
218
|
+
static onCreateQuery(builder) {
|
|
219
|
+
// Do nothing by default.
|
|
220
|
+
}
|
|
221
|
+
static beforeFind(args) {
|
|
222
|
+
// Do nothing by default.
|
|
223
|
+
}
|
|
224
|
+
static afterFind(args) {
|
|
225
|
+
// Do nothing by default.
|
|
226
|
+
}
|
|
227
|
+
static beforeInsert(args) {
|
|
228
|
+
// Do nothing by default.
|
|
229
|
+
}
|
|
230
|
+
static afterInsert(args) {
|
|
231
|
+
// Do nothing by default.
|
|
232
|
+
}
|
|
233
|
+
static beforeUpdate(args) {
|
|
234
|
+
// Do nothing by default.
|
|
235
|
+
}
|
|
236
|
+
static afterUpdate(args) {
|
|
237
|
+
// Do nothing by default.
|
|
238
|
+
}
|
|
239
|
+
static beforeDelete(args) {
|
|
240
|
+
// Do nothing by default.
|
|
241
|
+
}
|
|
242
|
+
static afterDelete(args) {
|
|
243
|
+
// Do nothing by default.
|
|
244
|
+
}
|
|
245
|
+
static omitImpl(obj, prop) {
|
|
246
|
+
delete obj[prop];
|
|
247
|
+
}
|
|
248
|
+
static joinTableAlias(relationPath) {
|
|
249
|
+
return `${relationPath}_join`;
|
|
250
|
+
}
|
|
251
|
+
static createValidator() {
|
|
252
|
+
return new AjvValidator({
|
|
253
|
+
options: {
|
|
254
|
+
allErrors: true,
|
|
255
|
+
validateSchema: false,
|
|
256
|
+
ownProperties: true,
|
|
257
|
+
v5: true,
|
|
258
|
+
},
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
static modifierNotFound(builder, modifier) {
|
|
262
|
+
throw new this.ModifierNotFoundError(modifier);
|
|
263
|
+
}
|
|
264
|
+
static createNotFoundError(queryContext, props) {
|
|
265
|
+
return new this.NotFoundError({ ...props, modelClass: this });
|
|
266
|
+
}
|
|
267
|
+
static createValidationError(props) {
|
|
268
|
+
return new this.ValidationError({ ...props, modelClass: this });
|
|
269
|
+
}
|
|
270
|
+
static getTableName() {
|
|
271
|
+
let tableName = this.tableName;
|
|
272
|
+
if (isFunction(tableName)) {
|
|
273
|
+
tableName = this.tableName();
|
|
274
|
+
}
|
|
275
|
+
if (!isString(tableName)) {
|
|
276
|
+
throw new Error(`Model ${this.name} must have a static property tableName`);
|
|
277
|
+
}
|
|
278
|
+
return tableName;
|
|
279
|
+
}
|
|
280
|
+
static getIdColumn() {
|
|
281
|
+
let idColumn = this.idColumn;
|
|
282
|
+
if (isFunction(idColumn)) {
|
|
283
|
+
idColumn = this.idColumn();
|
|
284
|
+
}
|
|
285
|
+
return idColumn;
|
|
286
|
+
}
|
|
287
|
+
static getValidator() {
|
|
288
|
+
return cachedGet(this, '$$validator', getValidator);
|
|
289
|
+
}
|
|
290
|
+
static getJsonSchema() {
|
|
291
|
+
return cachedGet(this, '$$jsonSchema', getJsonSchema);
|
|
292
|
+
}
|
|
293
|
+
static getJsonAttributes() {
|
|
294
|
+
return cachedGet(this, '$$jsonAttributes', getJsonAttributes);
|
|
295
|
+
}
|
|
296
|
+
static getColumnNameMappers() {
|
|
297
|
+
return cachedGet(this, '$$columnNameMappers', getColumnNameMappers);
|
|
298
|
+
}
|
|
299
|
+
static getConcurrency(knex) {
|
|
300
|
+
const DEFAULT_CONCURRENCY = 4;
|
|
301
|
+
if (this.concurrency === null) {
|
|
302
|
+
if (!knex) {
|
|
303
|
+
return DEFAULT_CONCURRENCY;
|
|
304
|
+
}
|
|
305
|
+
// The mssql driver is shit, and we cannot have concurrent queries.
|
|
306
|
+
// On MySQL, concurrent graph updates and deletes deadlock: MySQL needs
|
|
307
|
+
// them wrapped in a subquery, which locks the rows of the join tables
|
|
308
|
+
// it reads.
|
|
309
|
+
if (isMsSql(knex) || isMySql(knex)) {
|
|
310
|
+
return 1;
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
return DEFAULT_CONCURRENCY;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
if (isFunction(this.concurrency)) {
|
|
318
|
+
return this.concurrency();
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
return this.concurrency;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
static getModifiers() {
|
|
326
|
+
return this.modifiers || {};
|
|
327
|
+
}
|
|
328
|
+
static columnNameToPropertyName(columnName) {
|
|
329
|
+
const colToProp = cachedGet(this, '$$colToProp', () => new Map());
|
|
330
|
+
let propertyName = colToProp.get(columnName);
|
|
331
|
+
if (!propertyName) {
|
|
332
|
+
propertyName = columnNameToPropertyName(this, columnName);
|
|
333
|
+
colToProp.set(columnName, propertyName);
|
|
334
|
+
}
|
|
335
|
+
return propertyName;
|
|
336
|
+
}
|
|
337
|
+
static propertyNameToColumnName(propertyName) {
|
|
338
|
+
const propToCol = cachedGet(this, '$$propToCol', () => new Map());
|
|
339
|
+
let columnName = propToCol.get(propertyName);
|
|
340
|
+
if (!columnName) {
|
|
341
|
+
columnName = propertyNameToColumnName(this, propertyName);
|
|
342
|
+
propToCol.set(propertyName, columnName);
|
|
343
|
+
}
|
|
344
|
+
return columnName;
|
|
345
|
+
}
|
|
346
|
+
static getReadOnlyAttributes() {
|
|
347
|
+
return cachedGet(this, '$$readOnlyAttributes', getReadOnlyAttributes);
|
|
348
|
+
}
|
|
349
|
+
static getIdRelationProperty() {
|
|
350
|
+
return cachedGet(this, '$$idRelationProperty', getIdRelationProperty);
|
|
351
|
+
}
|
|
352
|
+
static getIdColumnArray() {
|
|
353
|
+
return this.getIdRelationProperty().cols;
|
|
354
|
+
}
|
|
355
|
+
static getIdPropertyArray() {
|
|
356
|
+
return this.getIdRelationProperty().props;
|
|
357
|
+
}
|
|
358
|
+
static getIdProperty() {
|
|
359
|
+
const idProps = this.getIdPropertyArray();
|
|
360
|
+
if (idProps.length === 1) {
|
|
361
|
+
return idProps[0];
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
return idProps;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
static getRelationMappings() {
|
|
368
|
+
return cachedGet(this, '$$relationMappings', getRelationMappings);
|
|
369
|
+
}
|
|
370
|
+
static getRelations() {
|
|
371
|
+
const relations = Object.create(null);
|
|
372
|
+
for (const relationName of this.getRelationNames()) {
|
|
373
|
+
relations[relationName] = this.getRelation(relationName);
|
|
374
|
+
}
|
|
375
|
+
return relations;
|
|
376
|
+
}
|
|
377
|
+
static getRelationNames() {
|
|
378
|
+
return cachedGet(this, '$$relationNames', getRelationNames);
|
|
379
|
+
}
|
|
380
|
+
static getVirtualAttributes() {
|
|
381
|
+
return cachedGet(this, '$$virtualAttributes', getVirtualAttributes);
|
|
382
|
+
}
|
|
383
|
+
static getDefaultGraphOptions() {
|
|
384
|
+
return this.defaultGraphOptions;
|
|
385
|
+
}
|
|
386
|
+
static getRelatedFindQueryMutates() {
|
|
387
|
+
return this.relatedFindQueryMutates;
|
|
388
|
+
}
|
|
389
|
+
static getRelatedInsertQueryMutates() {
|
|
390
|
+
return this.relatedInsertQueryMutates;
|
|
391
|
+
}
|
|
392
|
+
static query(trx) {
|
|
393
|
+
const query = this.QueryBuilder.forClass(this).transacting(trx);
|
|
394
|
+
this.onCreateQuery(query);
|
|
395
|
+
return query;
|
|
396
|
+
}
|
|
397
|
+
static relatedQuery(relationName, trx) {
|
|
398
|
+
return relatedQuery({
|
|
399
|
+
modelClass: this,
|
|
400
|
+
relationName,
|
|
401
|
+
transaction: trx,
|
|
402
|
+
alwaysReturnArray: true,
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
static fetchTableMetadata(opt) {
|
|
406
|
+
return fetchTableMetadata(this, opt);
|
|
407
|
+
}
|
|
408
|
+
static tableMetadata(opt) {
|
|
409
|
+
return tableMetadata(this, opt);
|
|
410
|
+
}
|
|
411
|
+
static knex(...args) {
|
|
412
|
+
if (args.length) {
|
|
413
|
+
defineNonEnumerableProperty(this, '$$knex', args[0]);
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
return this.$$knex;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
static transaction(knexOrTrx, cb) {
|
|
420
|
+
if (!cb) {
|
|
421
|
+
cb = knexOrTrx;
|
|
422
|
+
knexOrTrx = null;
|
|
423
|
+
}
|
|
424
|
+
return (knexOrTrx || this.knex()).transaction(cb);
|
|
425
|
+
}
|
|
426
|
+
static startTransaction(knexOrTrx) {
|
|
427
|
+
const { transaction } = require('../transaction');
|
|
428
|
+
return transaction.start(knexOrTrx || this.knex());
|
|
429
|
+
}
|
|
430
|
+
static get raw() {
|
|
431
|
+
return raw;
|
|
432
|
+
}
|
|
433
|
+
static get ref() {
|
|
434
|
+
return (...args) => {
|
|
435
|
+
return ref(...args).model(this);
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
static get fn() {
|
|
439
|
+
return fn;
|
|
440
|
+
}
|
|
441
|
+
static knexQuery() {
|
|
442
|
+
return this.knex().table(this.getTableName());
|
|
443
|
+
}
|
|
444
|
+
static uniqueTag() {
|
|
445
|
+
if (this.name) {
|
|
446
|
+
return `${this.getTableName()}_${this.name}`;
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
return this.getTableName();
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
static bindKnex(knex) {
|
|
453
|
+
return bindKnex(this, knex);
|
|
454
|
+
}
|
|
455
|
+
static bindTransaction(trx) {
|
|
456
|
+
return bindKnex(this, trx);
|
|
457
|
+
}
|
|
458
|
+
static ensureModel(model, options) {
|
|
459
|
+
const modelClass = this;
|
|
460
|
+
if (!model) {
|
|
461
|
+
return null;
|
|
462
|
+
}
|
|
463
|
+
if (model instanceof modelClass) {
|
|
464
|
+
return parseRelationsIntoModelInstances(model, model, options);
|
|
465
|
+
}
|
|
466
|
+
else {
|
|
467
|
+
return modelClass.fromJson(model, options);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
static ensureModelArray(input, options) {
|
|
471
|
+
if (!input) {
|
|
472
|
+
return [];
|
|
473
|
+
}
|
|
474
|
+
if (Array.isArray(input)) {
|
|
475
|
+
const models = new Array(input.length);
|
|
476
|
+
for (let i = 0, l = input.length; i < l; ++i) {
|
|
477
|
+
models[i] = this.ensureModel(input[i], options);
|
|
478
|
+
}
|
|
479
|
+
return models;
|
|
480
|
+
}
|
|
481
|
+
else {
|
|
482
|
+
return [this.ensureModel(input, options)];
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
static getRelationUnsafe(name) {
|
|
486
|
+
const mapping = this.getRelationMappings()[name];
|
|
487
|
+
if (!mapping) {
|
|
488
|
+
return null;
|
|
489
|
+
}
|
|
490
|
+
if (!Object.hasOwn(this, '$$relations')) {
|
|
491
|
+
defineNonEnumerableProperty(this, '$$relations', Object.create(null));
|
|
492
|
+
}
|
|
493
|
+
if (!this.$$relations[name]) {
|
|
494
|
+
this.$$relations[name] = new mapping.relation(name, this);
|
|
495
|
+
this.$$relations[name].setMapping(mapping);
|
|
496
|
+
}
|
|
497
|
+
return this.$$relations[name];
|
|
498
|
+
}
|
|
499
|
+
static getRelation(name) {
|
|
500
|
+
const relation = this.getRelationUnsafe(name);
|
|
501
|
+
if (!relation) {
|
|
502
|
+
throw new Error(`A model class ${this.name} doesn't have relation ${name}`);
|
|
503
|
+
}
|
|
504
|
+
return relation;
|
|
505
|
+
}
|
|
506
|
+
static fetchGraph($models, expression, options = {}) {
|
|
507
|
+
return this.query(options.transaction)
|
|
508
|
+
.resolve(this.ensureModelArray($models))
|
|
509
|
+
.findOptions({ dontCallFindHooks: true })
|
|
510
|
+
.withGraphFetched(expression, options)
|
|
511
|
+
.runAfter((models) => (Array.isArray($models) ? models : models[0]));
|
|
512
|
+
}
|
|
513
|
+
static traverse(...args) {
|
|
514
|
+
const { traverser, models, filterConstructor } = getTraverseArgs(...args);
|
|
515
|
+
if (!asSingle(models)) {
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
const modelClass = asSingle(models).constructor;
|
|
519
|
+
visitModels(models, modelClass, (model, _, parent, relation) => {
|
|
520
|
+
if (!filterConstructor || model instanceof filterConstructor) {
|
|
521
|
+
traverser(model, parent, relation && relation.name);
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
return this;
|
|
525
|
+
}
|
|
526
|
+
static traverseAsync(...args) {
|
|
527
|
+
const { traverser, models, filterConstructor } = getTraverseArgs(...args);
|
|
528
|
+
if (!asSingle(models)) {
|
|
529
|
+
return Promise.resolve();
|
|
530
|
+
}
|
|
531
|
+
const modelClass = asSingle(models).constructor;
|
|
532
|
+
const promises = [];
|
|
533
|
+
visitModels(models, modelClass, (model, _, parent, relation) => {
|
|
534
|
+
if (!filterConstructor || model instanceof filterConstructor) {
|
|
535
|
+
const maybePromise = traverser(model, parent, relation && relation.name);
|
|
536
|
+
promises.push(maybePromise);
|
|
537
|
+
}
|
|
538
|
+
});
|
|
539
|
+
return promiseMap(promises, (it) => it, { concurrency: this.getConcurrency(this.knex()) });
|
|
540
|
+
}
|
|
680
541
|
}
|
|
681
|
-
|
|
682
542
|
Object.defineProperties(Model, {
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
543
|
+
isObjectionModelClass: {
|
|
544
|
+
enumerable: false,
|
|
545
|
+
writable: false,
|
|
546
|
+
value: true,
|
|
547
|
+
},
|
|
688
548
|
});
|
|
689
|
-
|
|
690
549
|
Object.defineProperties(Model.prototype, {
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
},
|
|
550
|
+
$isObjectionModel: {
|
|
551
|
+
enumerable: false,
|
|
552
|
+
writable: false,
|
|
553
|
+
value: true,
|
|
554
|
+
},
|
|
555
|
+
$objectionModelClass: {
|
|
556
|
+
enumerable: false,
|
|
557
|
+
writable: false,
|
|
558
|
+
value: Model,
|
|
559
|
+
},
|
|
702
560
|
});
|
|
703
|
-
|
|
561
|
+
for (const hookName of [
|
|
562
|
+
'beforeFind',
|
|
563
|
+
'afterFind',
|
|
564
|
+
'beforeInsert',
|
|
565
|
+
'afterInsert',
|
|
566
|
+
'beforeUpdate',
|
|
567
|
+
'afterUpdate',
|
|
568
|
+
'beforeDelete',
|
|
569
|
+
'afterDelete',
|
|
570
|
+
]) {
|
|
571
|
+
markDefaultStaticHook(Model[hookName]);
|
|
572
|
+
}
|
|
573
|
+
setDefaultGetConcurrency(Model.getConcurrency);
|
|
704
574
|
Model.QueryBuilder = QueryBuilder;
|
|
705
|
-
|
|
706
575
|
Model.HasOneRelation = HasOneRelation;
|
|
707
576
|
Model.HasManyRelation = HasManyRelation;
|
|
708
577
|
Model.ManyToManyRelation = ManyToManyRelation;
|
|
709
578
|
Model.BelongsToOneRelation = BelongsToOneRelation;
|
|
710
579
|
Model.HasOneThroughRelation = HasOneThroughRelation;
|
|
711
|
-
|
|
712
580
|
Model.JoinEagerAlgorithm = 'JoinEagerAlgorithm';
|
|
713
581
|
Model.NaiveEagerAlgorithm = 'NaiveEagerAlgorithm';
|
|
714
582
|
Model.WhereInEagerAlgorithm = 'WhereInEagerAlgorithm';
|
|
715
|
-
|
|
716
583
|
Model.ValidationError = ValidationError;
|
|
717
584
|
Model.NotFoundError = NotFoundError;
|
|
718
585
|
Model.ModifierNotFoundError = ModifierNotFoundError;
|
|
719
|
-
|
|
720
586
|
Model.tableName = null;
|
|
721
587
|
Model.jsonSchema = null;
|
|
722
588
|
Model.idColumn = 'id';
|
|
723
589
|
Model.uidProp = '#id';
|
|
724
590
|
Model.uidRefProp = '#ref';
|
|
725
591
|
Model.dbRefProp = '#dbRef';
|
|
726
|
-
Model.propRefRegex = /#ref{([
|
|
592
|
+
Model.propRefRegex = /#ref{([^.]+)\.([^}]+)}/g;
|
|
727
593
|
Model.jsonAttributes = null;
|
|
728
594
|
Model.cloneObjectAttributes = true;
|
|
729
595
|
Model.virtualAttributes = null;
|
|
@@ -738,205 +604,169 @@ Model.columnNameMappers = null;
|
|
|
738
604
|
Model.relatedFindQueryMutates = false;
|
|
739
605
|
Model.relatedInsertQueryMutates = false;
|
|
740
606
|
Model.concurrency = null;
|
|
741
|
-
|
|
742
607
|
function instanceQuery({ instance, transaction }) {
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
return new InstanceFindOperation('find', { instance });
|
|
608
|
+
const modelClass = instance.constructor;
|
|
609
|
+
return modelClass
|
|
610
|
+
.query(transaction)
|
|
611
|
+
.findOperationFactory(() => {
|
|
612
|
+
return new InstanceFindOperation('find', { instance });
|
|
749
613
|
})
|
|
750
|
-
|
|
751
|
-
|
|
614
|
+
.insertOperationFactory(() => {
|
|
615
|
+
return new InstanceInsertOperation('insert', { instance });
|
|
752
616
|
})
|
|
753
|
-
|
|
754
|
-
|
|
617
|
+
.updateOperationFactory(() => {
|
|
618
|
+
return new InstanceUpdateOperation('update', { instance });
|
|
755
619
|
})
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
620
|
+
.patchOperationFactory(() => {
|
|
621
|
+
return new InstanceUpdateOperation('patch', {
|
|
622
|
+
instance,
|
|
623
|
+
modelOptions: { patch: true },
|
|
624
|
+
});
|
|
761
625
|
})
|
|
762
|
-
|
|
763
|
-
|
|
626
|
+
.deleteOperationFactory(() => {
|
|
627
|
+
return new InstanceDeleteOperation('delete', { instance });
|
|
764
628
|
})
|
|
765
|
-
|
|
766
|
-
|
|
629
|
+
.relateOperationFactory(() => {
|
|
630
|
+
throw new Error('`relate` makes no sense in this context');
|
|
767
631
|
})
|
|
768
|
-
|
|
769
|
-
|
|
632
|
+
.unrelateOperationFactory(() => {
|
|
633
|
+
throw new Error('`unrelate` makes no sense in this context');
|
|
770
634
|
});
|
|
771
635
|
}
|
|
772
|
-
|
|
773
636
|
function relatedQuery({ modelClass, relationName, transaction, alwaysReturnArray } = {}) {
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
operation.alwaysReturnArray = alwaysReturnArray;
|
|
789
|
-
operation.alias = isSubQuery ? relation.name : null;
|
|
790
|
-
|
|
791
|
-
return operation;
|
|
637
|
+
const relation = modelClass.getRelation(relationName);
|
|
638
|
+
const relatedModelClass = relation.relatedModelClass;
|
|
639
|
+
const query = relatedModelClass
|
|
640
|
+
.query(transaction)
|
|
641
|
+
.findOperationFactory((builder) => {
|
|
642
|
+
const isSubQuery = !builder.for();
|
|
643
|
+
const owner = isSubQuery
|
|
644
|
+
? RelationOwner.createParentReference(builder, relation)
|
|
645
|
+
: RelationOwner.create(builder.for());
|
|
646
|
+
const operation = relation.find(builder, owner);
|
|
647
|
+
operation.assignResultToOwner = modelClass.getRelatedFindQueryMutates();
|
|
648
|
+
operation.alwaysReturnArray = alwaysReturnArray;
|
|
649
|
+
operation.alias = isSubQuery ? relation.name : null;
|
|
650
|
+
return operation;
|
|
792
651
|
})
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
return operation;
|
|
652
|
+
.insertOperationFactory((builder) => {
|
|
653
|
+
const owner = writeOperationOwner(builder, 'insert');
|
|
654
|
+
const operation = relation.insert(builder, owner);
|
|
655
|
+
operation.assignResultToOwner = modelClass.getRelatedInsertQueryMutates();
|
|
656
|
+
return operation;
|
|
799
657
|
})
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
658
|
+
.updateOperationFactory((builder) => {
|
|
659
|
+
const owner = writeOperationOwner(builder, 'update');
|
|
660
|
+
return relation.update(builder, owner);
|
|
803
661
|
})
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
662
|
+
.patchOperationFactory((builder) => {
|
|
663
|
+
const owner = writeOperationOwner(builder, 'patch');
|
|
664
|
+
return relation.patch(builder, owner);
|
|
807
665
|
})
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
666
|
+
.deleteOperationFactory((builder) => {
|
|
667
|
+
const owner = writeOperationOwner(builder, 'delete');
|
|
668
|
+
return relation.delete(builder, owner);
|
|
811
669
|
})
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
670
|
+
.relateOperationFactory((builder) => {
|
|
671
|
+
const owner = writeOperationOwner(builder, 'relate');
|
|
672
|
+
return relation.relate(builder, owner);
|
|
815
673
|
})
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
674
|
+
.unrelateOperationFactory((builder) => {
|
|
675
|
+
const owner = writeOperationOwner(builder, 'unrelate');
|
|
676
|
+
return relation.unrelate(builder, owner);
|
|
819
677
|
});
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
return query;
|
|
678
|
+
query._isRelatedQuery = true;
|
|
679
|
+
return query;
|
|
823
680
|
}
|
|
824
|
-
|
|
825
681
|
function writeOperationOwner(builder, method) {
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
return RelationOwner.create(owner);
|
|
682
|
+
const owner = builder.for();
|
|
683
|
+
if ((owner === null || owner === undefined) && builder._forError === null) {
|
|
684
|
+
// Without an owner the write would silently do nothing or, in the case
|
|
685
|
+
// of inserts, create rows with null foreign keys. The error is thrown
|
|
686
|
+
// when the query is built or executed.
|
|
687
|
+
builder._forError = new Error(`relatedQuery(): for() must be called before ${method}()`);
|
|
688
|
+
}
|
|
689
|
+
return RelationOwner.create(owner);
|
|
836
690
|
}
|
|
837
|
-
|
|
838
691
|
function cachedGet(target, hiddenPropertyName, creator) {
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
return target[hiddenPropertyName];
|
|
692
|
+
if (!Object.hasOwn(target, hiddenPropertyName)) {
|
|
693
|
+
defineNonEnumerableProperty(target, hiddenPropertyName, creator(target));
|
|
694
|
+
}
|
|
695
|
+
return target[hiddenPropertyName];
|
|
844
696
|
}
|
|
845
|
-
|
|
846
697
|
function getValidator(modelClass) {
|
|
847
|
-
|
|
698
|
+
return modelClass.createValidator();
|
|
848
699
|
}
|
|
849
|
-
|
|
850
700
|
function getJsonSchema(modelClass) {
|
|
851
|
-
|
|
701
|
+
return modelClass.jsonSchema;
|
|
852
702
|
}
|
|
853
|
-
|
|
854
703
|
function getColumnNameMappers(modelClass) {
|
|
855
|
-
|
|
704
|
+
return modelClass.columnNameMappers;
|
|
856
705
|
}
|
|
857
|
-
|
|
858
706
|
function getIdRelationProperty(modelClass) {
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
return new RelationProperty(
|
|
862
|
-
idColumn.map((idCol) => `${modelClass.getTableName()}.${idCol}`),
|
|
863
|
-
() => modelClass,
|
|
864
|
-
);
|
|
707
|
+
const idColumn = asArray(modelClass.getIdColumn());
|
|
708
|
+
return new RelationProperty(idColumn.map((idCol) => `${modelClass.getTableName()}.${idCol}`), () => modelClass);
|
|
865
709
|
}
|
|
866
|
-
|
|
867
710
|
function getReadOnlyAttributes(modelClass) {
|
|
868
|
-
|
|
711
|
+
return [...new Set(getReadOnlyAttributesRecursively(modelClass))];
|
|
869
712
|
}
|
|
870
|
-
|
|
871
713
|
function getReadOnlyAttributesRecursively(modelClass) {
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
}),
|
|
885
|
-
];
|
|
714
|
+
if (modelClass === Model || modelClass.prototype == undefined) {
|
|
715
|
+
// Stop recursion to the model class or its prototype is null or undefined.
|
|
716
|
+
return [];
|
|
717
|
+
}
|
|
718
|
+
const propertyNames = Object.getOwnPropertyNames(modelClass.prototype);
|
|
719
|
+
return [
|
|
720
|
+
...getReadOnlyAttributes(Object.getPrototypeOf(modelClass)),
|
|
721
|
+
...propertyNames.filter((propName) => {
|
|
722
|
+
const desc = Object.getOwnPropertyDescriptor(modelClass.prototype, propName);
|
|
723
|
+
return (desc.get && !desc.set) || desc.writable === false || isFunction(desc.value);
|
|
724
|
+
}),
|
|
725
|
+
];
|
|
886
726
|
}
|
|
887
|
-
|
|
888
727
|
function getRelationMappings(modelClass) {
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
return relationMappings || {};
|
|
728
|
+
let relationMappings = modelClass.relationMappings;
|
|
729
|
+
if (isFunction(relationMappings)) {
|
|
730
|
+
relationMappings = relationMappings.call(modelClass);
|
|
731
|
+
}
|
|
732
|
+
return relationMappings || {};
|
|
896
733
|
}
|
|
897
|
-
|
|
898
734
|
function getRelationNames(modelClass) {
|
|
899
|
-
|
|
735
|
+
return Object.keys(modelClass.getRelationMappings());
|
|
900
736
|
}
|
|
901
|
-
|
|
902
737
|
function getVirtualAttributes(modelClass) {
|
|
903
|
-
|
|
738
|
+
return modelClass.virtualAttributes || [];
|
|
904
739
|
}
|
|
905
|
-
|
|
906
740
|
function getTraverseArgs(filterConstructor, models, traverser) {
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
models,
|
|
922
|
-
filterConstructor,
|
|
923
|
-
};
|
|
741
|
+
filterConstructor = filterConstructor || null;
|
|
742
|
+
if (traverser === undefined) {
|
|
743
|
+
traverser = models;
|
|
744
|
+
models = filterConstructor;
|
|
745
|
+
filterConstructor = null;
|
|
746
|
+
}
|
|
747
|
+
if (!isFunction(traverser)) {
|
|
748
|
+
throw new Error('traverser must be a function');
|
|
749
|
+
}
|
|
750
|
+
return {
|
|
751
|
+
traverser,
|
|
752
|
+
models,
|
|
753
|
+
filterConstructor,
|
|
754
|
+
};
|
|
924
755
|
}
|
|
925
|
-
|
|
926
756
|
function asPropsArray(props) {
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
757
|
+
if (props.length === 1) {
|
|
758
|
+
const arg = props[0];
|
|
759
|
+
if (Array.isArray(arg)) {
|
|
760
|
+
return arg;
|
|
761
|
+
}
|
|
762
|
+
else if (arg && typeof arg === 'object') {
|
|
763
|
+
return Object.entries(arg)
|
|
764
|
+
.filter(([, value]) => value)
|
|
765
|
+
.map(([key]) => key);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
return props;
|
|
938
769
|
}
|
|
939
|
-
|
|
940
770
|
module.exports = {
|
|
941
|
-
|
|
771
|
+
Model,
|
|
942
772
|
};
|