@abedmurrar/objection 0.0.0-stage → 4.0.0-alpha.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/LICENSE +23 -0
- package/README.md +69 -2
- package/lib/.eslintrc.json +8 -0
- package/lib/initialize.js +14 -0
- package/lib/model/AjvValidator.js +325 -0
- package/lib/model/Model.js +942 -0
- package/lib/model/ModifierNotFoundError.js +12 -0
- package/lib/model/NotFoundError.js +23 -0
- package/lib/model/RelationDoesNotExistError.js +14 -0
- package/lib/model/ValidationError.js +54 -0
- package/lib/model/Validator.js +26 -0
- package/lib/model/getModel.js +9 -0
- package/lib/model/graph/ModelGraph.js +80 -0
- package/lib/model/graph/ModelGraphBuilder.js +241 -0
- package/lib/model/graph/ModelGraphEdge.js +52 -0
- package/lib/model/graph/ModelGraphNode.js +240 -0
- package/lib/model/inheritModel.js +28 -0
- package/lib/model/modelBindKnex.js +83 -0
- package/lib/model/modelClone.js +121 -0
- package/lib/model/modelColPropMap.js +34 -0
- package/lib/model/modelId.js +62 -0
- package/lib/model/modelJsonAttributes.js +92 -0
- package/lib/model/modelParseRelations.js +82 -0
- package/lib/model/modelQueryProps.js +147 -0
- package/lib/model/modelSet.js +136 -0
- package/lib/model/modelTableMetadata.js +83 -0
- package/lib/model/modelToJson.js +187 -0
- package/lib/model/modelUtils.js +47 -0
- package/lib/model/modelValidate.js +48 -0
- package/lib/model/modelValues.js +126 -0
- package/lib/model/modelVisitor.js +41 -0
- package/lib/objection.js +114 -0
- package/lib/queryBuilder/FunctionBuilder.js +339 -0
- package/lib/queryBuilder/InternalOptions.js +29 -0
- package/lib/queryBuilder/JoinBuilder.js +157 -0
- package/lib/queryBuilder/QueryBuilder.js +1398 -0
- package/lib/queryBuilder/QueryBuilderBase.js +884 -0
- package/lib/queryBuilder/QueryBuilderContext.js +27 -0
- package/lib/queryBuilder/QueryBuilderContextBase.js +35 -0
- package/lib/queryBuilder/QueryBuilderOperationSupport.js +558 -0
- package/lib/queryBuilder/QueryBuilderUserContext.js +31 -0
- package/lib/queryBuilder/RawBuilder.js +93 -0
- package/lib/queryBuilder/ReferenceBuilder.js +284 -0
- package/lib/queryBuilder/RelationExpression.js +431 -0
- package/lib/queryBuilder/StaticHookArguments.js +134 -0
- package/lib/queryBuilder/ValueBuilder.js +127 -0
- package/lib/queryBuilder/expressions.js +151 -0
- package/lib/queryBuilder/functions/mysql.js +817 -0
- package/lib/queryBuilder/graph/GraphAction.js +69 -0
- package/lib/queryBuilder/graph/GraphData.js +14 -0
- package/lib/queryBuilder/graph/GraphFetcher.js +191 -0
- package/lib/queryBuilder/graph/GraphNodeDbExistence.js +184 -0
- package/lib/queryBuilder/graph/GraphOperation.js +27 -0
- package/lib/queryBuilder/graph/GraphOptions.js +207 -0
- package/lib/queryBuilder/graph/GraphUpsert.js +305 -0
- package/lib/queryBuilder/graph/delete/GraphDelete.js +20 -0
- package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +74 -0
- package/lib/queryBuilder/graph/insert/GraphInsert.js +145 -0
- package/lib/queryBuilder/graph/insert/GraphInsertAction.js +225 -0
- package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +92 -0
- package/lib/queryBuilder/graph/patch/GraphPatch.js +20 -0
- package/lib/queryBuilder/graph/patch/GraphPatchAction.js +258 -0
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +31 -0
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +126 -0
- package/lib/queryBuilder/join/JoinResultColumn.js +23 -0
- package/lib/queryBuilder/join/JoinResultParser.js +119 -0
- package/lib/queryBuilder/join/RelationJoiner.js +341 -0
- package/lib/queryBuilder/join/TableNode.js +163 -0
- package/lib/queryBuilder/join/TableTree.js +85 -0
- package/lib/queryBuilder/join/utils.js +25 -0
- package/lib/queryBuilder/operations/DelegateOperation.js +141 -0
- package/lib/queryBuilder/operations/DeleteOperation.js +51 -0
- package/lib/queryBuilder/operations/FindByIdOperation.js +37 -0
- package/lib/queryBuilder/operations/FindByIdsOperation.js +29 -0
- package/lib/queryBuilder/operations/FindOperation.js +164 -0
- package/lib/queryBuilder/operations/FirstOperation.js +27 -0
- package/lib/queryBuilder/operations/FromOperation.js +110 -0
- package/lib/queryBuilder/operations/InsertAndFetchOperation.js +49 -0
- package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +45 -0
- package/lib/queryBuilder/operations/InsertGraphOperation.js +108 -0
- package/lib/queryBuilder/operations/InsertOperation.js +206 -0
- package/lib/queryBuilder/operations/InstanceDeleteOperation.js +46 -0
- package/lib/queryBuilder/operations/InstanceFindOperation.js +21 -0
- package/lib/queryBuilder/operations/InstanceInsertOperation.js +24 -0
- package/lib/queryBuilder/operations/InstanceUpdateOperation.js +59 -0
- package/lib/queryBuilder/operations/JoinRelatedOperation.js +103 -0
- package/lib/queryBuilder/operations/KnexOperation.js +14 -0
- package/lib/queryBuilder/operations/MergeOperation.js +56 -0
- package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +192 -0
- package/lib/queryBuilder/operations/OnBuildKnexOperation.js +29 -0
- package/lib/queryBuilder/operations/OnBuildOperation.js +29 -0
- package/lib/queryBuilder/operations/OnErrorOperation.js +29 -0
- package/lib/queryBuilder/operations/QueryBuilderOperation.js +297 -0
- package/lib/queryBuilder/operations/RangeOperation.js +47 -0
- package/lib/queryBuilder/operations/RelateOperation.js +27 -0
- package/lib/queryBuilder/operations/ReturningOperation.js +30 -0
- package/lib/queryBuilder/operations/RunAfterOperation.js +29 -0
- package/lib/queryBuilder/operations/RunBeforeOperation.js +29 -0
- package/lib/queryBuilder/operations/UnrelateOperation.js +23 -0
- package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +80 -0
- package/lib/queryBuilder/operations/UpdateOperation.js +155 -0
- package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +45 -0
- package/lib/queryBuilder/operations/UpsertGraphOperation.js +66 -0
- package/lib/queryBuilder/operations/WhereCompositeOperation.js +57 -0
- package/lib/queryBuilder/operations/WhereInOperation.js +48 -0
- package/lib/queryBuilder/operations/eager/EagerOperation.js +61 -0
- package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +51 -0
- package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +13 -0
- package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +285 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +36 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +53 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +31 -0
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +31 -0
- package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +167 -0
- package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +164 -0
- package/lib/queryBuilder/operations/select/SelectOperation.js +66 -0
- package/lib/queryBuilder/operations/select/Selection.js +123 -0
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +117 -0
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +100 -0
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +104 -0
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +886 -0
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.pegjs +97 -0
- package/lib/queryBuilder/parsers/relationExpressionParser.js +1042 -0
- package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +141 -0
- package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +22 -0
- package/lib/queryBuilder/transformations/QueryTransformation.js +11 -0
- package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +71 -0
- package/lib/queryBuilder/transformations/index.js +15 -0
- package/lib/queryBuilder/whereScopes.js +227 -0
- package/lib/relations/Relation.js +401 -0
- package/lib/relations/RelationDeleteOperation.js +29 -0
- package/lib/relations/RelationFindOperation.js +149 -0
- package/lib/relations/RelationInsertOperation.js +33 -0
- package/lib/relations/RelationOwner.js +236 -0
- package/lib/relations/RelationProperty.js +247 -0
- package/lib/relations/RelationUpdateOperation.js +30 -0
- package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +23 -0
- package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +44 -0
- package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +50 -0
- package/lib/relations/belongsToOne/BelongsToOneRelation.js +53 -0
- package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +61 -0
- package/lib/relations/hasMany/HasManyInsertOperation.js +61 -0
- package/lib/relations/hasMany/HasManyRelateOperation.js +64 -0
- package/lib/relations/hasMany/HasManyRelation.js +49 -0
- package/lib/relations/hasMany/HasManyUnrelateOperation.js +33 -0
- package/lib/relations/hasOne/HasOneRelation.js +13 -0
- package/lib/relations/hasOneThrough/HasOneThroughRelation.js +13 -0
- package/lib/relations/manyToMany/ManyToManyModifyMixin.js +105 -0
- package/lib/relations/manyToMany/ManyToManyRelation.js +507 -0
- package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +36 -0
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +10 -0
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +26 -0
- package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +12 -0
- package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +105 -0
- package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +52 -0
- package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +81 -0
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +14 -0
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +34 -0
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +16 -0
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +10 -0
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +82 -0
- package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +12 -0
- package/lib/transaction.js +96 -0
- package/lib/utils/assert.js +25 -0
- package/lib/utils/buildUtils.js +21 -0
- package/lib/utils/classUtils.js +13 -0
- package/lib/utils/clone.js +2329 -0
- package/lib/utils/createModifier.js +42 -0
- package/lib/utils/deprecate.js +15 -0
- package/lib/utils/identifierMapping.js +227 -0
- package/lib/utils/internalPropUtils.js +11 -0
- package/lib/utils/knexUtils.js +76 -0
- package/lib/utils/mixin.js +25 -0
- package/lib/utils/mysqlJsonPath.js +55 -0
- package/lib/utils/normalizeIds.js +144 -0
- package/lib/utils/objectUtils.js +416 -0
- package/lib/utils/parseFieldExpression.js +51 -0
- package/lib/utils/postgresJsonPath.js +110 -0
- package/lib/utils/promiseUtils/after.js +17 -0
- package/lib/utils/promiseUtils/afterReturn.js +17 -0
- package/lib/utils/promiseUtils/index.js +17 -0
- package/lib/utils/promiseUtils/isPromise.js +11 -0
- package/lib/utils/promiseUtils/map.js +110 -0
- package/lib/utils/promiseUtils/mapAfterAllReturn.js +29 -0
- package/lib/utils/promiseUtils/try.js +22 -0
- package/lib/utils/resolveModel.js +107 -0
- package/lib/utils/tmpColumnUtils.js +16 -0
- package/package.json +86 -4
- package/typings/objection/index.d.ts +4104 -0
- package/typings/objection/relation-expression.d.ts +237 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { RelationInsertOperation } = require('../../RelationInsertOperation');
|
|
4
|
+
const { ManyToManyRelateOperation } = require('../relate/ManyToManyRelateOperation');
|
|
5
|
+
|
|
6
|
+
class ManyToManyInsertOperation extends RelationInsertOperation {
|
|
7
|
+
onAdd(builder, args) {
|
|
8
|
+
const retVal = super.onAdd(builder, args);
|
|
9
|
+
|
|
10
|
+
// Omit extra properties so that we don't try to insert
|
|
11
|
+
// them to the related table. We don't actually remove them
|
|
12
|
+
// from the objects, but simply mark them to be removed
|
|
13
|
+
// from the inserted row.
|
|
14
|
+
this.relation.omitExtraProps(this.models);
|
|
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
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (modelsToRelate.length === 0) {
|
|
40
|
+
return inserted;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
relateOp.onAdd(builder, [modelsToRelate]);
|
|
44
|
+
await relateOp.queryExecutor(builder);
|
|
45
|
+
|
|
46
|
+
return inserted;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports = {
|
|
51
|
+
ManyToManyInsertOperation,
|
|
52
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { isPostgres } = require('../../../utils/knexUtils');
|
|
4
|
+
const { normalizeIds } = require('../../../utils/normalizeIds');
|
|
5
|
+
const { RelateOperation } = require('../../../queryBuilder/operations/RelateOperation');
|
|
6
|
+
|
|
7
|
+
class ManyToManyRelateOperation extends RelateOperation {
|
|
8
|
+
onAdd(builder, args) {
|
|
9
|
+
this.input = args[0];
|
|
10
|
+
this.ids = normalizeIds(args[0], this.relation.relatedProp);
|
|
11
|
+
|
|
12
|
+
assertOwnerIsSingleItem(builder, this.owner, this.relation);
|
|
13
|
+
return true;
|
|
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
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return joinModelClass
|
|
30
|
+
.query()
|
|
31
|
+
.childQueryOf(builder)
|
|
32
|
+
.modify((query) => {
|
|
33
|
+
if (!this.opt.dontCopyReturning) {
|
|
34
|
+
query.copyFrom(builder, /returning/);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (!this.opt.dontCopyOnConflict) {
|
|
38
|
+
query.copyFrom(builder, /onConflict|ignore|merge/);
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
.runBefore((_, builder) => {
|
|
42
|
+
return this.relation.executeJoinTableBeforeInsert(joinModels, builder.context(), null);
|
|
43
|
+
})
|
|
44
|
+
.insert(joinModels)
|
|
45
|
+
.runAfter((models) => {
|
|
46
|
+
return Array.isArray(models) ? models.length : 1;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function assertOwnerIsSingleItem(builder, owner, relation) {
|
|
52
|
+
const { isModels, isIdentifiers, isQueryBuilder } = owner;
|
|
53
|
+
const { ownerProp } = relation;
|
|
54
|
+
|
|
55
|
+
const singleModel = isModels && owner.modelArray.length === 1;
|
|
56
|
+
const singleId = isIdentifiers && owner.getNormalizedIdentifiers(ownerProp).length === 1;
|
|
57
|
+
|
|
58
|
+
if (isPostgres(builder.unsafeKnex())) {
|
|
59
|
+
if (!isModels && !isIdentifiers && !isQueryBuilder) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
'Parent must be a list of identifiers or a list of models when relating a ManyToManyRelation',
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
if (!singleModel && !singleId && !isQueryBuilder) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
[
|
|
68
|
+
'Can only relate items for one parent at a time in case of ManyToManyRelation.',
|
|
69
|
+
'Otherwise multiple insert queries would need to be created.',
|
|
70
|
+
|
|
71
|
+
'If you need to relate items for multiple parents, simply loop through them.',
|
|
72
|
+
`That's the most performant way.`,
|
|
73
|
+
].join(' '),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = {
|
|
80
|
+
ManyToManyRelateOperation,
|
|
81
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { ManyToManyUnrelateOperationBase } = require('./ManyToManyUnrelateOperationBase');
|
|
4
|
+
const { ManyToManyModifyMixin } = require('../ManyToManyModifyMixin');
|
|
5
|
+
|
|
6
|
+
class ManyToManyUnrelateOperation extends ManyToManyModifyMixin(ManyToManyUnrelateOperationBase) {
|
|
7
|
+
get modifyMainQuery() {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = {
|
|
13
|
+
ManyToManyUnrelateOperation,
|
|
14
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { withWhereScope } = require('../../../queryBuilder/whereScopes');
|
|
4
|
+
const { UnrelateOperation } = require('../../../queryBuilder/operations/UnrelateOperation');
|
|
5
|
+
|
|
6
|
+
class ManyToManyUnrelateOperationBase extends UnrelateOperation {
|
|
7
|
+
queryExecutor(builder) {
|
|
8
|
+
const unrelateQuery = this.relation
|
|
9
|
+
.getJoinModelClass(builder.knex())
|
|
10
|
+
.query()
|
|
11
|
+
.childQueryOf(builder)
|
|
12
|
+
.delete();
|
|
13
|
+
|
|
14
|
+
withWhereScope(unrelateQuery, () => {
|
|
15
|
+
this.applyModifyFilterForJoinTable(unrelateQuery);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
return unrelateQuery.modify(this.relation.joinTableModify);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/* istanbul ignore next */
|
|
22
|
+
applyModifyFilterForRelatedTable(builder) {
|
|
23
|
+
throw new Error('not implemented');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/* istanbul ignore next */
|
|
27
|
+
applyModifyFilterForJoinTable(builder) {
|
|
28
|
+
throw new Error('not implemented');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = {
|
|
33
|
+
ManyToManyUnrelateOperationBase,
|
|
34
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { ManyToManyUnrelateOperationBase } = require('./ManyToManyUnrelateOperationBase');
|
|
4
|
+
const { ManyToManySqliteModifyMixin } = require('../ManyToManySqliteModifyMixin');
|
|
5
|
+
|
|
6
|
+
class ManyToManyUnrelateSqliteOperation extends ManyToManySqliteModifyMixin(
|
|
7
|
+
ManyToManyUnrelateOperationBase,
|
|
8
|
+
) {
|
|
9
|
+
get modifyMainQuery() {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = {
|
|
15
|
+
ManyToManyUnrelateSqliteOperation,
|
|
16
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { ManyToManyUpdateOperationBase } = require('./ManyToManyUpdateOperationBase');
|
|
4
|
+
const { ManyToManyModifyMixin } = require('../ManyToManyModifyMixin');
|
|
5
|
+
|
|
6
|
+
class ManyToManyUpdateOperation extends ManyToManyModifyMixin(ManyToManyUpdateOperationBase) {}
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
ManyToManyUpdateOperation,
|
|
10
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { withWhereScope } = require('../../../queryBuilder/whereScopes');
|
|
4
|
+
const { UpdateOperation } = require('../../../queryBuilder/operations/UpdateOperation');
|
|
5
|
+
|
|
6
|
+
class ManyToManyUpdateOperationBase extends UpdateOperation {
|
|
7
|
+
constructor(name, opt) {
|
|
8
|
+
super(name, opt);
|
|
9
|
+
|
|
10
|
+
this.relation = opt.relation;
|
|
11
|
+
this.owner = opt.owner;
|
|
12
|
+
|
|
13
|
+
this.hasExtraProps = false;
|
|
14
|
+
this.joinTablePatch = {};
|
|
15
|
+
this.joinTablePatchFilterQuery = null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
onAdd(builder, args) {
|
|
19
|
+
const obj = args[0];
|
|
20
|
+
|
|
21
|
+
// Copy all extra properties to the `joinTablePatch` object.
|
|
22
|
+
for (const extra of this.relation.joinTableExtras) {
|
|
23
|
+
if (extra.aliasProp in obj) {
|
|
24
|
+
this.hasExtraProps = true;
|
|
25
|
+
this.joinTablePatch[extra.joinTableProp] = obj[extra.aliasProp];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const res = super.onAdd(builder, args);
|
|
30
|
+
|
|
31
|
+
if (this.hasExtraProps) {
|
|
32
|
+
// Make sure we don't try to insert the extra properties
|
|
33
|
+
// to the target table.
|
|
34
|
+
this.relation.omitExtraProps([this.model]);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return res;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async onAfter1(builder, result) {
|
|
41
|
+
if (this.hasExtraProps) {
|
|
42
|
+
const joinTableUpdateQuery = this.relation
|
|
43
|
+
.getJoinModelClass(builder.knex())
|
|
44
|
+
.query()
|
|
45
|
+
.childQueryOf(builder)
|
|
46
|
+
.patch(this.joinTablePatch);
|
|
47
|
+
|
|
48
|
+
withWhereScope(joinTableUpdateQuery, () => {
|
|
49
|
+
this.applyModifyFilterForJoinTable(joinTableUpdateQuery);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
await joinTableUpdateQuery.modify(this.relation.joinTableModify);
|
|
53
|
+
return result;
|
|
54
|
+
} else {
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* istanbul ignore next */
|
|
60
|
+
applyModifyFilterForRelatedTable(builder) {
|
|
61
|
+
throw new Error('not implemented');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/* istanbul ignore next */
|
|
65
|
+
applyModifyFilterForJoinTable(builder) {
|
|
66
|
+
throw new Error('not implemented');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
clone() {
|
|
70
|
+
const clone = super.clone();
|
|
71
|
+
|
|
72
|
+
clone.hasExtraProps = this.hasExtraProps;
|
|
73
|
+
clone.joinTablePatch = this.joinTablePatch;
|
|
74
|
+
clone.joinTablePatchFilterQuery = this.joinTablePatchFilterQuery;
|
|
75
|
+
|
|
76
|
+
return clone;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
module.exports = {
|
|
81
|
+
ManyToManyUpdateOperationBase,
|
|
82
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { ManyToManyUpdateOperationBase } = require('./ManyToManyUpdateOperationBase');
|
|
4
|
+
const { ManyToManySqliteModifyMixin } = require('../ManyToManySqliteModifyMixin');
|
|
5
|
+
|
|
6
|
+
class ManyToManyUpdateSqliteOperation extends ManyToManySqliteModifyMixin(
|
|
7
|
+
ManyToManyUpdateOperationBase,
|
|
8
|
+
) {}
|
|
9
|
+
|
|
10
|
+
module.exports = {
|
|
11
|
+
ManyToManyUpdateSqliteOperation,
|
|
12
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const promiseUtils = require('./utils/promiseUtils');
|
|
4
|
+
const { isFunction } = require('./utils/objectUtils');
|
|
5
|
+
|
|
6
|
+
function transaction() {
|
|
7
|
+
// There must be at least one model class and the callback.
|
|
8
|
+
if (arguments.length < 2) {
|
|
9
|
+
return Promise.reject(
|
|
10
|
+
new Error(
|
|
11
|
+
'objection.transaction: provide at least one Model class to bind to the transaction or a knex instance',
|
|
12
|
+
),
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let args = Array.from(arguments);
|
|
17
|
+
|
|
18
|
+
if (!isModelClass(args[0]) && isFunction(args[0].transaction)) {
|
|
19
|
+
let knex = args[0];
|
|
20
|
+
args = args.slice(1);
|
|
21
|
+
|
|
22
|
+
return knex.transaction.apply(knex, args);
|
|
23
|
+
} else {
|
|
24
|
+
// The last argument should be the callback and all other Model subclasses.
|
|
25
|
+
let callback = args[args.length - 1];
|
|
26
|
+
let modelClasses = args.slice(0, args.length - 1);
|
|
27
|
+
let i;
|
|
28
|
+
|
|
29
|
+
for (i = 0; i < modelClasses.length; ++i) {
|
|
30
|
+
if (!isModelClass(modelClasses[i])) {
|
|
31
|
+
return Promise.reject(
|
|
32
|
+
new Error('objection.transaction: all but the last argument should be Model subclasses'),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let knex = modelClasses[0].knex();
|
|
38
|
+
for (i = 0; i < modelClasses.length; ++i) {
|
|
39
|
+
if (modelClasses[i].knex() !== knex) {
|
|
40
|
+
return Promise.reject(
|
|
41
|
+
new Error(
|
|
42
|
+
'objection.transaction: all Model subclasses must be bound to the same database',
|
|
43
|
+
),
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return knex.transaction((trx) => {
|
|
49
|
+
let args = new Array(modelClasses.length + 1);
|
|
50
|
+
|
|
51
|
+
for (let i = 0; i < modelClasses.length; ++i) {
|
|
52
|
+
args[i] = modelClasses[i].bindTransaction(trx);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
args[args.length - 1] = trx;
|
|
56
|
+
|
|
57
|
+
return promiseUtils.try(() => {
|
|
58
|
+
return callback.apply(trx, args);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
transaction.start = function (modelClassOrKnex) {
|
|
65
|
+
let knex = modelClassOrKnex;
|
|
66
|
+
|
|
67
|
+
if (isModelClass(modelClassOrKnex)) {
|
|
68
|
+
knex = modelClassOrKnex.knex();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!knex || !isFunction(knex.transaction)) {
|
|
72
|
+
return Promise.reject(
|
|
73
|
+
new Error(
|
|
74
|
+
'objection.transaction.start: first argument must be a model class or a knex instance',
|
|
75
|
+
),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return new Promise((resolve, reject) => {
|
|
80
|
+
knex
|
|
81
|
+
.transaction((trx) => {
|
|
82
|
+
resolve(trx);
|
|
83
|
+
})
|
|
84
|
+
.catch((err) => {
|
|
85
|
+
reject(err);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
function isModelClass(maybeModel) {
|
|
91
|
+
return isFunction(maybeModel) && maybeModel.isObjectionModelClass;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = {
|
|
95
|
+
transaction,
|
|
96
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function assertHasId(model) {
|
|
4
|
+
if (!model.$hasId()) {
|
|
5
|
+
const modelClass = model.constructor;
|
|
6
|
+
const ids = modelClass.getIdColumnArray().join(', ');
|
|
7
|
+
|
|
8
|
+
throw new Error(
|
|
9
|
+
`one of the identifier columns [${ids}] is null or undefined. Have you specified the correct identifier column for the model '${modelClass.name}' using the 'idColumn' property?`,
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function assertIdNotUndefined(id, message) {
|
|
15
|
+
if (Array.isArray(id)) {
|
|
16
|
+
id.forEach((id) => assertIdNotUndefined(id, message));
|
|
17
|
+
} else if (id === undefined) {
|
|
18
|
+
throw Error(message);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = {
|
|
23
|
+
assertHasId,
|
|
24
|
+
assertIdNotUndefined,
|
|
25
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { isObject, isFunction } = require('./objectUtils');
|
|
4
|
+
|
|
5
|
+
function buildArg(arg, builder) {
|
|
6
|
+
if (!isObject(arg)) {
|
|
7
|
+
return arg;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (isFunction(arg.toKnexRaw)) {
|
|
11
|
+
return arg.toKnexRaw(builder);
|
|
12
|
+
} else if (arg.isObjectionQueryBuilderBase === true) {
|
|
13
|
+
return arg.subqueryOf(builder).toKnexQuery();
|
|
14
|
+
} else {
|
|
15
|
+
return arg;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = {
|
|
20
|
+
buildArg,
|
|
21
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function inherit(Constructor, BaseConstructor) {
|
|
4
|
+
Constructor.prototype = Object.create(BaseConstructor.prototype);
|
|
5
|
+
Constructor.prototype.constructor = BaseConstructor;
|
|
6
|
+
Object.setPrototypeOf(Constructor, BaseConstructor);
|
|
7
|
+
|
|
8
|
+
return Constructor;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
module.exports = {
|
|
12
|
+
inherit,
|
|
13
|
+
};
|