@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,36 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ManyToManySqliteModifyMixin = void 0;
|
|
4
|
+
const ManyToManyModifyMixin_1 = require("./ManyToManyModifyMixin");
|
|
4
5
|
const SQLITE_BUILTIN_ROW_ID = '_rowid_';
|
|
5
|
-
|
|
6
6
|
// We need to override this mixin for sqlite because sqlite doesn't support
|
|
7
7
|
// multi-column where in statements with subqueries. We need to use the
|
|
8
8
|
// internal _rowid_ column instead.
|
|
9
9
|
const ManyToManySqliteModifyMixin = (Operation) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
.whereInComposite(rowIdRef, subquery)
|
|
29
|
-
.whereInComposite(joinTableOwnerRefs, ownerValues);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
module.exports = {
|
|
35
|
-
ManyToManySqliteModifyMixin,
|
|
10
|
+
return class extends (0, ManyToManyModifyMixin_1.ManyToManyModifyMixin)(Operation) {
|
|
11
|
+
applyModifyFilterForRelatedTable(builder) {
|
|
12
|
+
const tableRef = builder.tableRefFor(this.relation.relatedModelClass);
|
|
13
|
+
const rowIdRef = `${tableRef}.${SQLITE_BUILTIN_ROW_ID}`;
|
|
14
|
+
const subquery = this.modifyFilterSubquery.clone().select(rowIdRef);
|
|
15
|
+
return builder.whereInComposite(rowIdRef, subquery);
|
|
16
|
+
}
|
|
17
|
+
applyModifyFilterForJoinTable(builder) {
|
|
18
|
+
const joinTableOwnerRefs = this.relation.joinTableOwnerProp.refs(builder);
|
|
19
|
+
const tableRef = builder.tableRefFor(this.relation.getJoinModelClass(builder));
|
|
20
|
+
const rowIdRef = `${tableRef}.${SQLITE_BUILTIN_ROW_ID}`;
|
|
21
|
+
const ownerValues = this.owner.getProps(this.relation);
|
|
22
|
+
const subquery = this.modifyFilterSubquery.clone().select(rowIdRef);
|
|
23
|
+
return builder
|
|
24
|
+
.whereInComposite(rowIdRef, subquery)
|
|
25
|
+
.whereInComposite(joinTableOwnerRefs, ownerValues);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
36
28
|
};
|
|
29
|
+
exports.ManyToManySqliteModifyMixin = ManyToManySqliteModifyMixin;
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
class ManyToManyDeleteOperation extends ManyToManyModifyMixin(ManyToManyDeleteOperationBase) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
ManyToManyDeleteOperation,
|
|
10
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ManyToManyDeleteOperation = void 0;
|
|
4
|
+
const ManyToManyDeleteOperationBase_1 = require("./ManyToManyDeleteOperationBase");
|
|
5
|
+
const ManyToManyModifyMixin_1 = require("../ManyToManyModifyMixin");
|
|
6
|
+
class ManyToManyDeleteOperation extends (0, ManyToManyModifyMixin_1.ManyToManyModifyMixin)(ManyToManyDeleteOperationBase_1.ManyToManyDeleteOperationBase) {
|
|
7
|
+
}
|
|
8
|
+
exports.ManyToManyDeleteOperation = ManyToManyDeleteOperation;
|
|
@@ -1,26 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class ManyToManyDeleteOperationBase extends DeleteOperation {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
throw new Error('not implemented');
|
|
21
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ManyToManyDeleteOperationBase = void 0;
|
|
4
|
+
const DeleteOperation_1 = require("../../../queryBuilder/operations/DeleteOperation");
|
|
5
|
+
class ManyToManyDeleteOperationBase extends DeleteOperation_1.DeleteOperation {
|
|
6
|
+
constructor(name, opt) {
|
|
7
|
+
// @ts-expect-error The JavaScript QueryBuilderOperation's `name` is inferred as null.
|
|
8
|
+
super(name, opt);
|
|
9
|
+
this.relation = opt.relation;
|
|
10
|
+
this.owner = opt.owner;
|
|
11
|
+
}
|
|
12
|
+
/* istanbul ignore next */
|
|
13
|
+
applyModifyFilterForRelatedTable(builder) {
|
|
14
|
+
throw new Error('not implemented');
|
|
15
|
+
}
|
|
16
|
+
/* istanbul ignore next */
|
|
17
|
+
applyModifyFilterForJoinTable(builder) {
|
|
18
|
+
throw new Error('not implemented');
|
|
19
|
+
}
|
|
22
20
|
}
|
|
23
|
-
|
|
24
|
-
module.exports = {
|
|
25
|
-
ManyToManyDeleteOperationBase,
|
|
26
|
-
};
|
|
21
|
+
exports.ManyToManyDeleteOperationBase = ManyToManyDeleteOperationBase;
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
class ManyToManyDeleteSqliteOperation extends ManyToManySqliteModifyMixin(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
module.exports = {
|
|
11
|
-
ManyToManyDeleteSqliteOperation,
|
|
12
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ManyToManyDeleteSqliteOperation = void 0;
|
|
4
|
+
const ManyToManyDeleteOperationBase_1 = require("./ManyToManyDeleteOperationBase");
|
|
5
|
+
const ManyToManySqliteModifyMixin_1 = require("../ManyToManySqliteModifyMixin");
|
|
6
|
+
class ManyToManyDeleteSqliteOperation extends (0, ManyToManySqliteModifyMixin_1.ManyToManySqliteModifyMixin)(ManyToManyDeleteOperationBase_1.ManyToManyDeleteOperationBase) {
|
|
7
|
+
}
|
|
8
|
+
exports.ManyToManyDeleteSqliteOperation = ManyToManyDeleteSqliteOperation;
|
|
@@ -1,105 +1,82 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
class ManyToManyFindOperation extends RelationFindOperation {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
this.ownerJoinColumnAlias[i] = getTempColumn(i);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
onBuild(builder) {
|
|
18
|
-
const relatedModelClass = this.relation.relatedModelClass;
|
|
19
|
-
|
|
20
|
-
this.maybeApplyAlias(builder);
|
|
21
|
-
this.relation.findQuery(builder, this.owner);
|
|
22
|
-
|
|
23
|
-
if (!builder.hasSelects()) {
|
|
24
|
-
const table = builder.tableRefFor(relatedModelClass);
|
|
25
|
-
|
|
26
|
-
// If the user hasn't specified a select clause, select the related model's columns.
|
|
27
|
-
// If we don't do this we also get the join table's columns.
|
|
28
|
-
builder.select(`${table}.*`);
|
|
29
|
-
|
|
30
|
-
// Also select all extra columns.
|
|
31
|
-
for (const extra of this.relation.joinTableExtras) {
|
|
32
|
-
const joinTable = builder.tableRefFor(this.relation.joinTable);
|
|
33
|
-
builder.select(`${joinTable}.${extra.joinTableCol} as ${extra.aliasCol}`);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (this.assignResultToOwner && this.owner.isModels) {
|
|
38
|
-
this.selectMissingJoinColumns(builder);
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ManyToManyFindOperation = void 0;
|
|
4
|
+
const RelationFindOperation_1 = require("../../RelationFindOperation");
|
|
5
|
+
const tmpColumnUtils_1 = require("../../../utils/tmpColumnUtils");
|
|
6
|
+
class ManyToManyFindOperation extends RelationFindOperation_1.RelationFindOperation {
|
|
7
|
+
constructor(name, opt) {
|
|
8
|
+
super(name, opt);
|
|
9
|
+
this.ownerJoinColumnAlias = new Array(this.relation.joinTableOwnerProp.size);
|
|
10
|
+
for (let i = 0, l = this.ownerJoinColumnAlias.length; i < l; ++i) {
|
|
11
|
+
this.ownerJoinColumnAlias[i] = (0, tmpColumnUtils_1.getTempColumn)(i);
|
|
12
|
+
}
|
|
39
13
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (!arr) {
|
|
56
|
-
arr = [];
|
|
57
|
-
relatedByOwnerId.set(key, arr);
|
|
14
|
+
onBuild(builder) {
|
|
15
|
+
const relatedModelClass = this.relation.relatedModelClass;
|
|
16
|
+
this.maybeApplyAlias(builder);
|
|
17
|
+
this.relation.findQuery(builder, this.owner);
|
|
18
|
+
if (!builder.hasSelects()) {
|
|
19
|
+
const table = builder.tableRefFor(relatedModelClass);
|
|
20
|
+
// If the user hasn't specified a select clause, select the related model's columns.
|
|
21
|
+
// If we don't do this we also get the join table's columns.
|
|
22
|
+
builder.select(`${table}.*`);
|
|
23
|
+
// Also select all extra columns.
|
|
24
|
+
for (const extra of this.relation.joinTableExtras) {
|
|
25
|
+
const joinTable = builder.tableRefFor(this.relation.joinTable);
|
|
26
|
+
builder.select(`${joinTable}.${extra.joinTableCol} as ${extra.aliasCol}`);
|
|
27
|
+
}
|
|
58
28
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
for (let i = 0, l = owners.length; i < l; ++i) {
|
|
64
|
-
const own = owners[i];
|
|
65
|
-
const key = ownerProp.propKey(own);
|
|
66
|
-
const related = relatedByOwnerId.get(key);
|
|
67
|
-
|
|
68
|
-
if (isOneToOne) {
|
|
69
|
-
own[this.relationProperty] = (related && related[0]) || null;
|
|
70
|
-
} else {
|
|
71
|
-
own[this.relationProperty] = related || [];
|
|
29
|
+
if (this.assignResultToOwner && this.owner.isModels) {
|
|
30
|
+
this.selectMissingJoinColumns(builder);
|
|
72
31
|
}
|
|
73
|
-
}
|
|
74
32
|
}
|
|
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
|
-
|
|
33
|
+
onAfter2(_, related) {
|
|
34
|
+
const isOneToOne = this.relation.isOneToOne();
|
|
35
|
+
if (this.assignResultToOwner && this.owner.isModels) {
|
|
36
|
+
const owners = this.owner.modelArray;
|
|
37
|
+
const ownerProp = this.relation.ownerProp;
|
|
38
|
+
const relatedByOwnerId = new Map();
|
|
39
|
+
for (let i = 0, l = related.length; i < l; ++i) {
|
|
40
|
+
const rel = related[i];
|
|
41
|
+
const key = rel.$propKey(this.ownerJoinColumnAlias);
|
|
42
|
+
let arr = relatedByOwnerId.get(key);
|
|
43
|
+
if (!arr) {
|
|
44
|
+
arr = [];
|
|
45
|
+
relatedByOwnerId.set(key, arr);
|
|
46
|
+
}
|
|
47
|
+
arr.push(rel);
|
|
48
|
+
}
|
|
49
|
+
for (let i = 0, l = owners.length; i < l; ++i) {
|
|
50
|
+
const own = owners[i];
|
|
51
|
+
const key = ownerProp.propKey(own);
|
|
52
|
+
const related = relatedByOwnerId.get(key);
|
|
53
|
+
if (isOneToOne) {
|
|
54
|
+
own[this.relationProperty] = (related && related[0]) || null;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
own[this.relationProperty] = related || [];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return related;
|
|
62
|
+
}
|
|
63
|
+
clone() {
|
|
64
|
+
const clone = super.clone();
|
|
65
|
+
clone.ownerJoinColumnAlias = this.ownerJoinColumnAlias.slice();
|
|
66
|
+
return clone;
|
|
67
|
+
}
|
|
68
|
+
selectMissingJoinColumns(builder) {
|
|
69
|
+
const { relatedModelClass, joinTableOwnerProp } = this.relation;
|
|
70
|
+
// We must select the owner join columns so that we know for which owner model the related
|
|
71
|
+
// models belong to after the requests.
|
|
72
|
+
joinTableOwnerProp.forEach((i) => {
|
|
73
|
+
const joinTableOwnerRef = joinTableOwnerProp.ref(builder, i);
|
|
74
|
+
const propName = relatedModelClass.columnNameToPropertyName(this.ownerJoinColumnAlias[i]);
|
|
75
|
+
builder.select(joinTableOwnerRef.as(this.ownerJoinColumnAlias[i]));
|
|
76
|
+
// Mark them to be omitted later.
|
|
77
|
+
this.omitProps.push(propName);
|
|
78
|
+
});
|
|
79
|
+
super.selectMissingJoinColumns(builder);
|
|
80
|
+
}
|
|
101
81
|
}
|
|
102
|
-
|
|
103
|
-
module.exports = {
|
|
104
|
-
ManyToManyFindOperation,
|
|
105
|
-
};
|
|
82
|
+
exports.ManyToManyFindOperation = ManyToManyFindOperation;
|
|
@@ -1,52 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
class ManyToManyInsertOperation extends RelationInsertOperation {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return retVal;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async onAfter1(builder, ret) {
|
|
20
|
-
const inserted = await super.onAfter1(builder, ret);
|
|
21
|
-
|
|
22
|
-
const relateOp = new ManyToManyRelateOperation('relate', {
|
|
23
|
-
dontCopyReturning: true,
|
|
24
|
-
dontCopyOnConflict: true,
|
|
25
|
-
relation: this.relation,
|
|
26
|
-
owner: this.owner,
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
const modelsToRelate = inserted.filter((it) => {
|
|
30
|
-
return this.relation.relatedProp.hasProps(it);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
if (this.assignResultToOwner && this.owner.isModels) {
|
|
34
|
-
for (const owner of this.owner.modelArray) {
|
|
35
|
-
owner.$appendRelated(this.relation, inserted);
|
|
36
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ManyToManyInsertOperation = void 0;
|
|
4
|
+
const RelationInsertOperation_1 = require("../../RelationInsertOperation");
|
|
5
|
+
const ManyToManyRelateOperation_1 = require("../relate/ManyToManyRelateOperation");
|
|
6
|
+
class ManyToManyInsertOperation extends RelationInsertOperation_1.RelationInsertOperation {
|
|
7
|
+
onAdd(builder, args) {
|
|
8
|
+
const retVal = super.onAdd(builder, args);
|
|
9
|
+
// Omit extra properties so that we don't try to insert
|
|
10
|
+
// them to the related table. We don't actually remove them
|
|
11
|
+
// from the objects, but simply mark them to be removed
|
|
12
|
+
// from the inserted row.
|
|
13
|
+
this.relation.omitExtraProps(this.models);
|
|
14
|
+
return retVal;
|
|
37
15
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
16
|
+
async onAfter1(builder, ret) {
|
|
17
|
+
const inserted = await super.onAfter1(builder, ret);
|
|
18
|
+
const relateOp = new ManyToManyRelateOperation_1.ManyToManyRelateOperation('relate', {
|
|
19
|
+
dontCopyReturning: true,
|
|
20
|
+
dontCopyOnConflict: true,
|
|
21
|
+
relation: this.relation,
|
|
22
|
+
owner: this.owner,
|
|
23
|
+
});
|
|
24
|
+
const modelsToRelate = inserted.filter((it) => {
|
|
25
|
+
return this.relation.relatedProp.hasProps(it);
|
|
26
|
+
});
|
|
27
|
+
if (this.assignResultToOwner && this.owner.isModels) {
|
|
28
|
+
for (const owner of this.owner.modelArray) {
|
|
29
|
+
owner.$appendRelated(this.relation, inserted);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (modelsToRelate.length === 0) {
|
|
33
|
+
return inserted;
|
|
34
|
+
}
|
|
35
|
+
relateOp.onAdd(builder, [modelsToRelate]);
|
|
36
|
+
await relateOp.queryExecutor(builder);
|
|
37
|
+
return inserted;
|
|
41
38
|
}
|
|
42
|
-
|
|
43
|
-
relateOp.onAdd(builder, [modelsToRelate]);
|
|
44
|
-
await relateOp.queryExecutor(builder);
|
|
45
|
-
|
|
46
|
-
return inserted;
|
|
47
|
-
}
|
|
48
39
|
}
|
|
49
|
-
|
|
50
|
-
module.exports = {
|
|
51
|
-
ManyToManyInsertOperation,
|
|
52
|
-
};
|
|
40
|
+
exports.ManyToManyInsertOperation = ManyToManyInsertOperation;
|
|
@@ -1,81 +1,65 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
class ManyToManyRelateOperation extends RelateOperation {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
queryExecutor(builder) {
|
|
17
|
-
const joinModelClass = this.relation.getJoinModelClass(builder.knex());
|
|
18
|
-
const ownerValues = this.owner.getSplitProps(builder, this.relation);
|
|
19
|
-
const joinModels = [];
|
|
20
|
-
|
|
21
|
-
for (const ownerValue of ownerValues) {
|
|
22
|
-
for (const relatedValue of this.ids) {
|
|
23
|
-
joinModels.push(
|
|
24
|
-
joinModelClass.fromJson(this.relation.createJoinModel(ownerValue, relatedValue)),
|
|
25
|
-
);
|
|
26
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ManyToManyRelateOperation = void 0;
|
|
4
|
+
const knexUtils_1 = require("../../../utils/knexUtils");
|
|
5
|
+
const normalizeIds_1 = require("../../../utils/normalizeIds");
|
|
6
|
+
const RelateOperation_1 = require("../../../queryBuilder/operations/RelateOperation");
|
|
7
|
+
class ManyToManyRelateOperation extends RelateOperation_1.RelateOperation {
|
|
8
|
+
onAdd(builder, args) {
|
|
9
|
+
this.input = args[0];
|
|
10
|
+
// Without `arrayOutput`, these are id objects.
|
|
11
|
+
this.ids = (0, normalizeIds_1.normalizeIds)(args[0], this.relation.relatedProp);
|
|
12
|
+
assertOwnerIsSingleItem(builder, this.owner, this.relation);
|
|
13
|
+
return true;
|
|
27
14
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
15
|
+
queryExecutor(builder) {
|
|
16
|
+
const joinModelClass = this.relation.getJoinModelClass(builder.knex());
|
|
17
|
+
const ownerValues = this.owner.getSplitProps(builder, this.relation);
|
|
18
|
+
const joinModels = [];
|
|
19
|
+
for (const ownerValue of ownerValues) {
|
|
20
|
+
for (const relatedValue of this.ids) {
|
|
21
|
+
joinModels.push(joinModelClass.fromJson(this.relation.createJoinModel(ownerValue, relatedValue)));
|
|
22
|
+
}
|
|
35
23
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
24
|
+
return joinModelClass
|
|
25
|
+
.query()
|
|
26
|
+
.childQueryOf(builder)
|
|
27
|
+
.modify((query) => {
|
|
28
|
+
if (!this.opt.dontCopyReturning) {
|
|
29
|
+
query.copyFrom(builder, /returning/);
|
|
30
|
+
}
|
|
31
|
+
if (!this.opt.dontCopyOnConflict) {
|
|
32
|
+
query.copyFrom(builder, /onConflict|ignore|merge/);
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
.runBefore((_, builder) => {
|
|
36
|
+
return this.relation.executeJoinTableBeforeInsert(joinModels, builder.context(), null);
|
|
37
|
+
})
|
|
38
|
+
.insert(joinModels)
|
|
39
|
+
.runAfter((models) => {
|
|
40
|
+
return Array.isArray(models) ? models.length : 1;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
49
43
|
}
|
|
50
|
-
|
|
44
|
+
exports.ManyToManyRelateOperation = ManyToManyRelateOperation;
|
|
51
45
|
function assertOwnerIsSingleItem(builder, owner, relation) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
throw new Error(
|
|
61
|
-
'Parent must be a list of identifiers or a list of models when relating a ManyToManyRelation',
|
|
62
|
-
);
|
|
46
|
+
const { isModels, isIdentifiers, isQueryBuilder } = owner;
|
|
47
|
+
const { ownerProp } = relation;
|
|
48
|
+
const singleModel = isModels && owner.modelArray.length === 1;
|
|
49
|
+
const singleId = isIdentifiers && owner.getNormalizedIdentifiers(ownerProp).length === 1;
|
|
50
|
+
if ((0, knexUtils_1.isPostgres)(builder.unsafeKnex())) {
|
|
51
|
+
if (!isModels && !isIdentifiers && !isQueryBuilder) {
|
|
52
|
+
throw new Error('Parent must be a list of identifiers or a list of models when relating a ManyToManyRelation');
|
|
53
|
+
}
|
|
63
54
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
].join(' '),
|
|
74
|
-
);
|
|
55
|
+
else {
|
|
56
|
+
if (!singleModel && !singleId && !isQueryBuilder) {
|
|
57
|
+
throw new Error([
|
|
58
|
+
'Can only relate items for one parent at a time in case of ManyToManyRelation.',
|
|
59
|
+
'Otherwise multiple insert queries would need to be created.',
|
|
60
|
+
'If you need to relate items for multiple parents, simply loop through them.',
|
|
61
|
+
`That's the most performant way.`,
|
|
62
|
+
].join(' '));
|
|
63
|
+
}
|
|
75
64
|
}
|
|
76
|
-
}
|
|
77
65
|
}
|
|
78
|
-
|
|
79
|
-
module.exports = {
|
|
80
|
-
ManyToManyRelateOperation,
|
|
81
|
-
};
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
class ManyToManyUnrelateOperation extends ManyToManyModifyMixin(ManyToManyUnrelateOperationBase) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ManyToManyUnrelateOperation = void 0;
|
|
4
|
+
const ManyToManyUnrelateOperationBase_1 = require("./ManyToManyUnrelateOperationBase");
|
|
5
|
+
const ManyToManyModifyMixin_1 = require("../ManyToManyModifyMixin");
|
|
6
|
+
class ManyToManyUnrelateOperation extends (0, ManyToManyModifyMixin_1.ManyToManyModifyMixin)(ManyToManyUnrelateOperationBase_1.ManyToManyUnrelateOperationBase) {
|
|
7
|
+
get modifyMainQuery() {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
module.exports = {
|
|
13
|
-
ManyToManyUnrelateOperation,
|
|
14
|
-
};
|
|
11
|
+
exports.ManyToManyUnrelateOperation = ManyToManyUnrelateOperation;
|