@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.
- package/README.md +103 -67
- package/lib/index.mjs +6 -0
- package/lib/initialize.js +6 -9
- package/lib/model/AjvValidator.js +235 -282
- package/lib/model/Model.js +653 -823
- package/lib/model/ModifierNotFoundError.js +5 -7
- package/lib/model/NotFoundError.js +14 -18
- package/lib/model/RelationDoesNotExistError.js +6 -9
- package/lib/model/ValidationError.js +33 -41
- package/lib/model/Validator.js +15 -21
- package/lib/model/getModel.js +1 -3
- package/lib/model/graph/ModelGraph.js +44 -64
- package/lib/model/graph/ModelGraphBuilder.js +164 -217
- package/lib/model/graph/ModelGraphEdge.js +31 -43
- package/lib/model/graph/ModelGraphNode.js +192 -231
- package/lib/model/inheritModel.js +9 -18
- package/lib/model/modelBindKnex.js +48 -62
- package/lib/model/modelClone.js +79 -98
- package/lib/model/modelColPropMap.js +16 -26
- package/lib/model/modelConcurrency.js +24 -0
- package/lib/model/modelId.js +44 -49
- package/lib/model/modelJsonAttributes.js +56 -76
- package/lib/model/modelParseRelations.js +68 -67
- package/lib/model/modelQueryProps.js +76 -107
- package/lib/model/modelSet.js +92 -112
- package/lib/model/modelTableMetadata.js +60 -72
- package/lib/model/modelToJson.js +120 -150
- package/lib/model/modelUtils.js +30 -37
- package/lib/model/modelValidate.js +34 -42
- package/lib/model/modelValues.js +78 -91
- package/lib/model/modelVisitor.js +22 -29
- package/lib/objection.js +42 -67
- package/lib/queryBuilder/FunctionBuilder.js +231 -279
- package/lib/queryBuilder/InternalOptions.js +19 -24
- package/lib/queryBuilder/JoinBuilder.js +116 -149
- package/lib/queryBuilder/QueryBuilder.js +1009 -1271
- package/lib/queryBuilder/QueryBuilderBase.js +647 -865
- package/lib/queryBuilder/QueryBuilderContext.js +14 -21
- package/lib/queryBuilder/QueryBuilderContextBase.js +20 -29
- package/lib/queryBuilder/QueryBuilderOperationSupport.js +462 -536
- package/lib/queryBuilder/QueryBuilderUserContext.js +19 -25
- package/lib/queryBuilder/RawBuilder.js +58 -73
- package/lib/queryBuilder/ReferenceBuilder.js +232 -270
- package/lib/queryBuilder/RelationExpression.js +322 -380
- package/lib/queryBuilder/StaticHookArguments.js +94 -106
- package/lib/queryBuilder/ValueBuilder.js +100 -112
- package/lib/queryBuilder/expressions.js +86 -116
- package/lib/queryBuilder/functions/mysql.js +642 -697
- package/lib/queryBuilder/graph/GraphAction.js +44 -59
- package/lib/queryBuilder/graph/GraphData.js +7 -9
- package/lib/queryBuilder/graph/GraphFetcher.js +115 -151
- package/lib/queryBuilder/graph/GraphNodeDbExistence.js +114 -152
- package/lib/queryBuilder/graph/GraphOperation.js +16 -22
- package/lib/queryBuilder/graph/GraphOptions.js +143 -185
- package/lib/queryBuilder/graph/GraphUpsert.js +167 -241
- package/lib/queryBuilder/graph/delete/GraphDelete.js +8 -13
- package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +44 -62
- package/lib/queryBuilder/graph/insert/GraphInsert.js +99 -128
- package/lib/queryBuilder/graph/insert/GraphInsertAction.js +151 -193
- package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +57 -79
- package/lib/queryBuilder/graph/patch/GraphPatch.js +8 -13
- package/lib/queryBuilder/graph/patch/GraphPatchAction.js +181 -234
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +16 -21
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +80 -110
- package/lib/queryBuilder/join/JoinResultColumn.js +14 -18
- package/lib/queryBuilder/join/JoinResultParser.js +88 -101
- package/lib/queryBuilder/join/RelationJoiner.js +205 -288
- package/lib/queryBuilder/join/TableNode.js +117 -144
- package/lib/queryBuilder/join/TableTree.js +60 -76
- package/lib/queryBuilder/join/utils.js +10 -16
- package/lib/queryBuilder/operations/DelegateOperation.js +93 -133
- package/lib/queryBuilder/operations/DeleteOperation.js +33 -37
- package/lib/queryBuilder/operations/FindByIdOperation.js +20 -28
- package/lib/queryBuilder/operations/FindByIdsOperation.js +17 -23
- package/lib/queryBuilder/operations/FindOperation.js +114 -131
- package/lib/queryBuilder/operations/FirstOperation.js +14 -19
- package/lib/queryBuilder/operations/FromOperation.js +69 -88
- package/lib/queryBuilder/operations/InsertAndFetchOperation.js +28 -39
- package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +25 -35
- package/lib/queryBuilder/operations/InsertGraphOperation.js +77 -98
- package/lib/queryBuilder/operations/InsertOperation.js +132 -165
- package/lib/queryBuilder/operations/InstanceDeleteOperation.js +27 -36
- package/lib/queryBuilder/operations/InstanceFindOperation.js +9 -13
- package/lib/queryBuilder/operations/InstanceInsertOperation.js +13 -17
- package/lib/queryBuilder/operations/InstanceUpdateOperation.js +32 -46
- package/lib/queryBuilder/operations/JoinRelatedOperation.js +66 -86
- package/lib/queryBuilder/operations/KnexOperation.js +4 -7
- package/lib/queryBuilder/operations/MergeOperation.js +32 -45
- package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +131 -150
- package/lib/queryBuilder/operations/OnBuildKnexOperation.js +17 -23
- package/lib/queryBuilder/operations/OnBuildOperation.js +17 -23
- package/lib/queryBuilder/operations/OnErrorOperation.js +17 -23
- package/lib/queryBuilder/operations/QueryBuilderOperation.js +231 -270
- package/lib/queryBuilder/operations/RangeOperation.js +30 -40
- package/lib/queryBuilder/operations/RelateOperation.js +14 -21
- package/lib/queryBuilder/operations/ReturningOperation.js +12 -18
- package/lib/queryBuilder/operations/RunAfterOperation.js +17 -23
- package/lib/queryBuilder/operations/RunBeforeOperation.js +17 -23
- package/lib/queryBuilder/operations/UnrelateOperation.js +12 -17
- package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +48 -65
- package/lib/queryBuilder/operations/UpdateOperation.js +104 -129
- package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +25 -35
- package/lib/queryBuilder/operations/UpsertGraphOperation.js +36 -55
- package/lib/queryBuilder/operations/WhereCompositeOperation.js +30 -39
- package/lib/queryBuilder/operations/WhereInOperation.js +34 -36
- package/lib/queryBuilder/operations/eager/EagerOperation.js +37 -52
- package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +34 -44
- package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +4 -7
- package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +193 -251
- package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +18 -25
- package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +25 -41
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +19 -20
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +23 -21
- package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +106 -118
- package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +79 -108
- package/lib/queryBuilder/operations/select/SelectOperation.js +37 -53
- package/lib/queryBuilder/operations/select/Selection.js +94 -105
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +68 -101
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +65 -75
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +75 -84
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +760 -859
- package/lib/queryBuilder/parsers/relationExpressionParser.js +840 -952
- package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +10 -15
- package/lib/queryBuilder/transformations/QueryTransformation.js +4 -6
- package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +35 -49
- package/lib/queryBuilder/transformations/index.js +3 -9
- package/lib/queryBuilder/whereScopes.js +158 -186
- package/lib/relations/Relation.js +293 -375
- package/lib/relations/RelationDeleteOperation.js +24 -28
- package/lib/relations/RelationFindOperation.js +108 -138
- package/lib/relations/RelationInsertOperation.js +24 -32
- package/lib/relations/RelationOwner.js +175 -209
- package/lib/relations/RelationProperty.js +186 -223
- package/lib/relations/RelationUpdateOperation.js +23 -29
- package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +16 -21
- package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +30 -41
- package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +36 -47
- package/lib/relations/belongsToOne/BelongsToOneRelation.js +42 -50
- package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +43 -58
- package/lib/relations/hasMany/HasManyInsertOperation.js +40 -56
- package/lib/relations/hasMany/HasManyRelateOperation.js +45 -61
- package/lib/relations/hasMany/HasManyRelation.js +38 -46
- package/lib/relations/hasMany/HasManyUnrelateOperation.js +25 -32
- package/lib/relations/hasOne/HasOneRelation.js +9 -12
- package/lib/relations/hasOneThrough/HasOneThroughRelation.js +9 -12
- package/lib/relations/manyToMany/ManyToManyModifyMixin.js +73 -95
- package/lib/relations/manyToMany/ManyToManyRelation.js +374 -473
- package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +23 -30
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +8 -10
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +20 -25
- package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +8 -12
- package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +77 -100
- package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +37 -49
- package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +58 -74
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +10 -13
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +26 -33
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +10 -15
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +8 -10
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +61 -78
- package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +8 -12
- package/lib/transaction.js +53 -83
- package/lib/utils/assert.js +15 -21
- package/lib/utils/buildUtils.js +17 -19
- package/lib/utils/classUtils.js +7 -11
- package/lib/utils/cloneDeep.js +365 -0
- package/lib/utils/createModifier.js +36 -41
- package/lib/utils/deprecate.js +8 -12
- package/lib/utils/identifierMapping.js +153 -192
- package/lib/utils/internalPropUtils.js +4 -8
- package/lib/utils/knexUtils.js +61 -56
- package/lib/utils/mixin.js +16 -23
- package/lib/utils/mysqlJsonPath.js +55 -40
- package/lib/utils/normalizeIds.js +110 -127
- package/lib/utils/objectUtils.js +236 -338
- package/lib/utils/parseFieldExpression.js +66 -44
- package/lib/utils/postgresJsonPath.js +48 -62
- package/lib/utils/promiseUtils/after.js +10 -13
- package/lib/utils/promiseUtils/afterReturn.js +10 -13
- package/lib/utils/promiseUtils/index.js +15 -17
- package/lib/utils/promiseUtils/isPromise.js +6 -9
- package/lib/utils/promiseUtils/map.js +75 -89
- package/lib/utils/promiseUtils/mapAfterAllReturn.js +17 -23
- package/lib/utils/promiseUtils/try.js +15 -18
- package/lib/utils/resolveModel.js +91 -94
- package/lib/utils/tmpColumnUtils.js +4 -13
- package/package.json +50 -23
- package/typings/objection/index.d.mts +362 -0
- package/typings/objection/index.d.ts +36 -9
- package/lib/.eslintrc.json +0 -8
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.pegjs +0 -97
- package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +0 -141
- 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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
)
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
this
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
builder
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
this.
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
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
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
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
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
475
|
+
if (isString(modelClassOrTableName)) {
|
|
476
|
+
return modelClassOrTableName;
|
|
477
|
+
}
|
|
478
|
+
else {
|
|
479
|
+
return modelClassOrTableName.getTableName();
|
|
480
|
+
}
|
|
554
481
|
}
|
|
555
|
-
|
|
556
482
|
module.exports = {
|
|
557
|
-
|
|
483
|
+
QueryBuilderOperationSupport,
|
|
558
484
|
};
|