objection 2.1.6 → 2.2.3
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/model/Model.js +16 -4
- package/lib/queryBuilder/InternalOptions.js +2 -0
- package/lib/queryBuilder/QueryBuilder.js +18 -6
- package/lib/queryBuilder/QueryBuilderBase.js +4 -0
- package/lib/queryBuilder/graph/insert/GraphInsertAction.js +5 -5
- package/lib/queryBuilder/graph/patch/GraphPatchAction.js +4 -4
- package/lib/queryBuilder/operations/FindByIdOperation.js +5 -0
- package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +4 -4
- package/lib/relations/Relation.js +4 -4
- package/lib/relations/RelationProperty.js +6 -0
- package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +2 -2
- package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +2 -2
- package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +4 -4
- package/lib/relations/hasMany/HasManyRelateOperation.js +2 -2
- package/lib/relations/hasMany/HasManyUnrelateOperation.js +2 -2
- package/lib/relations/manyToMany/ManyToManyRelation.js +13 -9
- package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +2 -2
- package/lib/utils/assert.js +10 -1
- package/package.json +3 -3
- package/typings/objection/index.d.ts +58 -50
package/lib/model/Model.js
CHANGED
|
@@ -886,12 +886,24 @@ function getIdRelationProperty(modelClass) {
|
|
|
886
886
|
}
|
|
887
887
|
|
|
888
888
|
function getReadOnlyAttributes(modelClass) {
|
|
889
|
+
return [...new Set(getReadOnlyAttributesRecursively(modelClass))];
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
function getReadOnlyAttributesRecursively(modelClass) {
|
|
893
|
+
if (modelClass === Model) {
|
|
894
|
+
// Stop recursion to the model class.
|
|
895
|
+
return [];
|
|
896
|
+
}
|
|
897
|
+
|
|
889
898
|
const propertyNames = Object.getOwnPropertyNames(modelClass.prototype);
|
|
890
899
|
|
|
891
|
-
return
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
900
|
+
return [
|
|
901
|
+
...getReadOnlyAttributes(Object.getPrototypeOf(modelClass)),
|
|
902
|
+
...propertyNames.filter(propName => {
|
|
903
|
+
const desc = Object.getOwnPropertyDescriptor(modelClass.prototype, propName);
|
|
904
|
+
return (desc.get && !desc.set) || desc.writable === false || isFunction(desc.value);
|
|
905
|
+
})
|
|
906
|
+
];
|
|
895
907
|
}
|
|
896
908
|
|
|
897
909
|
function getRelationMappings(modelClass) {
|
|
@@ -6,6 +6,7 @@ class InternalOptions {
|
|
|
6
6
|
this.keepImplicitJoinProps = false;
|
|
7
7
|
this.isInternalQuery = false;
|
|
8
8
|
this.debug = false;
|
|
9
|
+
this.schema = undefined;
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
clone() {
|
|
@@ -15,6 +16,7 @@ class InternalOptions {
|
|
|
15
16
|
copy.keepImplicitJoinProps = this.keepImplicitJoinProps;
|
|
16
17
|
copy.isInternalQuery = this.isInternalQuery;
|
|
17
18
|
copy.debug = this.debug;
|
|
19
|
+
copy.schema = this.schema;
|
|
18
20
|
|
|
19
21
|
return copy;
|
|
20
22
|
}
|
|
@@ -8,6 +8,8 @@ const { createModifier } = require('../utils/createModifier');
|
|
|
8
8
|
const { ValidationErrorType } = require('../model/ValidationError');
|
|
9
9
|
const { isObject, isString, isFunction, last, flatten } = require('../utils/objectUtils');
|
|
10
10
|
const { RelationExpression, DuplicateRelationError } = require('./RelationExpression');
|
|
11
|
+
const { assertIdNotUndefined } = require('../utils/assert');
|
|
12
|
+
|
|
11
13
|
const { Selection } = require('./operations/select/Selection');
|
|
12
14
|
|
|
13
15
|
const { QueryBuilderContext } = require('./QueryBuilderContext');
|
|
@@ -682,7 +684,7 @@ class QueryBuilder extends QueryBuilderBase {
|
|
|
682
684
|
const result = await doExecute(builder);
|
|
683
685
|
return await afterExecute(builder, result);
|
|
684
686
|
} catch (error) {
|
|
685
|
-
await handleExecuteError(builder, error);
|
|
687
|
+
return await handleExecuteError(builder, error);
|
|
686
688
|
}
|
|
687
689
|
}
|
|
688
690
|
|
|
@@ -700,7 +702,7 @@ class QueryBuilder extends QueryBuilderBase {
|
|
|
700
702
|
});
|
|
701
703
|
}
|
|
702
704
|
|
|
703
|
-
throwIfNotFound() {
|
|
705
|
+
throwIfNotFound(data = {}) {
|
|
704
706
|
return this.runAfter(result => {
|
|
705
707
|
if (
|
|
706
708
|
(Array.isArray(result) && result.length === 0) ||
|
|
@@ -708,7 +710,7 @@ class QueryBuilder extends QueryBuilderBase {
|
|
|
708
710
|
result === undefined ||
|
|
709
711
|
result === 0
|
|
710
712
|
) {
|
|
711
|
-
throw this.modelClass().createNotFoundError(this.context());
|
|
713
|
+
throw this.modelClass().createNotFoundError(this.context(), data);
|
|
712
714
|
} else {
|
|
713
715
|
return result;
|
|
714
716
|
}
|
|
@@ -842,8 +844,11 @@ class QueryBuilder extends QueryBuilderBase {
|
|
|
842
844
|
const knex = this.knex();
|
|
843
845
|
const tableParts = table.split('.');
|
|
844
846
|
const columnInfoQuery = knex(last(tableParts)).columnInfo();
|
|
847
|
+
const schema = this.internalOptions().schema;
|
|
845
848
|
|
|
846
|
-
if (
|
|
849
|
+
if (schema) {
|
|
850
|
+
columnInfoQuery.withSchema(schema);
|
|
851
|
+
} else if (tableParts.length > 1) {
|
|
847
852
|
columnInfoQuery.withSchema(tableParts[0]);
|
|
848
853
|
}
|
|
849
854
|
|
|
@@ -855,6 +860,7 @@ class QueryBuilder extends QueryBuilderBase {
|
|
|
855
860
|
}
|
|
856
861
|
|
|
857
862
|
withSchema(schema) {
|
|
863
|
+
this.internalOptions().schema = schema;
|
|
858
864
|
this.internalContext().onBuild.push(builder => {
|
|
859
865
|
if (!builder.has(/withSchema/)) {
|
|
860
866
|
// Need to push this operation to the front because knex doesn't use the
|
|
@@ -1245,8 +1251,14 @@ class QueryBuilder extends QueryBuilderBase {
|
|
|
1245
1251
|
return this.rightOuterJoinRelated(...args);
|
|
1246
1252
|
}
|
|
1247
1253
|
|
|
1248
|
-
deleteById(
|
|
1249
|
-
return this.findById(
|
|
1254
|
+
deleteById(id) {
|
|
1255
|
+
return this.findById(id)
|
|
1256
|
+
.delete()
|
|
1257
|
+
.runBefore((_, builder) => {
|
|
1258
|
+
if (!builder.internalOptions().skipUndefined) {
|
|
1259
|
+
assertIdNotUndefined(id, `undefined was passed to deleteById`);
|
|
1260
|
+
}
|
|
1261
|
+
});
|
|
1250
1262
|
}
|
|
1251
1263
|
|
|
1252
1264
|
findById(...args) {
|
|
@@ -172,6 +172,10 @@ class QueryBuilderBase extends QueryBuilderOperationSupport {
|
|
|
172
172
|
return this.addOperation(new KnexOperation('whereNot'), args);
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
+
andWhereNot(...args) {
|
|
176
|
+
return this.addOperation(new KnexOperation('andWhereNot'), args);
|
|
177
|
+
}
|
|
178
|
+
|
|
175
179
|
orWhereNot(...args) {
|
|
176
180
|
return this.addOperation(new KnexOperation('orWhereNot'), args);
|
|
177
181
|
}
|
|
@@ -96,9 +96,9 @@ class GraphInsertAction extends GraphAction {
|
|
|
96
96
|
|
|
97
97
|
this._resolveReferences(dependencyNode);
|
|
98
98
|
|
|
99
|
-
|
|
99
|
+
targetProp.forEach(i => {
|
|
100
100
|
targetProp.setProp(dependentNode.obj, i, sourceProp.getProp(dependencyNode.obj, i));
|
|
101
|
-
}
|
|
101
|
+
});
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
_resolvePropertyReferenceNode(dependencyNode, edge) {
|
|
@@ -152,15 +152,15 @@ class GraphInsertAction extends GraphAction {
|
|
|
152
152
|
|
|
153
153
|
const prop = edge.isOwnerNode(node) ? edge.relation.ownerProp : edge.relation.relatedProp;
|
|
154
154
|
|
|
155
|
-
|
|
155
|
+
prop.forEach(i => {
|
|
156
156
|
const value = prop.getProp(node.obj, i);
|
|
157
157
|
|
|
158
158
|
if (value !== undefined) {
|
|
159
|
-
|
|
159
|
+
return;
|
|
160
160
|
}
|
|
161
161
|
|
|
162
162
|
prop.setProp(node.obj, i, prop.getProp(currentNode.obj, i));
|
|
163
|
-
}
|
|
163
|
+
});
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
}
|
|
@@ -93,7 +93,7 @@ class GraphPatchAction extends GraphAction {
|
|
|
93
93
|
) {
|
|
94
94
|
const { relation } = edge;
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
relation.ownerProp.forEach(i => {
|
|
97
97
|
const currentValue = currentNode && relation.ownerProp.getProp(currentNode.obj, i);
|
|
98
98
|
const relatedValue = relation.relatedProp.getProp(edge.relatedNode.obj, i);
|
|
99
99
|
|
|
@@ -101,7 +101,7 @@ class GraphPatchAction extends GraphAction {
|
|
|
101
101
|
relation.ownerProp.setProp(node.obj, i, relatedValue);
|
|
102
102
|
updatedProps.push(relation.ownerProp.props[i]);
|
|
103
103
|
}
|
|
104
|
-
}
|
|
104
|
+
});
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
|
|
@@ -125,14 +125,14 @@ class GraphPatchAction extends GraphAction {
|
|
|
125
125
|
) {
|
|
126
126
|
const { relation } = edge;
|
|
127
127
|
|
|
128
|
-
|
|
128
|
+
relation.ownerProp.forEach(i => {
|
|
129
129
|
const currentValue = relation.ownerProp.getProp(currentNode.obj, i);
|
|
130
130
|
|
|
131
131
|
if (currentValue != null) {
|
|
132
132
|
relation.ownerProp.setProp(node.obj, i, null);
|
|
133
133
|
updatedProps.push(relation.ownerProp.props[i]);
|
|
134
134
|
}
|
|
135
|
-
}
|
|
135
|
+
});
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
138
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { QueryBuilderOperation } = require('./QueryBuilderOperation');
|
|
4
|
+
const { assertIdNotUndefined } = require('../../utils/assert');
|
|
4
5
|
|
|
5
6
|
class FindByIdOperation extends QueryBuilderOperation {
|
|
6
7
|
constructor(name, opt) {
|
|
@@ -14,6 +15,10 @@ class FindByIdOperation extends QueryBuilderOperation {
|
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
onBuild(builder) {
|
|
18
|
+
if (!builder.internalOptions().skipUndefined) {
|
|
19
|
+
assertIdNotUndefined(this.id, `undefined was passed to ${this.name}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
17
22
|
builder.whereComposite(builder.fullIdColumn(), this.id);
|
|
18
23
|
}
|
|
19
24
|
|
|
@@ -109,7 +109,7 @@ class WhereInEagerOperation extends EagerOperation {
|
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
fetchRelationBatch(builder, models, relation, expr) {
|
|
112
|
-
if (this.shouldSkipFetched(models, expr)) {
|
|
112
|
+
if (this.shouldSkipFetched(models, relation, expr)) {
|
|
113
113
|
return this.createSkippedQuery(builder, models, relation, expr);
|
|
114
114
|
}
|
|
115
115
|
|
|
@@ -148,7 +148,7 @@ class WhereInEagerOperation extends EagerOperation {
|
|
|
148
148
|
return queryBuilder;
|
|
149
149
|
}
|
|
150
150
|
|
|
151
|
-
shouldSkipFetched(models, expr) {
|
|
151
|
+
shouldSkipFetched(models, relation, expr) {
|
|
152
152
|
if (!this.graphOptions.skipFetched) {
|
|
153
153
|
return false;
|
|
154
154
|
}
|
|
@@ -157,7 +157,7 @@ class WhereInEagerOperation extends EagerOperation {
|
|
|
157
157
|
return false;
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
-
const relationsToFetch = findRelationsToFetch(
|
|
160
|
+
const relationsToFetch = findRelationsToFetch(relation.relatedModelClass, expr);
|
|
161
161
|
const childModels = getRelatedModels(models, expr);
|
|
162
162
|
|
|
163
163
|
// We can only skip fetching a relation if all already fetched models
|
|
@@ -170,7 +170,7 @@ class WhereInEagerOperation extends EagerOperation {
|
|
|
170
170
|
const prop = ownerProp.props[c];
|
|
171
171
|
|
|
172
172
|
for (const model of childModels) {
|
|
173
|
-
if (model[prop] ===
|
|
173
|
+
if (model[prop] === undefined) {
|
|
174
174
|
return false;
|
|
175
175
|
}
|
|
176
176
|
}
|
|
@@ -153,12 +153,12 @@ class Relation {
|
|
|
153
153
|
const relatedProp = this.relatedProp;
|
|
154
154
|
const ownerProp = this.ownerProp;
|
|
155
155
|
|
|
156
|
-
|
|
156
|
+
relatedProp.forEach(i => {
|
|
157
157
|
const relatedRef = relatedProp.ref(builder, i).table(relatedTableAlias);
|
|
158
158
|
const ownerRef = ownerProp.ref(builder, i).table(ownerTable);
|
|
159
159
|
|
|
160
160
|
join.on(relatedRef, ownerRef);
|
|
161
|
-
}
|
|
161
|
+
});
|
|
162
162
|
});
|
|
163
163
|
}
|
|
164
164
|
|
|
@@ -211,9 +211,9 @@ class Relation {
|
|
|
211
211
|
}
|
|
212
212
|
|
|
213
213
|
setRelateProp(model, values) {
|
|
214
|
-
|
|
214
|
+
this.relatedProp.forEach(i => {
|
|
215
215
|
this.relatedProp.setProp(model, i, values[i]);
|
|
216
|
-
}
|
|
216
|
+
});
|
|
217
217
|
}
|
|
218
218
|
|
|
219
219
|
executeBeforeInsert(models, queryContext, result) {
|
|
@@ -79,6 +79,12 @@ class RelationProperty {
|
|
|
79
79
|
return this._cols;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
forEach(callback) {
|
|
83
|
+
for (let i = 0, l = this.size; i < l; ++i) {
|
|
84
|
+
callback(i);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
82
88
|
// Creates a concatenated string from the property values of the given object.
|
|
83
89
|
propKey(obj) {
|
|
84
90
|
const size = this.size;
|
|
@@ -8,9 +8,9 @@ class BelongsToOneDeleteOperation extends RelationDeleteOperation {
|
|
|
8
8
|
const ownerProp = this.relation.ownerProp;
|
|
9
9
|
|
|
10
10
|
for (const owner of this.owner.modelArray) {
|
|
11
|
-
|
|
11
|
+
ownerProp.forEach(i => {
|
|
12
12
|
ownerProp.setProp(owner, i, null);
|
|
13
|
-
}
|
|
13
|
+
});
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -16,7 +16,7 @@ class BelongsToOneRelateOperation extends RelateOperation {
|
|
|
16
16
|
const patch = {};
|
|
17
17
|
const ownerProp = this.relation.ownerProp;
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
ownerProp.forEach(i => {
|
|
20
20
|
const relatedValue = this.ids[0][i];
|
|
21
21
|
|
|
22
22
|
if (this.owner.isModels) {
|
|
@@ -26,7 +26,7 @@ class BelongsToOneRelateOperation extends RelateOperation {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
ownerProp.patch(patch, i, relatedValue);
|
|
29
|
-
}
|
|
29
|
+
});
|
|
30
30
|
|
|
31
31
|
const ownerIdProp = this.relation.ownerModelClass.getIdRelationProperty();
|
|
32
32
|
const ownerIds = this.owner.getProps(this.relation, ownerIdProp);
|
|
@@ -6,9 +6,9 @@ class BelongsToOneUnrelateOperation extends UnrelateOperation {
|
|
|
6
6
|
onAdd() {
|
|
7
7
|
const ids = new Array(this.relation.ownerProp.size);
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
this.relation.ownerProp.forEach(i => {
|
|
10
10
|
ids[i] = null;
|
|
11
|
-
}
|
|
11
|
+
});
|
|
12
12
|
|
|
13
13
|
this.ids = [ids];
|
|
14
14
|
return true;
|
|
@@ -18,7 +18,7 @@ class BelongsToOneUnrelateOperation extends UnrelateOperation {
|
|
|
18
18
|
const patch = {};
|
|
19
19
|
const ownerProp = this.relation.ownerProp;
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
ownerProp.forEach(i => {
|
|
22
22
|
const relatedValue = this.ids[0][i];
|
|
23
23
|
|
|
24
24
|
if (this.owner.isModels) {
|
|
@@ -28,7 +28,7 @@ class BelongsToOneUnrelateOperation extends UnrelateOperation {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
ownerProp.patch(patch, i, relatedValue);
|
|
31
|
-
}
|
|
31
|
+
});
|
|
32
32
|
|
|
33
33
|
const ownerIdProp = this.relation.ownerModelClass.getIdRelationProperty();
|
|
34
34
|
const ownerRefs = ownerIdProp.refs(builder);
|
|
@@ -20,9 +20,9 @@ class HasManyRelateOperation extends RelateOperation {
|
|
|
20
20
|
const relatedProp = this.relation.relatedProp;
|
|
21
21
|
const ownerValues = this.owner.getSplitProps(builder, this.relation);
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
relatedProp.forEach(i => {
|
|
24
24
|
relatedProp.patch(patch, i, ownerValues[0][i]);
|
|
25
|
-
}
|
|
25
|
+
});
|
|
26
26
|
|
|
27
27
|
return this.relation.relatedModelClass
|
|
28
28
|
.query()
|
|
@@ -9,9 +9,9 @@ class HasManyUnrelateOperation extends UnrelateOperation {
|
|
|
9
9
|
const ownerValues = this.owner.getProps(this.relation);
|
|
10
10
|
const relatedRefs = relatedProp.refs(builder);
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
relatedProp.forEach(i => {
|
|
13
13
|
relatedProp.patch(patch, i, null);
|
|
14
|
-
}
|
|
14
|
+
});
|
|
15
15
|
|
|
16
16
|
return this.relation.relatedModelClass
|
|
17
17
|
.query()
|
|
@@ -4,11 +4,11 @@ const getModel = () => require('../../model/Model').Model;
|
|
|
4
4
|
const { Relation } = require('../Relation');
|
|
5
5
|
const { RelationProperty } = require('../RelationProperty');
|
|
6
6
|
|
|
7
|
-
const { isSqlite
|
|
7
|
+
const { isSqlite } = require('../../utils/knexUtils');
|
|
8
8
|
const { inheritModel } = require('../../model/inheritModel');
|
|
9
9
|
const { resolveModel } = require('../../utils/resolveModel');
|
|
10
10
|
const { mapAfterAllReturn } = require('../../utils/promiseUtils');
|
|
11
|
-
const { isFunction, isObject } = require('../../utils/objectUtils');
|
|
11
|
+
const { isFunction, isObject, isString } = require('../../utils/objectUtils');
|
|
12
12
|
|
|
13
13
|
const { ManyToManyFindOperation } = require('./find/ManyToManyFindOperation');
|
|
14
14
|
const { ManyToManyInsertOperation } = require('./insert/ManyToManyInsertOperation');
|
|
@@ -71,12 +71,12 @@ class ManyToManyRelation extends Relation {
|
|
|
71
71
|
const joinTableAlias = builder.tableRefFor(this.joinTable);
|
|
72
72
|
|
|
73
73
|
builder.join(aliasedTableName(joinTable, joinTableAlias), join => {
|
|
74
|
-
|
|
74
|
+
this.relatedProp.forEach(i => {
|
|
75
75
|
const relatedRef = this.relatedProp.ref(builder, i);
|
|
76
76
|
const joinTableRelatedRef = this.joinTableRelatedProp.ref(builder, i);
|
|
77
77
|
|
|
78
78
|
join.on(relatedRef, joinTableRelatedRef);
|
|
79
|
-
}
|
|
79
|
+
});
|
|
80
80
|
});
|
|
81
81
|
|
|
82
82
|
owner.buildFindQuery(builder, this, joinTableOwnerRefs);
|
|
@@ -105,22 +105,22 @@ class ManyToManyRelation extends Relation {
|
|
|
105
105
|
const ownerProp = this.ownerProp;
|
|
106
106
|
const joinTableOwnerProp = this.joinTableOwnerProp;
|
|
107
107
|
|
|
108
|
-
|
|
108
|
+
ownerProp.forEach(i => {
|
|
109
109
|
const joinTableOwnerRef = joinTableOwnerProp.ref(builder, i).table(joinTableAlias);
|
|
110
110
|
const ownerRef = ownerProp.ref(builder, i).table(ownerTable);
|
|
111
111
|
|
|
112
112
|
join.on(joinTableOwnerRef, ownerRef);
|
|
113
|
-
}
|
|
113
|
+
});
|
|
114
114
|
})[joinOperation](relatedJoinSelect, join => {
|
|
115
115
|
const relatedProp = this.relatedProp;
|
|
116
116
|
const joinTableRelatedProp = this.joinTableRelatedProp;
|
|
117
117
|
|
|
118
|
-
|
|
118
|
+
relatedProp.forEach(i => {
|
|
119
119
|
const joinTableRelatedRef = joinTableRelatedProp.ref(builder, i).table(joinTableAlias);
|
|
120
120
|
const relatedRef = relatedProp.ref(builder, i).table(relatedTableAlias);
|
|
121
121
|
|
|
122
122
|
join.on(joinTableRelatedRef, relatedRef);
|
|
123
|
-
}
|
|
123
|
+
});
|
|
124
124
|
});
|
|
125
125
|
}
|
|
126
126
|
|
|
@@ -417,7 +417,11 @@ function parseExtras(ctx) {
|
|
|
417
417
|
return ctx;
|
|
418
418
|
}
|
|
419
419
|
|
|
420
|
-
if (
|
|
420
|
+
if (isString(extraDef)) {
|
|
421
|
+
extraDef = {
|
|
422
|
+
[extraDef]: extraDef
|
|
423
|
+
};
|
|
424
|
+
} else if (Array.isArray(extraDef)) {
|
|
421
425
|
extraDef = extraDef.reduce((extraDef, col) => {
|
|
422
426
|
extraDef[col] = col;
|
|
423
427
|
return extraDef;
|
|
@@ -87,14 +87,14 @@ class ManyToManyFindOperation extends RelationFindOperation {
|
|
|
87
87
|
|
|
88
88
|
// We must select the owner join columns so that we know for which owner model the related
|
|
89
89
|
// models belong to after the requests.
|
|
90
|
-
|
|
90
|
+
joinTableOwnerProp.forEach(i => {
|
|
91
91
|
const joinTableOwnerRef = joinTableOwnerProp.ref(builder, i);
|
|
92
92
|
const propName = relatedModelClass.columnNameToPropertyName(this.ownerJoinColumnAlias[i]);
|
|
93
93
|
|
|
94
94
|
builder.select(joinTableOwnerRef.as(this.ownerJoinColumnAlias[i]));
|
|
95
95
|
// Mark them to be omitted later.
|
|
96
96
|
this.omitProps.push(propName);
|
|
97
|
-
}
|
|
97
|
+
});
|
|
98
98
|
|
|
99
99
|
super.selectMissingJoinColumns(builder);
|
|
100
100
|
}
|
package/lib/utils/assert.js
CHANGED
|
@@ -11,6 +11,15 @@ function assertHasId(model) {
|
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
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
|
+
|
|
14
22
|
module.exports = {
|
|
15
|
-
assertHasId
|
|
23
|
+
assertHasId,
|
|
24
|
+
assertIdNotUndefined
|
|
16
25
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "objection",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.3",
|
|
4
4
|
"description": "An SQL-friendly ORM for Node.js",
|
|
5
5
|
"main": "lib/objection.js",
|
|
6
6
|
"license": "MIT",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"perf": "mocha --slow 60000 --timeout 60000 --reporter spec --recursive perf",
|
|
17
17
|
"perf-debug": "mocha --slow 60000 --timeout 60000 --reporter spec --inspect-brk perf",
|
|
18
18
|
"perf-opt": "mocha --slow 60000 --timeout 60000 --reporter spec --recursive perf --trace_opt --trace_deopt",
|
|
19
|
-
"prettier": "prettier --write \"{examples,lib,tests,typings,doc}/**/*.{js,ts
|
|
19
|
+
"prettier": "prettier --write \"{examples,lib,tests,typings,doc}/**/*.{js,ts}\"",
|
|
20
20
|
"eslint": "eslint --format codeframe \"examples/**/*.js\" \"lib/**/*.js\" \"tests/**/*.js\"",
|
|
21
21
|
"docs:dev": "vuepress dev doc",
|
|
22
22
|
"docs:build": "vuepress build doc"
|
|
@@ -115,7 +115,7 @@
|
|
|
115
115
|
"eslint --fix",
|
|
116
116
|
"git add"
|
|
117
117
|
],
|
|
118
|
-
"*.{
|
|
118
|
+
"*.{yml,ts}": [
|
|
119
119
|
"prettier --write",
|
|
120
120
|
"git add"
|
|
121
121
|
]
|
|
@@ -14,8 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
import * as ajv from 'ajv';
|
|
16
16
|
import * as dbErrors from 'db-errors';
|
|
17
|
-
import * as
|
|
18
|
-
import Knex = require('knex');
|
|
17
|
+
import * as Knex from 'knex';
|
|
19
18
|
|
|
20
19
|
// Export the entire Objection namespace.
|
|
21
20
|
export = Objection;
|
|
@@ -118,10 +117,10 @@ declare namespace Objection {
|
|
|
118
117
|
castTo(sqlType: string): this;
|
|
119
118
|
}
|
|
120
119
|
|
|
121
|
-
type Raw = RawBuilder |
|
|
120
|
+
type Raw = RawBuilder | Knex.Raw;
|
|
122
121
|
type Operator = string;
|
|
123
122
|
type ColumnRef = string | Raw | ReferenceBuilder;
|
|
124
|
-
type TableRef = ColumnRef | AnyQueryBuilder
|
|
123
|
+
type TableRef<QB extends AnyQueryBuilder> = ColumnRef | AnyQueryBuilder | CallbackVoid<QB>;
|
|
125
124
|
|
|
126
125
|
type PrimitiveValue =
|
|
127
126
|
| string
|
|
@@ -189,7 +188,7 @@ declare namespace Objection {
|
|
|
189
188
|
* A Pojo version of model.
|
|
190
189
|
*/
|
|
191
190
|
type ModelObject<T extends Model> = {
|
|
192
|
-
[K in NonFunctionPropertyNames<T>]: T[K];
|
|
191
|
+
[K in Exclude<NonFunctionPropertyNames<T>, 'QueryBuilderType'>]: T[K];
|
|
193
192
|
};
|
|
194
193
|
|
|
195
194
|
/**
|
|
@@ -350,10 +349,7 @@ declare namespace Objection {
|
|
|
350
349
|
}
|
|
351
350
|
|
|
352
351
|
interface FromMethod<QB extends AnyQueryBuilder> {
|
|
353
|
-
(table:
|
|
354
|
-
(cb: CallbackVoid<QB>): QB;
|
|
355
|
-
(raw: Raw): QB;
|
|
356
|
-
<QBP extends AnyQueryBuilder>(qb: QBP): QB;
|
|
352
|
+
(table: TableRef<QB>): QB;
|
|
357
353
|
}
|
|
358
354
|
|
|
359
355
|
interface WhereMethod<QB extends AnyQueryBuilder> {
|
|
@@ -524,10 +520,10 @@ declare namespace Objection {
|
|
|
524
520
|
}
|
|
525
521
|
|
|
526
522
|
interface JoinMethod<QB extends AnyQueryBuilder> {
|
|
527
|
-
(table: TableRef
|
|
528
|
-
(table: TableRef
|
|
529
|
-
(table: TableRef
|
|
530
|
-
(table: TableRef
|
|
523
|
+
(table: TableRef<QB>, leftCol: ColumnRef, op: Operator, rightCol: ColumnRef): QB;
|
|
524
|
+
(table: TableRef<QB>, leftCol: ColumnRef, rightCol: ColumnRef): QB;
|
|
525
|
+
(table: TableRef<QB>, cb: CallbackVoid<Knex.JoinClause>): QB;
|
|
526
|
+
(table: TableRef<QB>, raw: Raw): QB;
|
|
531
527
|
(raw: Raw): QB;
|
|
532
528
|
}
|
|
533
529
|
|
|
@@ -591,6 +587,10 @@ declare namespace Objection {
|
|
|
591
587
|
(arg: T): QB;
|
|
592
588
|
}
|
|
593
589
|
|
|
590
|
+
interface OptionalOneArgMethod<T, QB extends AnyQueryBuilder> {
|
|
591
|
+
(arg?: T): QB;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
594
|
interface StringReturningMethod {
|
|
595
595
|
(): string;
|
|
596
596
|
}
|
|
@@ -608,7 +608,7 @@ declare namespace Objection {
|
|
|
608
608
|
}
|
|
609
609
|
|
|
610
610
|
interface ColumnInfoMethod<QB extends AnyQueryBuilder> {
|
|
611
|
-
(): Promise<
|
|
611
|
+
(): Promise<Knex.ColumnInfo>;
|
|
612
612
|
}
|
|
613
613
|
|
|
614
614
|
interface TableRefForMethod {
|
|
@@ -667,7 +667,7 @@ declare namespace Objection {
|
|
|
667
667
|
}
|
|
668
668
|
|
|
669
669
|
interface OnBuildKnexCallback<QB extends AnyQueryBuilder> {
|
|
670
|
-
(this: QB, knexQuery:
|
|
670
|
+
(this: QB, knexQuery: Knex.QueryBuilder, query: QB): void;
|
|
671
671
|
}
|
|
672
672
|
|
|
673
673
|
interface OnBuildKnexMethod<QB extends AnyQueryBuilder> {
|
|
@@ -876,24 +876,29 @@ declare namespace Objection {
|
|
|
876
876
|
having: WhereMethod<this>;
|
|
877
877
|
andHaving: WhereMethod<this>;
|
|
878
878
|
orHaving: WhereMethod<this>;
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
879
|
+
|
|
880
|
+
havingRaw: WhereRawMethod<this>;
|
|
881
|
+
orHavingRaw: WhereRawMethod<this>;
|
|
882
|
+
|
|
883
|
+
havingIn: WhereInMethod<this>;
|
|
884
|
+
orHavingIn: WhereInMethod<this>;
|
|
885
|
+
havingNotIn: WhereInMethod<this>;
|
|
886
|
+
orHavingNotIn: WhereInMethod<this>;
|
|
887
|
+
|
|
888
|
+
havingNull: WhereNullMethod<this>;
|
|
889
|
+
orHavingNull: WhereNullMethod<this>;
|
|
890
|
+
havingNotNull: WhereNullMethod<this>;
|
|
891
|
+
orHavingNotNull: WhereNullMethod<this>;
|
|
892
|
+
|
|
893
|
+
havingExists: WhereExistsMethod<this>;
|
|
894
|
+
orHavingExists: WhereExistsMethod<this>;
|
|
895
|
+
havingNotExists: WhereExistsMethod<this>;
|
|
896
|
+
orHavingNotExists: WhereExistsMethod<this>;
|
|
897
|
+
|
|
898
|
+
havingBetween: WhereBetweenMethod<this>;
|
|
899
|
+
orHavingBetween: WhereBetweenMethod<this>;
|
|
900
|
+
havingNotBetween: WhereBetweenMethod<this>;
|
|
901
|
+
orHavingNotBetween: WhereBetweenMethod<this>;
|
|
897
902
|
|
|
898
903
|
whereComposite: WhereCompositeMethod<this>;
|
|
899
904
|
whereInComposite: WhereInCompositeMethod<this>;
|
|
@@ -1034,7 +1039,7 @@ declare namespace Objection {
|
|
|
1034
1039
|
// Deprecated
|
|
1035
1040
|
allowUpsert: AllowGraphMethod<this>;
|
|
1036
1041
|
|
|
1037
|
-
throwIfNotFound:
|
|
1042
|
+
throwIfNotFound: OptionalOneArgMethod<object, this>;
|
|
1038
1043
|
returning: ReturningMethod;
|
|
1039
1044
|
forUpdate: IdentityMethod<this>;
|
|
1040
1045
|
forShare: IdentityMethod<this>;
|
|
@@ -1066,10 +1071,11 @@ declare namespace Objection {
|
|
|
1066
1071
|
omit(modelClass: typeof Model, properties: string[]): this;
|
|
1067
1072
|
// Deprecated
|
|
1068
1073
|
omit(properties: string[]): this;
|
|
1069
|
-
|
|
1074
|
+
|
|
1070
1075
|
traverse(filterConstructor: typeof Model, traverser: TraverserFunction): R;
|
|
1071
|
-
// Deprecated
|
|
1072
1076
|
traverse(traverser: TraverserFunction): R;
|
|
1077
|
+
traverseAsync(filterConstructor: typeof Model, traverser: TraverserFunction): Promise<R>;
|
|
1078
|
+
traverseAsync(traverser: TraverserFunction): Promise<R>;
|
|
1073
1079
|
|
|
1074
1080
|
page(page: number, pageSize: number): PageQueryBuilder<this>;
|
|
1075
1081
|
range(): PageQueryBuilder<this>;
|
|
@@ -1173,8 +1179,8 @@ declare namespace Objection {
|
|
|
1173
1179
|
result?: R;
|
|
1174
1180
|
}
|
|
1175
1181
|
|
|
1176
|
-
export type Transaction =
|
|
1177
|
-
export type TransactionOrKnex = Transaction |
|
|
1182
|
+
export type Transaction = Knex.Transaction;
|
|
1183
|
+
export type TransactionOrKnex = Transaction | Knex;
|
|
1178
1184
|
|
|
1179
1185
|
export interface RelationMappings {
|
|
1180
1186
|
[relationName: string]: RelationMapping<any>;
|
|
@@ -1209,7 +1215,7 @@ declare namespace Objection {
|
|
|
1209
1215
|
export interface RelationThrough<M extends Model> {
|
|
1210
1216
|
from: RelationMappingColumnRef;
|
|
1211
1217
|
to: RelationMappingColumnRef;
|
|
1212
|
-
extra?: string[] | Record<string, string>;
|
|
1218
|
+
extra?: string | string[] | Record<string, string>;
|
|
1213
1219
|
modelClass?: ModelClassSpecifier;
|
|
1214
1220
|
beforeInsert?: RelationMappingHook<M>;
|
|
1215
1221
|
}
|
|
@@ -1359,7 +1365,7 @@ declare namespace Objection {
|
|
|
1359
1365
|
}
|
|
1360
1366
|
|
|
1361
1367
|
export interface FetchTableMetadataOptions {
|
|
1362
|
-
knex?:
|
|
1368
|
+
knex?: Knex;
|
|
1363
1369
|
force?: boolean;
|
|
1364
1370
|
table?: string;
|
|
1365
1371
|
}
|
|
@@ -1435,8 +1441,8 @@ declare namespace Objection {
|
|
|
1435
1441
|
tableMetadata(opt?: TableMetadataOptions): TableMetadata;
|
|
1436
1442
|
fetchTableMetadata(opt?: FetchTableMetadataOptions): Promise<TableMetadata>;
|
|
1437
1443
|
|
|
1438
|
-
knex(knex?:
|
|
1439
|
-
knexQuery():
|
|
1444
|
+
knex(knex?: Knex): Knex;
|
|
1445
|
+
knexQuery(): Knex.QueryBuilder;
|
|
1440
1446
|
startTransaction(knexOrTransaction?: TransactionOrKnex): Promise<Transaction>;
|
|
1441
1447
|
|
|
1442
1448
|
transaction<T>(callback: (trx: Transaction) => Promise<T>): Promise<T>;
|
|
@@ -1565,8 +1571,8 @@ declare namespace Objection {
|
|
|
1565
1571
|
static tableMetadata(opt?: TableMetadataOptions): TableMetadata;
|
|
1566
1572
|
static fetchTableMetadata(opt?: FetchTableMetadataOptions): Promise<TableMetadata>;
|
|
1567
1573
|
|
|
1568
|
-
static knex(knex?:
|
|
1569
|
-
static knexQuery():
|
|
1574
|
+
static knex(knex?: Knex): Knex;
|
|
1575
|
+
static knexQuery(): Knex.QueryBuilder;
|
|
1570
1576
|
static startTransaction(knexOrTransaction?: TransactionOrKnex): Promise<Transaction>;
|
|
1571
1577
|
|
|
1572
1578
|
static transaction<T>(callback: (trx: Transaction) => Promise<T>): Promise<T>;
|
|
@@ -1675,8 +1681,8 @@ declare namespace Objection {
|
|
|
1675
1681
|
$afterDelete(queryContext: QueryContext): Promise<any> | void;
|
|
1676
1682
|
|
|
1677
1683
|
$toDatabaseJson(): Pojo;
|
|
1678
|
-
$toJson(opt?: ToJsonOptions):
|
|
1679
|
-
toJSON(opt?: ToJsonOptions):
|
|
1684
|
+
$toJson(opt?: ToJsonOptions): ModelObject<this>;
|
|
1685
|
+
toJSON(opt?: ToJsonOptions): ModelObject<this>;
|
|
1680
1686
|
|
|
1681
1687
|
$setJson(json: object, opt?: ModelOptions): this;
|
|
1682
1688
|
$setDatabaseJson(json: object): this;
|
|
@@ -1697,9 +1703,11 @@ declare namespace Objection {
|
|
|
1697
1703
|
$clone(opt?: CloneOptions): this;
|
|
1698
1704
|
$traverse(filterConstructor: typeof Model, traverser: TraverserFunction): this;
|
|
1699
1705
|
$traverse(traverser: TraverserFunction): this;
|
|
1706
|
+
$traverseAsync(filterConstructor: typeof Model, traverser: TraverserFunction): Promise<this>;
|
|
1707
|
+
$traverseAsync(traverser: TraverserFunction): Promise<this>;
|
|
1700
1708
|
|
|
1701
|
-
$knex():
|
|
1702
|
-
$transaction():
|
|
1709
|
+
$knex(): Knex;
|
|
1710
|
+
$transaction(): Knex;
|
|
1703
1711
|
|
|
1704
1712
|
QueryBuilderType: QueryBuilder<this, this[]>;
|
|
1705
1713
|
}
|
|
@@ -1713,7 +1721,7 @@ declare namespace Objection {
|
|
|
1713
1721
|
* @tutorial https://vincit.github.io/objection.js/guide/transactions.html#creating-a-transaction
|
|
1714
1722
|
*/
|
|
1715
1723
|
export interface transaction {
|
|
1716
|
-
start(knexOrModel:
|
|
1724
|
+
start(knexOrModel: Knex | AnyModelConstructor): Promise<Transaction>;
|
|
1717
1725
|
|
|
1718
1726
|
<MC1 extends AnyModelConstructor, ReturnValue>(
|
|
1719
1727
|
modelClass1: MC1,
|
|
@@ -1790,7 +1798,7 @@ declare namespace Objection {
|
|
|
1790
1798
|
) => Promise<ReturnValue>
|
|
1791
1799
|
): Promise<ReturnValue>;
|
|
1792
1800
|
|
|
1793
|
-
<ReturnValue>(knex:
|
|
1801
|
+
<ReturnValue>(knex: Knex, callback: (trx: Transaction) => Promise<ReturnValue>): Promise<
|
|
1794
1802
|
ReturnValue
|
|
1795
1803
|
>;
|
|
1796
1804
|
}
|