objection 3.9.1 → 4.0.0-rc.2

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 (250) hide show
  1. package/lib/dbErrors/LICENSE +21 -0
  2. package/lib/dbErrors/errorCodes/mssql.js +19 -0
  3. package/lib/dbErrors/errorCodes/mysql.js +805 -0
  4. package/lib/dbErrors/errorCodes/postgres.js +241 -0
  5. package/lib/dbErrors/errors/CheckViolationError.js +10 -0
  6. package/lib/dbErrors/errors/ConstraintViolationError.js +3 -0
  7. package/lib/dbErrors/errors/DBError.js +9 -0
  8. package/lib/dbErrors/errors/DataError.js +3 -0
  9. package/lib/dbErrors/errors/ForeignKeyViolationError.js +11 -0
  10. package/lib/dbErrors/errors/NotNullViolationError.js +12 -0
  11. package/lib/dbErrors/errors/UniqueViolationError.js +12 -0
  12. package/lib/dbErrors/index.js +59 -0
  13. package/lib/dbErrors/parsers/mssql/DBError/CheckViolationError/parser.js +28 -0
  14. package/lib/dbErrors/parsers/mssql/DBError/DataError/parser.js +25 -0
  15. package/lib/dbErrors/parsers/mssql/DBError/ForeignKeyViolationError/parser.js +39 -0
  16. package/lib/dbErrors/parsers/mssql/DBError/NotNullViolationError/parser.js +30 -0
  17. package/lib/dbErrors/parsers/mssql/DBError/UniqueViolationError/parser.js +53 -0
  18. package/lib/dbErrors/parsers/mssql/DBError/parser.js +51 -0
  19. package/lib/dbErrors/parsers/mssql/DBError/util.js +16 -0
  20. package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +43 -0
  21. package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/NotNullViolationError/parser.js +29 -0
  22. package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/UniqueViolationError/parser.js +27 -0
  23. package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/parser.js +23 -0
  24. package/lib/dbErrors/parsers/mysql/DBError/DataError/parser.js +24 -0
  25. package/lib/dbErrors/parsers/mysql/DBError/parser.js +28 -0
  26. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/CheckViolationError/parser.js +18 -0
  27. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +18 -0
  28. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/NotNullViolationError/parser.js +18 -0
  29. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/UniqueViolationError/parser.js +40 -0
  30. package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/parser.js +25 -0
  31. package/lib/dbErrors/parsers/postgres/DBError/DataError/parser.js +16 -0
  32. package/lib/dbErrors/parsers/postgres/DBError/parser.js +27 -0
  33. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/CheckViolationError/parser.js +20 -0
  34. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +20 -0
  35. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/NotNullViolationError/parser.js +27 -0
  36. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/UniqueViolationError/parser.js +28 -0
  37. package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/parser.js +24 -0
  38. package/lib/dbErrors/parsers/sqlite/DBError/parser.js +23 -0
  39. package/lib/dbErrors/utils/sqlState.js +3 -0
  40. package/lib/initialize.js +1 -7
  41. package/lib/model/AjvValidator.js +51 -117
  42. package/lib/model/Model.js +84 -59
  43. package/lib/model/ModifierNotFoundError.js +1 -7
  44. package/lib/model/NotFoundError.js +1 -7
  45. package/lib/model/RelationDoesNotExistError.js +1 -7
  46. package/lib/model/ValidationError.js +3 -10
  47. package/lib/model/Validator.js +1 -7
  48. package/lib/model/getModel.js +7 -6
  49. package/lib/model/inheritModel.js +1 -7
  50. package/lib/model/modelBindKnex.js +4 -10
  51. package/lib/model/modelClone.js +5 -11
  52. package/lib/model/modelColPropMap.js +3 -10
  53. package/lib/model/modelId.js +3 -10
  54. package/lib/model/modelJsonAttributes.js +35 -16
  55. package/lib/model/modelParseRelations.js +2 -8
  56. package/lib/model/modelQueryProps.js +11 -23
  57. package/lib/model/modelSet.js +89 -41
  58. package/lib/model/modelTableMetadata.js +4 -11
  59. package/lib/model/modelToJson.js +6 -13
  60. package/lib/model/modelUtils.js +14 -13
  61. package/lib/model/modelValidate.js +3 -9
  62. package/lib/model/modelValues.js +5 -14
  63. package/lib/model/modelVisitor.js +1 -7
  64. package/lib/objection.js +33 -71
  65. package/lib/queryBuilder/FunctionBuilder.js +4 -11
  66. package/lib/queryBuilder/InternalOptions.js +4 -9
  67. package/lib/queryBuilder/JoinBuilder.js +5 -8
  68. package/lib/queryBuilder/QueryBuilder.js +126 -95
  69. package/lib/queryBuilder/QueryBuilderBase.js +41 -123
  70. package/lib/queryBuilder/QueryBuilderContext.js +2 -8
  71. package/lib/queryBuilder/QueryBuilderContextBase.js +2 -8
  72. package/lib/queryBuilder/QueryBuilderOperationSupport.js +7 -21
  73. package/lib/queryBuilder/QueryBuilderUserContext.js +1 -7
  74. package/lib/queryBuilder/RawBuilder.js +5 -13
  75. package/lib/queryBuilder/ReferenceBuilder.js +20 -48
  76. package/lib/queryBuilder/RelationExpression.js +18 -16
  77. package/lib/queryBuilder/StaticHookArguments.js +74 -16
  78. package/lib/queryBuilder/ValueBuilder.js +11 -13
  79. package/lib/queryBuilder/getJoinBuilder.js +10 -0
  80. package/lib/queryBuilder/graph/GraphLoader.js +314 -0
  81. package/lib/queryBuilder/graph/GraphNode.js +241 -0
  82. package/lib/queryBuilder/graph/GraphOptions.js +55 -248
  83. package/lib/queryBuilder/graph/GraphPlan.js +431 -0
  84. package/lib/queryBuilder/graph/GraphSchedule.js +513 -0
  85. package/lib/queryBuilder/graph/GraphUpsert.js +519 -412
  86. package/lib/queryBuilder/graph/GraphWriter.js +508 -0
  87. package/lib/queryBuilder/graph/graphKeys.js +929 -0
  88. package/lib/queryBuilder/join/JoinResultColumn.js +1 -7
  89. package/lib/queryBuilder/join/JoinResultParser.js +6 -11
  90. package/lib/queryBuilder/join/RelationJoiner.js +66 -84
  91. package/lib/queryBuilder/join/TableNode.js +2 -8
  92. package/lib/queryBuilder/join/TableTree.js +3 -9
  93. package/lib/queryBuilder/join/utils.js +5 -15
  94. package/lib/queryBuilder/jsonDialects/JsonDialect.js +51 -0
  95. package/lib/queryBuilder/jsonDialects/MySqlJsonDialect.js +81 -0
  96. package/lib/queryBuilder/jsonDialects/PostgresJsonDialect.js +45 -0
  97. package/lib/queryBuilder/jsonDialects/SqliteJsonDialect.js +67 -0
  98. package/lib/queryBuilder/jsonDialects/index.js +44 -0
  99. package/lib/queryBuilder/operations/DelegateOperation.js +2 -8
  100. package/lib/queryBuilder/operations/DeleteOperation.js +10 -39
  101. package/lib/queryBuilder/operations/FindByIdOperation.js +7 -12
  102. package/lib/queryBuilder/operations/FindByIdsOperation.js +4 -8
  103. package/lib/queryBuilder/operations/FindOperation.js +14 -34
  104. package/lib/queryBuilder/operations/FirstOperation.js +6 -10
  105. package/lib/queryBuilder/operations/FromOperation.js +3 -9
  106. package/lib/queryBuilder/operations/InsertAndFetchOperation.js +13 -12
  107. package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +4 -10
  108. package/lib/queryBuilder/operations/InsertGraphOperation.js +7 -18
  109. package/lib/queryBuilder/operations/InsertOperation.js +118 -45
  110. package/lib/queryBuilder/operations/InstanceDeleteOperation.js +5 -12
  111. package/lib/queryBuilder/operations/InstanceFindOperation.js +4 -11
  112. package/lib/queryBuilder/operations/InstanceInsertOperation.js +2 -8
  113. package/lib/queryBuilder/operations/InstanceUpdateOperation.js +7 -14
  114. package/lib/queryBuilder/operations/JoinRelatedOperation.js +5 -11
  115. package/lib/queryBuilder/operations/KnexOperation.js +2 -8
  116. package/lib/queryBuilder/operations/LimitPerParentOperation.js +7 -69
  117. package/lib/queryBuilder/operations/MergeOperation.js +5 -11
  118. package/lib/queryBuilder/operations/NoneOperation.js +2 -8
  119. package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +20 -49
  120. package/lib/queryBuilder/operations/OnBuildKnexOperation.js +2 -8
  121. package/lib/queryBuilder/operations/OnBuildOperation.js +2 -8
  122. package/lib/queryBuilder/operations/OnErrorOperation.js +2 -8
  123. package/lib/queryBuilder/operations/QueryBuilderOperation.js +1 -7
  124. package/lib/queryBuilder/operations/RangeOperation.js +11 -26
  125. package/lib/queryBuilder/operations/RelateOperation.js +2 -8
  126. package/lib/queryBuilder/operations/ReturningOperation.js +3 -9
  127. package/lib/queryBuilder/operations/RunAfterOperation.js +2 -12
  128. package/lib/queryBuilder/operations/RunBeforeOperation.js +2 -8
  129. package/lib/queryBuilder/operations/UnrelateOperation.js +2 -8
  130. package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +14 -17
  131. package/lib/queryBuilder/operations/UpdateOperation.js +32 -132
  132. package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +8 -11
  133. package/lib/queryBuilder/operations/UpsertGraphOperation.js +6 -12
  134. package/lib/queryBuilder/operations/WhereCompositeOperation.js +3 -9
  135. package/lib/queryBuilder/operations/WhereGroupOperation.js +2 -8
  136. package/lib/queryBuilder/operations/eager/EagerOperation.js +3 -9
  137. package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +72 -10
  138. package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +2 -8
  139. package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +23 -44
  140. package/lib/queryBuilder/operations/fieldExpressions.js +122 -0
  141. package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +20 -0
  142. package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +44 -0
  143. package/lib/queryBuilder/operations/jsonApi/WhereJsonTypeOperation.js +14 -0
  144. package/lib/queryBuilder/operations/select/SelectOperation.js +11 -21
  145. package/lib/queryBuilder/operations/select/Selection.js +19 -17
  146. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +4 -10
  147. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +4 -10
  148. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +4 -10
  149. package/lib/queryBuilder/operations/windowOrder.js +56 -0
  150. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +3 -7
  151. package/lib/queryBuilder/parsers/relationExpressionParser.js +42 -43
  152. package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +39 -36
  153. package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +2 -8
  154. package/lib/queryBuilder/transformations/QueryTransformation.js +1 -7
  155. package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +3 -9
  156. package/lib/queryBuilder/transformations/index.js +3 -11
  157. package/lib/queryBuilder/whereGroups.js +9 -24
  158. package/lib/queryBuilder/withoutOrder.js +4 -11
  159. package/lib/relations/Relation.js +47 -28
  160. package/lib/relations/RelationDeleteOperation.js +4 -14
  161. package/lib/relations/RelationFindOperation.js +119 -90
  162. package/lib/relations/RelationInsertOperation.js +2 -8
  163. package/lib/relations/RelationOwner.js +5 -11
  164. package/lib/relations/RelationProperty.js +4 -10
  165. package/lib/relations/RelationUpdateOperation.js +4 -14
  166. package/lib/relations/RelationWriteResultOperation.js +15 -0
  167. package/lib/relations/belongsToMany/BelongsToManyDeleteOperation.js +23 -0
  168. package/lib/relations/belongsToMany/BelongsToManyFindOperation.js +35 -0
  169. package/lib/relations/belongsToMany/BelongsToManyInsertOperation.js +28 -0
  170. package/lib/relations/belongsToMany/BelongsToManyRelateOperation.js +16 -0
  171. package/lib/relations/belongsToMany/BelongsToManyRelation.js +276 -0
  172. package/lib/relations/belongsToMany/BelongsToManyUnrelateOperation.js +11 -0
  173. package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +2 -8
  174. package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +6 -11
  175. package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +3 -9
  176. package/lib/relations/belongsToOne/BelongsToOneRelation.js +8 -19
  177. package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +53 -27
  178. package/lib/relations/hasMany/HasManyInsertOperation.js +5 -10
  179. package/lib/relations/hasMany/HasManyRelateOperation.js +5 -11
  180. package/lib/relations/hasMany/HasManyRelation.js +9 -11
  181. package/lib/relations/hasMany/HasManyUnrelateOperation.js +3 -9
  182. package/lib/relations/hasOne/HasOneRelation.js +2 -8
  183. package/lib/relations/hasOneThrough/HasOneThroughRelation.js +2 -8
  184. package/lib/relations/manyToMany/ManyToManyModifyMixin.js +10 -15
  185. package/lib/relations/manyToMany/ManyToManyMySqlModifyMixin.js +2 -8
  186. package/lib/relations/manyToMany/ManyToManyRelation.js +82 -50
  187. package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +2 -8
  188. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +5 -9
  189. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +2 -8
  190. package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +3 -9
  191. package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +20 -59
  192. package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +6 -11
  193. package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +4 -10
  194. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateMySqlOperation.js +3 -9
  195. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +5 -9
  196. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +3 -9
  197. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +3 -9
  198. package/lib/relations/manyToMany/update/ManyToManyUpdateMySqlOperation.js +3 -9
  199. package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +5 -9
  200. package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +64 -72
  201. package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +3 -9
  202. package/lib/transaction.js +3 -9
  203. package/lib/utils/assert.js +3 -11
  204. package/lib/utils/buildUtils.js +2 -8
  205. package/lib/utils/classUtils.js +1 -7
  206. package/lib/utils/createModifier.js +2 -8
  207. package/lib/utils/identifierMapping.js +23 -27
  208. package/lib/utils/internalPropUtils.js +1 -7
  209. package/lib/utils/knexUtils.js +84 -40
  210. package/lib/utils/mixin.js +3 -10
  211. package/lib/utils/normalizeIds.js +2 -8
  212. package/lib/utils/objectUtils.js +41 -60
  213. package/lib/utils/parseFieldExpression.js +16 -16
  214. package/lib/utils/promiseUtils/after.js +2 -8
  215. package/lib/utils/promiseUtils/afterReturn.js +2 -8
  216. package/lib/utils/promiseUtils/index.js +7 -16
  217. package/lib/utils/promiseUtils/isPromise.js +2 -8
  218. package/lib/utils/promiseUtils/map.js +2 -8
  219. package/lib/utils/promiseUtils/mapAfterAllReturn.js +2 -8
  220. package/lib/utils/promiseUtils/try.js +2 -8
  221. package/lib/utils/resolveModel.js +10 -17
  222. package/lib/utils/tmpColumnUtils.js +2 -9
  223. package/lib/utils/warnings.js +2 -14
  224. package/package.json +25 -17
  225. package/typings/objection/index.d.ts +3217 -2179
  226. package/lib/.eslintrc.json +0 -8
  227. package/lib/model/graph/ModelGraph.js +0 -80
  228. package/lib/model/graph/ModelGraphBuilder.js +0 -241
  229. package/lib/model/graph/ModelGraphEdge.js +0 -52
  230. package/lib/model/graph/ModelGraphNode.js +0 -238
  231. package/lib/queryBuilder/graph/GraphAction.js +0 -69
  232. package/lib/queryBuilder/graph/GraphData.js +0 -53
  233. package/lib/queryBuilder/graph/GraphFetcher.js +0 -197
  234. package/lib/queryBuilder/graph/GraphNodeDbExistence.js +0 -184
  235. package/lib/queryBuilder/graph/GraphOperation.js +0 -27
  236. package/lib/queryBuilder/graph/delete/GraphDelete.js +0 -73
  237. package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +0 -88
  238. package/lib/queryBuilder/graph/insert/DeferredRelationGraphInsertAction.js +0 -58
  239. package/lib/queryBuilder/graph/insert/GraphInsert.js +0 -296
  240. package/lib/queryBuilder/graph/insert/GraphInsertAction.js +0 -233
  241. package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +0 -92
  242. package/lib/queryBuilder/graph/patch/GraphPatch.js +0 -20
  243. package/lib/queryBuilder/graph/patch/GraphPatchAction.js +0 -289
  244. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +0 -31
  245. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +0 -126
  246. package/lib/queryBuilder/operations/jsonApi/WhereJsonHasPostgresOperation.js +0 -30
  247. package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectPostgresOperation.js +0 -37
  248. package/lib/queryBuilder/operations/jsonApi/WhereJsonPostgresOperation.js +0 -62
  249. package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +0 -192
  250. package/lib/utils/wrapDbError.js +0 -64
@@ -1,8 +0,0 @@
1
- {
2
- "parserOptions": {
3
- "sourceType": "script"
4
- },
5
- "rules": {
6
- "strict": "error"
7
- }
8
- }
@@ -1,80 +0,0 @@
1
- 'use strict';
2
-
3
- const { ModelGraphBuilder } = require('./ModelGraphBuilder');
4
- const NOT_CALCULATED = {};
5
-
6
- class ModelGraph {
7
- constructor(nodes, edges) {
8
- this.nodes = nodes;
9
- this.edges = edges;
10
-
11
- // These are calculated lazily.
12
- this._nodesByObjects = NOT_CALCULATED;
13
- this._nodesByIdPathKeys = NOT_CALCULATED;
14
- }
15
-
16
- static create(rootModelClass, roots) {
17
- const builder = ModelGraphBuilder.buildGraph(rootModelClass, roots);
18
- return new ModelGraph(builder.nodes, builder.edges);
19
- }
20
-
21
- static createEmpty() {
22
- return new ModelGraph([], []);
23
- }
24
-
25
- get rootObjects() {
26
- return this.nodes.filter((node) => !node.parentEdge).map((node) => node.obj);
27
- }
28
-
29
- nodeForObject(obj) {
30
- if (!obj) {
31
- return null;
32
- }
33
-
34
- if (this._nodesByObjects === NOT_CALCULATED) {
35
- this._nodesByObjects = createNodesByObjectsMap(this.nodes);
36
- }
37
-
38
- return this._nodesByObjects.get(obj) || null;
39
- }
40
-
41
- nodeForNode(node) {
42
- if (!node) {
43
- return null;
44
- }
45
-
46
- if (this._nodesByIdPathKeys === NOT_CALCULATED) {
47
- this._nodesByIdPathKeys = createNodesByIdPathKeysMap(this.nodes);
48
- }
49
-
50
- return this._nodesByIdPathKeys.get(node.idPathKey) || null;
51
- }
52
- }
53
-
54
- function createNodesByObjectsMap(nodes) {
55
- const nodesByObjects = new Map();
56
-
57
- for (const node of nodes) {
58
- nodesByObjects.set(node.obj, node);
59
- }
60
-
61
- return nodesByObjects;
62
- }
63
-
64
- function createNodesByIdPathKeysMap(nodes) {
65
- const nodesByIdPathKeys = new Map();
66
-
67
- for (const node of nodes) {
68
- const idPathKey = node.idPathKey;
69
-
70
- if (idPathKey !== null) {
71
- nodesByIdPathKeys.set(idPathKey, node);
72
- }
73
- }
74
-
75
- return nodesByIdPathKeys;
76
- }
77
-
78
- module.exports = {
79
- ModelGraph,
80
- };
@@ -1,241 +0,0 @@
1
- 'use strict';
2
-
3
- const { isObject, isString, asArray, asSingle } = require('../../utils/objectUtils');
4
- const { ValidationErrorType } = require('../../model/ValidationError');
5
- const { ModelGraphNode } = require('./ModelGraphNode');
6
- const { ModelGraphEdge } = require('./ModelGraphEdge');
7
-
8
- 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);
138
- }
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
- }
163
- }
164
-
165
- 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
- }
181
- }
182
-
183
- 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
- }
191
- }
192
-
193
- 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
- }
199
- }
200
-
201
- 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
- }
207
- }
208
-
209
- function forEachMatch(regex, str, cb) {
210
- let matchResult = regex.exec(str);
211
-
212
- while (matchResult) {
213
- cb(matchResult);
214
- matchResult = regex.exec(str);
215
- }
216
- }
217
-
218
- 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
- );
228
- }
229
-
230
- 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
- });
235
- }
236
-
237
- module.exports = {
238
- ModelGraphBuilder,
239
- createNotModelError,
240
- forEachPropertyReference,
241
- };
@@ -1,52 +0,0 @@
1
- 'use strict';
2
-
3
- const Type = {
4
- Relation: 'Relation',
5
- Reference: 'Reference',
6
- };
7
-
8
- const ReferenceType = {
9
- Object: 'Object',
10
- Property: 'Property',
11
- };
12
-
13
- 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
- }
48
- }
49
-
50
- module.exports = {
51
- ModelGraphEdge,
52
- };
@@ -1,238 +0,0 @@
1
- 'use strict';
2
-
3
- const { ModelGraphEdge } = require('./ModelGraphEdge');
4
- const { isNumber } = require('../../utils/objectUtils');
5
-
6
- const NOT_CALCULATED = {};
7
-
8
- 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 toBeUnrelated() {
50
- return this.obj[this.modelClass.graphUnrelateProp] === true;
51
- }
52
-
53
- get toBeDeleted() {
54
- return this.obj[this.modelClass.graphDeleteProp] === true;
55
- }
56
-
57
- get toBeUnrelatedOrDeleted() {
58
- return this.toBeUnrelated || this.toBeDeleted;
59
- }
60
-
61
- get parentNode() {
62
- if (this.parentEdge) {
63
- return this.parentEdge.ownerNode;
64
- } else {
65
- return null;
66
- }
67
- }
68
-
69
- get indexInRelation() {
70
- if (this.parentEdge) {
71
- return this.parentEdge.relationIndex;
72
- } else {
73
- return null;
74
- }
75
- }
76
-
77
- get relationName() {
78
- if (this.parentEdge) {
79
- return this.parentEdge.relation.name;
80
- } else {
81
- return null;
82
- }
83
- }
84
-
85
- get relationPath() {
86
- if (this._relationPath === NOT_CALCULATED) {
87
- this._relationPath = this._createRelationPath();
88
- }
89
-
90
- return this._relationPath;
91
- }
92
-
93
- get relationPathKey() {
94
- if (this._relationPathKey === NOT_CALCULATED) {
95
- this._relationPathKey = this._createRelationPathKey();
96
- }
97
-
98
- return this._relationPathKey;
99
- }
100
-
101
- get dataPath() {
102
- if (this._dataPath === NOT_CALCULATED) {
103
- this._dataPath = this._createDataPath();
104
- }
105
-
106
- return this._dataPath;
107
- }
108
-
109
- get dataPathKey() {
110
- if (this._dataPathKey === NOT_CALCULATED) {
111
- this._dataPathKey = this._createDataPathKey();
112
- }
113
-
114
- return this._dataPathKey;
115
- }
116
-
117
- get idPath() {
118
- if (this._idPath === NOT_CALCULATED) {
119
- this._idPath = this._createIdPath();
120
- }
121
-
122
- return this._idPath;
123
- }
124
-
125
- get idPathKey() {
126
- if (this._idPathKey === NOT_CALCULATED) {
127
- this._idPathKey = this._createIdPathKey();
128
- }
129
-
130
- return this._idPathKey;
131
- }
132
-
133
- /**
134
- * If this node is a reference, returns the referred node.
135
- */
136
- get referencedNode() {
137
- for (const edge of this.refEdges) {
138
- if (edge.refType === ModelGraphEdge.ReferenceType.Object && edge.isOwnerNode(this)) {
139
- return edge.relatedNode;
140
- }
141
- }
142
-
143
- return null;
144
- }
145
-
146
- get descendantRelationNodes() {
147
- return this._collectDescendantRelationNodes([]);
148
- }
149
-
150
- removeEdge(edge) {
151
- // Don't allow removing parent edges for now. It would
152
- // cause all kinds of cache invalidation.
153
- if (edge === this.parentEdge) {
154
- throw new Error('cannot remove parent edge');
155
- }
156
-
157
- this.edges = this.edges.filter((it) => it !== edge);
158
- this.refEdges = this.refEdges.filter((it) => it !== edge);
159
- }
160
-
161
- _collectDescendantRelationNodes(nodes) {
162
- for (const edge of this.edges) {
163
- if (edge.type === ModelGraphEdge.Type.Relation && edge.isOwnerNode(this)) {
164
- nodes.push(edge.relatedNode);
165
- edge.relatedNode._collectDescendantRelationNodes(nodes);
166
- }
167
- }
168
-
169
- return nodes;
170
- }
171
-
172
- _createRelationPath() {
173
- if (this.parentNode === null) {
174
- return [];
175
- } else {
176
- return [...this.parentNode.relationPath, this.relationName];
177
- }
178
- }
179
-
180
- _createRelationPathKey() {
181
- return this.relationPath.join('.');
182
- }
183
-
184
- _createDataPath() {
185
- if (this.parentEdge === null) {
186
- return [];
187
- } else if (this.parentEdge.relation.isOneToOne()) {
188
- return [...this.parentNode.dataPath, this.relationName];
189
- } else {
190
- return [...this.parentNode.dataPath, this.relationName, this.indexInRelation];
191
- }
192
- }
193
-
194
- _createDataPathKey() {
195
- const dataPathKey = this.dataPath.reduce((key, it) => {
196
- if (isNumber(it)) {
197
- return `${key}[${it}]`;
198
- } else {
199
- return key ? `${key}.${it}` : it;
200
- }
201
- }, '');
202
-
203
- return dataPathKey ? '.' + dataPathKey : dataPathKey;
204
- }
205
-
206
- _createIdPath() {
207
- if (!this.obj.$hasId()) {
208
- return null;
209
- }
210
-
211
- if (this.parentEdge === null) {
212
- return [this.obj.$idKey()];
213
- } else {
214
- const path = this.parentNode.idPath;
215
-
216
- if (path === null) {
217
- return null;
218
- }
219
-
220
- return [...path, this.relationName, this.obj.$idKey()];
221
- }
222
- }
223
-
224
- _createIdPathKey() {
225
- const idPath = this.idPath;
226
-
227
- if (idPath) {
228
- // JSON keeps the ids and relation names apart, whatever they contain.
229
- return JSON.stringify(idPath);
230
- } else {
231
- return null;
232
- }
233
- }
234
- }
235
-
236
- module.exports = {
237
- ModelGraphNode,
238
- };
@@ -1,69 +0,0 @@
1
- 'use strict';
2
-
3
- const { isPostgres } = require('../../utils/knexUtils');
4
-
5
- const POSTGRES_MAX_INSERT_BATCH_SIZE = 100;
6
- const MAX_CONCURRENCY = 100;
7
-
8
- class GraphAction {
9
- constructor(graphData) {
10
- this.graphData = graphData;
11
- }
12
-
13
- static get ReturningAllSelector() {
14
- return (op) => {
15
- // Only select `returning('*')` operation.
16
- return op.name === 'returning' && op.args.includes('*');
17
- };
18
- }
19
-
20
- static getConcurrency(builder, nodes) {
21
- return nodes.reduce((minConcurrency, node) => {
22
- return Math.min(minConcurrency, node.modelClass.getConcurrency(builder.unsafeKnex()));
23
- }, MAX_CONCURRENCY);
24
- }
25
-
26
- get graph() {
27
- return this.graphData.graph;
28
- }
29
-
30
- get currentGraph() {
31
- return this.graphData.currentGraph;
32
- }
33
-
34
- get graphOptions() {
35
- return this.graphData.graphOptions;
36
- }
37
-
38
- run(builder) {
39
- return null;
40
- }
41
-
42
- _getConcurrency(builder, nodes) {
43
- return GraphAction.getConcurrency(builder, nodes);
44
- }
45
-
46
- _getBatchSize(builder) {
47
- return isPostgres(builder.unsafeKnex()) ? POSTGRES_MAX_INSERT_BATCH_SIZE : 1;
48
- }
49
-
50
- _resolveReferences(node) {
51
- if (node.isReference) {
52
- this._resolveReference(node);
53
- }
54
- }
55
-
56
- _resolveReference(node) {
57
- const refNode = node.referencedNode;
58
-
59
- for (const prop of Object.keys(refNode.obj)) {
60
- if (!node.obj.hasOwnProperty(prop)) {
61
- node.obj[prop] = refNode.obj[prop];
62
- }
63
- }
64
- }
65
- }
66
-
67
- module.exports = {
68
- GraphAction,
69
- };