@abedmurrar/objection 4.0.0-alpha.0 → 4.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +103 -67
- package/lib/index.mjs +6 -0
- package/lib/initialize.js +6 -9
- package/lib/model/AjvValidator.js +235 -282
- package/lib/model/Model.js +653 -823
- package/lib/model/ModifierNotFoundError.js +5 -7
- package/lib/model/NotFoundError.js +14 -18
- package/lib/model/RelationDoesNotExistError.js +6 -9
- package/lib/model/ValidationError.js +33 -41
- package/lib/model/Validator.js +15 -21
- package/lib/model/getModel.js +1 -3
- package/lib/model/graph/ModelGraph.js +44 -64
- package/lib/model/graph/ModelGraphBuilder.js +164 -217
- package/lib/model/graph/ModelGraphEdge.js +31 -43
- package/lib/model/graph/ModelGraphNode.js +192 -231
- package/lib/model/inheritModel.js +9 -18
- package/lib/model/modelBindKnex.js +48 -62
- package/lib/model/modelClone.js +79 -98
- package/lib/model/modelColPropMap.js +16 -26
- package/lib/model/modelConcurrency.js +24 -0
- package/lib/model/modelId.js +44 -49
- package/lib/model/modelJsonAttributes.js +56 -76
- package/lib/model/modelParseRelations.js +68 -67
- package/lib/model/modelQueryProps.js +76 -107
- package/lib/model/modelSet.js +92 -112
- package/lib/model/modelTableMetadata.js +60 -72
- package/lib/model/modelToJson.js +120 -150
- package/lib/model/modelUtils.js +30 -37
- package/lib/model/modelValidate.js +34 -42
- package/lib/model/modelValues.js +78 -91
- package/lib/model/modelVisitor.js +22 -29
- package/lib/objection.js +42 -67
- package/lib/queryBuilder/FunctionBuilder.js +231 -279
- package/lib/queryBuilder/InternalOptions.js +19 -24
- package/lib/queryBuilder/JoinBuilder.js +116 -149
- package/lib/queryBuilder/QueryBuilder.js +1009 -1271
- package/lib/queryBuilder/QueryBuilderBase.js +647 -865
- package/lib/queryBuilder/QueryBuilderContext.js +14 -21
- package/lib/queryBuilder/QueryBuilderContextBase.js +20 -29
- package/lib/queryBuilder/QueryBuilderOperationSupport.js +462 -536
- package/lib/queryBuilder/QueryBuilderUserContext.js +19 -25
- package/lib/queryBuilder/RawBuilder.js +58 -73
- package/lib/queryBuilder/ReferenceBuilder.js +232 -270
- package/lib/queryBuilder/RelationExpression.js +322 -380
- package/lib/queryBuilder/StaticHookArguments.js +94 -106
- package/lib/queryBuilder/ValueBuilder.js +100 -112
- package/lib/queryBuilder/expressions.js +86 -116
- package/lib/queryBuilder/functions/mysql.js +642 -697
- package/lib/queryBuilder/graph/GraphAction.js +44 -59
- package/lib/queryBuilder/graph/GraphData.js +7 -9
- package/lib/queryBuilder/graph/GraphFetcher.js +115 -151
- package/lib/queryBuilder/graph/GraphNodeDbExistence.js +114 -152
- package/lib/queryBuilder/graph/GraphOperation.js +16 -22
- package/lib/queryBuilder/graph/GraphOptions.js +143 -185
- package/lib/queryBuilder/graph/GraphUpsert.js +167 -241
- package/lib/queryBuilder/graph/delete/GraphDelete.js +8 -13
- package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +44 -62
- package/lib/queryBuilder/graph/insert/GraphInsert.js +99 -128
- package/lib/queryBuilder/graph/insert/GraphInsertAction.js +151 -193
- package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +57 -79
- package/lib/queryBuilder/graph/patch/GraphPatch.js +8 -13
- package/lib/queryBuilder/graph/patch/GraphPatchAction.js +181 -234
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +16 -21
- package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +80 -110
- package/lib/queryBuilder/join/JoinResultColumn.js +14 -18
- package/lib/queryBuilder/join/JoinResultParser.js +88 -101
- package/lib/queryBuilder/join/RelationJoiner.js +205 -288
- package/lib/queryBuilder/join/TableNode.js +117 -144
- package/lib/queryBuilder/join/TableTree.js +60 -76
- package/lib/queryBuilder/join/utils.js +10 -16
- package/lib/queryBuilder/operations/DelegateOperation.js +93 -133
- package/lib/queryBuilder/operations/DeleteOperation.js +33 -37
- package/lib/queryBuilder/operations/FindByIdOperation.js +20 -28
- package/lib/queryBuilder/operations/FindByIdsOperation.js +17 -23
- package/lib/queryBuilder/operations/FindOperation.js +114 -131
- package/lib/queryBuilder/operations/FirstOperation.js +14 -19
- package/lib/queryBuilder/operations/FromOperation.js +69 -88
- package/lib/queryBuilder/operations/InsertAndFetchOperation.js +28 -39
- package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +25 -35
- package/lib/queryBuilder/operations/InsertGraphOperation.js +77 -98
- package/lib/queryBuilder/operations/InsertOperation.js +132 -165
- package/lib/queryBuilder/operations/InstanceDeleteOperation.js +27 -36
- package/lib/queryBuilder/operations/InstanceFindOperation.js +9 -13
- package/lib/queryBuilder/operations/InstanceInsertOperation.js +13 -17
- package/lib/queryBuilder/operations/InstanceUpdateOperation.js +32 -46
- package/lib/queryBuilder/operations/JoinRelatedOperation.js +66 -86
- package/lib/queryBuilder/operations/KnexOperation.js +4 -7
- package/lib/queryBuilder/operations/MergeOperation.js +32 -45
- package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +131 -150
- package/lib/queryBuilder/operations/OnBuildKnexOperation.js +17 -23
- package/lib/queryBuilder/operations/OnBuildOperation.js +17 -23
- package/lib/queryBuilder/operations/OnErrorOperation.js +17 -23
- package/lib/queryBuilder/operations/QueryBuilderOperation.js +231 -270
- package/lib/queryBuilder/operations/RangeOperation.js +30 -40
- package/lib/queryBuilder/operations/RelateOperation.js +14 -21
- package/lib/queryBuilder/operations/ReturningOperation.js +12 -18
- package/lib/queryBuilder/operations/RunAfterOperation.js +17 -23
- package/lib/queryBuilder/operations/RunBeforeOperation.js +17 -23
- package/lib/queryBuilder/operations/UnrelateOperation.js +12 -17
- package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +48 -65
- package/lib/queryBuilder/operations/UpdateOperation.js +104 -129
- package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +25 -35
- package/lib/queryBuilder/operations/UpsertGraphOperation.js +36 -55
- package/lib/queryBuilder/operations/WhereCompositeOperation.js +30 -39
- package/lib/queryBuilder/operations/WhereInOperation.js +34 -36
- package/lib/queryBuilder/operations/eager/EagerOperation.js +37 -52
- package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +34 -44
- package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +4 -7
- package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +193 -251
- package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +18 -25
- package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +25 -41
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +19 -20
- package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +23 -21
- package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +106 -118
- package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +79 -108
- package/lib/queryBuilder/operations/select/SelectOperation.js +37 -53
- package/lib/queryBuilder/operations/select/Selection.js +94 -105
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +68 -101
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +65 -75
- package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +75 -84
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +760 -859
- package/lib/queryBuilder/parsers/relationExpressionParser.js +840 -952
- package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +10 -15
- package/lib/queryBuilder/transformations/QueryTransformation.js +4 -6
- package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +35 -49
- package/lib/queryBuilder/transformations/index.js +3 -9
- package/lib/queryBuilder/whereScopes.js +158 -186
- package/lib/relations/Relation.js +293 -375
- package/lib/relations/RelationDeleteOperation.js +24 -28
- package/lib/relations/RelationFindOperation.js +108 -138
- package/lib/relations/RelationInsertOperation.js +24 -32
- package/lib/relations/RelationOwner.js +175 -209
- package/lib/relations/RelationProperty.js +186 -223
- package/lib/relations/RelationUpdateOperation.js +23 -29
- package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +16 -21
- package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +30 -41
- package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +36 -47
- package/lib/relations/belongsToOne/BelongsToOneRelation.js +42 -50
- package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +43 -58
- package/lib/relations/hasMany/HasManyInsertOperation.js +40 -56
- package/lib/relations/hasMany/HasManyRelateOperation.js +45 -61
- package/lib/relations/hasMany/HasManyRelation.js +38 -46
- package/lib/relations/hasMany/HasManyUnrelateOperation.js +25 -32
- package/lib/relations/hasOne/HasOneRelation.js +9 -12
- package/lib/relations/hasOneThrough/HasOneThroughRelation.js +9 -12
- package/lib/relations/manyToMany/ManyToManyModifyMixin.js +73 -95
- package/lib/relations/manyToMany/ManyToManyRelation.js +374 -473
- package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +23 -30
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +8 -10
- package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +20 -25
- package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +8 -12
- package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +77 -100
- package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +37 -49
- package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +58 -74
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +10 -13
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +26 -33
- package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +10 -15
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +8 -10
- package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +61 -78
- package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +8 -12
- package/lib/transaction.js +53 -83
- package/lib/utils/assert.js +15 -21
- package/lib/utils/buildUtils.js +17 -19
- package/lib/utils/classUtils.js +7 -11
- package/lib/utils/cloneDeep.js +365 -0
- package/lib/utils/createModifier.js +36 -41
- package/lib/utils/deprecate.js +8 -12
- package/lib/utils/identifierMapping.js +153 -192
- package/lib/utils/internalPropUtils.js +4 -8
- package/lib/utils/knexUtils.js +61 -56
- package/lib/utils/mixin.js +16 -23
- package/lib/utils/mysqlJsonPath.js +55 -40
- package/lib/utils/normalizeIds.js +110 -127
- package/lib/utils/objectUtils.js +236 -338
- package/lib/utils/parseFieldExpression.js +66 -44
- package/lib/utils/postgresJsonPath.js +48 -62
- package/lib/utils/promiseUtils/after.js +10 -13
- package/lib/utils/promiseUtils/afterReturn.js +10 -13
- package/lib/utils/promiseUtils/index.js +15 -17
- package/lib/utils/promiseUtils/isPromise.js +6 -9
- package/lib/utils/promiseUtils/map.js +75 -89
- package/lib/utils/promiseUtils/mapAfterAllReturn.js +17 -23
- package/lib/utils/promiseUtils/try.js +15 -18
- package/lib/utils/resolveModel.js +91 -94
- package/lib/utils/tmpColumnUtils.js +4 -13
- package/package.json +50 -23
- package/typings/objection/index.d.mts +362 -0
- package/typings/objection/index.d.ts +36 -9
- package/lib/.eslintrc.json +0 -8
- package/lib/queryBuilder/parsers/jsonFieldExpressionParser.pegjs +0 -97
- package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +0 -141
- package/lib/utils/clone.js +0 -2329
|
@@ -1,164 +1,147 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { QueryBuilderOperation } = require('./QueryBuilderOperation');
|
|
4
|
-
const { StaticHookArguments } = require('../StaticHookArguments');
|
|
3
|
+
const { StaticHookArguments, hasStaticHook } = require('../StaticHookArguments');
|
|
5
4
|
const { isPromise, after, afterReturn } = require('../../utils/promiseUtils');
|
|
6
5
|
const { isObject } = require('../../utils/objectUtils');
|
|
7
|
-
|
|
8
6
|
class FindOperation extends QueryBuilderOperation {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
onBefore2(builder, result) {
|
|
8
|
+
return afterReturn(callStaticBeforeFind(builder), result);
|
|
9
|
+
}
|
|
10
|
+
onAfter3(builder, results) {
|
|
11
|
+
const opt = builder.findOptions();
|
|
12
|
+
if (opt.dontCallFindHooks) {
|
|
13
|
+
return results;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
return callAfterFind(builder, results);
|
|
17
|
+
}
|
|
20
18
|
}
|
|
21
|
-
}
|
|
22
19
|
}
|
|
23
|
-
|
|
24
20
|
function callStaticBeforeFind(builder) {
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
if (!hasStaticHook(builder.modelClass(), 'beforeFind')) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
const args = StaticHookArguments.create({ builder });
|
|
25
|
+
return builder.modelClass().beforeFind(args);
|
|
27
26
|
}
|
|
28
|
-
|
|
29
27
|
function callAfterFind(builder, result) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return after(maybePromise, () => callStaticAfterFind(builder, result));
|
|
28
|
+
const opt = builder.findOptions();
|
|
29
|
+
const maybePromise = callInstanceAfterFind(builder.context(), result, opt.callAfterFindDeeply);
|
|
30
|
+
return after(maybePromise, () => callStaticAfterFind(builder, result));
|
|
34
31
|
}
|
|
35
|
-
|
|
36
32
|
function callStaticAfterFind(builder, result) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
33
|
+
if (!hasStaticHook(builder.modelClass(), 'afterFind')) {
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
const args = StaticHookArguments.create({ builder, result });
|
|
37
|
+
const maybePromise = builder.modelClass().afterFind(args);
|
|
38
|
+
return after(maybePromise, (maybeResult) => {
|
|
39
|
+
if (maybeResult === undefined) {
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return maybeResult;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
47
46
|
}
|
|
48
|
-
|
|
49
47
|
function callInstanceAfterFind(ctx, results, deep) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
48
|
+
if (Array.isArray(results)) {
|
|
49
|
+
if (results.length === 1) {
|
|
50
|
+
return callAfterFindForOne(ctx, results[0], results, deep);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
return callAfterFindArray(ctx, results, deep);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
return callAfterFindForOne(ctx, results, results, deep);
|
|
58
|
+
}
|
|
59
59
|
}
|
|
60
|
-
|
|
61
60
|
function callAfterFindArray(ctx, results, deep) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
} else {
|
|
80
|
-
return mapped;
|
|
81
|
-
}
|
|
61
|
+
if (results.length === 0 || !isObject(results[0])) {
|
|
62
|
+
return results;
|
|
63
|
+
}
|
|
64
|
+
const mapped = new Array(results.length);
|
|
65
|
+
let containsPromise = false;
|
|
66
|
+
for (let i = 0, l = results.length; i < l; ++i) {
|
|
67
|
+
mapped[i] = callAfterFindForOne(ctx, results[i], results[i], deep);
|
|
68
|
+
if (isPromise(mapped[i])) {
|
|
69
|
+
containsPromise = true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (containsPromise) {
|
|
73
|
+
return Promise.all(mapped);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
return mapped;
|
|
77
|
+
}
|
|
82
78
|
}
|
|
83
|
-
|
|
84
79
|
function callAfterFindForOne(ctx, model, result, deep) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
80
|
+
if (!isObject(model) || !model.$isObjectionModel) {
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
if (deep) {
|
|
84
|
+
const results = [];
|
|
85
|
+
const containsPromise = callAfterFindForRelations(ctx, model, results);
|
|
86
|
+
if (containsPromise) {
|
|
87
|
+
return Promise.all(results).then(() => {
|
|
88
|
+
return doCallAfterFind(ctx, model, result);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
return doCallAfterFind(ctx, model, result);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
95
96
|
return doCallAfterFind(ctx, model, result);
|
|
96
|
-
});
|
|
97
|
-
} else {
|
|
98
|
-
return doCallAfterFind(ctx, model, result);
|
|
99
97
|
}
|
|
100
|
-
} else {
|
|
101
|
-
return doCallAfterFind(ctx, model, result);
|
|
102
|
-
}
|
|
103
98
|
}
|
|
104
|
-
|
|
105
99
|
function callAfterFindForRelations(ctx, model, results) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
results.push(maybePromise);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return containsPromise;
|
|
100
|
+
const keys = Object.keys(model);
|
|
101
|
+
let containsPromise = false;
|
|
102
|
+
for (let i = 0, l = keys.length; i < l; ++i) {
|
|
103
|
+
const key = keys[i];
|
|
104
|
+
const value = model[key];
|
|
105
|
+
if (isRelation(value)) {
|
|
106
|
+
const maybePromise = callInstanceAfterFind(ctx, value, true);
|
|
107
|
+
if (isPromise(maybePromise)) {
|
|
108
|
+
containsPromise = true;
|
|
109
|
+
}
|
|
110
|
+
results.push(maybePromise);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return containsPromise;
|
|
125
114
|
}
|
|
126
|
-
|
|
127
115
|
function isRelation(value) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
(isNonEmptyObjectArray(value) && value[0].$isObjectionModel)
|
|
131
|
-
);
|
|
116
|
+
return ((isObject(value) && value.$isObjectionModel) ||
|
|
117
|
+
(isNonEmptyObjectArray(value) && value[0].$isObjectionModel));
|
|
132
118
|
}
|
|
133
|
-
|
|
134
119
|
function isNonEmptyObjectArray(value) {
|
|
135
|
-
|
|
120
|
+
return Array.isArray(value) && value.length > 0 && isObject(value[0]);
|
|
136
121
|
}
|
|
137
|
-
|
|
138
122
|
function doCallAfterFind(ctx, model, result) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
123
|
+
const afterFind = getAfterFindHook(model);
|
|
124
|
+
if (afterFind !== null) {
|
|
125
|
+
const maybePromise = afterFind.call(model, ctx);
|
|
126
|
+
if (isPromise(maybePromise)) {
|
|
127
|
+
return maybePromise.then(() => result);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
152
136
|
}
|
|
153
|
-
|
|
154
137
|
function getAfterFindHook(model) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
138
|
+
if (model.$afterFind !== model.$objectionModelClass.prototype.$afterFind) {
|
|
139
|
+
return model.$afterFind;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
160
144
|
}
|
|
161
|
-
|
|
162
145
|
module.exports = {
|
|
163
|
-
|
|
146
|
+
FindOperation,
|
|
164
147
|
};
|
|
@@ -1,27 +1,22 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { QueryBuilderOperation } = require('./QueryBuilderOperation');
|
|
4
|
-
|
|
5
3
|
class FirstOperation extends QueryBuilderOperation {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
onBuildKnex(knexBuilder, builder) {
|
|
5
|
+
const modelClass = builder.modelClass();
|
|
6
|
+
if (builder.isFind() && modelClass.useLimitInFirst) {
|
|
7
|
+
knexBuilder = knexBuilder.limit(1);
|
|
8
|
+
}
|
|
9
|
+
return knexBuilder;
|
|
11
10
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
} else {
|
|
20
|
-
return result;
|
|
11
|
+
onAfter3(_, result) {
|
|
12
|
+
if (Array.isArray(result)) {
|
|
13
|
+
return result[0];
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
21
18
|
}
|
|
22
|
-
}
|
|
23
19
|
}
|
|
24
|
-
|
|
25
20
|
module.exports = {
|
|
26
|
-
|
|
21
|
+
FirstOperation,
|
|
27
22
|
};
|
|
@@ -1,110 +1,91 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { ObjectionToKnexConvertingOperation } = require('./ObjectionToKnexConvertingOperation');
|
|
4
3
|
const { isPlainObject, isString } = require('../../utils/objectUtils');
|
|
5
4
|
const { isModelClass } = require('../../utils/resolveModel');
|
|
6
|
-
|
|
7
5
|
const ALIAS_REGEX = /\s+as\s+/i;
|
|
8
|
-
|
|
9
6
|
// FromOperation corresponds to a `.from(args)` call. The call is delegated to
|
|
10
7
|
// knex, but we first try to parse the arguments so that we can determine which
|
|
11
8
|
// tables have been mentioned in a query's from clause. We only parse string
|
|
12
9
|
// references and not `raw` or `ref` etc. references at this point thouhg.
|
|
13
10
|
class FromOperation extends ObjectionToKnexConvertingOperation {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
this.alias = null;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
onAdd(builder, args) {
|
|
22
|
-
const ret = super.onAdd(builder, args);
|
|
23
|
-
const parsed = parseTableAndAlias(this.args[0], builder);
|
|
24
|
-
|
|
25
|
-
if (parsed.table) {
|
|
26
|
-
builder.tableName(parsed.table);
|
|
27
|
-
this.table = parsed.table;
|
|
11
|
+
constructor(name, opt) {
|
|
12
|
+
super(name, opt);
|
|
13
|
+
this.table = null;
|
|
14
|
+
this.alias = null;
|
|
28
15
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
16
|
+
onAdd(builder, args) {
|
|
17
|
+
const ret = super.onAdd(builder, args);
|
|
18
|
+
const parsed = parseTableAndAlias(this.args[0], builder);
|
|
19
|
+
if (parsed.table) {
|
|
20
|
+
builder.tableName(parsed.table);
|
|
21
|
+
this.table = parsed.table;
|
|
22
|
+
}
|
|
23
|
+
if (parsed.alias) {
|
|
24
|
+
builder.aliasFor(builder.modelClass().getTableName(), parsed.alias);
|
|
25
|
+
this.alias = parsed.alias;
|
|
26
|
+
}
|
|
27
|
+
return ret;
|
|
28
|
+
}
|
|
29
|
+
onBuildKnex(knexBuilder, builder) {
|
|
30
|
+
// Simply call knex's from method with the converted arguments.
|
|
31
|
+
return knexBuilder.from.apply(knexBuilder, this.getKnexArgs(builder));
|
|
32
|
+
}
|
|
33
|
+
clone() {
|
|
34
|
+
const clone = super.clone();
|
|
35
|
+
clone.table = this.table;
|
|
36
|
+
clone.alias = this.alias;
|
|
37
|
+
return clone;
|
|
33
38
|
}
|
|
34
|
-
|
|
35
|
-
return ret;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
onBuildKnex(knexBuilder, builder) {
|
|
39
|
-
// Simply call knex's from method with the converted arguments.
|
|
40
|
-
return knexBuilder.from.apply(knexBuilder, this.getKnexArgs(builder));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
clone() {
|
|
44
|
-
const clone = super.clone();
|
|
45
|
-
|
|
46
|
-
clone.table = this.table;
|
|
47
|
-
clone.alias = this.alias;
|
|
48
|
-
|
|
49
|
-
return clone;
|
|
50
|
-
}
|
|
51
39
|
}
|
|
52
|
-
|
|
53
40
|
function parseTableAndAlias(arg, builder) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
41
|
+
if (isModelClass(arg)) {
|
|
42
|
+
return parseTableAndAliasFromString(arg.getTableName());
|
|
43
|
+
}
|
|
44
|
+
else if (isString(arg)) {
|
|
45
|
+
return parseTableAndAliasFromString(arg);
|
|
46
|
+
}
|
|
47
|
+
else if (isPlainObject(arg)) {
|
|
48
|
+
return parseTableAndAliasFromObject(arg, builder);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
// Could not parse table and alias from the arguments.
|
|
52
|
+
return {
|
|
53
|
+
table: null,
|
|
54
|
+
alias: null,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
67
57
|
}
|
|
68
|
-
|
|
69
58
|
function parseTableAndAliasFromString(arg) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
59
|
+
if (ALIAS_REGEX.test(arg)) {
|
|
60
|
+
const parts = arg.split(ALIAS_REGEX);
|
|
61
|
+
return {
|
|
62
|
+
table: parts[0].trim(),
|
|
63
|
+
alias: parts[1].trim(),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
return {
|
|
68
|
+
table: arg.trim(),
|
|
69
|
+
alias: null,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
83
72
|
}
|
|
84
|
-
|
|
85
73
|
function parseTableAndAliasFromObject(arg, builder) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
table,
|
|
99
|
-
};
|
|
74
|
+
for (const alias of Object.keys(arg)) {
|
|
75
|
+
const value = isModelClass(arg[alias]) ? arg[alias].getTableName() : arg[alias];
|
|
76
|
+
if (!isString(value)) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const table = value.trim();
|
|
80
|
+
if (table === builder.modelClass().getTableName()) {
|
|
81
|
+
return {
|
|
82
|
+
alias,
|
|
83
|
+
table,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
100
86
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
throw new Error(
|
|
104
|
-
`one of the tables in ${JSON.stringify(arg)} must be the query's model class's table.`,
|
|
105
|
-
);
|
|
87
|
+
throw new Error(`one of the tables in ${JSON.stringify(arg)} must be the query's model class's table.`);
|
|
106
88
|
}
|
|
107
|
-
|
|
108
89
|
module.exports = {
|
|
109
|
-
|
|
90
|
+
FromOperation,
|
|
110
91
|
};
|
|
@@ -1,49 +1,38 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { InsertOperation } = require('./InsertOperation');
|
|
4
3
|
const { DelegateOperation } = require('./DelegateOperation');
|
|
5
4
|
const { keyByProps } = require('../../model/modelUtils');
|
|
6
5
|
const { asArray } = require('../../utils/objectUtils');
|
|
7
|
-
|
|
8
6
|
class InsertAndFetchOperation extends DelegateOperation {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
constructor(name, opt) {
|
|
8
|
+
super(name, opt);
|
|
9
|
+
if (!this.delegate.is(InsertOperation)) {
|
|
10
|
+
throw new Error('Invalid delegate');
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
get models() {
|
|
14
|
+
return this.delegate.models;
|
|
15
|
+
}
|
|
16
|
+
async onAfter2(builder, inserted) {
|
|
17
|
+
const modelClass = builder.modelClass();
|
|
18
|
+
const insertedModels = await super.onAfter2(builder, inserted);
|
|
19
|
+
const insertedModelArray = asArray(insertedModels);
|
|
20
|
+
const idProps = modelClass.getIdPropertyArray();
|
|
21
|
+
const ids = insertedModelArray.map((model) => model.$id());
|
|
22
|
+
const fetchedModels = await modelClass
|
|
23
|
+
.query()
|
|
24
|
+
.childQueryOf(builder)
|
|
25
|
+
.findByIds(ids)
|
|
26
|
+
.castTo(builder.resultModelClass());
|
|
27
|
+
const modelsById = keyByProps(fetchedModels, idProps);
|
|
28
|
+
// Instead of returning the freshly fetched models, update the input
|
|
29
|
+
// models with the fresh values.
|
|
30
|
+
insertedModelArray.forEach((insertedModel) => {
|
|
31
|
+
insertedModel.$set(modelsById.get(insertedModel.$propKey(idProps)));
|
|
32
|
+
});
|
|
33
|
+
return insertedModels;
|
|
14
34
|
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
get models() {
|
|
18
|
-
return this.delegate.models;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async onAfter2(builder, inserted) {
|
|
22
|
-
const modelClass = builder.modelClass();
|
|
23
|
-
const insertedModels = await super.onAfter2(builder, inserted);
|
|
24
|
-
|
|
25
|
-
const insertedModelArray = asArray(insertedModels);
|
|
26
|
-
const idProps = modelClass.getIdPropertyArray();
|
|
27
|
-
const ids = insertedModelArray.map((model) => model.$id());
|
|
28
|
-
|
|
29
|
-
const fetchedModels = await modelClass
|
|
30
|
-
.query()
|
|
31
|
-
.childQueryOf(builder)
|
|
32
|
-
.findByIds(ids)
|
|
33
|
-
.castTo(builder.resultModelClass());
|
|
34
|
-
|
|
35
|
-
const modelsById = keyByProps(fetchedModels, idProps);
|
|
36
|
-
|
|
37
|
-
// Instead of returning the freshly fetched models, update the input
|
|
38
|
-
// models with the fresh values.
|
|
39
|
-
insertedModelArray.forEach((insertedModel) => {
|
|
40
|
-
insertedModel.$set(modelsById.get(insertedModel.$propKey(idProps)));
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
return insertedModels;
|
|
44
|
-
}
|
|
45
35
|
}
|
|
46
|
-
|
|
47
36
|
module.exports = {
|
|
48
|
-
|
|
37
|
+
InsertAndFetchOperation,
|
|
49
38
|
};
|
|
@@ -1,45 +1,35 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { DelegateOperation } = require('./DelegateOperation');
|
|
4
3
|
const { InsertGraphOperation } = require('./InsertGraphOperation');
|
|
5
4
|
const { RelationExpression } = require('../RelationExpression');
|
|
6
|
-
|
|
7
5
|
class InsertGraphAndFetchOperation extends DelegateOperation {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
constructor(name, opt) {
|
|
7
|
+
super(name, opt);
|
|
8
|
+
if (!this.delegate.is(InsertGraphOperation)) {
|
|
9
|
+
throw new Error('Invalid delegate');
|
|
10
|
+
}
|
|
13
11
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
12
|
+
get models() {
|
|
13
|
+
return this.delegate.models;
|
|
14
|
+
}
|
|
15
|
+
get isArray() {
|
|
16
|
+
return this.delegate.isArray;
|
|
17
|
+
}
|
|
18
|
+
async onAfter2(builder) {
|
|
19
|
+
if (this.models.length === 0) {
|
|
20
|
+
return this.isArray ? [] : null;
|
|
21
|
+
}
|
|
22
|
+
const eager = RelationExpression.fromModelGraph(this.models);
|
|
23
|
+
const modelClass = this.models[0].constructor;
|
|
24
|
+
const ids = this.models.map((model) => model.$id());
|
|
25
|
+
const models = await modelClass
|
|
26
|
+
.query()
|
|
27
|
+
.childQueryOf(builder)
|
|
28
|
+
.findByIds(ids)
|
|
29
|
+
.withGraphFetched(eager);
|
|
30
|
+
return this.isArray ? models : models[0] || null;
|
|
27
31
|
}
|
|
28
|
-
|
|
29
|
-
const eager = RelationExpression.fromModelGraph(this.models);
|
|
30
|
-
const modelClass = this.models[0].constructor;
|
|
31
|
-
const ids = this.models.map((model) => model.$id());
|
|
32
|
-
|
|
33
|
-
const models = await modelClass
|
|
34
|
-
.query()
|
|
35
|
-
.childQueryOf(builder)
|
|
36
|
-
.findByIds(ids)
|
|
37
|
-
.withGraphFetched(eager);
|
|
38
|
-
|
|
39
|
-
return this.isArray ? models : models[0] || null;
|
|
40
|
-
}
|
|
41
32
|
}
|
|
42
|
-
|
|
43
33
|
module.exports = {
|
|
44
|
-
|
|
34
|
+
InsertGraphAndFetchOperation,
|
|
45
35
|
};
|