@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,558 +1,484 @@
1
1
  'use strict';
2
-
3
2
  const { isString, isFunction, isRegExp, mergeMaps, last } = require('../utils/objectUtils');
4
3
  const { QueryBuilderContextBase } = require('./QueryBuilderContextBase');
5
4
  const { QueryBuilderUserContext } = require('./QueryBuilderUserContext');
6
5
  const { deprecate } = require('../utils/deprecate');
7
6
  const { effectiveWhereScope, createWhereGrouping } = require('./whereScopes');
8
-
9
7
  const AllSelector = () => true;
10
- const SelectSelector =
11
- /^(select|columns|column|distinct|count|countDistinct|min|max|sum|sumDistinct|avg|avgDistinct)$/;
8
+ const SelectSelector = /^(select|columns|column|distinct|count|countDistinct|min|max|sum|sumDistinct|avg|avgDistinct)$/;
12
9
  const WhereSelector = /^(where|orWhere|andWhere|find\w+)/;
13
10
  const OnSelector = /^(on|orOn|andOn)/;
14
11
  const OrderBySelector = /orderBy/;
15
12
  const JoinSelector = /(join|joinRaw|joinRelated)$/i;
16
13
  const FromSelector = /^(from|fromRaw|into|table)$/;
17
-
18
14
  class QueryBuilderOperationSupport {
19
- constructor(...args) {
20
- this.constructor.init(this, ...args);
21
- }
22
-
23
- static init(self, modelClass) {
24
- self._modelClass = modelClass;
25
- self._operations = [];
26
- self._context = new this.QueryBuilderContext(self);
27
- self._parentQuery = null;
28
- self._isPartialQuery = false;
29
- self._activeOperations = [];
30
- self._whereScope = null;
31
- }
32
-
33
- static forClass(modelClass) {
34
- return new this(modelClass);
35
- }
36
-
37
- static get AllSelector() {
38
- return AllSelector;
39
- }
40
-
41
- static get QueryBuilderContext() {
42
- return QueryBuilderContextBase;
43
- }
44
-
45
- static get QueryBuilderUserContext() {
46
- return QueryBuilderUserContext;
47
- }
48
-
49
- static get SelectSelector() {
50
- return SelectSelector;
51
- }
52
-
53
- static get WhereSelector() {
54
- return WhereSelector;
55
- }
56
-
57
- static get OnSelector() {
58
- return OnSelector;
59
- }
60
-
61
- static get JoinSelector() {
62
- return JoinSelector;
63
- }
64
-
65
- static get FromSelector() {
66
- return FromSelector;
67
- }
68
-
69
- static get OrderBySelector() {
70
- return OrderBySelector;
71
- }
72
-
73
- modelClass() {
74
- return this._modelClass;
75
- }
76
-
77
- context(obj) {
78
- const ctx = this._context;
79
-
80
- if (arguments.length === 0) {
81
- return ctx.userContext;
82
- } else {
83
- ctx.userContext = ctx.userContext.newMerge(this, obj);
84
- return this;
85
- }
86
- }
87
-
88
- clearContext() {
89
- const ctx = this._context;
90
- ctx.userContext = new this.constructor.QueryBuilderUserContext(this);
91
- return this;
92
- }
93
-
94
- internalContext(ctx) {
95
- if (arguments.length === 0) {
96
- return this._context;
97
- } else {
98
- this._context = ctx;
99
- return this;
100
- }
101
- }
102
-
103
- internalOptions(opt) {
104
- if (arguments.length === 0) {
105
- return this._context.options;
106
- } else {
107
- const oldOpt = this._context.options;
108
- this._context.options = Object.assign(oldOpt, opt);
109
- return this;
110
- }
111
- }
112
-
113
- isPartial(isPartial) {
114
- if (arguments.length === 0) {
115
- return this._isPartialQuery;
116
- } else {
117
- this._isPartialQuery = isPartial;
118
- return this;
119
- }
120
- }
121
-
122
- isInternal() {
123
- return this.internalOptions().isInternalQuery;
124
- }
125
-
126
- tableNameFor(modelClassOrTableName, newTableName) {
127
- const tableName = getTableName(modelClassOrTableName);
128
- const ctx = this.internalContext();
129
- const tableMap = ctx.tableMap;
130
-
131
- if (isString(newTableName)) {
132
- ctx.tableMap = tableMap || new Map();
133
- ctx.tableMap.set(tableName, newTableName);
134
- return this;
135
- } else {
136
- return (tableMap && tableMap.get(tableName)) || tableName;
137
- }
138
- }
139
-
140
- aliasFor(modelClassOrTableName, alias) {
141
- const tableName = getTableName(modelClassOrTableName);
142
- const ctx = this.internalContext();
143
- const aliasMap = ctx.aliasMap;
144
-
145
- if (isString(alias)) {
146
- ctx.aliasMap = aliasMap || new Map();
147
- ctx.aliasMap.set(tableName, alias);
148
- return this;
149
- } else {
150
- return (aliasMap && aliasMap.get(tableName)) || null;
151
- }
152
- }
153
-
154
- tableRefFor(modelClassOrTableName) {
155
- return this.aliasFor(modelClassOrTableName) || this.tableNameFor(modelClassOrTableName);
156
- }
157
-
158
- childQueryOf(query, { fork, isInternalQuery } = {}) {
159
- if (query) {
160
- let currentCtx = this.context();
161
- let ctx = query.internalContext();
162
-
163
- if (fork) {
164
- ctx = ctx.clone();
165
- }
166
-
167
- if (isInternalQuery) {
168
- ctx.options.isInternalQuery = true;
169
- }
170
-
171
- this._parentQuery = query;
172
- this.internalContext(ctx);
173
- this.context(currentCtx);
174
-
175
- // Use the parent's knex if there was no knex in `ctx`.
176
- if (this.unsafeKnex() === null) {
177
- this.knex(query.unsafeKnex());
178
- }
179
- }
180
-
181
- return this;
182
- }
183
-
184
- subqueryOf(query) {
185
- if (query) {
186
- if (this._isPartialQuery) {
187
- // Merge alias and table name maps for "partial" subqueries.
15
+ constructor(...args) {
16
+ this.constructor.init(this, ...args);
17
+ }
18
+ static init(self, modelClass) {
19
+ self._modelClass = modelClass;
20
+ self._operations = [];
21
+ self._context = new this.QueryBuilderContext(self);
22
+ self._parentQuery = null;
23
+ self._isPartialQuery = false;
24
+ self._activeOperations = [];
25
+ self._whereScope = null;
26
+ }
27
+ static forClass(modelClass) {
28
+ return new this(modelClass);
29
+ }
30
+ static get AllSelector() {
31
+ return AllSelector;
32
+ }
33
+ static get QueryBuilderContext() {
34
+ return QueryBuilderContextBase;
35
+ }
36
+ static get QueryBuilderUserContext() {
37
+ return QueryBuilderUserContext;
38
+ }
39
+ static get SelectSelector() {
40
+ return SelectSelector;
41
+ }
42
+ static get WhereSelector() {
43
+ return WhereSelector;
44
+ }
45
+ static get OnSelector() {
46
+ return OnSelector;
47
+ }
48
+ static get JoinSelector() {
49
+ return JoinSelector;
50
+ }
51
+ static get FromSelector() {
52
+ return FromSelector;
53
+ }
54
+ static get OrderBySelector() {
55
+ return OrderBySelector;
56
+ }
57
+ modelClass() {
58
+ return this._modelClass;
59
+ }
60
+ context(obj) {
61
+ const ctx = this._context;
62
+ if (arguments.length === 0) {
63
+ return ctx.userContext;
64
+ }
65
+ else {
66
+ ctx.userContext = ctx.userContext.newMerge(this, obj);
67
+ return this;
68
+ }
69
+ }
70
+ clearContext() {
71
+ const ctx = this._context;
72
+ ctx.userContext = new this.constructor.QueryBuilderUserContext(this);
73
+ return this;
74
+ }
75
+ internalContext(ctx) {
76
+ if (arguments.length === 0) {
77
+ return this._context;
78
+ }
79
+ else {
80
+ this._context = ctx;
81
+ return this;
82
+ }
83
+ }
84
+ internalOptions(opt) {
85
+ if (arguments.length === 0) {
86
+ return this._context.options;
87
+ }
88
+ else {
89
+ const oldOpt = this._context.options;
90
+ this._context.options = Object.assign(oldOpt, opt);
91
+ return this;
92
+ }
93
+ }
94
+ isPartial(isPartial) {
95
+ if (arguments.length === 0) {
96
+ return this._isPartialQuery;
97
+ }
98
+ else {
99
+ this._isPartialQuery = isPartial;
100
+ return this;
101
+ }
102
+ }
103
+ isInternal() {
104
+ return this.internalOptions().isInternalQuery;
105
+ }
106
+ tableNameFor(modelClassOrTableName, newTableName) {
107
+ const tableName = getTableName(modelClassOrTableName);
108
+ const ctx = this.internalContext();
109
+ const tableMap = ctx.tableMap;
110
+ if (isString(newTableName)) {
111
+ ctx.tableMap = tableMap || new Map();
112
+ ctx.tableMap.set(tableName, newTableName);
113
+ return this;
114
+ }
115
+ else {
116
+ return (tableMap && tableMap.get(tableName)) || tableName;
117
+ }
118
+ }
119
+ aliasFor(modelClassOrTableName, alias) {
120
+ const tableName = getTableName(modelClassOrTableName);
188
121
  const ctx = this.internalContext();
189
-
190
- ctx.aliasMap = mergeMaps(query.internalContext().aliasMap, ctx.aliasMap);
191
- ctx.tableMap = mergeMaps(query.internalContext().tableMap, ctx.tableMap);
192
- }
193
-
194
- this._parentQuery = query;
195
-
196
- if (this.unsafeKnex() === null) {
197
- this.knex(query.unsafeKnex());
198
- }
199
- }
200
-
201
- return this;
202
- }
203
-
204
- parentQuery() {
205
- return this._parentQuery;
206
- }
207
-
208
- knex(...args) {
209
- if (args.length === 0) {
210
- const knex = this.unsafeKnex();
211
-
212
- if (!knex) {
213
- throw new Error(
214
- `no database connection available for a query. You need to bind the model class or the query to a knex instance.`,
215
- );
216
- }
217
-
218
- return knex;
219
- } else {
220
- this._context.knex = args[0];
221
- return this;
222
- }
223
- }
224
-
225
- unsafeKnex() {
226
- return this._context.knex || this._modelClass.knex() || null;
227
- }
228
-
229
- clear(operationSelector) {
230
- const operationsToRemove = new Set();
231
-
232
- this.forEachOperation(operationSelector, (op) => {
233
- // If an ancestor operation has already been removed,
234
- // there's no need to remove the children anymore.
235
- if (!op.isAncestorInSet(operationsToRemove)) {
236
- operationsToRemove.add(op);
237
- }
238
- });
239
-
240
- for (const op of operationsToRemove) {
241
- this.removeOperation(op);
242
- }
243
-
244
- return this;
245
- }
246
-
247
- toFindQuery() {
248
- const findQuery = this.clone();
249
- const operationsToReplace = [];
250
- const operationsToRemove = [];
251
-
252
- findQuery.forEachOperation(
253
- (op) => op.hasToFindOperation(),
254
- (op) => {
255
- const findOp = op.toFindOperation(findQuery);
256
-
257
- if (!findOp) {
258
- operationsToRemove.push(op);
259
- } else {
260
- operationsToReplace.push({ op, findOp });
122
+ const aliasMap = ctx.aliasMap;
123
+ if (isString(alias)) {
124
+ ctx.aliasMap = aliasMap || new Map();
125
+ ctx.aliasMap.set(tableName, alias);
126
+ return this;
127
+ }
128
+ else {
129
+ return (aliasMap && aliasMap.get(tableName)) || null;
130
+ }
131
+ }
132
+ tableRefFor(modelClassOrTableName) {
133
+ return this.aliasFor(modelClassOrTableName) || this.tableNameFor(modelClassOrTableName);
134
+ }
135
+ childQueryOf(query, { fork, isInternalQuery } = {}) {
136
+ if (query) {
137
+ const currentCtx = this.context();
138
+ let ctx = query.internalContext();
139
+ if (fork) {
140
+ ctx = ctx.clone();
141
+ }
142
+ if (isInternalQuery) {
143
+ ctx.options.isInternalQuery = true;
144
+ }
145
+ this._parentQuery = query;
146
+ this.internalContext(ctx);
147
+ this.context(currentCtx);
148
+ // Use the parent's knex if there was no knex in `ctx`.
149
+ if (this.unsafeKnex() === null) {
150
+ this.knex(query.unsafeKnex());
151
+ }
152
+ }
153
+ return this;
154
+ }
155
+ subqueryOf(query) {
156
+ if (query) {
157
+ if (this._isPartialQuery) {
158
+ // Merge alias and table name maps for "partial" subqueries.
159
+ const ctx = this.internalContext();
160
+ ctx.aliasMap = mergeMaps(query.internalContext().aliasMap, ctx.aliasMap);
161
+ ctx.tableMap = mergeMaps(query.internalContext().tableMap, ctx.tableMap);
162
+ }
163
+ this._parentQuery = query;
164
+ if (this.unsafeKnex() === null) {
165
+ this.knex(query.unsafeKnex());
166
+ }
167
+ }
168
+ return this;
169
+ }
170
+ parentQuery() {
171
+ return this._parentQuery;
172
+ }
173
+ knex(...args) {
174
+ if (args.length === 0) {
175
+ const knex = this.unsafeKnex();
176
+ if (!knex) {
177
+ throw new Error(`no database connection available for a query. You need to bind the model class or the query to a knex instance.`);
178
+ }
179
+ return knex;
180
+ }
181
+ else {
182
+ this._context.knex = args[0];
183
+ return this;
184
+ }
185
+ }
186
+ unsafeKnex() {
187
+ return this._context.knex || this._modelClass.knex() || null;
188
+ }
189
+ clear(operationSelector) {
190
+ const operationsToRemove = new Set();
191
+ this.forEachOperation(operationSelector, (op) => {
192
+ // If an ancestor operation has already been removed,
193
+ // there's no need to remove the children anymore.
194
+ if (!op.isAncestorInSet(operationsToRemove)) {
195
+ operationsToRemove.add(op);
196
+ }
197
+ });
198
+ for (const op of operationsToRemove) {
199
+ this.removeOperation(op);
200
+ }
201
+ return this;
202
+ }
203
+ toFindQuery() {
204
+ const findQuery = this.clone();
205
+ const operationsToReplace = [];
206
+ const operationsToRemove = [];
207
+ findQuery.forEachOperation((op) => op.hasToFindOperation(), (op) => {
208
+ const findOp = op.toFindOperation(findQuery);
209
+ if (!findOp) {
210
+ operationsToRemove.push(op);
211
+ }
212
+ else {
213
+ operationsToReplace.push({ op, findOp });
214
+ }
215
+ });
216
+ for (const op of operationsToRemove) {
217
+ findQuery.removeOperation(op);
261
218
  }
262
- },
263
- );
264
-
265
- for (const op of operationsToRemove) {
266
- findQuery.removeOperation(op);
267
- }
268
-
269
- for (const { op, findOp } of operationsToReplace) {
270
- findQuery.replaceOperation(op, findOp);
271
- }
272
-
273
- return findQuery;
274
- }
275
-
276
- clearSelect() {
277
- return this.clear(SelectSelector);
278
- }
279
-
280
- clearWhere() {
281
- return this.clear(WhereSelector);
282
- }
283
-
284
- clearOrder() {
285
- return this.clear(OrderBySelector);
286
- }
287
-
288
- copyFrom(queryBuilder, operationSelector) {
289
- const operationsToAdd = new Set();
290
-
291
- queryBuilder.forEachOperation(operationSelector, (op) => {
292
- // If an ancestor operation has already been added,
293
- // there is no need to add
294
- if (!op.isAncestorInSet(operationsToAdd)) {
295
- operationsToAdd.add(op);
296
- }
297
- });
298
-
299
- for (const op of operationsToAdd) {
300
- const opClone = op.clone();
301
-
302
- // The scope may come from a parent operation that is not copied.
303
- opClone.whereScope = effectiveWhereScope(op);
304
-
305
- // We may be moving nested operations to the root. Clear
306
- // any links to the parent operations.
307
- opClone.parentOperation = null;
308
- opClone.adderHookName = null;
309
-
310
- // We don't use `addOperation` here because we don't what to
311
- // call `onAdd` or add these operations as child operations.
312
- this._operations.push(opClone);
313
- }
314
-
315
- return this;
316
- }
317
-
318
- has(operationSelector) {
319
- return !!this.findOperation(operationSelector);
320
- }
321
-
322
- forEachOperation(operationSelector, callback, match = true) {
323
- const selector = buildFunctionForOperationSelector(operationSelector);
324
-
325
- for (const op of this._operations) {
326
- if (selector(op) === match && callback(op) === false) {
327
- break;
328
- }
329
-
330
- const childRes = op.forEachDescendantOperation((op) => {
331
- if (selector(op) === match && callback(op) === false) {
332
- return false;
219
+ for (const { op, findOp } of operationsToReplace) {
220
+ findQuery.replaceOperation(op, findOp);
333
221
  }
334
- });
335
-
336
- if (childRes === false) {
337
- break;
338
- }
339
- }
340
-
341
- return this;
342
- }
343
-
344
- findOperation(operationSelector) {
345
- let op = null;
346
-
347
- this.forEachOperation(operationSelector, (it) => {
348
- op = it;
349
- return false;
350
- });
351
-
352
- return op;
353
- }
354
-
355
- findLastOperation(operationSelector) {
356
- let op = null;
357
-
358
- this.forEachOperation(operationSelector, (it) => {
359
- op = it;
360
- });
361
-
362
- return op;
363
- }
364
-
365
- everyOperation(operationSelector) {
366
- let every = true;
367
-
368
- this.forEachOperation(
369
- operationSelector,
370
- () => {
371
- every = false;
372
- return false;
373
- },
374
- false,
375
- );
376
-
377
- return every;
378
- }
379
-
380
- callOperationMethod(operation, hookName, args) {
381
- try {
382
- operation.removeChildOperationsByHookName(hookName);
383
-
384
- this._activeOperations.push({
385
- operation,
386
- hookName,
387
- });
388
-
389
- return operation[hookName](...args);
390
- } finally {
391
- this._activeOperations.pop();
392
- }
393
- }
394
-
395
- async callAsyncOperationMethod(operation, hookName, args) {
396
- operation.removeChildOperationsByHookName(hookName);
397
-
398
- this._activeOperations.push({
399
- operation,
400
- hookName,
401
- });
402
-
403
- try {
404
- return await operation[hookName](...args);
405
- } finally {
406
- this._activeOperations.pop();
407
- }
408
- }
409
-
410
- addOperation(operation, args) {
411
- const ret = this.addOperationUsingMethod('push', operation, args);
412
- return ret;
413
- }
414
-
415
- addOperationToFront(operation, args) {
416
- return this.addOperationUsingMethod('unshift', operation, args);
417
- }
418
-
419
- addOperationUsingMethod(arrayMethod, operation, args) {
420
- const shouldAdd = this.callOperationMethod(operation, 'onAdd', [this, args]);
421
-
422
- if (shouldAdd) {
423
- if (this._whereScope !== null && operation.whereScope === null) {
424
- operation.whereScope = this._whereScope;
425
- }
426
-
427
- if (this._activeOperations.length) {
428
- const { operation: parentOperation, hookName } = last(this._activeOperations);
429
- parentOperation.addChildOperation(hookName, operation);
430
- } else {
431
- this._operations[arrayMethod](operation);
432
- }
433
- }
434
-
435
- return this;
436
- }
437
-
438
- removeOperation(operation) {
439
- if (operation.parentOperation) {
440
- operation.parentOperation.removeChildOperation(operation);
441
- } else {
442
- const index = this._operations.indexOf(operation);
443
-
444
- if (index !== -1) {
445
- this._operations.splice(index, 1);
446
- }
447
- }
448
-
449
- return this;
450
- }
451
-
452
- replaceOperation(operation, newOperation) {
453
- if (operation.parentOperation) {
454
- operation.parentOperation.replaceChildOperation(operation, newOperation);
455
- } else {
456
- const index = this._operations.indexOf(operation);
457
-
458
- if (index !== -1) {
459
- this._operations[index] = newOperation;
460
- }
461
- }
462
-
463
- return this;
464
- }
465
-
466
- clone() {
467
- return this.baseCloneInto(new this.constructor(this.unsafeKnex()));
468
- }
469
-
470
- baseCloneInto(builder) {
471
- builder._modelClass = this._modelClass;
472
- builder._operations = this._operations.map((it) => it.clone());
473
- builder._context = this._context.clone();
474
- builder._parentQuery = this._parentQuery;
475
- builder._isPartialQuery = this._isPartialQuery;
476
-
477
- // Don't copy the active operation stack. We never continue (nor can we)
478
- // a query from the exact mid-hook-call state.
479
- builder._activeOperations = [];
480
-
481
- return builder;
482
- }
483
-
484
- toKnexQuery(knexBuilder = this.knex().queryBuilder()) {
485
- this.executeOnBuild();
486
- return this.executeOnBuildKnex(knexBuilder);
487
- }
488
-
489
- executeOnBuild() {
490
- this.forEachOperation(true, (op) => {
491
- if (op.hasOnBuild()) {
492
- this.callOperationMethod(op, 'onBuild', [this]);
493
- }
494
- });
495
- }
496
-
497
- executeOnBuildKnex(knexBuilder) {
498
- const whereGrouping = createWhereGrouping(this);
499
-
500
- this.forEachOperation(true, (op) => {
501
- if (whereGrouping !== null && whereGrouping.isGrouped(op)) {
502
- knexBuilder = whereGrouping.build(op, knexBuilder);
503
- } else if (op.hasOnBuildKnex()) {
504
- const newKnexBuilder = this.callOperationMethod(op, 'onBuildKnex', [knexBuilder, this]);
505
- // Default to the input knex builder for backwards compatibility
506
- // with QueryBuilder.onBuildKnex hooks.
507
- knexBuilder = newKnexBuilder || knexBuilder;
508
- }
509
- });
510
-
511
- return knexBuilder;
512
- }
513
-
514
- toString() {
515
- return this.toKnexQuery().toString();
516
- }
517
-
518
- toSql() {
519
- return this.toString();
520
- }
521
-
522
- skipUndefined() {
523
- deprecate('skipUndefined() is deprecated and will be removed in objection 4.0');
524
- this.internalOptions().skipUndefined = true;
525
- return this;
526
- }
222
+ return findQuery;
223
+ }
224
+ clearSelect() {
225
+ return this.clear(SelectSelector);
226
+ }
227
+ clearWhere() {
228
+ return this.clear(WhereSelector);
229
+ }
230
+ clearOrder() {
231
+ return this.clear(OrderBySelector);
232
+ }
233
+ copyFrom(queryBuilder, operationSelector) {
234
+ const operationsToAdd = new Set();
235
+ queryBuilder.forEachOperation(operationSelector, (op) => {
236
+ // If an ancestor operation has already been added,
237
+ // there is no need to add
238
+ if (!op.isAncestorInSet(operationsToAdd)) {
239
+ operationsToAdd.add(op);
240
+ }
241
+ });
242
+ for (const op of operationsToAdd) {
243
+ const opClone = op.clone();
244
+ // The scope may come from a parent operation that is not copied.
245
+ opClone.whereScope = effectiveWhereScope(op);
246
+ // We may be moving nested operations to the root. Clear
247
+ // any links to the parent operations.
248
+ opClone.parentOperation = null;
249
+ opClone.adderHookName = null;
250
+ // We don't use `addOperation` here because we don't what to
251
+ // call `onAdd` or add these operations as child operations.
252
+ this._operations.push(opClone);
253
+ }
254
+ return this;
255
+ }
256
+ has(operationSelector) {
257
+ return !!this.findOperation(operationSelector);
258
+ }
259
+ forEachOperation(operationSelector, callback, match = true) {
260
+ visitOperations(this._operations, selectorKind(operationSelector), operationSelector, callback, match);
261
+ return this;
262
+ }
263
+ findOperation(operationSelector) {
264
+ let op = null;
265
+ this.forEachOperation(operationSelector, (it) => {
266
+ op = it;
267
+ return false;
268
+ });
269
+ return op;
270
+ }
271
+ findLastOperation(operationSelector) {
272
+ let op = null;
273
+ this.forEachOperation(operationSelector, (it) => {
274
+ op = it;
275
+ });
276
+ return op;
277
+ }
278
+ everyOperation(operationSelector) {
279
+ let every = true;
280
+ this.forEachOperation(operationSelector, () => {
281
+ every = false;
282
+ return false;
283
+ }, false);
284
+ return every;
285
+ }
286
+ callOperationMethod(operation, hookName, args) {
287
+ try {
288
+ operation.removeChildOperationsByHookName(hookName);
289
+ this._activeOperations.push({
290
+ operation,
291
+ hookName,
292
+ });
293
+ return operation[hookName](...args);
294
+ }
295
+ finally {
296
+ this._activeOperations.pop();
297
+ }
298
+ }
299
+ async callAsyncOperationMethod(operation, hookName, args) {
300
+ operation.removeChildOperationsByHookName(hookName);
301
+ this._activeOperations.push({
302
+ operation,
303
+ hookName,
304
+ });
305
+ try {
306
+ return await operation[hookName](...args);
307
+ }
308
+ finally {
309
+ this._activeOperations.pop();
310
+ }
311
+ }
312
+ addOperation(operation, args) {
313
+ const ret = this.addOperationUsingMethod('push', operation, args);
314
+ return ret;
315
+ }
316
+ addOperationToFront(operation, args) {
317
+ return this.addOperationUsingMethod('unshift', operation, args);
318
+ }
319
+ addOperationUsingMethod(arrayMethod, operation, args) {
320
+ const shouldAdd = this.callOperationMethod(operation, 'onAdd', [this, args]);
321
+ if (shouldAdd) {
322
+ if (this._whereScope !== null && operation.whereScope === null) {
323
+ operation.whereScope = this._whereScope;
324
+ }
325
+ if (this._activeOperations.length) {
326
+ const { operation: parentOperation, hookName } = last(this._activeOperations);
327
+ parentOperation.addChildOperation(hookName, operation);
328
+ }
329
+ else {
330
+ this._operations[arrayMethod](operation);
331
+ }
332
+ }
333
+ return this;
334
+ }
335
+ removeOperation(operation) {
336
+ if (operation.parentOperation) {
337
+ operation.parentOperation.removeChildOperation(operation);
338
+ }
339
+ else {
340
+ const index = this._operations.indexOf(operation);
341
+ if (index !== -1) {
342
+ this._operations.splice(index, 1);
343
+ }
344
+ }
345
+ return this;
346
+ }
347
+ replaceOperation(operation, newOperation) {
348
+ if (operation.parentOperation) {
349
+ operation.parentOperation.replaceChildOperation(operation, newOperation);
350
+ }
351
+ else {
352
+ const index = this._operations.indexOf(operation);
353
+ if (index !== -1) {
354
+ this._operations[index] = newOperation;
355
+ }
356
+ }
357
+ return this;
358
+ }
359
+ clone() {
360
+ return this.baseCloneInto(new this.constructor(this.unsafeKnex()));
361
+ }
362
+ baseCloneInto(builder) {
363
+ builder._modelClass = this._modelClass;
364
+ builder._operations = this._operations.map((it) => it.clone());
365
+ builder._context = this._context.clone();
366
+ builder._parentQuery = this._parentQuery;
367
+ builder._isPartialQuery = this._isPartialQuery;
368
+ // Don't copy the active operation stack. We never continue (nor can we)
369
+ // a query from the exact mid-hook-call state.
370
+ builder._activeOperations = [];
371
+ return builder;
372
+ }
373
+ toKnexQuery(knexBuilder = this.knex().queryBuilder()) {
374
+ this.executeOnBuild();
375
+ return this.executeOnBuildKnex(knexBuilder);
376
+ }
377
+ executeOnBuild() {
378
+ this.forEachOperation(true, (op) => {
379
+ if (op.hasOnBuild()) {
380
+ this.callOperationMethod(op, 'onBuild', [this]);
381
+ }
382
+ });
383
+ }
384
+ executeOnBuildKnex(knexBuilder) {
385
+ const whereGrouping = createWhereGrouping(this);
386
+ this.forEachOperation(true, (op) => {
387
+ if (whereGrouping !== null && whereGrouping.isGrouped(op)) {
388
+ knexBuilder = whereGrouping.build(op, knexBuilder);
389
+ }
390
+ else if (op.hasOnBuildKnex()) {
391
+ const newKnexBuilder = this.callOperationMethod(op, 'onBuildKnex', [knexBuilder, this]);
392
+ // Default to the input knex builder for backwards compatibility
393
+ // with QueryBuilder.onBuildKnex hooks.
394
+ knexBuilder = newKnexBuilder || knexBuilder;
395
+ }
396
+ });
397
+ return knexBuilder;
398
+ }
399
+ toString() {
400
+ return this.toKnexQuery().toString();
401
+ }
402
+ toSql() {
403
+ return this.toString();
404
+ }
405
+ skipUndefined() {
406
+ deprecate('skipUndefined() is deprecated and will be removed in objection 4.0');
407
+ this.internalOptions().skipUndefined = true;
408
+ return this;
409
+ }
527
410
  }
528
-
529
- function buildFunctionForOperationSelector(operationSelector) {
530
- if (operationSelector === true) {
531
- return AllSelector;
532
- } else if (isRegExp(operationSelector)) {
533
- return (op) => operationSelector.test(op.name);
534
- } else if (isString(operationSelector)) {
535
- return (op) => op.name === operationSelector;
536
- } else if (
537
- isFunction(operationSelector) &&
538
- operationSelector.isObjectionQueryBuilderOperationClass
539
- ) {
540
- return (op) => op.is(operationSelector);
541
- } else if (isFunction(operationSelector)) {
542
- return operationSelector;
543
- } else {
544
- return () => false;
545
- }
411
+ // Operation selectors are `true` (every operation), a regex or a string that
412
+ // is matched against the operation name, an operation class, or a predicate.
413
+ // Queries walk their operations many times, so the selector is classified
414
+ // once per walk instead of being wrapped in a new function.
415
+ const SELECT_ALL = 0;
416
+ const SELECT_REGEX = 1;
417
+ const SELECT_NAME = 2;
418
+ const SELECT_CLASS = 3;
419
+ const SELECT_PREDICATE = 4;
420
+ const SELECT_NONE = 5;
421
+ function selectorKind(operationSelector) {
422
+ if (operationSelector === true) {
423
+ return SELECT_ALL;
424
+ }
425
+ else if (isRegExp(operationSelector)) {
426
+ return SELECT_REGEX;
427
+ }
428
+ else if (isString(operationSelector)) {
429
+ return SELECT_NAME;
430
+ }
431
+ else if (isFunction(operationSelector) &&
432
+ operationSelector.isObjectionQueryBuilderOperationClass) {
433
+ return SELECT_CLASS;
434
+ }
435
+ else if (isFunction(operationSelector)) {
436
+ return SELECT_PREDICATE;
437
+ }
438
+ else {
439
+ return SELECT_NONE;
440
+ }
441
+ }
442
+ function matchesSelector(op, kind, operationSelector) {
443
+ switch (kind) {
444
+ case SELECT_ALL:
445
+ return true;
446
+ case SELECT_REGEX:
447
+ return operationSelector.test(op.name);
448
+ case SELECT_NAME:
449
+ return op.name === operationSelector;
450
+ case SELECT_CLASS:
451
+ return op.is(operationSelector);
452
+ case SELECT_PREDICATE:
453
+ return operationSelector(op);
454
+ default:
455
+ return false;
456
+ }
457
+ }
458
+ // Visits the operations and their descendants depth first, each operation
459
+ // before its children. Returns false if the callback stopped the walk by
460
+ // returning false. The arrays are read as the walk goes, so operations that
461
+ // the callback adds are visited too, as before.
462
+ function visitOperations(operations, kind, operationSelector, callback, match) {
463
+ for (const op of operations) {
464
+ if (matchesSelector(op, kind, operationSelector) === match && callback(op) === false) {
465
+ return false;
466
+ }
467
+ if (op.childOperations.length !== 0 &&
468
+ visitOperations(op.childOperations, kind, operationSelector, callback, match) === false) {
469
+ return false;
470
+ }
471
+ }
472
+ return true;
546
473
  }
547
-
548
474
  function getTableName(modelClassOrTableName) {
549
- if (isString(modelClassOrTableName)) {
550
- return modelClassOrTableName;
551
- } else {
552
- return modelClassOrTableName.getTableName();
553
- }
475
+ if (isString(modelClassOrTableName)) {
476
+ return modelClassOrTableName;
477
+ }
478
+ else {
479
+ return modelClassOrTableName.getTableName();
480
+ }
554
481
  }
555
-
556
482
  module.exports = {
557
- QueryBuilderOperationSupport,
483
+ QueryBuilderOperationSupport,
558
484
  };