@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.
Files changed (192) hide show
  1. package/README.md +103 -67
  2. package/lib/index.mjs +6 -0
  3. package/lib/initialize.js +6 -9
  4. package/lib/model/AjvValidator.js +235 -282
  5. package/lib/model/Model.js +653 -823
  6. package/lib/model/ModifierNotFoundError.js +5 -7
  7. package/lib/model/NotFoundError.js +14 -18
  8. package/lib/model/RelationDoesNotExistError.js +6 -9
  9. package/lib/model/ValidationError.js +33 -41
  10. package/lib/model/Validator.js +15 -21
  11. package/lib/model/getModel.js +1 -3
  12. package/lib/model/graph/ModelGraph.js +44 -64
  13. package/lib/model/graph/ModelGraphBuilder.js +164 -217
  14. package/lib/model/graph/ModelGraphEdge.js +31 -43
  15. package/lib/model/graph/ModelGraphNode.js +192 -231
  16. package/lib/model/inheritModel.js +9 -18
  17. package/lib/model/modelBindKnex.js +48 -62
  18. package/lib/model/modelClone.js +79 -98
  19. package/lib/model/modelColPropMap.js +16 -26
  20. package/lib/model/modelConcurrency.js +24 -0
  21. package/lib/model/modelId.js +44 -49
  22. package/lib/model/modelJsonAttributes.js +56 -76
  23. package/lib/model/modelParseRelations.js +68 -67
  24. package/lib/model/modelQueryProps.js +76 -107
  25. package/lib/model/modelSet.js +92 -112
  26. package/lib/model/modelTableMetadata.js +60 -72
  27. package/lib/model/modelToJson.js +120 -150
  28. package/lib/model/modelUtils.js +30 -37
  29. package/lib/model/modelValidate.js +34 -42
  30. package/lib/model/modelValues.js +78 -91
  31. package/lib/model/modelVisitor.js +22 -29
  32. package/lib/objection.js +42 -67
  33. package/lib/queryBuilder/FunctionBuilder.js +231 -279
  34. package/lib/queryBuilder/InternalOptions.js +19 -24
  35. package/lib/queryBuilder/JoinBuilder.js +116 -149
  36. package/lib/queryBuilder/QueryBuilder.js +1009 -1271
  37. package/lib/queryBuilder/QueryBuilderBase.js +647 -865
  38. package/lib/queryBuilder/QueryBuilderContext.js +14 -21
  39. package/lib/queryBuilder/QueryBuilderContextBase.js +20 -29
  40. package/lib/queryBuilder/QueryBuilderOperationSupport.js +462 -536
  41. package/lib/queryBuilder/QueryBuilderUserContext.js +19 -25
  42. package/lib/queryBuilder/RawBuilder.js +58 -73
  43. package/lib/queryBuilder/ReferenceBuilder.js +232 -270
  44. package/lib/queryBuilder/RelationExpression.js +322 -380
  45. package/lib/queryBuilder/StaticHookArguments.js +94 -106
  46. package/lib/queryBuilder/ValueBuilder.js +100 -112
  47. package/lib/queryBuilder/expressions.js +86 -116
  48. package/lib/queryBuilder/functions/mysql.js +642 -697
  49. package/lib/queryBuilder/graph/GraphAction.js +44 -59
  50. package/lib/queryBuilder/graph/GraphData.js +7 -9
  51. package/lib/queryBuilder/graph/GraphFetcher.js +115 -151
  52. package/lib/queryBuilder/graph/GraphNodeDbExistence.js +114 -152
  53. package/lib/queryBuilder/graph/GraphOperation.js +16 -22
  54. package/lib/queryBuilder/graph/GraphOptions.js +143 -185
  55. package/lib/queryBuilder/graph/GraphUpsert.js +167 -241
  56. package/lib/queryBuilder/graph/delete/GraphDelete.js +8 -13
  57. package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +44 -62
  58. package/lib/queryBuilder/graph/insert/GraphInsert.js +99 -128
  59. package/lib/queryBuilder/graph/insert/GraphInsertAction.js +151 -193
  60. package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +57 -79
  61. package/lib/queryBuilder/graph/patch/GraphPatch.js +8 -13
  62. package/lib/queryBuilder/graph/patch/GraphPatchAction.js +181 -234
  63. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +16 -21
  64. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +80 -110
  65. package/lib/queryBuilder/join/JoinResultColumn.js +14 -18
  66. package/lib/queryBuilder/join/JoinResultParser.js +88 -101
  67. package/lib/queryBuilder/join/RelationJoiner.js +205 -288
  68. package/lib/queryBuilder/join/TableNode.js +117 -144
  69. package/lib/queryBuilder/join/TableTree.js +60 -76
  70. package/lib/queryBuilder/join/utils.js +10 -16
  71. package/lib/queryBuilder/operations/DelegateOperation.js +93 -133
  72. package/lib/queryBuilder/operations/DeleteOperation.js +33 -37
  73. package/lib/queryBuilder/operations/FindByIdOperation.js +20 -28
  74. package/lib/queryBuilder/operations/FindByIdsOperation.js +17 -23
  75. package/lib/queryBuilder/operations/FindOperation.js +114 -131
  76. package/lib/queryBuilder/operations/FirstOperation.js +14 -19
  77. package/lib/queryBuilder/operations/FromOperation.js +69 -88
  78. package/lib/queryBuilder/operations/InsertAndFetchOperation.js +28 -39
  79. package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +25 -35
  80. package/lib/queryBuilder/operations/InsertGraphOperation.js +77 -98
  81. package/lib/queryBuilder/operations/InsertOperation.js +132 -165
  82. package/lib/queryBuilder/operations/InstanceDeleteOperation.js +27 -36
  83. package/lib/queryBuilder/operations/InstanceFindOperation.js +9 -13
  84. package/lib/queryBuilder/operations/InstanceInsertOperation.js +13 -17
  85. package/lib/queryBuilder/operations/InstanceUpdateOperation.js +32 -46
  86. package/lib/queryBuilder/operations/JoinRelatedOperation.js +66 -86
  87. package/lib/queryBuilder/operations/KnexOperation.js +4 -7
  88. package/lib/queryBuilder/operations/MergeOperation.js +32 -45
  89. package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +131 -150
  90. package/lib/queryBuilder/operations/OnBuildKnexOperation.js +17 -23
  91. package/lib/queryBuilder/operations/OnBuildOperation.js +17 -23
  92. package/lib/queryBuilder/operations/OnErrorOperation.js +17 -23
  93. package/lib/queryBuilder/operations/QueryBuilderOperation.js +231 -270
  94. package/lib/queryBuilder/operations/RangeOperation.js +30 -40
  95. package/lib/queryBuilder/operations/RelateOperation.js +14 -21
  96. package/lib/queryBuilder/operations/ReturningOperation.js +12 -18
  97. package/lib/queryBuilder/operations/RunAfterOperation.js +17 -23
  98. package/lib/queryBuilder/operations/RunBeforeOperation.js +17 -23
  99. package/lib/queryBuilder/operations/UnrelateOperation.js +12 -17
  100. package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +48 -65
  101. package/lib/queryBuilder/operations/UpdateOperation.js +104 -129
  102. package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +25 -35
  103. package/lib/queryBuilder/operations/UpsertGraphOperation.js +36 -55
  104. package/lib/queryBuilder/operations/WhereCompositeOperation.js +30 -39
  105. package/lib/queryBuilder/operations/WhereInOperation.js +34 -36
  106. package/lib/queryBuilder/operations/eager/EagerOperation.js +37 -52
  107. package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +34 -44
  108. package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +4 -7
  109. package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +193 -251
  110. package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +18 -25
  111. package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +25 -41
  112. package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +19 -20
  113. package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +23 -21
  114. package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +106 -118
  115. package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +79 -108
  116. package/lib/queryBuilder/operations/select/SelectOperation.js +37 -53
  117. package/lib/queryBuilder/operations/select/Selection.js +94 -105
  118. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +68 -101
  119. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +65 -75
  120. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +75 -84
  121. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +760 -859
  122. package/lib/queryBuilder/parsers/relationExpressionParser.js +840 -952
  123. package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +10 -15
  124. package/lib/queryBuilder/transformations/QueryTransformation.js +4 -6
  125. package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +35 -49
  126. package/lib/queryBuilder/transformations/index.js +3 -9
  127. package/lib/queryBuilder/whereScopes.js +158 -186
  128. package/lib/relations/Relation.js +279 -363
  129. package/lib/relations/RelationDeleteOperation.js +16 -22
  130. package/lib/relations/RelationFindOperation.js +100 -133
  131. package/lib/relations/RelationInsertOperation.js +19 -27
  132. package/lib/relations/RelationOwner.js +167 -204
  133. package/lib/relations/RelationProperty.js +177 -217
  134. package/lib/relations/RelationUpdateOperation.js +16 -23
  135. package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +11 -16
  136. package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +24 -35
  137. package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +30 -41
  138. package/lib/relations/belongsToOne/BelongsToOneRelation.js +33 -41
  139. package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +38 -53
  140. package/lib/relations/hasMany/HasManyInsertOperation.js +35 -51
  141. package/lib/relations/hasMany/HasManyRelateOperation.js +38 -54
  142. package/lib/relations/hasMany/HasManyRelation.js +30 -38
  143. package/lib/relations/hasMany/HasManyUnrelateOperation.js +19 -26
  144. package/lib/relations/hasOne/HasOneRelation.js +4 -7
  145. package/lib/relations/hasOneThrough/HasOneThroughRelation.js +4 -7
  146. package/lib/relations/manyToMany/ManyToManyModifyMixin.js +67 -90
  147. package/lib/relations/manyToMany/ManyToManyRelation.js +349 -451
  148. package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +19 -26
  149. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +3 -5
  150. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +14 -20
  151. package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +3 -7
  152. package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +71 -94
  153. package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +31 -43
  154. package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +50 -67
  155. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +4 -7
  156. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +20 -27
  157. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +5 -10
  158. package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +3 -5
  159. package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +55 -72
  160. package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +3 -7
  161. package/lib/transaction.js +53 -83
  162. package/lib/utils/assert.js +15 -21
  163. package/lib/utils/buildUtils.js +17 -19
  164. package/lib/utils/classUtils.js +7 -11
  165. package/lib/utils/cloneDeep.js +365 -0
  166. package/lib/utils/createModifier.js +36 -41
  167. package/lib/utils/deprecate.js +8 -12
  168. package/lib/utils/identifierMapping.js +153 -192
  169. package/lib/utils/internalPropUtils.js +4 -8
  170. package/lib/utils/knexUtils.js +61 -56
  171. package/lib/utils/mixin.js +16 -23
  172. package/lib/utils/mysqlJsonPath.js +55 -40
  173. package/lib/utils/normalizeIds.js +110 -127
  174. package/lib/utils/objectUtils.js +236 -338
  175. package/lib/utils/parseFieldExpression.js +66 -44
  176. package/lib/utils/postgresJsonPath.js +48 -62
  177. package/lib/utils/promiseUtils/after.js +10 -13
  178. package/lib/utils/promiseUtils/afterReturn.js +10 -13
  179. package/lib/utils/promiseUtils/index.js +15 -17
  180. package/lib/utils/promiseUtils/isPromise.js +6 -9
  181. package/lib/utils/promiseUtils/map.js +75 -89
  182. package/lib/utils/promiseUtils/mapAfterAllReturn.js +17 -23
  183. package/lib/utils/promiseUtils/try.js +15 -18
  184. package/lib/utils/resolveModel.js +91 -94
  185. package/lib/utils/tmpColumnUtils.js +4 -13
  186. package/package.json +50 -23
  187. package/typings/objection/index.d.mts +362 -0
  188. package/typings/objection/index.d.ts +36 -9
  189. package/lib/.eslintrc.json +0 -8
  190. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.pegjs +0 -97
  191. package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +0 -141
  192. 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
- constructor(modelClass, obj) {
10
- this.modelClass = modelClass;
11
- this.obj = obj;
12
- this.edges = [];
13
- this.userData = {};
14
- this.hasId = obj.$hasId();
15
- this.uid = obj[modelClass.uidProp];
16
-
17
- // These are also included in `edges`. These are simply
18
- // shortcuts for commonly used edges.
19
- this.refEdges = [];
20
- this.parentEdge = null;
21
-
22
- // These are calculated lazily.
23
- this._relationPath = NOT_CALCULATED;
24
- this._relationPathKey = NOT_CALCULATED;
25
-
26
- this._dataPath = NOT_CALCULATED;
27
- this._dataPathKey = NOT_CALCULATED;
28
-
29
- this._idPath = NOT_CALCULATED;
30
- this._idPathKey = NOT_CALCULATED;
31
- }
32
-
33
- get isReference() {
34
- return this.reference !== undefined;
35
- }
36
-
37
- get isDbReference() {
38
- return this.dbReference !== undefined;
39
- }
40
-
41
- get reference() {
42
- return this.obj[this.modelClass.uidRefProp];
43
- }
44
-
45
- get dbReference() {
46
- return this.obj[this.modelClass.dbRefProp];
47
- }
48
-
49
- get parentNode() {
50
- if (this.parentEdge) {
51
- return this.parentEdge.ownerNode;
52
- } else {
53
- return null;
54
- }
55
- }
56
-
57
- get indexInRelation() {
58
- if (this.parentEdge) {
59
- return this.parentEdge.relationIndex;
60
- } else {
61
- return null;
62
- }
63
- }
64
-
65
- get relationName() {
66
- if (this.parentEdge) {
67
- return this.parentEdge.relation.name;
68
- } else {
69
- return null;
70
- }
71
- }
72
-
73
- get relationPath() {
74
- if (this._relationPath === NOT_CALCULATED) {
75
- this._relationPath = this._createRelationPath();
76
- }
77
-
78
- return this._relationPath;
79
- }
80
-
81
- get relationPathKey() {
82
- if (this._relationPathKey === NOT_CALCULATED) {
83
- this._relationPathKey = this._createRelationPathKey();
84
- }
85
-
86
- return this._relationPathKey;
87
- }
88
-
89
- get dataPath() {
90
- if (this._dataPath === NOT_CALCULATED) {
91
- this._dataPath = this._createDataPath();
92
- }
93
-
94
- return this._dataPath;
95
- }
96
-
97
- get dataPathKey() {
98
- if (this._dataPathKey === NOT_CALCULATED) {
99
- this._dataPathKey = this._createDataPathKey();
100
- }
101
-
102
- return this._dataPathKey;
103
- }
104
-
105
- get idPath() {
106
- if (this._idPath === NOT_CALCULATED) {
107
- this._idPath = this._createIdPath();
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
- return [...path, this.relationName, this.obj.$idKey()];
224
- }
225
- }
226
-
227
- _createIdPathKey() {
228
- const idPath = this.idPath;
229
-
230
- if (idPath) {
231
- return this.idPath.join('.');
232
- } else {
233
- return null;
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
- ModelGraphNode,
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
- let inherit = cache.get(modelClass.name);
7
-
8
- if (!inherit) {
9
- inherit = createClassInheritor(modelClass.name);
10
- cache.set(modelClass.name, inherit);
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
- return new Function(
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
- inheritModel,
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
- let BoundModelClass = getBoundModelFromCache(modelClass, knex);
9
-
10
- if (BoundModelClass === null) {
11
- BoundModelClass = inheritModel(modelClass);
12
- BoundModelClass = copyHiddenProperties(modelClass, BoundModelClass);
13
-
14
- BoundModelClass.knex(knex);
15
-
16
- BoundModelClass = putBoundModelToCache(modelClass, BoundModelClass, knex);
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
- const cache = getCache(knex);
25
- const cacheKey = modelClass.uniqueTag();
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
- if (!knex.$$objection) {
32
- createCache(knex);
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
- defineNonEnumerableProperty(knex, '$$objection', {
40
- boundModels: new Map(),
41
- });
28
+ defineNonEnumerableProperty(knex, '$$objection', {
29
+ boundModels: new Map(),
30
+ });
42
31
  }
43
-
44
32
  function copyHiddenProperties(modelClass, BoundModelClass) {
45
- for (const prop of staticHiddenProps) {
46
- // $$relations and $$relationArray are handled in separately.
47
- if (modelClass.hasOwnProperty(prop) && prop !== '$$relations' && prop !== '$$relationArray') {
48
- defineNonEnumerableProperty(BoundModelClass, prop, modelClass[prop]);
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
- return BoundModelClass;
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
- const cache = getCache(knex);
57
- const cacheKey = modelClass.uniqueTag();
58
-
59
- cache.set(cacheKey, BoundModelClass);
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
- const boundRelations = Object.create(null);
65
- const boundRelationArray = [];
66
-
67
- for (const relationName of modelClass.getRelationNames()) {
68
- const relation = modelClass.getRelation(relationName);
69
- const boundRelation = relation.bindKnex(knex);
70
-
71
- boundRelations[relation.name] = boundRelation;
72
- boundRelationArray.push(boundRelation);
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
- bindKnex,
68
+ bindKnex,
83
69
  };