@abedmurrar/objection 4.0.0-alpha.0 → 4.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +279 -363
- package/lib/relations/RelationDeleteOperation.js +16 -22
- package/lib/relations/RelationFindOperation.js +100 -133
- package/lib/relations/RelationInsertOperation.js +19 -27
- package/lib/relations/RelationOwner.js +167 -204
- package/lib/relations/RelationProperty.js +177 -217
- package/lib/relations/RelationUpdateOperation.js +16 -23
- package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +11 -16
- package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +24 -35
- package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +30 -41
- package/lib/relations/belongsToOne/BelongsToOneRelation.js +33 -41
- package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +38 -53
- package/lib/relations/hasMany/HasManyInsertOperation.js +35 -51
- package/lib/relations/hasMany/HasManyRelateOperation.js +38 -54
- package/lib/relations/hasMany/HasManyRelation.js +30 -38
- package/lib/relations/hasMany/HasManyUnrelateOperation.js +19 -26
- package/lib/relations/hasOne/HasOneRelation.js +4 -7
- package/lib/relations/hasOneThrough/HasOneThroughRelation.js +4 -7
- package/lib/relations/manyToMany/ManyToManyModifyMixin.js +67 -90
- package/lib/relations/manyToMany/ManyToManyRelation.js +349 -451
- package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +19 -26
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +3 -5
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +14 -20
- package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +3 -7
- package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +71 -94
- package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +31 -43
- package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +50 -67
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +4 -7
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +20 -27
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +5 -10
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +3 -5
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +55 -72
- package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +3 -7
- 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,53 +1,45 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { Relation } = require('../Relation');
|
|
4
3
|
const { BelongsToOneInsertOperation } = require('./BelongsToOneInsertOperation');
|
|
5
4
|
const { BelongsToOneDeleteOperation } = require('./BelongsToOneDeleteOperation');
|
|
6
5
|
const { BelongsToOneRelateOperation } = require('./BelongsToOneRelateOperation');
|
|
7
6
|
const { BelongsToOneUnrelateOperation } = require('./BelongsToOneUnrelateOperation');
|
|
8
|
-
|
|
9
7
|
class BelongsToOneRelation extends Relation {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
relation: this,
|
|
38
|
-
owner,
|
|
39
|
-
});
|
|
40
|
-
}
|
|
8
|
+
isOneToOne() {
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
insert(_, owner) {
|
|
12
|
+
return new BelongsToOneInsertOperation('insert', {
|
|
13
|
+
relation: this,
|
|
14
|
+
owner,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
delete(_, owner) {
|
|
18
|
+
return new BelongsToOneDeleteOperation('delete', {
|
|
19
|
+
relation: this,
|
|
20
|
+
owner,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
relate(_, owner) {
|
|
24
|
+
return new BelongsToOneRelateOperation('relate', {
|
|
25
|
+
relation: this,
|
|
26
|
+
owner,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
unrelate(_, owner) {
|
|
30
|
+
return new BelongsToOneUnrelateOperation('unrelate', {
|
|
31
|
+
relation: this,
|
|
32
|
+
owner,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
41
35
|
}
|
|
42
|
-
|
|
43
36
|
Object.defineProperties(BelongsToOneRelation.prototype, {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
37
|
+
isObjectionBelongsToOneRelation: {
|
|
38
|
+
enumerable: false,
|
|
39
|
+
writable: false,
|
|
40
|
+
value: true,
|
|
41
|
+
},
|
|
49
42
|
});
|
|
50
|
-
|
|
51
43
|
module.exports = {
|
|
52
|
-
|
|
44
|
+
BelongsToOneRelation,
|
|
53
45
|
};
|
|
@@ -1,61 +1,46 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { UnrelateOperation } = require('../../queryBuilder/operations/UnrelateOperation');
|
|
4
|
-
|
|
5
3
|
class BelongsToOneUnrelateOperation extends UnrelateOperation {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
4
|
+
onAdd() {
|
|
5
|
+
const ids = new Array(this.relation.ownerProp.size);
|
|
6
|
+
this.relation.ownerProp.forEach((i) => {
|
|
7
|
+
ids[i] = null;
|
|
8
|
+
});
|
|
9
|
+
this.ids = [ids];
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
queryExecutor(builder) {
|
|
13
|
+
const patch = {};
|
|
14
|
+
const ownerProp = this.relation.ownerProp;
|
|
15
|
+
ownerProp.forEach((i) => {
|
|
16
|
+
const relatedValue = this.ids[0][i];
|
|
17
|
+
if (this.owner.isModels) {
|
|
18
|
+
for (const owner of this.owner.modelArray) {
|
|
19
|
+
ownerProp.setProp(owner, i, relatedValue);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
ownerProp.patch(patch, i, relatedValue);
|
|
23
|
+
});
|
|
24
|
+
const ownerIdProp = this.relation.ownerModelClass.getIdRelationProperty();
|
|
25
|
+
const ownerRefs = ownerIdProp.refs(builder);
|
|
26
|
+
const ownerIds = this.owner.getProps(this.relation, ownerIdProp);
|
|
27
|
+
const query = this.relation.ownerModelClass
|
|
28
|
+
.query()
|
|
29
|
+
.childQueryOf(builder)
|
|
30
|
+
.patch(patch)
|
|
31
|
+
.whereInComposite(ownerRefs, ownerIds);
|
|
32
|
+
// We are creating a query to the related items. So any `where` statements
|
|
33
|
+
// must filter the *related* items, not the root query above, which is actually
|
|
34
|
+
// for the owners.
|
|
35
|
+
if (builder.has(builder.constructor.WhereSelector)) {
|
|
36
|
+
query.whereExists(this.relation.ownerModelClass
|
|
37
|
+
.relatedQuery(this.relation.name)
|
|
38
|
+
.copyFrom(builder, builder.constructor.JoinSelector)
|
|
39
|
+
.copyFrom(builder, builder.constructor.WhereSelector));
|
|
27
40
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
ownerProp.patch(patch, i, relatedValue);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
const ownerIdProp = this.relation.ownerModelClass.getIdRelationProperty();
|
|
34
|
-
const ownerRefs = ownerIdProp.refs(builder);
|
|
35
|
-
const ownerIds = this.owner.getProps(this.relation, ownerIdProp);
|
|
36
|
-
|
|
37
|
-
const query = this.relation.ownerModelClass
|
|
38
|
-
.query()
|
|
39
|
-
.childQueryOf(builder)
|
|
40
|
-
.patch(patch)
|
|
41
|
-
.whereInComposite(ownerRefs, ownerIds);
|
|
42
|
-
|
|
43
|
-
// We are creating a query to the related items. So any `where` statements
|
|
44
|
-
// must filter the *related* items, not the root query above, which is actually
|
|
45
|
-
// for the owners.
|
|
46
|
-
if (builder.has(builder.constructor.WhereSelector)) {
|
|
47
|
-
query.whereExists(
|
|
48
|
-
this.relation.ownerModelClass
|
|
49
|
-
.relatedQuery(this.relation.name)
|
|
50
|
-
.copyFrom(builder, builder.constructor.JoinSelector)
|
|
51
|
-
.copyFrom(builder, builder.constructor.WhereSelector),
|
|
52
|
-
);
|
|
41
|
+
return query;
|
|
53
42
|
}
|
|
54
|
-
|
|
55
|
-
return query;
|
|
56
|
-
}
|
|
57
43
|
}
|
|
58
|
-
|
|
59
44
|
module.exports = {
|
|
60
|
-
|
|
45
|
+
BelongsToOneUnrelateOperation,
|
|
61
46
|
};
|
|
@@ -1,61 +1,45 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { RelationInsertOperation } = require('../RelationInsertOperation');
|
|
4
|
-
|
|
5
3
|
class HasManyInsertOperation extends RelationInsertOperation {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
4
|
+
onAdd(builder, args) {
|
|
5
|
+
const retVal = super.onAdd(builder, args);
|
|
6
|
+
assertOwnerIsSingleItem(this.owner, this.relation);
|
|
7
|
+
const ownerValues = this.owner.getSplitProps(builder, this.relation);
|
|
8
|
+
const relatedProp = this.relation.relatedProp;
|
|
9
|
+
for (const model of this.models) {
|
|
10
|
+
for (let j = 0, lp = relatedProp.size; j < lp; ++j) {
|
|
11
|
+
relatedProp.setProp(model, j, ownerValues[0][j]);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return retVal;
|
|
17
15
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
16
|
+
async onAfter1(builder, ret) {
|
|
17
|
+
const inserted = await super.onAfter1(builder, ret);
|
|
18
|
+
if (!this.assignResultToOwner) {
|
|
19
|
+
return inserted;
|
|
20
|
+
}
|
|
21
|
+
if (this.owner.isModels) {
|
|
22
|
+
for (const owner of this.owner.modelArray) {
|
|
23
|
+
owner.$appendRelated(this.relation, inserted);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return inserted;
|
|
27
27
|
}
|
|
28
|
-
|
|
29
|
-
if (this.owner.isModels) {
|
|
30
|
-
for (const owner of this.owner.modelArray) {
|
|
31
|
-
owner.$appendRelated(this.relation, inserted);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return inserted;
|
|
36
|
-
}
|
|
37
28
|
}
|
|
38
|
-
|
|
39
29
|
function assertOwnerIsSingleItem(owner, relation) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
'
|
|
51
|
-
|
|
52
|
-
'If you need to insert items for multiple parents, simply loop through them.',
|
|
53
|
-
`That's the most performant way.`,
|
|
54
|
-
].join(' '),
|
|
55
|
-
);
|
|
56
|
-
}
|
|
30
|
+
const { isModels, isIdentifiers, isQueryBuilder } = owner;
|
|
31
|
+
const { ownerProp } = relation;
|
|
32
|
+
const singleModel = isModels && owner.modelArray.length === 1;
|
|
33
|
+
const singleId = isIdentifiers && owner.getNormalizedIdentifiers(ownerProp).length === 1;
|
|
34
|
+
if (!singleModel && !singleId && !isQueryBuilder) {
|
|
35
|
+
throw new Error([
|
|
36
|
+
'Can only insert items for one parent at a time in case of HasManyRelation.',
|
|
37
|
+
'Otherwise multiple insert queries would need to be created.',
|
|
38
|
+
'If you need to insert items for multiple parents, simply loop through them.',
|
|
39
|
+
`That's the most performant way.`,
|
|
40
|
+
].join(' '));
|
|
41
|
+
}
|
|
57
42
|
}
|
|
58
|
-
|
|
59
43
|
module.exports = {
|
|
60
|
-
|
|
44
|
+
HasManyInsertOperation,
|
|
61
45
|
};
|
|
@@ -1,64 +1,48 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { withWhereScope } = require('../../queryBuilder/whereScopes');
|
|
4
3
|
const { normalizeIds } = require('../../utils/normalizeIds');
|
|
5
4
|
const { RelateOperation } = require('../../queryBuilder/operations/RelateOperation');
|
|
6
|
-
|
|
7
5
|
class HasManyRelateOperation extends RelateOperation {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
.copyFrom(builder, builder.constructor.WhereSelector);
|
|
34
|
-
|
|
35
|
-
withWhereScope(query, () => query.findByIds(this.ids));
|
|
36
|
-
withWhereScope(query, () => query.modify(this.relation.modify));
|
|
37
|
-
|
|
38
|
-
return query;
|
|
39
|
-
}
|
|
6
|
+
onAdd(_, args) {
|
|
7
|
+
this.input = args[0];
|
|
8
|
+
this.ids = normalizeIds(args[0], this.relation.relatedModelClass.getIdRelationProperty(), {
|
|
9
|
+
arrayOutput: true,
|
|
10
|
+
});
|
|
11
|
+
assertOwnerIsSingleItem(this.owner, this.relation);
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
queryExecutor(builder) {
|
|
15
|
+
const patch = {};
|
|
16
|
+
const relatedProp = this.relation.relatedProp;
|
|
17
|
+
const ownerValues = this.owner.getSplitProps(builder, this.relation);
|
|
18
|
+
relatedProp.forEach((i) => {
|
|
19
|
+
relatedProp.patch(patch, i, ownerValues[0][i]);
|
|
20
|
+
});
|
|
21
|
+
const query = this.relation.relatedModelClass
|
|
22
|
+
.query()
|
|
23
|
+
.childQueryOf(builder)
|
|
24
|
+
.patch(patch)
|
|
25
|
+
.copyFrom(builder, builder.constructor.JoinSelector)
|
|
26
|
+
.copyFrom(builder, builder.constructor.WhereSelector);
|
|
27
|
+
withWhereScope(query, () => query.findByIds(this.ids));
|
|
28
|
+
withWhereScope(query, () => query.modify(this.relation.modify));
|
|
29
|
+
return query;
|
|
30
|
+
}
|
|
40
31
|
}
|
|
41
|
-
|
|
42
32
|
function assertOwnerIsSingleItem(owner, relation) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
'
|
|
54
|
-
|
|
55
|
-
'If you need to relate items for multiple parents, simply loop through them.',
|
|
56
|
-
`That's the most performant way.`,
|
|
57
|
-
].join(' '),
|
|
58
|
-
);
|
|
59
|
-
}
|
|
33
|
+
const { isModels, isIdentifiers, isQueryBuilder } = owner;
|
|
34
|
+
const { ownerProp } = relation;
|
|
35
|
+
const singleModel = isModels && owner.modelArray.length === 1;
|
|
36
|
+
const singleId = isIdentifiers && owner.getNormalizedIdentifiers(ownerProp).length === 1;
|
|
37
|
+
if (!singleModel && !singleId && !isQueryBuilder) {
|
|
38
|
+
throw new Error([
|
|
39
|
+
'Can only relate items for one parent at a time in case of HasManyRelation.',
|
|
40
|
+
'Otherwise multiple update queries would need to be created.',
|
|
41
|
+
'If you need to relate items for multiple parents, simply loop through them.',
|
|
42
|
+
`That's the most performant way.`,
|
|
43
|
+
].join(' '));
|
|
44
|
+
}
|
|
60
45
|
}
|
|
61
|
-
|
|
62
46
|
module.exports = {
|
|
63
|
-
|
|
47
|
+
HasManyRelateOperation,
|
|
64
48
|
};
|
|
@@ -1,49 +1,41 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { Relation } = require('../Relation');
|
|
4
3
|
const { HasManyInsertOperation } = require('./HasManyInsertOperation');
|
|
5
4
|
const { HasManyRelateOperation } = require('./HasManyRelateOperation');
|
|
6
5
|
const { HasManyUnrelateOperation } = require('./HasManyUnrelateOperation');
|
|
7
|
-
|
|
8
6
|
class HasManyRelation extends Relation {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
setRelateProp(model, values) {
|
|
35
|
-
model.$id(values);
|
|
36
|
-
}
|
|
7
|
+
insert(_, owner) {
|
|
8
|
+
return new HasManyInsertOperation('insert', {
|
|
9
|
+
relation: this,
|
|
10
|
+
owner: owner,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
relate(_, owner) {
|
|
14
|
+
return new HasManyRelateOperation('relate', {
|
|
15
|
+
relation: this,
|
|
16
|
+
owner: owner,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
unrelate(_, owner) {
|
|
20
|
+
return new HasManyUnrelateOperation('unrelate', {
|
|
21
|
+
relation: this,
|
|
22
|
+
owner: owner,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
hasRelateProp(model) {
|
|
26
|
+
return model.$hasId();
|
|
27
|
+
}
|
|
28
|
+
setRelateProp(model, values) {
|
|
29
|
+
model.$id(values);
|
|
30
|
+
}
|
|
37
31
|
}
|
|
38
|
-
|
|
39
32
|
Object.defineProperties(HasManyRelation.prototype, {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
33
|
+
isObjectionHasManyRelation: {
|
|
34
|
+
enumerable: false,
|
|
35
|
+
writable: false,
|
|
36
|
+
value: true,
|
|
37
|
+
},
|
|
45
38
|
});
|
|
46
|
-
|
|
47
39
|
module.exports = {
|
|
48
|
-
|
|
40
|
+
HasManyRelation,
|
|
49
41
|
};
|
|
@@ -1,33 +1,26 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { withWhereScope } = require('../../queryBuilder/whereScopes');
|
|
4
3
|
const { UnrelateOperation } = require('../../queryBuilder/operations/UnrelateOperation');
|
|
5
|
-
|
|
6
4
|
class HasManyUnrelateOperation extends UnrelateOperation {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
withWhereScope(query, () => query.modify(this.relation.modify));
|
|
26
|
-
|
|
27
|
-
return query;
|
|
28
|
-
}
|
|
5
|
+
queryExecutor(builder) {
|
|
6
|
+
const patch = {};
|
|
7
|
+
const relatedProp = this.relation.relatedProp;
|
|
8
|
+
const ownerValues = this.owner.getProps(this.relation);
|
|
9
|
+
const relatedRefs = relatedProp.refs(builder);
|
|
10
|
+
relatedProp.forEach((i) => {
|
|
11
|
+
relatedProp.patch(patch, i, null);
|
|
12
|
+
});
|
|
13
|
+
const query = this.relation.relatedModelClass
|
|
14
|
+
.query()
|
|
15
|
+
.childQueryOf(builder)
|
|
16
|
+
.patch(patch)
|
|
17
|
+
.copyFrom(builder, builder.constructor.JoinSelector)
|
|
18
|
+
.copyFrom(builder, builder.constructor.WhereSelector);
|
|
19
|
+
withWhereScope(query, () => query.whereInComposite(relatedRefs, ownerValues));
|
|
20
|
+
withWhereScope(query, () => query.modify(this.relation.modify));
|
|
21
|
+
return query;
|
|
22
|
+
}
|
|
29
23
|
}
|
|
30
|
-
|
|
31
24
|
module.exports = {
|
|
32
|
-
|
|
25
|
+
HasManyUnrelateOperation,
|
|
33
26
|
};
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { HasManyRelation } = require('../hasMany/HasManyRelation');
|
|
4
|
-
|
|
5
3
|
class HasOneRelation extends HasManyRelation {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
isOneToOne() {
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
9
7
|
}
|
|
10
|
-
|
|
11
8
|
module.exports = {
|
|
12
|
-
|
|
9
|
+
HasOneRelation,
|
|
13
10
|
};
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { ManyToManyRelation } = require('../manyToMany/ManyToManyRelation');
|
|
4
|
-
|
|
5
3
|
class HasOneThroughRelation extends ManyToManyRelation {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
isOneToOne() {
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
9
7
|
}
|
|
10
|
-
|
|
11
8
|
module.exports = {
|
|
12
|
-
|
|
9
|
+
HasOneThroughRelation,
|
|
13
10
|
};
|