@abedmurrar/objection 4.0.0-alpha.0 → 4.0.0-beta.1

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 +293 -375
  129. package/lib/relations/RelationDeleteOperation.js +24 -28
  130. package/lib/relations/RelationFindOperation.js +108 -138
  131. package/lib/relations/RelationInsertOperation.js +24 -32
  132. package/lib/relations/RelationOwner.js +175 -209
  133. package/lib/relations/RelationProperty.js +186 -223
  134. package/lib/relations/RelationUpdateOperation.js +23 -29
  135. package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +16 -21
  136. package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +30 -41
  137. package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +36 -47
  138. package/lib/relations/belongsToOne/BelongsToOneRelation.js +42 -50
  139. package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +43 -58
  140. package/lib/relations/hasMany/HasManyInsertOperation.js +40 -56
  141. package/lib/relations/hasMany/HasManyRelateOperation.js +45 -61
  142. package/lib/relations/hasMany/HasManyRelation.js +38 -46
  143. package/lib/relations/hasMany/HasManyUnrelateOperation.js +25 -32
  144. package/lib/relations/hasOne/HasOneRelation.js +9 -12
  145. package/lib/relations/hasOneThrough/HasOneThroughRelation.js +9 -12
  146. package/lib/relations/manyToMany/ManyToManyModifyMixin.js +73 -95
  147. package/lib/relations/manyToMany/ManyToManyRelation.js +374 -473
  148. package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +23 -30
  149. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +8 -10
  150. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +20 -25
  151. package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +8 -12
  152. package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +77 -100
  153. package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +37 -49
  154. package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +58 -74
  155. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +10 -13
  156. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +26 -33
  157. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +10 -15
  158. package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +8 -10
  159. package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +61 -78
  160. package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +8 -12
  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,20 +1,18 @@
1
1
  'use strict';
2
-
3
2
  const hookNameToHasMethodName = {
4
- onAdd: 'hasOnAdd',
5
- onBefore1: 'hasOnBefore1',
6
- onBefore2: 'hasOnBefore2',
7
- onBefore3: 'hasOnBefore3',
8
- onBuild: 'hasOnBuild',
9
- onBuildKnex: 'hasOnBuildKnex',
10
- onRawResult: 'hasOnRawResult',
11
- queryExecutor: 'hasQueryExecutor',
12
- onAfter1: 'hasOnAfter1',
13
- onAfter2: 'hasOnAfter2',
14
- onAfter3: 'hasOnAfter3',
15
- onError: 'hasOnError',
3
+ onAdd: 'hasOnAdd',
4
+ onBefore1: 'hasOnBefore1',
5
+ onBefore2: 'hasOnBefore2',
6
+ onBefore3: 'hasOnBefore3',
7
+ onBuild: 'hasOnBuild',
8
+ onBuildKnex: 'hasOnBuildKnex',
9
+ onRawResult: 'hasOnRawResult',
10
+ queryExecutor: 'hasQueryExecutor',
11
+ onAfter1: 'hasOnAfter1',
12
+ onAfter2: 'hasOnAfter2',
13
+ onAfter3: 'hasOnAfter3',
14
+ onError: 'hasOnError',
16
15
  };
17
-
18
16
  // An abstract base class for all query builder operations. QueryBuilderOperations almost always
19
17
  // correspond to a single query builder method call. For example SelectOperation could be added when
20
18
  // a `select` method is called.
@@ -28,270 +26,233 @@ const hookNameToHasMethodName = {
28
26
  // a builder has 5 operations, onBefore1 hook is called for each of them (and their results are awaited)
29
27
  // before onBefore2 hook is called for any of the operations.
30
28
  class QueryBuilderOperation {
31
- constructor(name = null, opt = {}) {
32
- this.name = name;
33
- this.opt = opt;
34
-
35
- // From which hook was this operation added as a child
36
- // operation.
37
- this.adderHookName = null;
38
-
39
- // The parent operation that added this operation.
40
- this.parentOperation = null;
41
-
42
- // Operations this operation added in any of its hooks.
43
- this.childOperations = [];
44
-
45
- // The where scope (see `whereScopes.js`) this operation was added in.
46
- this.whereScope = null;
47
- }
48
-
49
- is(OperationClass) {
50
- return this instanceof OperationClass;
51
- }
52
-
53
- hasHook(hookName) {
54
- return this[hookNameToHasMethodName[hookName]]();
55
- }
56
-
57
- // This is called immediately when a query builder method is called.
58
- //
59
- // This method must be synchronous.
60
- // This method should never call any methods that add operations to the builder.
61
- onAdd(builder, args) {
62
- return true;
63
- }
64
- hasOnAdd() {
65
- return true;
66
- }
67
-
68
- // This is called as the first thing when the query is executed but before
69
- // the actual database operation (knex query) is executed.
70
- //
71
- // This method can be asynchronous.
72
- // You may call methods that add operations to to the builder.
73
- onBefore1(builder, result) {}
74
- hasOnBefore1() {
75
- return this.onBefore1 !== QueryBuilderOperation.prototype.onBefore1;
76
- }
77
-
78
- // This is called as the second thing when the query is executed but before
79
- // the actual database operation (knex query) is executed.
80
- //
81
- // This method can be asynchronous.
82
- // You may call methods that add operations to to the builder.
83
- onBefore2(builder, result) {}
84
- hasOnBefore2() {
85
- return this.onBefore2 !== QueryBuilderOperation.prototype.onBefore2;
86
- }
87
-
88
- // This is called as the third thing when the query is executed but before
89
- // the actual database operation (knex query) is executed.
90
- //
91
- // This method can be asynchronous.
92
- // You may call methods that add operations to to the builder.
93
- onBefore3(builder, result) {}
94
- hasOnBefore3() {
95
- return this.onBefore3 !== QueryBuilderOperation.prototype.onBefore3;
96
- }
97
-
98
- // This is called as the last thing when the query is executed but before
99
- // the actual database operation (knex query) is executed. If your operation
100
- // needs to call other query building operations (methods that add QueryBuilderOperations)
101
- // this is the best and last place to do it.
102
- //
103
- // This method must be synchronous.
104
- // You may call methods that add operations to to the builder.
105
- onBuild(builder) {}
106
- hasOnBuild() {
107
- return this.onBuild !== QueryBuilderOperation.prototype.onBuild;
108
- }
109
-
110
- // This is called when the knex query is built. Here you should only call knex
111
- // methods. You may call getters and other immutable methods of the `builder`
112
- // but you should never call methods that add QueryBuilderOperations.
113
- //
114
- // This method must be synchronous.
115
- // This method should never call any methods that add operations to the builder.
116
- // This method should always return the knex query builder.
117
- onBuildKnex(knexBuilder, builder) {
118
- return knexBuilder;
119
- }
120
- hasOnBuildKnex() {
121
- return this.onBuildKnex !== QueryBuilderOperation.prototype.onBuildKnex;
122
- }
123
-
124
- // The raw knex result is passed to this method right after the database query
125
- // has finished. This method may modify it and return the modified rows. The
126
- // rows are automatically converted to models (if possible) after this hook
127
- // is called.
128
- //
129
- // This method can be asynchronous.
130
- onRawResult(builder, rows) {
131
- return rows;
132
- }
133
- hasOnRawResult() {
134
- return this.onRawResult !== QueryBuilderOperation.prototype.onRawResult;
135
- }
136
-
137
- // This is called as the first thing after the query has been executed and
138
- // rows have been converted to model instances.
139
- //
140
- // This method can be asynchronous.
141
- onAfter1(builder, result) {
142
- return result;
143
- }
144
- hasOnAfter1() {
145
- return this.onAfter1 !== QueryBuilderOperation.prototype.onAfter1;
146
- }
147
-
148
- // This is called as the second thing after the query has been executed and
149
- // rows have been converted to model instances.
150
- //
151
- // This method can be asynchronous.
152
- onAfter2(builder, result) {
153
- return result;
154
- }
155
- hasOnAfter2() {
156
- return this.onAfter2 !== QueryBuilderOperation.prototype.onAfter2;
157
- }
158
-
159
- // This is called as the third thing after the query has been executed and
160
- // rows have been converted to model instances.
161
- //
162
- // This method can be asynchronous.
163
- onAfter3(builder, result) {
164
- return result;
165
- }
166
- hasOnAfter3() {
167
- return this.onAfter3 !== QueryBuilderOperation.prototype.onAfter3;
168
- }
169
-
170
- // This method can be implemented to return another operation that will replace
171
- // this one. This method is called after all `onBeforeX` and `onBuildX` hooks
172
- // but before the database query is executed.
173
- //
174
- // This method must return a QueryBuilder instance.
175
- queryExecutor(builder) {}
176
- hasQueryExecutor() {
177
- return this.queryExecutor !== QueryBuilderOperation.prototype.queryExecutor;
178
- }
179
-
180
- // This is called if an error occurs in the query execution.
181
- //
182
- // This method must return a QueryBuilder instance.
183
- onError(builder, error) {}
184
- hasOnError() {
185
- return this.onError !== QueryBuilderOperation.prototype.onError;
186
- }
187
-
188
- // Returns the "find" equivalent of this operation.
189
- //
190
- // For example an operation that finds an item and updates it
191
- // should return an operation that simply finds the item but
192
- // doesn't update anything. An insert operation should return
193
- // null since there is no find equivalent for it etc.
194
- toFindOperation(builder) {
195
- return this;
196
- }
197
- hasToFindOperation() {
198
- return this.toFindOperation !== QueryBuilderOperation.prototype.toFindOperation;
199
- }
200
-
201
- // Given a set of operations, returns true if any of this operation's
202
- // ancestor operations are included in the set.
203
- isAncestorInSet(operationSet) {
204
- let ancestor = this.parentOperation;
205
-
206
- while (ancestor) {
207
- if (operationSet.has(ancestor)) {
29
+ constructor(name = null, opt = {}) {
30
+ this.name = name;
31
+ this.opt = opt;
32
+ // From which hook was this operation added as a child
33
+ // operation.
34
+ this.adderHookName = null;
35
+ // The parent operation that added this operation.
36
+ this.parentOperation = null;
37
+ // Operations this operation added in any of its hooks.
38
+ this.childOperations = [];
39
+ // The where scope (see `whereScopes.js`) this operation was added in.
40
+ this.whereScope = null;
41
+ }
42
+ is(OperationClass) {
43
+ return this instanceof OperationClass;
44
+ }
45
+ hasHook(hookName) {
46
+ return this[hookNameToHasMethodName[hookName]]();
47
+ }
48
+ // This is called immediately when a query builder method is called.
49
+ //
50
+ // This method must be synchronous.
51
+ // This method should never call any methods that add operations to the builder.
52
+ onAdd(builder, args) {
53
+ return true;
54
+ }
55
+ hasOnAdd() {
208
56
  return true;
209
- }
210
-
211
- ancestor = ancestor.parentOperation;
212
57
  }
213
-
214
- return false;
215
- }
216
-
217
- // Takes a deep clone of this operation.
218
- clone() {
219
- const clone = new this.constructor(this.name, this.opt);
220
-
221
- clone.adderHookName = this.adderHookName;
222
- clone.parentOperation = this.parentOperation;
223
- clone.whereScope = this.whereScope;
224
-
225
- clone.childOperations = this.childOperations.map((childOp) => {
226
- const childOpClone = childOp.clone();
227
-
228
- childOpClone.parentOperation = clone;
229
- return childOpClone;
230
- });
231
-
232
- return clone;
233
- }
234
-
235
- // Add an operation as a child operation. `hookName` must be the
236
- // name of the parent operation's hook that called this method.
237
- addChildOperation(hookName, operation) {
238
- operation.adderHookName = hookName;
239
- operation.parentOperation = this;
240
-
241
- this.childOperations.push(operation);
242
- }
243
-
244
- // Removes a single child operation.
245
- removeChildOperation(operation) {
246
- const index = this.childOperations.indexOf(operation);
247
-
248
- if (index !== -1) {
249
- operation.parentOperation = null;
250
- this.childOperations.splice(index, 1);
58
+ // This is called as the first thing when the query is executed but before
59
+ // the actual database operation (knex query) is executed.
60
+ //
61
+ // This method can be asynchronous.
62
+ // You may call methods that add operations to to the builder.
63
+ onBefore1(builder, result) { }
64
+ hasOnBefore1() {
65
+ return this.onBefore1 !== QueryBuilderOperation.prototype.onBefore1;
251
66
  }
252
- }
253
-
254
- // Replaces a single child operation.
255
- replaceChildOperation(operation, newOperation) {
256
- const index = this.childOperations.indexOf(operation);
257
-
258
- if (index !== -1) {
259
- newOperation.adderHookName = operation.adderHookName;
260
- newOperation.parentOperation = this;
261
- operation.parentOperation = null;
262
- this.childOperations[index] = newOperation;
67
+ // This is called as the second thing when the query is executed but before
68
+ // the actual database operation (knex query) is executed.
69
+ //
70
+ // This method can be asynchronous.
71
+ // You may call methods that add operations to to the builder.
72
+ onBefore2(builder, result) { }
73
+ hasOnBefore2() {
74
+ return this.onBefore2 !== QueryBuilderOperation.prototype.onBefore2;
263
75
  }
264
- }
265
-
266
- // Removes all child operations that were added from the `hookName` hook.
267
- removeChildOperationsByHookName(hookName) {
268
- this.childOperations = this.childOperations.filter((op) => op.adderHookName !== hookName);
269
- }
270
-
271
- // Iterates through all descendant operations recursively.
272
- forEachDescendantOperation(callback) {
273
- for (const operation of this.childOperations) {
274
- if (callback(operation) === false) {
275
- return false;
276
- }
277
-
278
- if (operation.forEachDescendantOperation(callback) === false) {
76
+ // This is called as the third thing when the query is executed but before
77
+ // the actual database operation (knex query) is executed.
78
+ //
79
+ // This method can be asynchronous.
80
+ // You may call methods that add operations to to the builder.
81
+ onBefore3(builder, result) { }
82
+ hasOnBefore3() {
83
+ return this.onBefore3 !== QueryBuilderOperation.prototype.onBefore3;
84
+ }
85
+ // This is called as the last thing when the query is executed but before
86
+ // the actual database operation (knex query) is executed. If your operation
87
+ // needs to call other query building operations (methods that add QueryBuilderOperations)
88
+ // this is the best and last place to do it.
89
+ //
90
+ // This method must be synchronous.
91
+ // You may call methods that add operations to to the builder.
92
+ onBuild(builder) { }
93
+ hasOnBuild() {
94
+ return this.onBuild !== QueryBuilderOperation.prototype.onBuild;
95
+ }
96
+ // This is called when the knex query is built. Here you should only call knex
97
+ // methods. You may call getters and other immutable methods of the `builder`
98
+ // but you should never call methods that add QueryBuilderOperations.
99
+ //
100
+ // This method must be synchronous.
101
+ // This method should never call any methods that add operations to the builder.
102
+ // This method should always return the knex query builder.
103
+ onBuildKnex(knexBuilder, builder) {
104
+ return knexBuilder;
105
+ }
106
+ hasOnBuildKnex() {
107
+ return this.onBuildKnex !== QueryBuilderOperation.prototype.onBuildKnex;
108
+ }
109
+ // The raw knex result is passed to this method right after the database query
110
+ // has finished. This method may modify it and return the modified rows. The
111
+ // rows are automatically converted to models (if possible) after this hook
112
+ // is called.
113
+ //
114
+ // This method can be asynchronous.
115
+ onRawResult(builder, rows) {
116
+ return rows;
117
+ }
118
+ hasOnRawResult() {
119
+ return this.onRawResult !== QueryBuilderOperation.prototype.onRawResult;
120
+ }
121
+ // This is called as the first thing after the query has been executed and
122
+ // rows have been converted to model instances.
123
+ //
124
+ // This method can be asynchronous.
125
+ onAfter1(builder, result) {
126
+ return result;
127
+ }
128
+ hasOnAfter1() {
129
+ return this.onAfter1 !== QueryBuilderOperation.prototype.onAfter1;
130
+ }
131
+ // This is called as the second thing after the query has been executed and
132
+ // rows have been converted to model instances.
133
+ //
134
+ // This method can be asynchronous.
135
+ onAfter2(builder, result) {
136
+ return result;
137
+ }
138
+ hasOnAfter2() {
139
+ return this.onAfter2 !== QueryBuilderOperation.prototype.onAfter2;
140
+ }
141
+ // This is called as the third thing after the query has been executed and
142
+ // rows have been converted to model instances.
143
+ //
144
+ // This method can be asynchronous.
145
+ onAfter3(builder, result) {
146
+ return result;
147
+ }
148
+ hasOnAfter3() {
149
+ return this.onAfter3 !== QueryBuilderOperation.prototype.onAfter3;
150
+ }
151
+ // This method can be implemented to return another operation that will replace
152
+ // this one. This method is called after all `onBeforeX` and `onBuildX` hooks
153
+ // but before the database query is executed.
154
+ //
155
+ // This method must return a QueryBuilder instance.
156
+ queryExecutor(builder) { }
157
+ hasQueryExecutor() {
158
+ return this.queryExecutor !== QueryBuilderOperation.prototype.queryExecutor;
159
+ }
160
+ // This is called if an error occurs in the query execution.
161
+ //
162
+ // This method must return a QueryBuilder instance.
163
+ onError(builder, error) { }
164
+ hasOnError() {
165
+ return this.onError !== QueryBuilderOperation.prototype.onError;
166
+ }
167
+ // Returns the "find" equivalent of this operation.
168
+ //
169
+ // For example an operation that finds an item and updates it
170
+ // should return an operation that simply finds the item but
171
+ // doesn't update anything. An insert operation should return
172
+ // null since there is no find equivalent for it etc.
173
+ toFindOperation(builder) {
174
+ return this;
175
+ }
176
+ hasToFindOperation() {
177
+ return this.toFindOperation !== QueryBuilderOperation.prototype.toFindOperation;
178
+ }
179
+ // Given a set of operations, returns true if any of this operation's
180
+ // ancestor operations are included in the set.
181
+ isAncestorInSet(operationSet) {
182
+ let ancestor = this.parentOperation;
183
+ while (ancestor) {
184
+ if (operationSet.has(ancestor)) {
185
+ return true;
186
+ }
187
+ ancestor = ancestor.parentOperation;
188
+ }
279
189
  return false;
280
- }
281
190
  }
282
-
283
- return true;
284
- }
191
+ // Takes a deep clone of this operation.
192
+ clone() {
193
+ const clone = new this.constructor(this.name, this.opt);
194
+ clone.adderHookName = this.adderHookName;
195
+ clone.parentOperation = this.parentOperation;
196
+ clone.whereScope = this.whereScope;
197
+ clone.childOperations = this.childOperations.map((childOp) => {
198
+ const childOpClone = childOp.clone();
199
+ childOpClone.parentOperation = clone;
200
+ return childOpClone;
201
+ });
202
+ return clone;
203
+ }
204
+ // Add an operation as a child operation. `hookName` must be the
205
+ // name of the parent operation's hook that called this method.
206
+ addChildOperation(hookName, operation) {
207
+ operation.adderHookName = hookName;
208
+ operation.parentOperation = this;
209
+ this.childOperations.push(operation);
210
+ }
211
+ // Removes a single child operation.
212
+ removeChildOperation(operation) {
213
+ const index = this.childOperations.indexOf(operation);
214
+ if (index !== -1) {
215
+ operation.parentOperation = null;
216
+ this.childOperations.splice(index, 1);
217
+ }
218
+ }
219
+ // Replaces a single child operation.
220
+ replaceChildOperation(operation, newOperation) {
221
+ const index = this.childOperations.indexOf(operation);
222
+ if (index !== -1) {
223
+ newOperation.adderHookName = operation.adderHookName;
224
+ newOperation.parentOperation = this;
225
+ operation.parentOperation = null;
226
+ this.childOperations[index] = newOperation;
227
+ }
228
+ }
229
+ // Removes all child operations that were added from the `hookName` hook.
230
+ removeChildOperationsByHookName(hookName) {
231
+ // Called before every hook. Most operations have no children.
232
+ if (this.childOperations.length !== 0) {
233
+ this.childOperations = this.childOperations.filter((op) => op.adderHookName !== hookName);
234
+ }
235
+ }
236
+ // Iterates through all descendant operations recursively.
237
+ forEachDescendantOperation(callback) {
238
+ for (const operation of this.childOperations) {
239
+ if (callback(operation) === false) {
240
+ return false;
241
+ }
242
+ if (operation.forEachDescendantOperation(callback) === false) {
243
+ return false;
244
+ }
245
+ }
246
+ return true;
247
+ }
285
248
  }
286
-
287
249
  Object.defineProperties(QueryBuilderOperation, {
288
- isObjectionQueryBuilderOperationClass: {
289
- enumerable: false,
290
- writable: false,
291
- value: true,
292
- },
250
+ isObjectionQueryBuilderOperationClass: {
251
+ enumerable: false,
252
+ writable: false,
253
+ value: true,
254
+ },
293
255
  });
294
-
295
256
  module.exports = {
296
- QueryBuilderOperation,
257
+ QueryBuilderOperation,
297
258
  };
@@ -1,47 +1,37 @@
1
1
  'use strict';
2
-
3
2
  const { QueryBuilderOperation } = require('./QueryBuilderOperation');
4
-
5
3
  class RangeOperation extends QueryBuilderOperation {
6
- constructor(name, opt) {
7
- super(name, opt);
8
- this.resultSizeBuilder = null;
9
- }
10
-
11
- onAdd(builder, args) {
12
- if (args.length === 2) {
13
- const start = args[0];
14
- const end = args[1];
15
-
16
- // Need to set these here instead of `onBuildKnex` so that they
17
- // don't end up in the resultSize query.
18
- builder.limit(end - start + 1).offset(start);
4
+ constructor(name, opt) {
5
+ super(name, opt);
6
+ this.resultSizeBuilder = null;
7
+ }
8
+ onAdd(builder, args) {
9
+ if (args.length === 2) {
10
+ const start = args[0];
11
+ const end = args[1];
12
+ // Need to set these here instead of `onBuildKnex` so that they
13
+ // don't end up in the resultSize query.
14
+ builder.limit(end - start + 1).offset(start);
15
+ }
16
+ return true;
17
+ }
18
+ onBefore1(builder, result) {
19
+ this.resultSizeBuilder = builder.clone();
20
+ return super.onBefore1(builder, result);
21
+ }
22
+ async onAfter3(_, results) {
23
+ const resultSize = await this.resultSizeBuilder.resultSize();
24
+ return {
25
+ results,
26
+ total: parseInt(resultSize, 10),
27
+ };
28
+ }
29
+ clone() {
30
+ const clone = super.clone();
31
+ clone.resultSizeBuilder = this.resultSizeBuilder;
32
+ return clone;
19
33
  }
20
-
21
- return true;
22
- }
23
-
24
- onBefore1(builder, result) {
25
- this.resultSizeBuilder = builder.clone();
26
- return super.onBefore1(builder, result);
27
- }
28
-
29
- async onAfter3(_, results) {
30
- const resultSize = await this.resultSizeBuilder.resultSize();
31
-
32
- return {
33
- results,
34
- total: parseInt(resultSize),
35
- };
36
- }
37
-
38
- clone() {
39
- const clone = super.clone();
40
- clone.resultSizeBuilder = this.resultSizeBuilder;
41
- return clone;
42
- }
43
34
  }
44
-
45
35
  module.exports = {
46
- RangeOperation,
36
+ RangeOperation,
47
37
  };
@@ -1,27 +1,20 @@
1
1
  'use strict';
2
-
3
2
  const { QueryBuilderOperation } = require('./QueryBuilderOperation');
4
-
5
3
  class RelateOperation extends QueryBuilderOperation {
6
- constructor(name, opt) {
7
- super(name, opt);
8
-
9
- this.relation = opt.relation;
10
- this.owner = opt.owner;
11
- this.input = null;
12
- this.ids = null;
13
- }
14
-
15
- clone() {
16
- const clone = super.clone();
17
-
18
- clone.input = this.input;
19
- clone.ids = this.ids;
20
-
21
- return clone;
22
- }
4
+ constructor(name, opt) {
5
+ super(name, opt);
6
+ this.relation = opt.relation;
7
+ this.owner = opt.owner;
8
+ this.input = null;
9
+ this.ids = null;
10
+ }
11
+ clone() {
12
+ const clone = super.clone();
13
+ clone.input = this.input;
14
+ clone.ids = this.ids;
15
+ return clone;
16
+ }
23
17
  }
24
-
25
18
  module.exports = {
26
- RelateOperation,
19
+ RelateOperation,
27
20
  };
@@ -1,30 +1,24 @@
1
1
  'use strict';
2
-
3
2
  const { flatten } = require('../../utils/objectUtils');
4
3
  const { ObjectionToKnexConvertingOperation } = require('./ObjectionToKnexConvertingOperation');
5
-
6
4
  // This class's only purpose is to normalize the arguments into an array.
7
5
  //
8
6
  // In knex, if a single column is given to `returning` it returns an array with the that column's value
9
7
  // in it. If an array is given with a one item inside, the return value is an object.
10
8
  class ReturningOperation extends ObjectionToKnexConvertingOperation {
11
- onAdd(builder, args) {
12
- args = flatten(args);
13
-
14
- // Don't add an empty returning list.
15
- if (args.length === 0) {
16
- return false;
9
+ onAdd(builder, args) {
10
+ args = flatten(args);
11
+ // Don't add an empty returning list.
12
+ if (args.length === 0) {
13
+ return false;
14
+ }
15
+ return super.onAdd(builder, args);
16
+ }
17
+ onBuildKnex(knexBuilder, builder) {
18
+ // Always pass an array of columns to knex.returning.
19
+ return knexBuilder.returning(this.getKnexArgs(builder));
17
20
  }
18
-
19
- return super.onAdd(builder, args);
20
- }
21
-
22
- onBuildKnex(knexBuilder, builder) {
23
- // Always pass an array of columns to knex.returning.
24
- return knexBuilder.returning(this.getKnexArgs(builder));
25
- }
26
21
  }
27
-
28
22
  module.exports = {
29
- ReturningOperation,
23
+ ReturningOperation,
30
24
  };