@abedmurrar/objection 0.0.0-stage → 4.0.0-alpha.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 (190) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +69 -2
  3. package/lib/.eslintrc.json +8 -0
  4. package/lib/initialize.js +14 -0
  5. package/lib/model/AjvValidator.js +325 -0
  6. package/lib/model/Model.js +942 -0
  7. package/lib/model/ModifierNotFoundError.js +12 -0
  8. package/lib/model/NotFoundError.js +23 -0
  9. package/lib/model/RelationDoesNotExistError.js +14 -0
  10. package/lib/model/ValidationError.js +54 -0
  11. package/lib/model/Validator.js +26 -0
  12. package/lib/model/getModel.js +9 -0
  13. package/lib/model/graph/ModelGraph.js +80 -0
  14. package/lib/model/graph/ModelGraphBuilder.js +241 -0
  15. package/lib/model/graph/ModelGraphEdge.js +52 -0
  16. package/lib/model/graph/ModelGraphNode.js +240 -0
  17. package/lib/model/inheritModel.js +28 -0
  18. package/lib/model/modelBindKnex.js +83 -0
  19. package/lib/model/modelClone.js +121 -0
  20. package/lib/model/modelColPropMap.js +34 -0
  21. package/lib/model/modelId.js +62 -0
  22. package/lib/model/modelJsonAttributes.js +92 -0
  23. package/lib/model/modelParseRelations.js +82 -0
  24. package/lib/model/modelQueryProps.js +147 -0
  25. package/lib/model/modelSet.js +136 -0
  26. package/lib/model/modelTableMetadata.js +83 -0
  27. package/lib/model/modelToJson.js +187 -0
  28. package/lib/model/modelUtils.js +47 -0
  29. package/lib/model/modelValidate.js +48 -0
  30. package/lib/model/modelValues.js +126 -0
  31. package/lib/model/modelVisitor.js +41 -0
  32. package/lib/objection.js +114 -0
  33. package/lib/queryBuilder/FunctionBuilder.js +339 -0
  34. package/lib/queryBuilder/InternalOptions.js +29 -0
  35. package/lib/queryBuilder/JoinBuilder.js +157 -0
  36. package/lib/queryBuilder/QueryBuilder.js +1398 -0
  37. package/lib/queryBuilder/QueryBuilderBase.js +884 -0
  38. package/lib/queryBuilder/QueryBuilderContext.js +27 -0
  39. package/lib/queryBuilder/QueryBuilderContextBase.js +35 -0
  40. package/lib/queryBuilder/QueryBuilderOperationSupport.js +558 -0
  41. package/lib/queryBuilder/QueryBuilderUserContext.js +31 -0
  42. package/lib/queryBuilder/RawBuilder.js +93 -0
  43. package/lib/queryBuilder/ReferenceBuilder.js +284 -0
  44. package/lib/queryBuilder/RelationExpression.js +431 -0
  45. package/lib/queryBuilder/StaticHookArguments.js +134 -0
  46. package/lib/queryBuilder/ValueBuilder.js +127 -0
  47. package/lib/queryBuilder/expressions.js +151 -0
  48. package/lib/queryBuilder/functions/mysql.js +817 -0
  49. package/lib/queryBuilder/graph/GraphAction.js +69 -0
  50. package/lib/queryBuilder/graph/GraphData.js +14 -0
  51. package/lib/queryBuilder/graph/GraphFetcher.js +191 -0
  52. package/lib/queryBuilder/graph/GraphNodeDbExistence.js +184 -0
  53. package/lib/queryBuilder/graph/GraphOperation.js +27 -0
  54. package/lib/queryBuilder/graph/GraphOptions.js +207 -0
  55. package/lib/queryBuilder/graph/GraphUpsert.js +305 -0
  56. package/lib/queryBuilder/graph/delete/GraphDelete.js +20 -0
  57. package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +74 -0
  58. package/lib/queryBuilder/graph/insert/GraphInsert.js +145 -0
  59. package/lib/queryBuilder/graph/insert/GraphInsertAction.js +225 -0
  60. package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +92 -0
  61. package/lib/queryBuilder/graph/patch/GraphPatch.js +20 -0
  62. package/lib/queryBuilder/graph/patch/GraphPatchAction.js +258 -0
  63. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +31 -0
  64. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +126 -0
  65. package/lib/queryBuilder/join/JoinResultColumn.js +23 -0
  66. package/lib/queryBuilder/join/JoinResultParser.js +119 -0
  67. package/lib/queryBuilder/join/RelationJoiner.js +341 -0
  68. package/lib/queryBuilder/join/TableNode.js +163 -0
  69. package/lib/queryBuilder/join/TableTree.js +85 -0
  70. package/lib/queryBuilder/join/utils.js +25 -0
  71. package/lib/queryBuilder/operations/DelegateOperation.js +141 -0
  72. package/lib/queryBuilder/operations/DeleteOperation.js +51 -0
  73. package/lib/queryBuilder/operations/FindByIdOperation.js +37 -0
  74. package/lib/queryBuilder/operations/FindByIdsOperation.js +29 -0
  75. package/lib/queryBuilder/operations/FindOperation.js +164 -0
  76. package/lib/queryBuilder/operations/FirstOperation.js +27 -0
  77. package/lib/queryBuilder/operations/FromOperation.js +110 -0
  78. package/lib/queryBuilder/operations/InsertAndFetchOperation.js +49 -0
  79. package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +45 -0
  80. package/lib/queryBuilder/operations/InsertGraphOperation.js +108 -0
  81. package/lib/queryBuilder/operations/InsertOperation.js +206 -0
  82. package/lib/queryBuilder/operations/InstanceDeleteOperation.js +46 -0
  83. package/lib/queryBuilder/operations/InstanceFindOperation.js +21 -0
  84. package/lib/queryBuilder/operations/InstanceInsertOperation.js +24 -0
  85. package/lib/queryBuilder/operations/InstanceUpdateOperation.js +59 -0
  86. package/lib/queryBuilder/operations/JoinRelatedOperation.js +103 -0
  87. package/lib/queryBuilder/operations/KnexOperation.js +14 -0
  88. package/lib/queryBuilder/operations/MergeOperation.js +56 -0
  89. package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +192 -0
  90. package/lib/queryBuilder/operations/OnBuildKnexOperation.js +29 -0
  91. package/lib/queryBuilder/operations/OnBuildOperation.js +29 -0
  92. package/lib/queryBuilder/operations/OnErrorOperation.js +29 -0
  93. package/lib/queryBuilder/operations/QueryBuilderOperation.js +297 -0
  94. package/lib/queryBuilder/operations/RangeOperation.js +47 -0
  95. package/lib/queryBuilder/operations/RelateOperation.js +27 -0
  96. package/lib/queryBuilder/operations/ReturningOperation.js +30 -0
  97. package/lib/queryBuilder/operations/RunAfterOperation.js +29 -0
  98. package/lib/queryBuilder/operations/RunBeforeOperation.js +29 -0
  99. package/lib/queryBuilder/operations/UnrelateOperation.js +23 -0
  100. package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +80 -0
  101. package/lib/queryBuilder/operations/UpdateOperation.js +155 -0
  102. package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +45 -0
  103. package/lib/queryBuilder/operations/UpsertGraphOperation.js +66 -0
  104. package/lib/queryBuilder/operations/WhereCompositeOperation.js +57 -0
  105. package/lib/queryBuilder/operations/WhereInOperation.js +48 -0
  106. package/lib/queryBuilder/operations/eager/EagerOperation.js +61 -0
  107. package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +51 -0
  108. package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +13 -0
  109. package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +285 -0
  110. package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +36 -0
  111. package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +53 -0
  112. package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +31 -0
  113. package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +31 -0
  114. package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +167 -0
  115. package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +164 -0
  116. package/lib/queryBuilder/operations/select/SelectOperation.js +66 -0
  117. package/lib/queryBuilder/operations/select/Selection.js +123 -0
  118. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +117 -0
  119. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +100 -0
  120. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +104 -0
  121. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +886 -0
  122. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.pegjs +97 -0
  123. package/lib/queryBuilder/parsers/relationExpressionParser.js +1042 -0
  124. package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +141 -0
  125. package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +22 -0
  126. package/lib/queryBuilder/transformations/QueryTransformation.js +11 -0
  127. package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +71 -0
  128. package/lib/queryBuilder/transformations/index.js +15 -0
  129. package/lib/queryBuilder/whereScopes.js +227 -0
  130. package/lib/relations/Relation.js +401 -0
  131. package/lib/relations/RelationDeleteOperation.js +29 -0
  132. package/lib/relations/RelationFindOperation.js +149 -0
  133. package/lib/relations/RelationInsertOperation.js +33 -0
  134. package/lib/relations/RelationOwner.js +236 -0
  135. package/lib/relations/RelationProperty.js +247 -0
  136. package/lib/relations/RelationUpdateOperation.js +30 -0
  137. package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +23 -0
  138. package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +44 -0
  139. package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +50 -0
  140. package/lib/relations/belongsToOne/BelongsToOneRelation.js +53 -0
  141. package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +61 -0
  142. package/lib/relations/hasMany/HasManyInsertOperation.js +61 -0
  143. package/lib/relations/hasMany/HasManyRelateOperation.js +64 -0
  144. package/lib/relations/hasMany/HasManyRelation.js +49 -0
  145. package/lib/relations/hasMany/HasManyUnrelateOperation.js +33 -0
  146. package/lib/relations/hasOne/HasOneRelation.js +13 -0
  147. package/lib/relations/hasOneThrough/HasOneThroughRelation.js +13 -0
  148. package/lib/relations/manyToMany/ManyToManyModifyMixin.js +105 -0
  149. package/lib/relations/manyToMany/ManyToManyRelation.js +507 -0
  150. package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +36 -0
  151. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +10 -0
  152. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +26 -0
  153. package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +12 -0
  154. package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +105 -0
  155. package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +52 -0
  156. package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +81 -0
  157. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +14 -0
  158. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +34 -0
  159. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +16 -0
  160. package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +10 -0
  161. package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +82 -0
  162. package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +12 -0
  163. package/lib/transaction.js +96 -0
  164. package/lib/utils/assert.js +25 -0
  165. package/lib/utils/buildUtils.js +21 -0
  166. package/lib/utils/classUtils.js +13 -0
  167. package/lib/utils/clone.js +2329 -0
  168. package/lib/utils/createModifier.js +42 -0
  169. package/lib/utils/deprecate.js +15 -0
  170. package/lib/utils/identifierMapping.js +227 -0
  171. package/lib/utils/internalPropUtils.js +11 -0
  172. package/lib/utils/knexUtils.js +76 -0
  173. package/lib/utils/mixin.js +25 -0
  174. package/lib/utils/mysqlJsonPath.js +55 -0
  175. package/lib/utils/normalizeIds.js +144 -0
  176. package/lib/utils/objectUtils.js +416 -0
  177. package/lib/utils/parseFieldExpression.js +51 -0
  178. package/lib/utils/postgresJsonPath.js +110 -0
  179. package/lib/utils/promiseUtils/after.js +17 -0
  180. package/lib/utils/promiseUtils/afterReturn.js +17 -0
  181. package/lib/utils/promiseUtils/index.js +17 -0
  182. package/lib/utils/promiseUtils/isPromise.js +11 -0
  183. package/lib/utils/promiseUtils/map.js +110 -0
  184. package/lib/utils/promiseUtils/mapAfterAllReturn.js +29 -0
  185. package/lib/utils/promiseUtils/try.js +22 -0
  186. package/lib/utils/resolveModel.js +107 -0
  187. package/lib/utils/tmpColumnUtils.js +16 -0
  188. package/package.json +86 -4
  189. package/typings/objection/index.d.ts +4104 -0
  190. package/typings/objection/relation-expression.d.ts +237 -0
@@ -0,0 +1,297 @@
1
+ 'use strict';
2
+
3
+ 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',
16
+ };
17
+
18
+ // An abstract base class for all query builder operations. QueryBuilderOperations almost always
19
+ // correspond to a single query builder method call. For example SelectOperation could be added when
20
+ // a `select` method is called.
21
+ //
22
+ // QueryBuilderOperation is just a bunch of query execution lifecycle hooks that subclasses
23
+ // can (but don't have to) implement.
24
+ //
25
+ // Basically a query builder is nothing but an array of QueryBuilderOperations. When the query is
26
+ // executed the hooks are called in the order explained below. The hooks are called so that a
27
+ // certain hook is called for _all_ operations before the next hook is called. For example if
28
+ // a builder has 5 operations, onBefore1 hook is called for each of them (and their results are awaited)
29
+ // before onBefore2 hook is called for any of the operations.
30
+ 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)) {
208
+ return true;
209
+ }
210
+
211
+ ancestor = ancestor.parentOperation;
212
+ }
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);
251
+ }
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;
263
+ }
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) {
279
+ return false;
280
+ }
281
+ }
282
+
283
+ return true;
284
+ }
285
+ }
286
+
287
+ Object.defineProperties(QueryBuilderOperation, {
288
+ isObjectionQueryBuilderOperationClass: {
289
+ enumerable: false,
290
+ writable: false,
291
+ value: true,
292
+ },
293
+ });
294
+
295
+ module.exports = {
296
+ QueryBuilderOperation,
297
+ };
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ const { QueryBuilderOperation } = require('./QueryBuilderOperation');
4
+
5
+ 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);
19
+ }
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
+ }
44
+
45
+ module.exports = {
46
+ RangeOperation,
47
+ };
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+
3
+ const { QueryBuilderOperation } = require('./QueryBuilderOperation');
4
+
5
+ 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
+ }
23
+ }
24
+
25
+ module.exports = {
26
+ RelateOperation,
27
+ };
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ const { flatten } = require('../../utils/objectUtils');
4
+ const { ObjectionToKnexConvertingOperation } = require('./ObjectionToKnexConvertingOperation');
5
+
6
+ // This class's only purpose is to normalize the arguments into an array.
7
+ //
8
+ // In knex, if a single column is given to `returning` it returns an array with the that column's value
9
+ // in it. If an array is given with a one item inside, the return value is an object.
10
+ 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;
17
+ }
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
+ }
27
+
28
+ module.exports = {
29
+ ReturningOperation,
30
+ };
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ const { QueryBuilderOperation } = require('./QueryBuilderOperation');
4
+
5
+ class RunAfterOperation extends QueryBuilderOperation {
6
+ constructor(name, opt) {
7
+ super(name, opt);
8
+ this.func = null;
9
+ }
10
+
11
+ onAdd(_, args) {
12
+ this.func = args[0];
13
+ return true;
14
+ }
15
+
16
+ onAfter3(builder, result) {
17
+ return this.func.call(builder, result, builder);
18
+ }
19
+
20
+ clone() {
21
+ const clone = super.clone();
22
+ clone.func = this.func;
23
+ return clone;
24
+ }
25
+ }
26
+
27
+ module.exports = {
28
+ RunAfterOperation,
29
+ };
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ const { QueryBuilderOperation } = require('./QueryBuilderOperation');
4
+
5
+ class RunBeforeOperation extends QueryBuilderOperation {
6
+ constructor(name, opt) {
7
+ super(name, opt);
8
+ this.func = null;
9
+ }
10
+
11
+ onAdd(_, args) {
12
+ this.func = args[0];
13
+ return true;
14
+ }
15
+
16
+ onBefore1(builder, result) {
17
+ return this.func.call(builder, result, builder);
18
+ }
19
+
20
+ clone() {
21
+ const clone = super.clone();
22
+ clone.func = this.func;
23
+ return clone;
24
+ }
25
+ }
26
+
27
+ module.exports = {
28
+ RunBeforeOperation,
29
+ };
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ const { QueryBuilderOperation } = require('./QueryBuilderOperation');
4
+
5
+ class UnrelateOperation extends QueryBuilderOperation {
6
+ constructor(name, opt) {
7
+ super(name, opt);
8
+
9
+ this.relation = opt.relation;
10
+ this.owner = opt.owner;
11
+ this.ids = null;
12
+ }
13
+
14
+ clone() {
15
+ const clone = super.clone();
16
+ clone.ids = this.ids;
17
+ return clone;
18
+ }
19
+ }
20
+
21
+ module.exports = {
22
+ UnrelateOperation,
23
+ };
@@ -0,0 +1,80 @@
1
+ 'use strict';
2
+
3
+ const { withWhereScope } = require('../whereScopes');
4
+ const { DelegateOperation } = require('./DelegateOperation');
5
+ const { FindByIdOperation } = require('./FindByIdOperation');
6
+ const { UpdateOperation } = require('./UpdateOperation');
7
+
8
+ class UpdateAndFetchOperation extends DelegateOperation {
9
+ constructor(name, opt) {
10
+ super(name, opt);
11
+
12
+ if (!this.delegate.is(UpdateOperation)) {
13
+ throw new Error('Invalid delegate');
14
+ }
15
+
16
+ this.id = null;
17
+ this.skipIdWhere = false;
18
+ }
19
+
20
+ get model() {
21
+ return this.delegate.model;
22
+ }
23
+
24
+ onAdd(builder, args) {
25
+ this.id = args[0];
26
+ return this.delegate.onAdd(builder, args.slice(1));
27
+ }
28
+
29
+ onBuild(builder) {
30
+ if (!this.skipIdWhere) {
31
+ withWhereScope(builder, () => builder.findById(this.id));
32
+ }
33
+
34
+ super.onBuild(builder);
35
+ }
36
+
37
+ async onAfter2(builder, numUpdated) {
38
+ if (numUpdated == 0) {
39
+ // If nothing was updated, we should fetch nothing.
40
+ await super.onAfter2(builder, numUpdated);
41
+ return undefined;
42
+ }
43
+
44
+ const fetched = await builder
45
+ .emptyInstance()
46
+ .childQueryOf(builder)
47
+ .modify((builder) => {
48
+ if (!this.skipIdWhere) {
49
+ builder.findById(this.id);
50
+ }
51
+ })
52
+ .castTo(builder.resultModelClass());
53
+
54
+ if (fetched) {
55
+ this.model.$set(fetched);
56
+ }
57
+
58
+ await super.onAfter2(builder, numUpdated);
59
+ return fetched ? this.model : undefined;
60
+ }
61
+
62
+ toFindOperation() {
63
+ return new FindByIdOperation('findById', {
64
+ id: this.id,
65
+ });
66
+ }
67
+
68
+ clone() {
69
+ const clone = super.clone();
70
+
71
+ clone.id = this.id;
72
+ clone.skipIdWhere = this.skipIdWhere;
73
+
74
+ return clone;
75
+ }
76
+ }
77
+
78
+ module.exports = {
79
+ UpdateAndFetchOperation,
80
+ };