@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
|
@@ -1,240 +1,201 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { ModelGraphEdge } = require('./ModelGraphEdge');
|
|
4
3
|
const { isNumber } = require('../../utils/objectUtils');
|
|
5
|
-
|
|
6
4
|
const NOT_CALCULATED = {};
|
|
7
|
-
|
|
8
5
|
class ModelGraphNode {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
return this._idPath;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
get idPathKey() {
|
|
114
|
-
if (this._idPathKey === NOT_CALCULATED) {
|
|
115
|
-
this._idPathKey = this._createIdPathKey();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return this._idPathKey;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* If this node is a reference, returns the referred node.
|
|
123
|
-
*/
|
|
124
|
-
get referencedNode() {
|
|
125
|
-
for (const edge of this.refEdges) {
|
|
126
|
-
if (edge.refType === ModelGraphEdge.ReferenceType.Object && edge.isOwnerNode(this)) {
|
|
127
|
-
return edge.relatedNode;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Returns all nodes that are references to this node.
|
|
136
|
-
*/
|
|
137
|
-
get referencingNodes() {
|
|
138
|
-
const nodes = [];
|
|
139
|
-
|
|
140
|
-
for (const edge of this.refEdges) {
|
|
141
|
-
if (edge.refType === ModelGraphEdge.ReferenceType.Object && edge.isRelatedNode(this)) {
|
|
142
|
-
nodes.push(edge.ownerNode);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return nodes;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
get descendantRelationNodes() {
|
|
150
|
-
return this._collectDescendantRelationNodes([]);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
removeEdge(edge) {
|
|
154
|
-
// Don't allow removing parent edges for now. It would
|
|
155
|
-
// cause all kinds of cache invalidation.
|
|
156
|
-
if (edge === this.parentEdge) {
|
|
157
|
-
throw new Error('cannot remove parent edge');
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
this.edges = this.edges.filter((it) => it !== edge);
|
|
161
|
-
this.refEdges = this.refEdges.filter((it) => it !== edge);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
_collectDescendantRelationNodes(nodes) {
|
|
165
|
-
for (const edge of this.edges) {
|
|
166
|
-
if (edge.type === ModelGraphEdge.Type.Relation && edge.isOwnerNode(this)) {
|
|
167
|
-
nodes.push(edge.relatedNode);
|
|
168
|
-
edge.relatedNode._collectDescendantRelationNodes(nodes);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return nodes;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
_createRelationPath() {
|
|
176
|
-
if (this.parentNode === null) {
|
|
177
|
-
return [];
|
|
178
|
-
} else {
|
|
179
|
-
return [...this.parentNode.relationPath, this.relationName];
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
_createRelationPathKey() {
|
|
184
|
-
return this.relationPath.join('.');
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
_createDataPath() {
|
|
188
|
-
if (this.parentEdge === null) {
|
|
189
|
-
return [];
|
|
190
|
-
} else if (this.parentEdge.relation.isOneToOne()) {
|
|
191
|
-
return [...this.parentNode.dataPath, this.relationName];
|
|
192
|
-
} else {
|
|
193
|
-
return [...this.parentNode.dataPath, this.relationName, this.indexInRelation];
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
_createDataPathKey() {
|
|
198
|
-
const dataPathKey = this.dataPath.reduce((key, it) => {
|
|
199
|
-
if (isNumber(it)) {
|
|
200
|
-
return `${key}[${it}]`;
|
|
201
|
-
} else {
|
|
202
|
-
return key ? `${key}.${it}` : it;
|
|
203
|
-
}
|
|
204
|
-
}, '');
|
|
205
|
-
|
|
206
|
-
return dataPathKey ? '.' + dataPathKey : dataPathKey;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
_createIdPath() {
|
|
210
|
-
if (!this.obj.$hasId()) {
|
|
211
|
-
return null;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
if (this.parentEdge === null) {
|
|
215
|
-
return [this.obj.$idKey()];
|
|
216
|
-
} else {
|
|
217
|
-
const path = this.parentNode.idPath;
|
|
218
|
-
|
|
219
|
-
if (path === null) {
|
|
6
|
+
constructor(modelClass, obj) {
|
|
7
|
+
this.modelClass = modelClass;
|
|
8
|
+
this.obj = obj;
|
|
9
|
+
this.edges = [];
|
|
10
|
+
this.userData = {};
|
|
11
|
+
this.hasId = obj.$hasId();
|
|
12
|
+
this.uid = obj[modelClass.uidProp];
|
|
13
|
+
// These are also included in `edges`. These are simply
|
|
14
|
+
// shortcuts for commonly used edges.
|
|
15
|
+
this.refEdges = [];
|
|
16
|
+
this.parentEdge = null;
|
|
17
|
+
// These are calculated lazily.
|
|
18
|
+
this._relationPath = NOT_CALCULATED;
|
|
19
|
+
this._relationPathKey = NOT_CALCULATED;
|
|
20
|
+
this._dataPath = NOT_CALCULATED;
|
|
21
|
+
this._dataPathKey = NOT_CALCULATED;
|
|
22
|
+
this._idPath = NOT_CALCULATED;
|
|
23
|
+
this._idPathKey = NOT_CALCULATED;
|
|
24
|
+
}
|
|
25
|
+
get isReference() {
|
|
26
|
+
return this.reference !== undefined;
|
|
27
|
+
}
|
|
28
|
+
get isDbReference() {
|
|
29
|
+
return this.dbReference !== undefined;
|
|
30
|
+
}
|
|
31
|
+
get reference() {
|
|
32
|
+
return this.obj[this.modelClass.uidRefProp];
|
|
33
|
+
}
|
|
34
|
+
get dbReference() {
|
|
35
|
+
return this.obj[this.modelClass.dbRefProp];
|
|
36
|
+
}
|
|
37
|
+
get parentNode() {
|
|
38
|
+
if (this.parentEdge) {
|
|
39
|
+
return this.parentEdge.ownerNode;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
get indexInRelation() {
|
|
46
|
+
if (this.parentEdge) {
|
|
47
|
+
return this.parentEdge.relationIndex;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
get relationName() {
|
|
54
|
+
if (this.parentEdge) {
|
|
55
|
+
return this.parentEdge.relation.name;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
get relationPath() {
|
|
62
|
+
if (this._relationPath === NOT_CALCULATED) {
|
|
63
|
+
this._relationPath = this._createRelationPath();
|
|
64
|
+
}
|
|
65
|
+
return this._relationPath;
|
|
66
|
+
}
|
|
67
|
+
get relationPathKey() {
|
|
68
|
+
if (this._relationPathKey === NOT_CALCULATED) {
|
|
69
|
+
this._relationPathKey = this._createRelationPathKey();
|
|
70
|
+
}
|
|
71
|
+
return this._relationPathKey;
|
|
72
|
+
}
|
|
73
|
+
get dataPath() {
|
|
74
|
+
if (this._dataPath === NOT_CALCULATED) {
|
|
75
|
+
this._dataPath = this._createDataPath();
|
|
76
|
+
}
|
|
77
|
+
return this._dataPath;
|
|
78
|
+
}
|
|
79
|
+
get dataPathKey() {
|
|
80
|
+
if (this._dataPathKey === NOT_CALCULATED) {
|
|
81
|
+
this._dataPathKey = this._createDataPathKey();
|
|
82
|
+
}
|
|
83
|
+
return this._dataPathKey;
|
|
84
|
+
}
|
|
85
|
+
get idPath() {
|
|
86
|
+
if (this._idPath === NOT_CALCULATED) {
|
|
87
|
+
this._idPath = this._createIdPath();
|
|
88
|
+
}
|
|
89
|
+
return this._idPath;
|
|
90
|
+
}
|
|
91
|
+
get idPathKey() {
|
|
92
|
+
if (this._idPathKey === NOT_CALCULATED) {
|
|
93
|
+
this._idPathKey = this._createIdPathKey();
|
|
94
|
+
}
|
|
95
|
+
return this._idPathKey;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* If this node is a reference, returns the referred node.
|
|
99
|
+
*/
|
|
100
|
+
get referencedNode() {
|
|
101
|
+
for (const edge of this.refEdges) {
|
|
102
|
+
if (edge.refType === ModelGraphEdge.ReferenceType.Object && edge.isOwnerNode(this)) {
|
|
103
|
+
return edge.relatedNode;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
220
106
|
return null;
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Returns all nodes that are references to this node.
|
|
110
|
+
*/
|
|
111
|
+
get referencingNodes() {
|
|
112
|
+
const nodes = [];
|
|
113
|
+
for (const edge of this.refEdges) {
|
|
114
|
+
if (edge.refType === ModelGraphEdge.ReferenceType.Object && edge.isRelatedNode(this)) {
|
|
115
|
+
nodes.push(edge.ownerNode);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return nodes;
|
|
119
|
+
}
|
|
120
|
+
get descendantRelationNodes() {
|
|
121
|
+
return this._collectDescendantRelationNodes([]);
|
|
122
|
+
}
|
|
123
|
+
removeEdge(edge) {
|
|
124
|
+
// Don't allow removing parent edges for now. It would
|
|
125
|
+
// cause all kinds of cache invalidation.
|
|
126
|
+
if (edge === this.parentEdge) {
|
|
127
|
+
throw new Error('cannot remove parent edge');
|
|
128
|
+
}
|
|
129
|
+
this.edges = this.edges.filter((it) => it !== edge);
|
|
130
|
+
this.refEdges = this.refEdges.filter((it) => it !== edge);
|
|
131
|
+
}
|
|
132
|
+
_collectDescendantRelationNodes(nodes) {
|
|
133
|
+
for (const edge of this.edges) {
|
|
134
|
+
if (edge.type === ModelGraphEdge.Type.Relation && edge.isOwnerNode(this)) {
|
|
135
|
+
nodes.push(edge.relatedNode);
|
|
136
|
+
edge.relatedNode._collectDescendantRelationNodes(nodes);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return nodes;
|
|
140
|
+
}
|
|
141
|
+
_createRelationPath() {
|
|
142
|
+
if (this.parentNode === null) {
|
|
143
|
+
return [];
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
return [...this.parentNode.relationPath, this.relationName];
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
_createRelationPathKey() {
|
|
150
|
+
return this.relationPath.join('.');
|
|
151
|
+
}
|
|
152
|
+
_createDataPath() {
|
|
153
|
+
if (this.parentEdge === null) {
|
|
154
|
+
return [];
|
|
155
|
+
}
|
|
156
|
+
else if (this.parentEdge.relation.isOneToOne()) {
|
|
157
|
+
return [...this.parentNode.dataPath, this.relationName];
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
return [...this.parentNode.dataPath, this.relationName, this.indexInRelation];
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
_createDataPathKey() {
|
|
164
|
+
const dataPathKey = this.dataPath.reduce((key, it) => {
|
|
165
|
+
if (isNumber(it)) {
|
|
166
|
+
return `${key}[${it}]`;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
return key ? `${key}.${it}` : it;
|
|
170
|
+
}
|
|
171
|
+
}, '');
|
|
172
|
+
return dataPathKey ? '.' + dataPathKey : dataPathKey;
|
|
173
|
+
}
|
|
174
|
+
_createIdPath() {
|
|
175
|
+
if (!this.obj.$hasId()) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
if (this.parentEdge === null) {
|
|
179
|
+
return [this.obj.$idKey()];
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
const path = this.parentNode.idPath;
|
|
183
|
+
if (path === null) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
return [...path, this.relationName, this.obj.$idKey()];
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
_createIdPathKey() {
|
|
190
|
+
const idPath = this.idPath;
|
|
191
|
+
if (idPath) {
|
|
192
|
+
return this.idPath.join('.');
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
236
198
|
}
|
|
237
|
-
|
|
238
199
|
module.exports = {
|
|
239
|
-
|
|
200
|
+
ModelGraphNode,
|
|
240
201
|
};
|
|
@@ -1,28 +1,19 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const cache = new Map();
|
|
4
|
-
|
|
5
3
|
function inheritModel(modelClass) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return inherit(modelClass);
|
|
4
|
+
let inherit = cache.get(modelClass.name);
|
|
5
|
+
if (!inherit) {
|
|
6
|
+
inherit = createClassInheritor(modelClass.name);
|
|
7
|
+
cache.set(modelClass.name, inherit);
|
|
8
|
+
}
|
|
9
|
+
return inherit(modelClass);
|
|
14
10
|
}
|
|
15
|
-
|
|
16
11
|
function createClassInheritor(className) {
|
|
17
|
-
|
|
18
|
-
'BaseClass',
|
|
19
|
-
`
|
|
12
|
+
return new Function('BaseClass', `
|
|
20
13
|
'use strict';
|
|
21
14
|
return class ${className} extends BaseClass {}
|
|
22
|
-
|
|
23
|
-
);
|
|
15
|
+
`);
|
|
24
16
|
}
|
|
25
|
-
|
|
26
17
|
module.exports = {
|
|
27
|
-
|
|
18
|
+
inheritModel,
|
|
28
19
|
};
|
|
@@ -1,83 +1,69 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { inheritModel } = require('./inheritModel');
|
|
4
3
|
const { staticHiddenProps } = require('./modelUtils');
|
|
5
4
|
const { defineNonEnumerableProperty } = require('./modelUtils');
|
|
6
|
-
|
|
7
5
|
function bindKnex(modelClass, knex) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
BoundModelClass = bindRelations(modelClass, BoundModelClass, knex);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return BoundModelClass;
|
|
6
|
+
let BoundModelClass = getBoundModelFromCache(modelClass, knex);
|
|
7
|
+
if (BoundModelClass === null) {
|
|
8
|
+
BoundModelClass = inheritModel(modelClass);
|
|
9
|
+
BoundModelClass = copyHiddenProperties(modelClass, BoundModelClass);
|
|
10
|
+
BoundModelClass.knex(knex);
|
|
11
|
+
BoundModelClass = putBoundModelToCache(modelClass, BoundModelClass, knex);
|
|
12
|
+
BoundModelClass = bindRelations(modelClass, BoundModelClass, knex);
|
|
13
|
+
}
|
|
14
|
+
return BoundModelClass;
|
|
21
15
|
}
|
|
22
|
-
|
|
23
16
|
function getBoundModelFromCache(modelClass, knex) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return cache.get(cacheKey) || null;
|
|
17
|
+
const cache = getCache(knex);
|
|
18
|
+
const cacheKey = modelClass.uniqueTag();
|
|
19
|
+
return cache.get(cacheKey) || null;
|
|
28
20
|
}
|
|
29
|
-
|
|
30
21
|
function getCache(knex) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return knex.$$objection.boundModels;
|
|
22
|
+
if (!knex.$$objection) {
|
|
23
|
+
createCache(knex);
|
|
24
|
+
}
|
|
25
|
+
return knex.$$objection.boundModels;
|
|
36
26
|
}
|
|
37
|
-
|
|
38
27
|
function createCache(knex) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
28
|
+
defineNonEnumerableProperty(knex, '$$objection', {
|
|
29
|
+
boundModels: new Map(),
|
|
30
|
+
});
|
|
42
31
|
}
|
|
43
|
-
|
|
44
32
|
function copyHiddenProperties(modelClass, BoundModelClass) {
|
|
45
|
-
|
|
46
|
-
//
|
|
47
|
-
|
|
48
|
-
|
|
33
|
+
// Bound classes share the validator of the class they are bound from, which
|
|
34
|
+
// is copied below. Create it first, so that a class bound to a new knex
|
|
35
|
+
// instance, like every transaction, doesn't create and compile a validator
|
|
36
|
+
// of its own.
|
|
37
|
+
if (!Object.hasOwn(modelClass, '$$validator')) {
|
|
38
|
+
modelClass.getValidator();
|
|
49
39
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
40
|
+
for (const prop of staticHiddenProps) {
|
|
41
|
+
// $$relations and $$relationArray are handled in separately.
|
|
42
|
+
if (Object.hasOwn(modelClass, prop) && prop !== '$$relations' && prop !== '$$relationArray') {
|
|
43
|
+
defineNonEnumerableProperty(BoundModelClass, prop, modelClass[prop]);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return BoundModelClass;
|
|
53
47
|
}
|
|
54
|
-
|
|
55
48
|
function putBoundModelToCache(modelClass, BoundModelClass, knex) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return BoundModelClass;
|
|
49
|
+
const cache = getCache(knex);
|
|
50
|
+
const cacheKey = modelClass.uniqueTag();
|
|
51
|
+
cache.set(cacheKey, BoundModelClass);
|
|
52
|
+
return BoundModelClass;
|
|
61
53
|
}
|
|
62
|
-
|
|
63
54
|
function bindRelations(modelClass, BoundModelClass, knex) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
defineNonEnumerableProperty(BoundModelClass, '$$relations', boundRelations);
|
|
76
|
-
defineNonEnumerableProperty(BoundModelClass, '$$relationArray', boundRelationArray);
|
|
77
|
-
|
|
78
|
-
return BoundModelClass;
|
|
55
|
+
const boundRelations = Object.create(null);
|
|
56
|
+
const boundRelationArray = [];
|
|
57
|
+
for (const relationName of modelClass.getRelationNames()) {
|
|
58
|
+
const relation = modelClass.getRelation(relationName);
|
|
59
|
+
const boundRelation = relation.bindKnex(knex);
|
|
60
|
+
boundRelations[relation.name] = boundRelation;
|
|
61
|
+
boundRelationArray.push(boundRelation);
|
|
62
|
+
}
|
|
63
|
+
defineNonEnumerableProperty(BoundModelClass, '$$relations', boundRelations);
|
|
64
|
+
defineNonEnumerableProperty(BoundModelClass, '$$relationArray', boundRelationArray);
|
|
65
|
+
return BoundModelClass;
|
|
79
66
|
}
|
|
80
|
-
|
|
81
67
|
module.exports = {
|
|
82
|
-
|
|
68
|
+
bindKnex,
|
|
83
69
|
};
|