@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,24 +1,20 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { asArray, isObject, uniqBy, get, set } = require('../utils/objectUtils');
|
|
4
3
|
const { ref: createRef } = require('../queryBuilder/ReferenceBuilder');
|
|
5
4
|
const { propToStr, PROP_KEY_PREFIX } = require('../model/modelValues');
|
|
6
|
-
|
|
7
5
|
class ModelNotFoundError extends Error {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
constructor(tableName) {
|
|
7
|
+
super();
|
|
8
|
+
this.name = this.constructor.name;
|
|
9
|
+
this.tableName = tableName;
|
|
10
|
+
}
|
|
13
11
|
}
|
|
14
|
-
|
|
15
12
|
class InvalidReferenceError extends Error {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
constructor() {
|
|
14
|
+
super();
|
|
15
|
+
this.name = this.constructor.name;
|
|
16
|
+
}
|
|
20
17
|
}
|
|
21
|
-
|
|
22
18
|
// A pair of these define how two tables are related to each other.
|
|
23
19
|
// Both the owner and the related table have one of these.
|
|
24
20
|
//
|
|
@@ -26,222 +22,186 @@ class InvalidReferenceError extends Error {
|
|
|
26
22
|
// (composite key) a json column reference, an array of json column
|
|
27
23
|
// references or any combination of the above.
|
|
28
24
|
class RelationProperty {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
// Returns an instance of ReferenceBuilder that points to the index:th
|
|
142
|
-
// value of a row.
|
|
143
|
-
ref(builder, index) {
|
|
144
|
-
const table = builder.tableRefFor(this.modelClass);
|
|
145
|
-
|
|
146
|
-
return this._refs[index].clone().table(table);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// Returns an array of reference builders. `ref(builder, i)` for each i.
|
|
150
|
-
refs(builder) {
|
|
151
|
-
const refs = new Array(this.size);
|
|
152
|
-
|
|
153
|
-
for (let i = 0, l = refs.length; i < l; ++i) {
|
|
154
|
-
refs[i] = this.ref(builder, i);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return refs;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// Appends an update operation for the index:th column into `patch` object.
|
|
161
|
-
patch(patch, index, value) {
|
|
162
|
-
return this._patchers[index](patch, value);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// String representation of this property's index:th column for logging.
|
|
166
|
-
propDescription(index) {
|
|
167
|
-
return this._refs[index].expression;
|
|
168
|
-
}
|
|
25
|
+
// references must be a reference string like `Table.column:maybe.some.json[1].path`.
|
|
26
|
+
// or an array of such references (composite key).
|
|
27
|
+
//
|
|
28
|
+
// modelClassResolver must be a function that takes a table name
|
|
29
|
+
// and returns a model class.
|
|
30
|
+
constructor(references, modelClassResolver) {
|
|
31
|
+
const refs = createRefs(asArray(references));
|
|
32
|
+
const paths = createPaths(refs, modelClassResolver);
|
|
33
|
+
const modelClass = resolveModelClass(paths);
|
|
34
|
+
this._refs = refs.map((ref) => ref.model(modelClass));
|
|
35
|
+
this._modelClass = modelClass;
|
|
36
|
+
this._props = paths.map((it) => it.path[0]);
|
|
37
|
+
this._cols = refs.map((it) => it.column);
|
|
38
|
+
this._propGetters = paths.map((it) => createGetter(it.path));
|
|
39
|
+
this._propSetters = paths.map((it) => createSetter(it.path));
|
|
40
|
+
this._patchers = refs.map((it, i) => createPatcher(it, paths[i].path));
|
|
41
|
+
}
|
|
42
|
+
static get ModelNotFoundError() {
|
|
43
|
+
return ModelNotFoundError;
|
|
44
|
+
}
|
|
45
|
+
static get InvalidReferenceError() {
|
|
46
|
+
return InvalidReferenceError;
|
|
47
|
+
}
|
|
48
|
+
// The number of columns.
|
|
49
|
+
get size() {
|
|
50
|
+
return this._refs.length;
|
|
51
|
+
}
|
|
52
|
+
// The model class that owns the property.
|
|
53
|
+
get modelClass() {
|
|
54
|
+
return this._modelClass;
|
|
55
|
+
}
|
|
56
|
+
// An array of property names. Contains multiple values in case of composite key.
|
|
57
|
+
// This may be different from `cols` if the model class has some kind of conversion
|
|
58
|
+
// between database and "external" formats, for example a snake_case to camelCase
|
|
59
|
+
// conversion.
|
|
60
|
+
get props() {
|
|
61
|
+
return this._props;
|
|
62
|
+
}
|
|
63
|
+
// An array of column names. Contains multiple values in case of composite key.
|
|
64
|
+
// This may be different from `props` if the model class has some kind of conversion
|
|
65
|
+
// between database and "external" formats, for example a snake_case to camelCase
|
|
66
|
+
// conversion.
|
|
67
|
+
get cols() {
|
|
68
|
+
return this._cols;
|
|
69
|
+
}
|
|
70
|
+
forEach(callback) {
|
|
71
|
+
for (let i = 0, l = this.size; i < l; ++i) {
|
|
72
|
+
callback(i);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// Creates a concatenated string from the property values of the given object.
|
|
76
|
+
propKey(obj) {
|
|
77
|
+
const size = this.size;
|
|
78
|
+
let key = PROP_KEY_PREFIX;
|
|
79
|
+
for (let i = 0; i < size; ++i) {
|
|
80
|
+
key += propToStr(this.getProp(obj, i));
|
|
81
|
+
if (i !== size - 1) {
|
|
82
|
+
key += ',';
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return key;
|
|
86
|
+
}
|
|
87
|
+
// Returns the property values of the given object as an array.
|
|
88
|
+
getProps(obj) {
|
|
89
|
+
const size = this.size;
|
|
90
|
+
const props = new Array(size);
|
|
91
|
+
for (let i = 0; i < size; ++i) {
|
|
92
|
+
props[i] = this.getProp(obj, i);
|
|
93
|
+
}
|
|
94
|
+
return props;
|
|
95
|
+
}
|
|
96
|
+
// Returns true if the given object has a non-null value in all properties.
|
|
97
|
+
hasProps(obj) {
|
|
98
|
+
const size = this.size;
|
|
99
|
+
for (let i = 0; i < size; ++i) {
|
|
100
|
+
const prop = this.getProp(obj, i);
|
|
101
|
+
if (prop === null || prop === undefined) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
// Returns the index:th property value of the given object.
|
|
108
|
+
getProp(obj, index) {
|
|
109
|
+
return this._propGetters[index](obj);
|
|
110
|
+
}
|
|
111
|
+
// Sets the index:th property value of the given object.
|
|
112
|
+
setProp(obj, index, value) {
|
|
113
|
+
return this._propSetters[index](obj, value);
|
|
114
|
+
}
|
|
115
|
+
// Returns an instance of ReferenceBuilder that points to the index:th
|
|
116
|
+
// value of a row.
|
|
117
|
+
ref(builder, index) {
|
|
118
|
+
const table = builder.tableRefFor(this.modelClass);
|
|
119
|
+
return this._refs[index].clone().table(table);
|
|
120
|
+
}
|
|
121
|
+
// Returns an array of reference builders. `ref(builder, i)` for each i.
|
|
122
|
+
refs(builder) {
|
|
123
|
+
const refs = new Array(this.size);
|
|
124
|
+
for (let i = 0, l = refs.length; i < l; ++i) {
|
|
125
|
+
refs[i] = this.ref(builder, i);
|
|
126
|
+
}
|
|
127
|
+
return refs;
|
|
128
|
+
}
|
|
129
|
+
// Appends an update operation for the index:th column into `patch` object.
|
|
130
|
+
patch(patch, index, value) {
|
|
131
|
+
return this._patchers[index](patch, value);
|
|
132
|
+
}
|
|
133
|
+
// String representation of this property's index:th column for logging.
|
|
134
|
+
propDescription(index) {
|
|
135
|
+
return this._refs[index].expression;
|
|
136
|
+
}
|
|
169
137
|
}
|
|
170
|
-
|
|
171
138
|
function createRefs(refs) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
139
|
+
try {
|
|
140
|
+
return refs.map((it) => {
|
|
141
|
+
if (!isObject(it) || !it.isObjectionReferenceBuilder) {
|
|
142
|
+
return createRef(it);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
return it;
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
catch (err) {
|
|
150
|
+
throw new InvalidReferenceError();
|
|
151
|
+
}
|
|
183
152
|
}
|
|
184
|
-
|
|
185
153
|
function createPaths(refs, modelClassResolver) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
path: [prop].concat(jsonPath),
|
|
202
|
-
modelClass,
|
|
203
|
-
};
|
|
204
|
-
});
|
|
154
|
+
return refs.map((ref) => {
|
|
155
|
+
if (!ref.tableName) {
|
|
156
|
+
throw new InvalidReferenceError();
|
|
157
|
+
}
|
|
158
|
+
const modelClass = modelClassResolver(ref.tableName);
|
|
159
|
+
if (!modelClass) {
|
|
160
|
+
throw new ModelNotFoundError(ref.tableName);
|
|
161
|
+
}
|
|
162
|
+
const prop = modelClass.columnNameToPropertyName(ref.column);
|
|
163
|
+
const jsonPath = ref.parsedExpr.access.map((it) => it.ref);
|
|
164
|
+
return {
|
|
165
|
+
path: [prop].concat(jsonPath),
|
|
166
|
+
modelClass,
|
|
167
|
+
};
|
|
168
|
+
});
|
|
205
169
|
}
|
|
206
|
-
|
|
207
170
|
function resolveModelClass(paths) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
return modelClasses[0];
|
|
171
|
+
const modelClasses = paths.map((it) => it.modelClass);
|
|
172
|
+
const uniqueModelClasses = uniqBy(modelClasses);
|
|
173
|
+
if (uniqueModelClasses.length !== 1) {
|
|
174
|
+
throw new InvalidReferenceError();
|
|
175
|
+
}
|
|
176
|
+
return modelClasses[0];
|
|
216
177
|
}
|
|
217
|
-
|
|
218
178
|
function createGetter(path) {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
179
|
+
if (path.length === 1) {
|
|
180
|
+
const prop = path[0];
|
|
181
|
+
return (obj) => obj[prop];
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
return (obj) => get(obj, path);
|
|
185
|
+
}
|
|
225
186
|
}
|
|
226
|
-
|
|
227
187
|
function createSetter(path) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
188
|
+
if (path.length === 1) {
|
|
189
|
+
const prop = path[0];
|
|
190
|
+
return (obj, value) => (obj[prop] = value);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
return (obj, value) => set(obj, path, value);
|
|
194
|
+
}
|
|
234
195
|
}
|
|
235
|
-
|
|
236
196
|
function createPatcher(ref, path) {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
197
|
+
if (ref.isPlainColumnRef) {
|
|
198
|
+
return (patch, value) => (patch[path[0]] = value);
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
// Objection `patch`, `update` etc. methods understand field expressions.
|
|
202
|
+
return (patch, value) => (patch[ref.expression] = value);
|
|
203
|
+
}
|
|
243
204
|
}
|
|
244
|
-
|
|
245
205
|
module.exports = {
|
|
246
|
-
|
|
206
|
+
RelationProperty,
|
|
247
207
|
};
|
|
@@ -1,30 +1,23 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { UpdateOperation } = require('../queryBuilder/operations/UpdateOperation');
|
|
4
3
|
const { RelationFindOperation } = require('./RelationFindOperation');
|
|
5
|
-
|
|
6
4
|
class RelationUpdateOperation extends UpdateOperation {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
relation: this.relation,
|
|
23
|
-
owner: this.owner,
|
|
24
|
-
});
|
|
25
|
-
}
|
|
5
|
+
constructor(name, opt) {
|
|
6
|
+
super(name, opt);
|
|
7
|
+
this.relation = opt.relation;
|
|
8
|
+
this.owner = opt.owner;
|
|
9
|
+
}
|
|
10
|
+
onBuild(builder) {
|
|
11
|
+
super.onBuild(builder);
|
|
12
|
+
this.relation.findQuery(builder, this.owner);
|
|
13
|
+
}
|
|
14
|
+
toFindOperation() {
|
|
15
|
+
return new RelationFindOperation('find', {
|
|
16
|
+
relation: this.relation,
|
|
17
|
+
owner: this.owner,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
26
20
|
}
|
|
27
|
-
|
|
28
21
|
module.exports = {
|
|
29
|
-
|
|
22
|
+
RelationUpdateOperation,
|
|
30
23
|
};
|
|
@@ -1,23 +1,18 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { RelationDeleteOperation } = require('../RelationDeleteOperation');
|
|
4
|
-
|
|
5
3
|
class BelongsToOneDeleteOperation extends RelationDeleteOperation {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
onAfter1(_, result) {
|
|
5
|
+
if (this.owner.isModels) {
|
|
6
|
+
const ownerProp = this.relation.ownerProp;
|
|
7
|
+
for (const owner of this.owner.modelArray) {
|
|
8
|
+
ownerProp.forEach((i) => {
|
|
9
|
+
ownerProp.setProp(owner, i, null);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
15
14
|
}
|
|
16
|
-
|
|
17
|
-
return result;
|
|
18
|
-
}
|
|
19
15
|
}
|
|
20
|
-
|
|
21
16
|
module.exports = {
|
|
22
|
-
|
|
17
|
+
BelongsToOneDeleteOperation,
|
|
23
18
|
};
|
|
@@ -1,44 +1,33 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { RelationInsertOperation } = require('../RelationInsertOperation');
|
|
4
3
|
const { BelongsToOneRelateOperation } = require('./BelongsToOneRelateOperation');
|
|
5
|
-
|
|
6
4
|
class BelongsToOneInsertOperation extends RelationInsertOperation {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
onAdd(builder, args) {
|
|
6
|
+
const retVal = super.onAdd(builder, args);
|
|
7
|
+
if (this.models.length > 1) {
|
|
8
|
+
throw this.relation.createError('can only insert one model to a BelongsToOneRelation');
|
|
9
|
+
}
|
|
10
|
+
return retVal;
|
|
12
11
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
async onAfter1(builder, ret) {
|
|
13
|
+
const inserted = await super.onAfter1(builder, ret);
|
|
14
|
+
if (!builder.isExecutable()) {
|
|
15
|
+
return inserted;
|
|
16
|
+
}
|
|
17
|
+
const relateOp = new BelongsToOneRelateOperation('relate', {
|
|
18
|
+
relation: this.relation,
|
|
19
|
+
owner: this.owner,
|
|
20
|
+
});
|
|
21
|
+
if (this.assignResultToOwner && this.owner.isModels) {
|
|
22
|
+
for (const owner of this.owner.modelArray) {
|
|
23
|
+
owner.$setRelated(this.relation, inserted);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
relateOp.onAdd(builder, [inserted]);
|
|
27
|
+
await relateOp.queryExecutor(builder);
|
|
28
|
+
return inserted;
|
|
22
29
|
}
|
|
23
|
-
|
|
24
|
-
const relateOp = new BelongsToOneRelateOperation('relate', {
|
|
25
|
-
relation: this.relation,
|
|
26
|
-
owner: this.owner,
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
if (this.assignResultToOwner && this.owner.isModels) {
|
|
30
|
-
for (const owner of this.owner.modelArray) {
|
|
31
|
-
owner.$setRelated(this.relation, inserted);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
relateOp.onAdd(builder, [inserted]);
|
|
36
|
-
await relateOp.queryExecutor(builder);
|
|
37
|
-
|
|
38
|
-
return inserted;
|
|
39
|
-
}
|
|
40
30
|
}
|
|
41
|
-
|
|
42
31
|
module.exports = {
|
|
43
|
-
|
|
32
|
+
BelongsToOneInsertOperation,
|
|
44
33
|
};
|
|
@@ -1,50 +1,39 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { normalizeIds } = require('../../utils/normalizeIds');
|
|
4
3
|
const { RelateOperation } = require('../../queryBuilder/operations/RelateOperation');
|
|
5
|
-
|
|
6
4
|
class BelongsToOneRelateOperation extends RelateOperation {
|
|
7
|
-
|
|
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
|
-
|
|
34
|
-
return this.relation.ownerModelClass
|
|
35
|
-
.query()
|
|
36
|
-
.childQueryOf(builder)
|
|
37
|
-
.patch(patch)
|
|
38
|
-
.whereInComposite(ownerIdProp.refs(builder), ownerIds);
|
|
39
|
-
}
|
|
5
|
+
onAdd(_, args) {
|
|
6
|
+
this.input = args[0];
|
|
7
|
+
this.ids = normalizeIds(args[0], this.relation.relatedProp, { arrayOutput: true });
|
|
8
|
+
assertOneId(this.ids);
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
queryExecutor(builder) {
|
|
12
|
+
const patch = {};
|
|
13
|
+
const ownerProp = this.relation.ownerProp;
|
|
14
|
+
ownerProp.forEach((i) => {
|
|
15
|
+
const relatedValue = this.ids[0][i];
|
|
16
|
+
if (this.owner.isModels) {
|
|
17
|
+
for (const owner of this.owner.modelArray) {
|
|
18
|
+
ownerProp.setProp(owner, i, relatedValue);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
ownerProp.patch(patch, i, relatedValue);
|
|
22
|
+
});
|
|
23
|
+
const ownerIdProp = this.relation.ownerModelClass.getIdRelationProperty();
|
|
24
|
+
const ownerIds = this.owner.getProps(this.relation, ownerIdProp);
|
|
25
|
+
return this.relation.ownerModelClass
|
|
26
|
+
.query()
|
|
27
|
+
.childQueryOf(builder)
|
|
28
|
+
.patch(patch)
|
|
29
|
+
.whereInComposite(ownerIdProp.refs(builder), ownerIds);
|
|
30
|
+
}
|
|
40
31
|
}
|
|
41
|
-
|
|
42
32
|
function assertOneId(ids) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
33
|
+
if (ids.length > 1) {
|
|
34
|
+
throw new Error('can only relate one model to a BelongsToOneRelation');
|
|
35
|
+
}
|
|
46
36
|
}
|
|
47
|
-
|
|
48
37
|
module.exports = {
|
|
49
|
-
|
|
38
|
+
BelongsToOneRelateOperation,
|
|
50
39
|
};
|