@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,241 +1,188 @@
1
1
  'use strict';
2
-
3
2
  const { isObject, isString, asArray, asSingle } = require('../../utils/objectUtils');
4
3
  const { ValidationErrorType } = require('../../model/ValidationError');
5
4
  const { ModelGraphNode } = require('./ModelGraphNode');
6
5
  const { ModelGraphEdge } = require('./ModelGraphEdge');
7
-
8
6
  class ModelGraphBuilder {
9
- constructor() {
10
- this.nodes = [];
11
- this.edges = [];
12
- }
13
-
14
- static buildGraph(rootModelClass, roots) {
15
- const builder = new this();
16
- builder._buildGraph(rootModelClass, roots);
17
-
18
- return builder;
19
- }
20
-
21
- _buildGraph(rootModelClass, roots) {
22
- if (roots) {
23
- if (Array.isArray(roots)) {
24
- this._buildNodes(rootModelClass, roots);
25
- } else if (isObject(roots)) {
26
- this._buildNode(rootModelClass, roots);
27
- } else {
28
- throw createNotModelError(rootModelClass, roots);
29
- }
30
- }
31
-
32
- this._buildReferences();
33
- }
34
-
35
- _buildNodes(modelClass, objs, parentNode = null, relation = null) {
36
- objs = asArray(objs);
37
-
38
- objs.forEach((obj, index) => {
39
- this._buildNode(modelClass, obj, parentNode, relation, index);
40
- });
41
- }
42
-
43
- _buildNode(modelClass, obj, parentNode = null, relation = null, index = null) {
44
- obj = asSingle(obj);
45
-
46
- if (!isObject(obj) || !obj.$isObjectionModel) {
47
- throw createNotModelError(modelClass, obj);
48
- }
49
-
50
- const node = new ModelGraphNode(modelClass, obj);
51
- this.nodes.push(node);
52
-
53
- if (parentNode) {
54
- const edge = new ModelGraphEdge(
55
- ModelGraphEdge.Type.Relation,
56
- parentNode,
57
- node,
58
- relation,
59
- index,
60
- );
61
-
62
- node.parentEdge = edge;
63
- this._addEdge(parentNode, node, edge);
64
- }
65
-
66
- this._buildRelationNodes(node);
67
- }
68
-
69
- _buildRelationNodes(node) {
70
- for (const relationName of node.modelClass.getRelationNames()) {
71
- const relatedObjects = node.obj[relationName];
72
-
73
- if (!relatedObjects) {
74
- continue;
75
- }
76
-
77
- const relation = node.modelClass.getRelation(relationName);
78
-
79
- if (relation.isOneToOne()) {
80
- this._buildNode(relation.relatedModelClass, relatedObjects, node, relation);
81
- } else {
82
- this._buildNodes(relation.relatedModelClass, relatedObjects, node, relation);
83
- }
84
- }
85
- }
86
-
87
- _buildReferences() {
88
- const nodesByUid = this._nodesByUid();
89
-
90
- this._buildObjectReferences(nodesByUid);
91
- this._buildPropertyReferences(nodesByUid);
92
- }
93
-
94
- _nodesByUid() {
95
- const nodesByUid = new Map();
96
-
97
- for (const node of this.nodes) {
98
- const uid = node.uid;
99
-
100
- if (uid === undefined) {
101
- continue;
102
- }
103
-
104
- nodesByUid.set(uid, node);
105
- }
106
-
107
- return nodesByUid;
108
- }
109
-
110
- _buildObjectReferences(nodesByUid) {
111
- for (const node of this.nodes) {
112
- const ref = node.reference;
113
-
114
- if (ref === undefined) {
115
- continue;
116
- }
117
-
118
- const refNode = nodesByUid.get(ref);
119
-
120
- if (!refNode) {
121
- throw createReferenceNotFoundError(ref);
122
- }
123
-
124
- const edge = new ModelGraphEdge(ModelGraphEdge.Type.Reference, node, refNode);
125
- edge.refType = ModelGraphEdge.ReferenceType.Object;
126
-
127
- this._addEdge(node, refNode, edge);
128
- }
129
- }
130
-
131
- _buildPropertyReferences(nodesByUid) {
132
- for (const node of this.nodes) {
133
- forEachPropertyReference(node.obj, ({ path, refMatch, ref, refPath }) => {
134
- const refNode = nodesByUid.get(ref);
135
-
136
- if (!refNode) {
137
- throw createReferenceNotFoundError(ref);
7
+ constructor() {
8
+ this.nodes = [];
9
+ this.edges = [];
10
+ }
11
+ static buildGraph(rootModelClass, roots) {
12
+ const builder = new this();
13
+ builder._buildGraph(rootModelClass, roots);
14
+ return builder;
15
+ }
16
+ _buildGraph(rootModelClass, roots) {
17
+ if (roots) {
18
+ if (Array.isArray(roots)) {
19
+ this._buildNodes(rootModelClass, roots);
20
+ }
21
+ else if (isObject(roots)) {
22
+ this._buildNode(rootModelClass, roots);
23
+ }
24
+ else {
25
+ throw createNotModelError(rootModelClass, roots);
26
+ }
27
+ }
28
+ this._buildReferences();
29
+ }
30
+ _buildNodes(modelClass, objs, parentNode = null, relation = null) {
31
+ objs = asArray(objs);
32
+ objs.forEach((obj, index) => {
33
+ this._buildNode(modelClass, obj, parentNode, relation, index);
34
+ });
35
+ }
36
+ _buildNode(modelClass, obj, parentNode = null, relation = null, index = null) {
37
+ obj = asSingle(obj);
38
+ if (!isObject(obj) || !obj.$isObjectionModel) {
39
+ throw createNotModelError(modelClass, obj);
138
40
  }
139
-
140
- const edge = new ModelGraphEdge(ModelGraphEdge.Type.Reference, node, refNode);
141
-
142
- edge.refType = ModelGraphEdge.ReferenceType.Property;
143
- edge.refMatch = refMatch;
144
- edge.refOwnerDataPath = path.slice();
145
- edge.refRelatedDataPath = refPath;
146
-
147
- this._addEdge(node, refNode, edge);
148
- });
149
- }
150
- }
151
-
152
- _addEdge(ownerNode, relatedNode, edge) {
153
- this.edges.push(edge);
154
-
155
- ownerNode.edges.push(edge);
156
- relatedNode.edges.push(edge);
157
-
158
- if (edge.type === ModelGraphEdge.Type.Reference) {
159
- ownerNode.refEdges.push(edge);
160
- relatedNode.refEdges.push(edge);
161
- }
162
- }
41
+ const node = new ModelGraphNode(modelClass, obj);
42
+ this.nodes.push(node);
43
+ if (parentNode) {
44
+ const edge = new ModelGraphEdge(ModelGraphEdge.Type.Relation, parentNode, node, relation, index);
45
+ node.parentEdge = edge;
46
+ this._addEdge(parentNode, node, edge);
47
+ }
48
+ this._buildRelationNodes(node);
49
+ }
50
+ _buildRelationNodes(node) {
51
+ for (const relationName of node.modelClass.getRelationNames()) {
52
+ const relatedObjects = node.obj[relationName];
53
+ if (!relatedObjects) {
54
+ continue;
55
+ }
56
+ const relation = node.modelClass.getRelation(relationName);
57
+ if (relation.isOneToOne()) {
58
+ this._buildNode(relation.relatedModelClass, relatedObjects, node, relation);
59
+ }
60
+ else {
61
+ this._buildNodes(relation.relatedModelClass, relatedObjects, node, relation);
62
+ }
63
+ }
64
+ }
65
+ _buildReferences() {
66
+ const nodesByUid = this._nodesByUid();
67
+ this._buildObjectReferences(nodesByUid);
68
+ this._buildPropertyReferences(nodesByUid);
69
+ }
70
+ _nodesByUid() {
71
+ const nodesByUid = new Map();
72
+ for (const node of this.nodes) {
73
+ const uid = node.uid;
74
+ if (uid === undefined) {
75
+ continue;
76
+ }
77
+ nodesByUid.set(uid, node);
78
+ }
79
+ return nodesByUid;
80
+ }
81
+ _buildObjectReferences(nodesByUid) {
82
+ for (const node of this.nodes) {
83
+ const ref = node.reference;
84
+ if (ref === undefined) {
85
+ continue;
86
+ }
87
+ const refNode = nodesByUid.get(ref);
88
+ if (!refNode) {
89
+ throw createReferenceNotFoundError(ref);
90
+ }
91
+ const edge = new ModelGraphEdge(ModelGraphEdge.Type.Reference, node, refNode);
92
+ edge.refType = ModelGraphEdge.ReferenceType.Object;
93
+ this._addEdge(node, refNode, edge);
94
+ }
95
+ }
96
+ _buildPropertyReferences(nodesByUid) {
97
+ for (const node of this.nodes) {
98
+ forEachPropertyReference(node.obj, ({ path, refMatch, ref, refPath }) => {
99
+ const refNode = nodesByUid.get(ref);
100
+ if (!refNode) {
101
+ throw createReferenceNotFoundError(ref);
102
+ }
103
+ const edge = new ModelGraphEdge(ModelGraphEdge.Type.Reference, node, refNode);
104
+ edge.refType = ModelGraphEdge.ReferenceType.Property;
105
+ edge.refMatch = refMatch;
106
+ edge.refOwnerDataPath = path.slice();
107
+ edge.refRelatedDataPath = refPath;
108
+ this._addEdge(node, refNode, edge);
109
+ });
110
+ }
111
+ }
112
+ _addEdge(ownerNode, relatedNode, edge) {
113
+ this.edges.push(edge);
114
+ ownerNode.edges.push(edge);
115
+ relatedNode.edges.push(edge);
116
+ if (edge.type === ModelGraphEdge.Type.Reference) {
117
+ ownerNode.refEdges.push(edge);
118
+ relatedNode.refEdges.push(edge);
119
+ }
120
+ }
163
121
  }
164
-
165
122
  function forEachPropertyReference(obj, callback) {
166
- const modelClass = obj.constructor;
167
- const relationNames = modelClass.getRelationNames();
168
-
169
- for (const prop of Object.keys(obj)) {
170
- if (relationNames.includes(prop)) {
171
- continue;
172
- }
173
-
174
- visitStrings(obj[prop], [prop], (str, path) => {
175
- forEachMatch(modelClass.propRefRegex, str, (match) => {
176
- const [refMatch, ref, refPath] = match;
177
- callback({ path, refMatch, ref, refPath: refPath.trim().split('.') });
178
- });
179
- });
180
- }
123
+ const modelClass = obj.constructor;
124
+ const relationNames = modelClass.getRelationNames();
125
+ for (const prop of Object.keys(obj)) {
126
+ if (relationNames.includes(prop)) {
127
+ continue;
128
+ }
129
+ visitStrings(obj[prop], [prop], (str, path) => {
130
+ forEachMatch(modelClass.propRefRegex, str, (match) => {
131
+ const [refMatch, ref, refPath] = match;
132
+ callback({ path, refMatch, ref, refPath: refPath.trim().split('.') });
133
+ });
134
+ });
135
+ }
181
136
  }
182
-
183
137
  function visitStrings(value, path, visit) {
184
- if (Array.isArray(value)) {
185
- visitStringsInArray(value, path, visit);
186
- } else if (isObject(value) && !Buffer.isBuffer(value)) {
187
- visitStringsInObject(value, path, visit);
188
- } else if (isString(value)) {
189
- visit(value, path);
190
- }
138
+ if (Array.isArray(value)) {
139
+ visitStringsInArray(value, path, visit);
140
+ }
141
+ else if (isObject(value) && !Buffer.isBuffer(value)) {
142
+ visitStringsInObject(value, path, visit);
143
+ }
144
+ else if (isString(value)) {
145
+ visit(value, path);
146
+ }
191
147
  }
192
-
193
148
  function visitStringsInArray(value, path, visit) {
194
- for (let i = 0; i < value.length; ++i) {
195
- path.push(i);
196
- visitStrings(value[i], path, visit);
197
- path.pop();
198
- }
149
+ for (let i = 0; i < value.length; ++i) {
150
+ path.push(i);
151
+ visitStrings(value[i], path, visit);
152
+ path.pop();
153
+ }
199
154
  }
200
-
201
155
  function visitStringsInObject(value, path, visit) {
202
- for (const prop of Object.keys(value)) {
203
- path.push(prop);
204
- visitStrings(value[prop], path, visit);
205
- path.pop();
206
- }
156
+ for (const prop of Object.keys(value)) {
157
+ path.push(prop);
158
+ visitStrings(value[prop], path, visit);
159
+ path.pop();
160
+ }
207
161
  }
208
-
209
162
  function forEachMatch(regex, str, cb) {
210
- let matchResult = regex.exec(str);
211
-
212
- while (matchResult) {
213
- cb(matchResult);
214
- matchResult = regex.exec(str);
215
- }
163
+ let matchResult = regex.exec(str);
164
+ while (matchResult) {
165
+ cb(matchResult);
166
+ matchResult = regex.exec(str);
167
+ }
216
168
  }
217
-
218
169
  function createReferenceNotFoundError(ref) {
219
- return new Error(
220
- [
221
- `could not resolve reference ${ref} in a graph.`,
222
- `If you are sure the #id exist in the same graph,`,
223
- `this may be due to a limitation that a subgraph under a related item`,
224
- `cannot reference an item in a subgraph under another related item.`,
225
- `If you run into this limitation, please open an issue in objection github.`,
226
- ].join(' '),
227
- );
170
+ return new Error([
171
+ `could not resolve reference ${ref} in a graph.`,
172
+ `If you are sure the #id exist in the same graph,`,
173
+ `this may be due to a limitation that a subgraph under a related item`,
174
+ `cannot reference an item in a subgraph under another related item.`,
175
+ `If you run into this limitation, please open an issue in objection github.`,
176
+ ].join(' '));
228
177
  }
229
-
230
178
  function createNotModelError(modelClass, value) {
231
- throw modelClass.createValidationError({
232
- type: ValidationErrorType.InvalidGraph,
233
- message: `expected value "${value}" to be an instance of ${modelClass.name}`,
234
- });
179
+ throw modelClass.createValidationError({
180
+ type: ValidationErrorType.InvalidGraph,
181
+ message: `expected value "${value}" to be an instance of ${modelClass.name}`,
182
+ });
235
183
  }
236
-
237
184
  module.exports = {
238
- ModelGraphBuilder,
239
- createNotModelError,
240
- forEachPropertyReference,
185
+ ModelGraphBuilder,
186
+ createNotModelError,
187
+ forEachPropertyReference,
241
188
  };
@@ -1,52 +1,40 @@
1
1
  'use strict';
2
-
3
2
  const Type = {
4
- Relation: 'Relation',
5
- Reference: 'Reference',
3
+ Relation: 'Relation',
4
+ Reference: 'Reference',
6
5
  };
7
-
8
6
  const ReferenceType = {
9
- Object: 'Object',
10
- Property: 'Property',
7
+ Object: 'Object',
8
+ Property: 'Property',
11
9
  };
12
-
13
10
  class ModelGraphEdge {
14
- constructor(type, ownerNode, relatedNode, relation = null, relationIndex = null) {
15
- this.type = type;
16
-
17
- this.ownerNode = ownerNode;
18
- this.relatedNode = relatedNode;
19
- this.relation = relation;
20
- this.relationIndex = relationIndex;
21
-
22
- this.refType = null;
23
- this.refMatch = null;
24
-
25
- this.refOwnerDataPath = null;
26
- this.refRelatedDataPath = null;
27
- }
28
-
29
- static get Type() {
30
- return Type;
31
- }
32
-
33
- static get ReferenceType() {
34
- return ReferenceType;
35
- }
36
-
37
- getOtherNode(node) {
38
- return this.isOwnerNode(node) ? this.relatedNode : this.ownerNode;
39
- }
40
-
41
- isOwnerNode(node) {
42
- return node === this.ownerNode;
43
- }
44
-
45
- isRelatedNode(node) {
46
- return node === this.relatedNode;
47
- }
11
+ constructor(type, ownerNode, relatedNode, relation = null, relationIndex = null) {
12
+ this.type = type;
13
+ this.ownerNode = ownerNode;
14
+ this.relatedNode = relatedNode;
15
+ this.relation = relation;
16
+ this.relationIndex = relationIndex;
17
+ this.refType = null;
18
+ this.refMatch = null;
19
+ this.refOwnerDataPath = null;
20
+ this.refRelatedDataPath = null;
21
+ }
22
+ static get Type() {
23
+ return Type;
24
+ }
25
+ static get ReferenceType() {
26
+ return ReferenceType;
27
+ }
28
+ getOtherNode(node) {
29
+ return this.isOwnerNode(node) ? this.relatedNode : this.ownerNode;
30
+ }
31
+ isOwnerNode(node) {
32
+ return node === this.ownerNode;
33
+ }
34
+ isRelatedNode(node) {
35
+ return node === this.relatedNode;
36
+ }
48
37
  }
49
-
50
38
  module.exports = {
51
- ModelGraphEdge,
39
+ ModelGraphEdge,
52
40
  };