objection 3.9.1 → 4.0.0-rc.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/lib/dbErrors/LICENSE +21 -0
- package/lib/dbErrors/errorCodes/mssql.js +19 -0
- package/lib/dbErrors/errorCodes/mysql.js +805 -0
- package/lib/dbErrors/errorCodes/postgres.js +241 -0
- package/lib/dbErrors/errors/CheckViolationError.js +10 -0
- package/lib/dbErrors/errors/ConstraintViolationError.js +3 -0
- package/lib/dbErrors/errors/DBError.js +9 -0
- package/lib/dbErrors/errors/DataError.js +3 -0
- package/lib/dbErrors/errors/ForeignKeyViolationError.js +11 -0
- package/lib/dbErrors/errors/NotNullViolationError.js +12 -0
- package/lib/dbErrors/errors/UniqueViolationError.js +12 -0
- package/lib/dbErrors/index.js +59 -0
- package/lib/dbErrors/parsers/mssql/DBError/CheckViolationError/parser.js +28 -0
- package/lib/dbErrors/parsers/mssql/DBError/DataError/parser.js +25 -0
- package/lib/dbErrors/parsers/mssql/DBError/ForeignKeyViolationError/parser.js +39 -0
- package/lib/dbErrors/parsers/mssql/DBError/NotNullViolationError/parser.js +30 -0
- package/lib/dbErrors/parsers/mssql/DBError/UniqueViolationError/parser.js +53 -0
- package/lib/dbErrors/parsers/mssql/DBError/parser.js +51 -0
- package/lib/dbErrors/parsers/mssql/DBError/util.js +16 -0
- package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +43 -0
- package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/NotNullViolationError/parser.js +29 -0
- package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/UniqueViolationError/parser.js +27 -0
- package/lib/dbErrors/parsers/mysql/DBError/ConstraintViolationError/parser.js +23 -0
- package/lib/dbErrors/parsers/mysql/DBError/DataError/parser.js +24 -0
- package/lib/dbErrors/parsers/mysql/DBError/parser.js +28 -0
- package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/CheckViolationError/parser.js +18 -0
- package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +18 -0
- package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/NotNullViolationError/parser.js +18 -0
- package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/UniqueViolationError/parser.js +40 -0
- package/lib/dbErrors/parsers/postgres/DBError/ConstraintViolationError/parser.js +25 -0
- package/lib/dbErrors/parsers/postgres/DBError/DataError/parser.js +16 -0
- package/lib/dbErrors/parsers/postgres/DBError/parser.js +27 -0
- package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/CheckViolationError/parser.js +20 -0
- package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/ForeignKeyViolationError/parser.js +20 -0
- package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/NotNullViolationError/parser.js +27 -0
- package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/UniqueViolationError/parser.js +28 -0
- package/lib/dbErrors/parsers/sqlite/DBError/ConstraintViolationError/parser.js +24 -0
- package/lib/dbErrors/parsers/sqlite/DBError/parser.js +23 -0
- package/lib/dbErrors/utils/sqlState.js +3 -0
- package/lib/initialize.js +1 -7
- package/lib/model/AjvValidator.js +51 -117
- package/lib/model/Model.js +84 -59
- package/lib/model/ModifierNotFoundError.js +1 -7
- package/lib/model/NotFoundError.js +1 -7
- package/lib/model/RelationDoesNotExistError.js +1 -7
- package/lib/model/ValidationError.js +3 -10
- package/lib/model/Validator.js +1 -7
- package/lib/model/getModel.js +7 -6
- package/lib/model/inheritModel.js +1 -7
- package/lib/model/modelBindKnex.js +4 -10
- package/lib/model/modelClone.js +5 -11
- package/lib/model/modelColPropMap.js +3 -10
- package/lib/model/modelId.js +3 -10
- package/lib/model/modelJsonAttributes.js +35 -16
- package/lib/model/modelParseRelations.js +2 -8
- package/lib/model/modelQueryProps.js +11 -23
- package/lib/model/modelSet.js +89 -41
- package/lib/model/modelTableMetadata.js +4 -11
- package/lib/model/modelToJson.js +6 -13
- package/lib/model/modelUtils.js +14 -13
- package/lib/model/modelValidate.js +3 -9
- package/lib/model/modelValues.js +5 -14
- package/lib/model/modelVisitor.js +1 -7
- package/lib/objection.js +33 -71
- package/lib/queryBuilder/FunctionBuilder.js +4 -11
- package/lib/queryBuilder/InternalOptions.js +4 -9
- package/lib/queryBuilder/JoinBuilder.js +5 -8
- package/lib/queryBuilder/QueryBuilder.js +126 -95
- package/lib/queryBuilder/QueryBuilderBase.js +41 -123
- package/lib/queryBuilder/QueryBuilderContext.js +2 -8
- package/lib/queryBuilder/QueryBuilderContextBase.js +2 -8
- package/lib/queryBuilder/QueryBuilderOperationSupport.js +7 -21
- package/lib/queryBuilder/QueryBuilderUserContext.js +1 -7
- package/lib/queryBuilder/RawBuilder.js +5 -13
- package/lib/queryBuilder/ReferenceBuilder.js +20 -48
- package/lib/queryBuilder/RelationExpression.js +18 -16
- package/lib/queryBuilder/StaticHookArguments.js +74 -16
- package/lib/queryBuilder/ValueBuilder.js +11 -13
- package/lib/queryBuilder/getJoinBuilder.js +10 -0
- package/lib/queryBuilder/graph/GraphLoader.js +304 -0
- package/lib/queryBuilder/graph/GraphNode.js +235 -0
- package/lib/queryBuilder/graph/GraphOptions.js +55 -248
- package/lib/queryBuilder/graph/GraphPlan.js +395 -0
- package/lib/queryBuilder/graph/GraphUpsert.js +466 -414
- package/lib/queryBuilder/graph/GraphWriter.js +1263 -0
- package/lib/queryBuilder/join/JoinResultColumn.js +1 -7
- package/lib/queryBuilder/join/JoinResultParser.js +6 -11
- package/lib/queryBuilder/join/RelationJoiner.js +66 -84
- package/lib/queryBuilder/join/TableNode.js +2 -8
- package/lib/queryBuilder/join/TableTree.js +3 -9
- package/lib/queryBuilder/join/utils.js +5 -15
- package/lib/queryBuilder/jsonDialects/JsonDialect.js +51 -0
- package/lib/queryBuilder/jsonDialects/MySqlJsonDialect.js +81 -0
- package/lib/queryBuilder/jsonDialects/PostgresJsonDialect.js +45 -0
- package/lib/queryBuilder/jsonDialects/SqliteJsonDialect.js +67 -0
- package/lib/queryBuilder/jsonDialects/index.js +44 -0
- package/lib/queryBuilder/operations/DelegateOperation.js +2 -8
- package/lib/queryBuilder/operations/DeleteOperation.js +10 -39
- package/lib/queryBuilder/operations/FindByIdOperation.js +7 -12
- package/lib/queryBuilder/operations/FindByIdsOperation.js +4 -8
- package/lib/queryBuilder/operations/FindOperation.js +14 -34
- package/lib/queryBuilder/operations/FirstOperation.js +6 -10
- package/lib/queryBuilder/operations/FromOperation.js +3 -9
- package/lib/queryBuilder/operations/InsertAndFetchOperation.js +13 -12
- package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +4 -10
- package/lib/queryBuilder/operations/InsertGraphOperation.js +7 -18
- package/lib/queryBuilder/operations/InsertOperation.js +118 -45
- package/lib/queryBuilder/operations/InstanceDeleteOperation.js +5 -12
- package/lib/queryBuilder/operations/InstanceFindOperation.js +4 -11
- package/lib/queryBuilder/operations/InstanceInsertOperation.js +2 -8
- package/lib/queryBuilder/operations/InstanceUpdateOperation.js +7 -14
- package/lib/queryBuilder/operations/JoinRelatedOperation.js +5 -11
- package/lib/queryBuilder/operations/KnexOperation.js +2 -8
- package/lib/queryBuilder/operations/LimitPerParentOperation.js +7 -69
- package/lib/queryBuilder/operations/MergeOperation.js +5 -11
- package/lib/queryBuilder/operations/NoneOperation.js +2 -8
- package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +20 -49
- package/lib/queryBuilder/operations/OnBuildKnexOperation.js +2 -8
- package/lib/queryBuilder/operations/OnBuildOperation.js +2 -8
- package/lib/queryBuilder/operations/OnErrorOperation.js +2 -8
- package/lib/queryBuilder/operations/QueryBuilderOperation.js +1 -7
- package/lib/queryBuilder/operations/RangeOperation.js +11 -26
- package/lib/queryBuilder/operations/RelateOperation.js +2 -8
- package/lib/queryBuilder/operations/ReturningOperation.js +3 -9
- package/lib/queryBuilder/operations/RunAfterOperation.js +2 -12
- package/lib/queryBuilder/operations/RunBeforeOperation.js +2 -8
- package/lib/queryBuilder/operations/UnrelateOperation.js +2 -8
- package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +14 -17
- package/lib/queryBuilder/operations/UpdateOperation.js +32 -132
- package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +8 -11
- package/lib/queryBuilder/operations/UpsertGraphOperation.js +6 -12
- package/lib/queryBuilder/operations/WhereCompositeOperation.js +3 -9
- package/lib/queryBuilder/operations/WhereGroupOperation.js +2 -8
- package/lib/queryBuilder/operations/eager/EagerOperation.js +3 -9
- package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +72 -10
- package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +2 -8
- package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +23 -44
- package/lib/queryBuilder/operations/fieldExpressions.js +122 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +20 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +44 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonTypeOperation.js +14 -0
- package/lib/queryBuilder/operations/select/SelectOperation.js +11 -21
- package/lib/queryBuilder/operations/select/Selection.js +19 -17
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +4 -10
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +4 -10
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +4 -10
- package/lib/queryBuilder/operations/windowOrder.js +56 -0
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +3 -7
- package/lib/queryBuilder/parsers/relationExpressionParser.js +42 -43
- package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +39 -36
- package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +2 -8
- package/lib/queryBuilder/transformations/QueryTransformation.js +1 -7
- package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +3 -9
- package/lib/queryBuilder/transformations/index.js +3 -11
- package/lib/queryBuilder/whereGroups.js +9 -24
- package/lib/queryBuilder/withoutOrder.js +4 -11
- package/lib/relations/Relation.js +47 -28
- package/lib/relations/RelationDeleteOperation.js +4 -14
- package/lib/relations/RelationFindOperation.js +119 -90
- package/lib/relations/RelationInsertOperation.js +2 -8
- package/lib/relations/RelationOwner.js +5 -11
- package/lib/relations/RelationProperty.js +4 -10
- package/lib/relations/RelationUpdateOperation.js +4 -14
- package/lib/relations/RelationWriteResultOperation.js +15 -0
- package/lib/relations/belongsToMany/BelongsToManyDeleteOperation.js +23 -0
- package/lib/relations/belongsToMany/BelongsToManyFindOperation.js +35 -0
- package/lib/relations/belongsToMany/BelongsToManyInsertOperation.js +28 -0
- package/lib/relations/belongsToMany/BelongsToManyRelateOperation.js +16 -0
- package/lib/relations/belongsToMany/BelongsToManyRelation.js +276 -0
- package/lib/relations/belongsToMany/BelongsToManyUnrelateOperation.js +11 -0
- package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +2 -8
- package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +6 -11
- package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +3 -9
- package/lib/relations/belongsToOne/BelongsToOneRelation.js +8 -19
- package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +53 -27
- package/lib/relations/hasMany/HasManyInsertOperation.js +5 -10
- package/lib/relations/hasMany/HasManyRelateOperation.js +5 -11
- package/lib/relations/hasMany/HasManyRelation.js +9 -11
- package/lib/relations/hasMany/HasManyUnrelateOperation.js +3 -9
- package/lib/relations/hasOne/HasOneRelation.js +2 -8
- package/lib/relations/hasOneThrough/HasOneThroughRelation.js +2 -8
- package/lib/relations/manyToMany/ManyToManyModifyMixin.js +10 -15
- package/lib/relations/manyToMany/ManyToManyMySqlModifyMixin.js +2 -8
- package/lib/relations/manyToMany/ManyToManyRelation.js +82 -50
- package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +2 -8
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +5 -9
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +2 -8
- package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +3 -9
- package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +20 -59
- package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +6 -11
- package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +4 -10
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateMySqlOperation.js +3 -9
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +5 -9
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +3 -9
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +3 -9
- package/lib/relations/manyToMany/update/ManyToManyUpdateMySqlOperation.js +3 -9
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +5 -9
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +64 -72
- package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +3 -9
- package/lib/transaction.js +3 -9
- package/lib/utils/assert.js +3 -11
- package/lib/utils/buildUtils.js +2 -8
- package/lib/utils/classUtils.js +1 -7
- package/lib/utils/createModifier.js +2 -8
- package/lib/utils/identifierMapping.js +23 -27
- package/lib/utils/internalPropUtils.js +1 -7
- package/lib/utils/knexUtils.js +84 -40
- package/lib/utils/mixin.js +3 -10
- package/lib/utils/normalizeIds.js +2 -8
- package/lib/utils/objectUtils.js +41 -60
- package/lib/utils/parseFieldExpression.js +16 -16
- package/lib/utils/promiseUtils/after.js +2 -8
- package/lib/utils/promiseUtils/afterReturn.js +2 -8
- package/lib/utils/promiseUtils/index.js +7 -16
- package/lib/utils/promiseUtils/isPromise.js +2 -8
- package/lib/utils/promiseUtils/map.js +2 -8
- package/lib/utils/promiseUtils/mapAfterAllReturn.js +2 -8
- package/lib/utils/promiseUtils/try.js +2 -8
- package/lib/utils/resolveModel.js +10 -17
- package/lib/utils/tmpColumnUtils.js +2 -9
- package/lib/utils/warnings.js +2 -14
- package/package.json +25 -17
- package/typings/objection/index.d.ts +3217 -2179
- package/lib/.eslintrc.json +0 -8
- package/lib/model/graph/ModelGraph.js +0 -80
- package/lib/model/graph/ModelGraphBuilder.js +0 -241
- package/lib/model/graph/ModelGraphEdge.js +0 -52
- package/lib/model/graph/ModelGraphNode.js +0 -238
- package/lib/queryBuilder/graph/GraphAction.js +0 -69
- package/lib/queryBuilder/graph/GraphData.js +0 -53
- package/lib/queryBuilder/graph/GraphFetcher.js +0 -197
- package/lib/queryBuilder/graph/GraphNodeDbExistence.js +0 -184
- package/lib/queryBuilder/graph/GraphOperation.js +0 -27
- package/lib/queryBuilder/graph/delete/GraphDelete.js +0 -73
- package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +0 -88
- package/lib/queryBuilder/graph/insert/DeferredRelationGraphInsertAction.js +0 -58
- package/lib/queryBuilder/graph/insert/GraphInsert.js +0 -296
- package/lib/queryBuilder/graph/insert/GraphInsertAction.js +0 -233
- package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +0 -92
- package/lib/queryBuilder/graph/patch/GraphPatch.js +0 -20
- package/lib/queryBuilder/graph/patch/GraphPatchAction.js +0 -289
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +0 -31
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +0 -126
- package/lib/queryBuilder/operations/jsonApi/WhereJsonHasPostgresOperation.js +0 -30
- package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectPostgresOperation.js +0 -37
- package/lib/queryBuilder/operations/jsonApi/WhereJsonPostgresOperation.js +0 -62
- package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +0 -192
- package/lib/utils/wrapDbError.js +0 -64
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { QueryBuilderOperation } from './QueryBuilderOperation.js';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class RunBeforeOperation extends QueryBuilderOperation {
|
|
3
|
+
export class RunBeforeOperation extends QueryBuilderOperation {
|
|
6
4
|
constructor(name, opt) {
|
|
7
5
|
super(name, opt);
|
|
8
6
|
this.func = null;
|
|
@@ -23,7 +21,3 @@ class RunBeforeOperation extends QueryBuilderOperation {
|
|
|
23
21
|
return clone;
|
|
24
22
|
}
|
|
25
23
|
}
|
|
26
|
-
|
|
27
|
-
module.exports = {
|
|
28
|
-
RunBeforeOperation,
|
|
29
|
-
};
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { QueryBuilderOperation } from './QueryBuilderOperation.js';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class UnrelateOperation extends QueryBuilderOperation {
|
|
3
|
+
export class UnrelateOperation extends QueryBuilderOperation {
|
|
6
4
|
constructor(name, opt) {
|
|
7
5
|
super(name, opt);
|
|
8
6
|
|
|
@@ -17,7 +15,3 @@ class UnrelateOperation extends QueryBuilderOperation {
|
|
|
17
15
|
return clone;
|
|
18
16
|
}
|
|
19
17
|
}
|
|
20
|
-
|
|
21
|
-
module.exports = {
|
|
22
|
-
UnrelateOperation,
|
|
23
|
-
};
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { DelegateOperation } from './DelegateOperation.js';
|
|
2
|
+
import { FindByIdOperation } from './FindByIdOperation.js';
|
|
3
|
+
import { UpdateOperation } from './UpdateOperation.js';
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
const { FindByIdOperation } = require('./FindByIdOperation');
|
|
5
|
-
const { UpdateOperation } = require('./UpdateOperation');
|
|
6
|
-
const { addWhereGroup, assignWhereGroup } = require('../whereGroups');
|
|
7
|
-
|
|
8
|
-
class UpdateAndFetchOperation extends DelegateOperation {
|
|
5
|
+
export class UpdateAndFetchOperation extends DelegateOperation {
|
|
9
6
|
constructor(name, opt) {
|
|
10
7
|
super(name, opt);
|
|
11
8
|
|
|
@@ -42,7 +39,7 @@ class UpdateAndFetchOperation extends DelegateOperation {
|
|
|
42
39
|
|
|
43
40
|
onBuild(builder) {
|
|
44
41
|
if (!this.skipIdWhere) {
|
|
45
|
-
|
|
42
|
+
builder.findById(this.id);
|
|
46
43
|
}
|
|
47
44
|
|
|
48
45
|
super.onBuild(builder);
|
|
@@ -67,6 +64,13 @@ class UpdateAndFetchOperation extends DelegateOperation {
|
|
|
67
64
|
.castTo(builder.resultModelClass());
|
|
68
65
|
|
|
69
66
|
if (fetched) {
|
|
67
|
+
if (!fetched.$isObjectionModel) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Unable to merge the result of ${builder.modelClass().name}.afterFind() in ` +
|
|
70
|
+
`${this.name}(): expected a model.`,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
70
74
|
this.model.$set(fetched);
|
|
71
75
|
// A custom `select` may not include the id. The row was fetched by
|
|
72
76
|
// id, so make sure the result still has it.
|
|
@@ -84,12 +88,9 @@ class UpdateAndFetchOperation extends DelegateOperation {
|
|
|
84
88
|
// many-to-many updates move to a subquery when the query is built.
|
|
85
89
|
const delegate = this.delegate.toFindOperation(builder);
|
|
86
90
|
|
|
87
|
-
// Keep the id condition in its own where group, like `onBuild()` does. The
|
|
88
|
-
// delegate isn't named `findById`, as that would put all the wheres that it
|
|
89
|
-
// adds in the user's group.
|
|
90
91
|
return delegate
|
|
91
92
|
? new FindByIdDelegateOperation('find', { delegate, id: this.id })
|
|
92
|
-
:
|
|
93
|
+
: new FindByIdOperation('findById', { id: this.id });
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
clone() {
|
|
@@ -107,10 +108,6 @@ class UpdateAndFetchOperation extends DelegateOperation {
|
|
|
107
108
|
class FindByIdDelegateOperation extends DelegateOperation {
|
|
108
109
|
onBuild(builder) {
|
|
109
110
|
super.onBuild(builder);
|
|
110
|
-
|
|
111
|
+
builder.findById(this.opt.id);
|
|
111
112
|
}
|
|
112
113
|
}
|
|
113
|
-
|
|
114
|
-
module.exports = {
|
|
115
|
-
UpdateAndFetchOperation,
|
|
116
|
-
};
|
|
@@ -1,20 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const { StaticHookArguments } = require('../StaticHookArguments');
|
|
13
|
-
const { ValidationErrorType } = require('../../model/ValidationError');
|
|
14
|
-
const { removeOverriddenValues } = require('../../model/modelQueryProps');
|
|
15
|
-
const { warnOnce } = require('../../utils/warnings');
|
|
16
|
-
|
|
17
|
-
class UpdateOperation extends QueryBuilderOperation {
|
|
1
|
+
import { isEmpty } from '../../utils/objectUtils.js';
|
|
2
|
+
import { QueryBuilderOperation } from './QueryBuilderOperation.js';
|
|
3
|
+
import { StaticHookArguments } from '../StaticHookArguments.js';
|
|
4
|
+
import { removeOverriddenValues } from '../../model/modelQueryProps.js';
|
|
5
|
+
import {
|
|
6
|
+
checkFieldExpressions,
|
|
7
|
+
convertFieldExpressionsToRaw,
|
|
8
|
+
removeFieldExpressions,
|
|
9
|
+
} from './fieldExpressions.js';
|
|
10
|
+
|
|
11
|
+
export class UpdateOperation extends QueryBuilderOperation {
|
|
18
12
|
constructor(name, opt) {
|
|
19
13
|
super(name, opt);
|
|
20
14
|
|
|
@@ -55,26 +49,38 @@ class UpdateOperation extends QueryBuilderOperation {
|
|
|
55
49
|
return builder.isExplicitlyResolvedOrRejected() && !this.hasNothingToUpdate;
|
|
56
50
|
}
|
|
57
51
|
|
|
52
|
+
// Returns true if the query was resolved without writing anything, by a hook
|
|
53
|
+
// or because there is nothing to update in the table.
|
|
54
|
+
// @overridable
|
|
55
|
+
isResolvedWithoutWrite(builder) {
|
|
56
|
+
return builder.isExplicitlyResolvedOrRejected();
|
|
57
|
+
}
|
|
58
|
+
|
|
58
59
|
// knex leaves the offset out of updates, and on SQL Server the limit as well.
|
|
59
|
-
//
|
|
60
|
+
// Throw when the query is executed.
|
|
60
61
|
onAfterBuild(builder) {
|
|
61
62
|
if (builder.has('offset')) {
|
|
62
|
-
|
|
63
|
-
'offset() has no effect in update queries, so no rows are skipped, and on SQL Server ' +
|
|
64
|
-
'limit() is ignored too. This will throw an error in objection 4.0.',
|
|
65
|
-
);
|
|
63
|
+
throw new Error('offset() is not supported in update queries, as knex leaves it out.');
|
|
66
64
|
}
|
|
67
65
|
}
|
|
68
66
|
|
|
69
67
|
onBuildKnex(knexBuilder, builder) {
|
|
70
68
|
const json = this.model.$toDatabaseJson(builder);
|
|
71
|
-
const convertedJson = convertFieldExpressionsToRaw(builder, this.model, json);
|
|
72
69
|
|
|
73
|
-
|
|
70
|
+
if (this.modelOptions.patch) {
|
|
71
|
+
return knexBuilder.update(convertFieldExpressionsToRaw(builder, json));
|
|
72
|
+
} else {
|
|
73
|
+
checkFieldExpressions(builder.modelClass(), this.model, 'update');
|
|
74
|
+
return knexBuilder.update(json);
|
|
75
|
+
}
|
|
74
76
|
}
|
|
75
77
|
|
|
76
|
-
onAfter2(builder, numUpdated) {
|
|
77
|
-
|
|
78
|
+
async onAfter2(builder, numUpdated) {
|
|
79
|
+
await this.model.$afterUpdate(this.modelOptions, builder.context());
|
|
80
|
+
// The patched model is what the write returns, e.g. to `upsertGraph()` and
|
|
81
|
+
// `patchAndFetch()`, or what it merges into the instance of `$query()`:
|
|
82
|
+
removeFieldExpressions(builder.modelClass(), this.model);
|
|
83
|
+
return numUpdated;
|
|
78
84
|
}
|
|
79
85
|
|
|
80
86
|
toFindOperation() {
|
|
@@ -102,109 +108,3 @@ function callStaticBeforeUpdate(builder) {
|
|
|
102
108
|
const args = StaticHookArguments.create({ builder });
|
|
103
109
|
return builder.modelClass().beforeUpdate(args);
|
|
104
110
|
}
|
|
105
|
-
|
|
106
|
-
async function callAfterUpdate(builder, model, modelOptions, result) {
|
|
107
|
-
await callInstanceAfterUpdate(builder, model, modelOptions);
|
|
108
|
-
return callStaticAfterUpdate(builder, result);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function callInstanceAfterUpdate(builder, model, modelOptions) {
|
|
112
|
-
return model.$afterUpdate(modelOptions, builder.context());
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
async function callStaticAfterUpdate(builder, result) {
|
|
116
|
-
const args = StaticHookArguments.create({ builder, result });
|
|
117
|
-
const maybeResult = await builder.modelClass().afterUpdate(args);
|
|
118
|
-
|
|
119
|
-
if (maybeResult === undefined) {
|
|
120
|
-
return result;
|
|
121
|
-
} else {
|
|
122
|
-
return maybeResult;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function convertFieldExpressionsToRaw(builder, model, json) {
|
|
127
|
-
const knex = builder.knex();
|
|
128
|
-
const modelClass = builder.modelClass();
|
|
129
|
-
const declaredColumns = getDeclaredColumns(modelClass);
|
|
130
|
-
const convertedJson = {};
|
|
131
|
-
|
|
132
|
-
for (const key of Object.keys(json)) {
|
|
133
|
-
let val = json[key];
|
|
134
|
-
|
|
135
|
-
if (isFieldExpressionKey(key, declaredColumns)) {
|
|
136
|
-
// 'col:attr' : ref('other:lol') is transformed to
|
|
137
|
-
// "col" : raw(`jsonb_set("table"."col", '{attr}', to_jsonb("other"#>'{lol}'), true)`)
|
|
138
|
-
// with the column qualified, as it is ambiguous with `excluded` in merge().
|
|
139
|
-
warnIfNotPostgres(knex);
|
|
140
|
-
|
|
141
|
-
let parsed = ref(key);
|
|
142
|
-
|
|
143
|
-
// The field expression would be lost or produce invalid SQL next to a
|
|
144
|
-
// value for the whole json column. Report it with the property names:
|
|
145
|
-
if (Object.prototype.hasOwnProperty.call(json, parsed.column)) {
|
|
146
|
-
const property = modelClass.columnNameToPropertyName(parsed.column);
|
|
147
|
-
throw modelClass.createValidationError({
|
|
148
|
-
type: ValidationErrorType.ModelValidation,
|
|
149
|
-
data: {
|
|
150
|
-
[modelClass.columnNameToPropertyName(key)]: [
|
|
151
|
-
{
|
|
152
|
-
message: `can't be combined with a value for the json column '${property}'`,
|
|
153
|
-
keyword: null,
|
|
154
|
-
params: { property },
|
|
155
|
-
},
|
|
156
|
-
],
|
|
157
|
-
},
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
let jsonPath = toPostgresJsonPathLiteral(parsed.parsedExpr.access);
|
|
162
|
-
let valuePlaceholder = '?';
|
|
163
|
-
|
|
164
|
-
if (isKnexQueryBuilder(val) || isKnexRaw(val)) {
|
|
165
|
-
valuePlaceholder = 'to_jsonb(?)';
|
|
166
|
-
} else {
|
|
167
|
-
val = JSON.stringify(val);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
convertedJson[parsed.column] = knex.raw(
|
|
171
|
-
`jsonb_set(??, ${jsonPath}, ${valuePlaceholder}, true)`,
|
|
172
|
-
[convertedJson[parsed.column] || `${builder.tableRef()}.${parsed.column}`, val],
|
|
173
|
-
);
|
|
174
|
-
|
|
175
|
-
delete model[key];
|
|
176
|
-
} else {
|
|
177
|
-
convertedJson[key] = val;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return convertedJson;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
const declaredColumnsByModelClass = new WeakMap();
|
|
185
|
-
|
|
186
|
-
// The keys of `json` are column names, so map the properties with a ':' that the
|
|
187
|
-
// schema declares to their columns, once per model class. Properties that only
|
|
188
|
-
// the schema returned by `$beforeValidate()` declares aren't known here.
|
|
189
|
-
function getDeclaredColumns(modelClass) {
|
|
190
|
-
let declaredColumns = declaredColumnsByModelClass.get(modelClass);
|
|
191
|
-
|
|
192
|
-
if (!declaredColumns) {
|
|
193
|
-
const jsonSchema = modelClass.getJsonSchema();
|
|
194
|
-
const properties = jsonSchema && jsonSchema.properties;
|
|
195
|
-
declaredColumns = Object.fromEntries(
|
|
196
|
-
(isObject(properties) ? Object.keys(properties) : [])
|
|
197
|
-
.filter((key) => key.includes(':'))
|
|
198
|
-
.map((key) => [modelClass.propertyNameToColumnName(key), true]),
|
|
199
|
-
);
|
|
200
|
-
declaredColumnsByModelClass.set(modelClass, declaredColumns);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return declaredColumns;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
module.exports = {
|
|
207
|
-
UpdateOperation,
|
|
208
|
-
convertFieldExpressionsToRaw,
|
|
209
|
-
getDeclaredColumns,
|
|
210
|
-
};
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { DelegateOperation } from './DelegateOperation.js';
|
|
2
|
+
import { UpsertGraphOperation } from './UpsertGraphOperation.js';
|
|
3
|
+
import { RelationExpression } from '../RelationExpression.js';
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
const { UpsertGraphOperation } = require('./UpsertGraphOperation');
|
|
5
|
-
const { RelationExpression } = require('../RelationExpression');
|
|
6
|
-
|
|
7
|
-
class UpsertGraphAndFetchOperation extends DelegateOperation {
|
|
5
|
+
export class UpsertGraphAndFetchOperation extends DelegateOperation {
|
|
8
6
|
constructor(name, opt) {
|
|
9
7
|
super(name, opt);
|
|
10
8
|
|
|
@@ -21,7 +19,10 @@ class UpsertGraphAndFetchOperation extends DelegateOperation {
|
|
|
21
19
|
return this.delegate.isArray;
|
|
22
20
|
}
|
|
23
21
|
|
|
24
|
-
|
|
22
|
+
// Refetch the graph before the graph operations of the query run, so that a
|
|
23
|
+
// `withGraphFetched()` with its modifiers and options loads its relations
|
|
24
|
+
// into the fetched models, like in `InsertGraphAndFetchOperation` (#839).
|
|
25
|
+
async onAfter2(builder) {
|
|
25
26
|
if (this.models.length === 0) {
|
|
26
27
|
return this.isArray ? [] : null;
|
|
27
28
|
}
|
|
@@ -39,7 +40,3 @@ class UpsertGraphAndFetchOperation extends DelegateOperation {
|
|
|
39
40
|
return this.isArray ? models : models[0] || null;
|
|
40
41
|
}
|
|
41
42
|
}
|
|
42
|
-
|
|
43
|
-
module.exports = {
|
|
44
|
-
UpsertGraphAndFetchOperation,
|
|
45
|
-
};
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { QueryBuilderOperation } from './QueryBuilderOperation.js';
|
|
2
|
+
import { GraphUpsert } from '../graph/GraphUpsert.js';
|
|
3
|
+
import { RelationFindOperation } from '../../relations/RelationFindOperation.js';
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
const { GraphUpsert } = require('../graph/GraphUpsert');
|
|
5
|
-
const { RelationFindOperation } = require('../../relations/RelationFindOperation');
|
|
6
|
-
|
|
7
|
-
class UpsertGraphOperation extends QueryBuilderOperation {
|
|
5
|
+
export class UpsertGraphOperation extends QueryBuilderOperation {
|
|
8
6
|
constructor(name, opt) {
|
|
9
7
|
super(
|
|
10
8
|
name,
|
|
@@ -41,14 +39,14 @@ class UpsertGraphOperation extends QueryBuilderOperation {
|
|
|
41
39
|
}
|
|
42
40
|
|
|
43
41
|
onBefore1(builder, result) {
|
|
44
|
-
GraphUpsert.
|
|
42
|
+
GraphUpsert.checkForOnConflict(builder, this.name);
|
|
45
43
|
return result;
|
|
46
44
|
}
|
|
47
45
|
|
|
48
46
|
onAfter1(builder) {
|
|
49
47
|
if (hasOtherSqlModifyingQueryBuilderCalls(builder)) {
|
|
50
48
|
throw new Error(
|
|
51
|
-
'upsertGraph query should contain no other query builder calls like `findById`, `where` or `$relatedQuery` that would affect the SQL. They have no effect.',
|
|
49
|
+
'upsertGraph query should contain no other query builder calls like `findById`, `where`, `$query` or `$relatedQuery` that would affect the SQL. They have no effect.',
|
|
52
50
|
);
|
|
53
51
|
}
|
|
54
52
|
|
|
@@ -65,7 +63,3 @@ class UpsertGraphOperation extends QueryBuilderOperation {
|
|
|
65
63
|
function hasOtherSqlModifyingQueryBuilderCalls(builder) {
|
|
66
64
|
return builder.has(/where/) || builder.has(RelationFindOperation);
|
|
67
65
|
}
|
|
68
|
-
|
|
69
|
-
module.exports = {
|
|
70
|
-
UpsertGraphOperation,
|
|
71
|
-
};
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { ObjectionToKnexConvertingOperation } from './ObjectionToKnexConvertingOperation.js';
|
|
2
|
+
import { asSingle } from '../../utils/objectUtils.js';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
const { asSingle } = require('../../utils/objectUtils');
|
|
5
|
-
|
|
6
|
-
class WhereCompositeOperation extends ObjectionToKnexConvertingOperation {
|
|
4
|
+
export class WhereCompositeOperation extends ObjectionToKnexConvertingOperation {
|
|
7
5
|
onBuildKnex(knexBuilder, builder) {
|
|
8
6
|
const args = this.getKnexArgs(builder);
|
|
9
7
|
|
|
@@ -51,7 +49,3 @@ function buildCompositeWhereArgs(cols, op, values) {
|
|
|
51
49
|
},
|
|
52
50
|
];
|
|
53
51
|
}
|
|
54
|
-
|
|
55
|
-
module.exports = {
|
|
56
|
-
WhereCompositeOperation,
|
|
57
|
-
};
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const { QueryBuilderOperation } = require('./QueryBuilderOperation');
|
|
1
|
+
import { QueryBuilderOperation } from './QueryBuilderOperation.js';
|
|
4
2
|
|
|
5
3
|
// Builds a set of where operations inside a single knex `where(callback)`
|
|
6
4
|
// group, i.e. wraps them in parentheses. See `getWhereGroupOperations()`.
|
|
7
|
-
class WhereGroupOperation extends QueryBuilderOperation {
|
|
5
|
+
export class WhereGroupOperation extends QueryBuilderOperation {
|
|
8
6
|
constructor(name, opt) {
|
|
9
7
|
super(name, opt);
|
|
10
8
|
this.operations = opt.operations;
|
|
@@ -29,7 +27,3 @@ class WhereGroupOperation extends QueryBuilderOperation {
|
|
|
29
27
|
});
|
|
30
28
|
}
|
|
31
29
|
}
|
|
32
|
-
|
|
33
|
-
module.exports = {
|
|
34
|
-
WhereGroupOperation,
|
|
35
|
-
};
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { QueryBuilderOperation } from '../QueryBuilderOperation.js';
|
|
2
|
+
import { RelationExpression } from '../../RelationExpression.js';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
const { RelationExpression } = require('../../RelationExpression');
|
|
5
|
-
|
|
6
|
-
class EagerOperation extends QueryBuilderOperation {
|
|
4
|
+
export class EagerOperation extends QueryBuilderOperation {
|
|
7
5
|
constructor(name, opt) {
|
|
8
6
|
super(name, opt);
|
|
9
7
|
|
|
@@ -57,7 +55,3 @@ class EagerOperation extends QueryBuilderOperation {
|
|
|
57
55
|
function getModifierName(index) {
|
|
58
56
|
return `_f${index}_`;
|
|
59
57
|
}
|
|
60
|
-
|
|
61
|
-
module.exports = {
|
|
62
|
-
EagerOperation,
|
|
63
|
-
};
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
import { EagerOperation } from './EagerOperation.js';
|
|
2
|
+
import { RelationJoiner } from '../../join/RelationJoiner.js';
|
|
3
|
+
import { JoinedStaticHookArguments } from '../../StaticHookArguments.js';
|
|
4
|
+
import { asArray } from '../../../utils/objectUtils.js';
|
|
2
5
|
|
|
3
|
-
|
|
4
|
-
const { RelationJoiner } = require('../../join/RelationJoiner');
|
|
5
|
-
|
|
6
|
-
class JoinEagerOperation extends EagerOperation {
|
|
6
|
+
export class JoinEagerOperation extends EagerOperation {
|
|
7
7
|
constructor(name, opt) {
|
|
8
8
|
super(name, opt);
|
|
9
9
|
this.joiner = null;
|
|
10
|
+
this.models = null;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
onAdd(builder) {
|
|
@@ -43,7 +44,72 @@ class JoinEagerOperation extends EagerOperation {
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
onRawResult(builder, rows) {
|
|
46
|
-
|
|
47
|
+
this.models = this.joiner.parseResult(builder, rows);
|
|
48
|
+
return this.models;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Call the static `afterFind()` hooks of the joined relations bottom-up, after
|
|
52
|
+
// the `$afterFind()` hooks of all models, and assign the related models that
|
|
53
|
+
// they return, like `withGraphFetched()` does.
|
|
54
|
+
async onAfter3(builder, result) {
|
|
55
|
+
if (Array.isArray(this.models)) {
|
|
56
|
+
const [rootNode, ...nodes] = this.joiner.tableTree.nodes;
|
|
57
|
+
const modelsByNode = new Map([[rootNode, this.models]]);
|
|
58
|
+
const ownerByModel = new Map();
|
|
59
|
+
|
|
60
|
+
// The nodes are ordered from the root down, so the owners come first:
|
|
61
|
+
for (const node of nodes) {
|
|
62
|
+
const models = [];
|
|
63
|
+
|
|
64
|
+
for (const owner of modelsByNode.get(node.parentNode)) {
|
|
65
|
+
for (const model of asArray(owner[node.relationProperty])) {
|
|
66
|
+
if (model) {
|
|
67
|
+
models.push(model);
|
|
68
|
+
ownerByModel.set(model, owner);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
modelsByNode.set(node, models);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
for (const node of nodes.reverse()) {
|
|
77
|
+
const { modelClass, relation, relationProperty } = node;
|
|
78
|
+
const owners = modelsByNode.get(node.parentNode);
|
|
79
|
+
const models = modelsByNode.get(node);
|
|
80
|
+
const args = JoinedStaticHookArguments.create({
|
|
81
|
+
builder,
|
|
82
|
+
result: models,
|
|
83
|
+
relation,
|
|
84
|
+
items: owners,
|
|
85
|
+
});
|
|
86
|
+
const related = await modelClass.afterFind(args);
|
|
87
|
+
|
|
88
|
+
if (related !== undefined) {
|
|
89
|
+
const relatedByOwner = new Map(owners.map((owner) => [owner, []]));
|
|
90
|
+
|
|
91
|
+
for (const model of asArray(related)) {
|
|
92
|
+
const relatedOfOwner = relatedByOwner.get(ownerByModel.get(model));
|
|
93
|
+
|
|
94
|
+
if (!relatedOfOwner) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
`Unable to assign the result of ${modelClass.name}.afterFind() to the relation ` +
|
|
97
|
+
`"${relation.name}" of ${relation.ownerModelClass.name}: return the models ` +
|
|
98
|
+
'you got, filtered or reordered.',
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
relatedOfOwner.push(model);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
for (const [owner, related] of relatedByOwner) {
|
|
106
|
+
owner[relationProperty] = relation.isOneToOne() ? related[0] || null : related;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return result;
|
|
47
113
|
}
|
|
48
114
|
|
|
49
115
|
clone() {
|
|
@@ -52,7 +118,3 @@ class JoinEagerOperation extends EagerOperation {
|
|
|
52
118
|
return clone;
|
|
53
119
|
}
|
|
54
120
|
}
|
|
55
|
-
|
|
56
|
-
module.exports = {
|
|
57
|
-
JoinEagerOperation,
|
|
58
|
-
};
|
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { WhereInEagerOperation } from './WhereInEagerOperation.js';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class NaiveEagerOperation extends WhereInEagerOperation {
|
|
3
|
+
export class NaiveEagerOperation extends WhereInEagerOperation {
|
|
6
4
|
batchSize() {
|
|
7
5
|
return 1;
|
|
8
6
|
}
|
|
9
7
|
}
|
|
10
|
-
|
|
11
|
-
module.exports = {
|
|
12
|
-
NaiveEagerOperation,
|
|
13
|
-
};
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class WhereInEagerOperation extends EagerOperation {
|
|
1
|
+
import * as promiseUtils from '../../../utils/promiseUtils/index.js';
|
|
2
|
+
|
|
3
|
+
import { EagerOperation } from './EagerOperation.js';
|
|
4
|
+
import { LimitPerParentOperation } from '../LimitPerParentOperation.js';
|
|
5
|
+
import { isMsSql, isOracle, isSqlite } from '../../../utils/knexUtils.js';
|
|
6
|
+
import { isObject, asArray, flatten, chunk } from '../../../utils/objectUtils.js';
|
|
7
|
+
import { ValidationErrorType } from '../../../model/ValidationError.js';
|
|
8
|
+
import { createModifier } from '../../../utils/createModifier.js';
|
|
9
|
+
import { RelationDoesNotExistError } from '../../../model/RelationDoesNotExistError.js';
|
|
10
|
+
import { RelationOwner } from '../../../relations/RelationOwner.js';
|
|
11
|
+
import { hasAliasSelection } from '../select/Selection.js';
|
|
12
|
+
|
|
13
|
+
export class WhereInEagerOperation extends EagerOperation {
|
|
15
14
|
constructor(name, opt) {
|
|
16
15
|
super(name, opt);
|
|
17
16
|
|
|
@@ -64,10 +63,6 @@ class WhereInEagerOperation extends EagerOperation {
|
|
|
64
63
|
async onAfter2(builder, result) {
|
|
65
64
|
const modelClass = builder.resultModelClass();
|
|
66
65
|
|
|
67
|
-
if (!result) {
|
|
68
|
-
return result;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
66
|
const models = asArray(result);
|
|
72
67
|
|
|
73
68
|
// Check models to be actual objects, to filter out `count` results (#2397).
|
|
@@ -86,9 +81,7 @@ class WhereInEagerOperation extends EagerOperation {
|
|
|
86
81
|
|
|
87
82
|
async fetchRelation(builder, models, relation, expr) {
|
|
88
83
|
const modelClass = builder.resultModelClass();
|
|
89
|
-
const batchSize = this.
|
|
90
|
-
? 1
|
|
91
|
-
: this.batchSize(builder.knex());
|
|
84
|
+
const batchSize = this.batchSize(builder.knex());
|
|
92
85
|
const modelBatches = chunk(models, batchSize);
|
|
93
86
|
|
|
94
87
|
const result = await promiseUtils.map(
|
|
@@ -139,30 +132,16 @@ class WhereInEagerOperation extends EagerOperation {
|
|
|
139
132
|
}
|
|
140
133
|
}
|
|
141
134
|
|
|
142
|
-
if (this.graphOptions.limitPerParent) {
|
|
135
|
+
if (this.graphOptions.limitPerParent !== false) {
|
|
143
136
|
queryBuilder.addOperation(
|
|
144
137
|
new LimitPerParentOperation('limitPerParent', { findOperation }),
|
|
145
138
|
[],
|
|
146
139
|
);
|
|
147
140
|
}
|
|
148
141
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
// Without window functions, the related models can only be limited per
|
|
153
|
-
// parent with one query per parent.
|
|
154
|
-
shouldFetchPerParent(builder, models, relation, expr) {
|
|
155
|
-
if (this.graphOptions.limitPerParent && !hasWindowFunctions(builder.knex())) {
|
|
156
|
-
// Build the query to find its limits, also those of the relation's
|
|
157
|
-
// `modify` and of `onBuildKnex()` hooks. This runs its modifiers and
|
|
158
|
-
// build hooks once more than for fetching the relation.
|
|
159
|
-
const query = this.fetchRelationBatch(builder, models, relation, expr);
|
|
160
|
-
query.executeOnBuild();
|
|
161
|
-
const knexQuery = query.executeOnBuildKnex(query.knex().queryBuilder());
|
|
162
|
-
return !!query.findOperation(LimitPerParentOperation)?.getRange(knexQuery, query);
|
|
163
|
-
} else {
|
|
164
|
-
return false;
|
|
165
|
-
}
|
|
142
|
+
// The relation gets what its query returns, after its hooks and `runAfter()`
|
|
143
|
+
// callbacks:
|
|
144
|
+
return queryBuilder.then((result) => findOperation.assignResult(queryBuilder, result));
|
|
166
145
|
}
|
|
167
146
|
|
|
168
147
|
shouldSkipFetched(models, relation, expr) {
|
|
@@ -262,7 +241,11 @@ function findRelationPropsToSelect(builder, relationsToFetch) {
|
|
|
262
241
|
const prop = ownerProp.props[c];
|
|
263
242
|
const col = ownerProp.cols[c];
|
|
264
243
|
|
|
265
|
-
if (
|
|
244
|
+
if (
|
|
245
|
+
!builder.hasSelectionAs(fullCol, col) &&
|
|
246
|
+
!hasAliasSelection(builder, col) &&
|
|
247
|
+
selectionsToAdd.indexOf(fullCol) === -1
|
|
248
|
+
) {
|
|
266
249
|
selectedProps.push(prop);
|
|
267
250
|
selectionsToAdd.push(fullCol);
|
|
268
251
|
}
|
|
@@ -294,7 +277,3 @@ function getRelatedModels(models, expr) {
|
|
|
294
277
|
|
|
295
278
|
return allRelated;
|
|
296
279
|
}
|
|
297
|
-
|
|
298
|
-
module.exports = {
|
|
299
|
-
WhereInEagerOperation,
|
|
300
|
-
};
|