@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,82 +1,83 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { isObject } = require('../utils/objectUtils');
|
|
4
|
-
|
|
5
3
|
function parseRelationsIntoModelInstances(model, json, options = {}) {
|
|
6
|
-
|
|
7
|
-
options
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
options.cache.set(json, model);
|
|
13
|
-
|
|
14
|
-
for (const relationName of model.constructor.getRelationNames()) {
|
|
15
|
-
const relationJson = json[relationName];
|
|
16
|
-
|
|
17
|
-
if (relationJson !== undefined) {
|
|
18
|
-
const relation = model.constructor.getRelation(relationName);
|
|
19
|
-
const relationModel = parseRelation(relationJson, relation, options);
|
|
20
|
-
|
|
21
|
-
if (relationModel !== relationJson) {
|
|
22
|
-
model[relation.name] = relationModel;
|
|
23
|
-
}
|
|
4
|
+
const relationNames = model.constructor.getRelationNames();
|
|
5
|
+
if (!options.cache && !hasRelation(json, relationNames)) {
|
|
6
|
+
// Nothing to parse. The cache would only be used for the relations.
|
|
7
|
+
return model;
|
|
24
8
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
9
|
+
if (!options.cache) {
|
|
10
|
+
options = Object.assign({}, options, {
|
|
11
|
+
cache: new Map(),
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
options.cache.set(json, model);
|
|
15
|
+
for (const relationName of relationNames) {
|
|
16
|
+
const relationJson = json[relationName];
|
|
17
|
+
if (relationJson !== undefined) {
|
|
18
|
+
const relation = model.constructor.getRelation(relationName);
|
|
19
|
+
const relationModel = parseRelation(relationJson, relation, options);
|
|
20
|
+
if (relationModel !== relationJson) {
|
|
21
|
+
model[relation.name] = relationModel;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return model;
|
|
26
|
+
}
|
|
27
|
+
function hasRelation(json, relationNames) {
|
|
28
|
+
for (let i = 0, l = relationNames.length; i < l; ++i) {
|
|
29
|
+
if (json[relationNames[i]] !== undefined) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
28
34
|
}
|
|
29
|
-
|
|
30
35
|
function parseRelation(json, relation, options) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
if (Array.isArray(json)) {
|
|
37
|
+
return parseRelationArray(json, relation, options);
|
|
38
|
+
}
|
|
39
|
+
else if (json) {
|
|
40
|
+
return parseRelationObject(json, relation, options);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
38
45
|
}
|
|
39
|
-
|
|
40
46
|
function parseRelationArray(json, relation, options) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
const models = new Array(json.length);
|
|
48
|
+
let didChange = false;
|
|
49
|
+
for (let i = 0, l = json.length; i < l; ++i) {
|
|
50
|
+
const model = parseRelationObject(json[i], relation, options);
|
|
51
|
+
if (model !== json[i]) {
|
|
52
|
+
didChange = true;
|
|
53
|
+
}
|
|
54
|
+
models[i] = model;
|
|
55
|
+
}
|
|
56
|
+
if (didChange) {
|
|
57
|
+
return models;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
return json;
|
|
49
61
|
}
|
|
50
|
-
|
|
51
|
-
models[i] = model;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (didChange) {
|
|
55
|
-
return models;
|
|
56
|
-
} else {
|
|
57
|
-
return json;
|
|
58
|
-
}
|
|
59
62
|
}
|
|
60
|
-
|
|
61
63
|
function parseRelationObject(json, relation, options) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
64
|
+
if (isObject(json)) {
|
|
65
|
+
const modelClass = relation.relatedModelClass;
|
|
66
|
+
let model = options.cache.get(json);
|
|
67
|
+
if (model === undefined) {
|
|
68
|
+
if (json instanceof modelClass) {
|
|
69
|
+
model = parseRelationsIntoModelInstances(json, json, options);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
model = modelClass.fromJson(json, options);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return model;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
return json;
|
|
72
79
|
}
|
|
73
|
-
|
|
74
|
-
return model;
|
|
75
|
-
} else {
|
|
76
|
-
return json;
|
|
77
|
-
}
|
|
78
80
|
}
|
|
79
|
-
|
|
80
81
|
module.exports = {
|
|
81
|
-
|
|
82
|
+
parseRelationsIntoModelInstances,
|
|
82
83
|
};
|
|
@@ -1,147 +1,116 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { isObject, isFunction } = require('../utils/objectUtils');
|
|
4
3
|
const { defineNonEnumerableProperty } = require('./modelUtils');
|
|
5
4
|
const { isKnexRaw, isKnexQueryBuilder } = require('../utils/knexUtils');
|
|
6
|
-
|
|
7
5
|
const QUERY_PROPS_PROPERTY = '$$queryProps';
|
|
8
|
-
|
|
9
6
|
// Removes query properties from `json` and stores them into a hidden property
|
|
10
7
|
// inside `model` so that they can be later merged back to `json`.
|
|
11
8
|
function splitQueryProps(model, json) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
9
|
+
const keys = Object.keys(json);
|
|
10
|
+
if (hasQueryProps(json, keys)) {
|
|
11
|
+
const queryProps = {};
|
|
12
|
+
const modelProps = {};
|
|
13
|
+
for (let i = 0, l = keys.length; i < l; ++i) {
|
|
14
|
+
const key = keys[i];
|
|
15
|
+
const value = json[key];
|
|
16
|
+
if (isQueryProp(value)) {
|
|
17
|
+
queryProps[key] = value;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
modelProps[key] = value;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
defineNonEnumerableProperty(model, QUERY_PROPS_PROPERTY, queryProps);
|
|
24
|
+
return modelProps;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
return json;
|
|
27
28
|
}
|
|
28
|
-
|
|
29
|
-
defineNonEnumerableProperty(model, QUERY_PROPS_PROPERTY, queryProps);
|
|
30
|
-
|
|
31
|
-
return modelProps;
|
|
32
|
-
} else {
|
|
33
|
-
return json;
|
|
34
|
-
}
|
|
35
29
|
}
|
|
36
|
-
|
|
37
30
|
function hasQueryProps(json, keys) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
31
|
+
for (let i = 0, l = keys.length; i < l; ++i) {
|
|
32
|
+
if (isQueryProp(json[keys[i]])) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
41
35
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return false;
|
|
36
|
+
return false;
|
|
45
37
|
}
|
|
46
|
-
|
|
47
38
|
function isQueryProp(value) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
isKnexRawConvertable(value) ||
|
|
56
|
-
value.isObjectionQueryBuilderBase
|
|
57
|
-
);
|
|
39
|
+
if (!isObject(value)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return (isKnexQueryBuilder(value) ||
|
|
43
|
+
isKnexRaw(value) ||
|
|
44
|
+
isKnexRawConvertable(value) ||
|
|
45
|
+
value.isObjectionQueryBuilderBase);
|
|
58
46
|
}
|
|
59
|
-
|
|
60
47
|
// Merges and converts `model`'s query properties into `json`.
|
|
61
48
|
function mergeQueryProps(model, json, omitProps, builder) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return json;
|
|
49
|
+
json = convertExistingQueryProps(json, builder);
|
|
50
|
+
json = convertAndMergeHiddenQueryProps(model, json, omitProps, builder);
|
|
51
|
+
return json;
|
|
66
52
|
}
|
|
67
|
-
|
|
68
53
|
// Converts the query properties in `json` to knex raw instances.
|
|
69
54
|
// `json` may have query properties even though we removed them.
|
|
70
55
|
// For example they may have been added in lifecycle hooks.
|
|
71
56
|
function convertExistingQueryProps(json, builder) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
json[key] = queryPropToKnexRaw(value, builder);
|
|
57
|
+
const keys = Object.keys(json);
|
|
58
|
+
for (let i = 0, l = keys.length; i < l; ++i) {
|
|
59
|
+
const key = keys[i];
|
|
60
|
+
const value = json[key];
|
|
61
|
+
if (isQueryProp(value)) {
|
|
62
|
+
json[key] = queryPropToKnexRaw(value, builder);
|
|
63
|
+
}
|
|
80
64
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return json;
|
|
65
|
+
return json;
|
|
84
66
|
}
|
|
85
|
-
|
|
86
67
|
// Converts and merges the query props that were split from the model
|
|
87
68
|
// and stored into QUERY_PROPS_PROPERTY.
|
|
88
69
|
function convertAndMergeHiddenQueryProps(model, json, omitProps, builder) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (!omitProps || !omitProps.includes(key)) {
|
|
103
|
-
const queryProp = queryPropToKnexRaw(queryProps[key], builder);
|
|
104
|
-
json[modelClass.propertyNameToColumnName(key)] = queryProp;
|
|
70
|
+
const queryProps = model[QUERY_PROPS_PROPERTY];
|
|
71
|
+
if (!queryProps) {
|
|
72
|
+
// The model has no query properties.
|
|
73
|
+
return json;
|
|
74
|
+
}
|
|
75
|
+
const modelClass = model.constructor;
|
|
76
|
+
const keys = Object.keys(queryProps);
|
|
77
|
+
for (let i = 0, l = keys.length; i < l; ++i) {
|
|
78
|
+
const key = keys[i];
|
|
79
|
+
if (!omitProps || !omitProps.includes(key)) {
|
|
80
|
+
const queryProp = queryPropToKnexRaw(queryProps[key], builder);
|
|
81
|
+
json[modelClass.propertyNameToColumnName(key)] = queryProp;
|
|
82
|
+
}
|
|
105
83
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
return json;
|
|
84
|
+
return json;
|
|
109
85
|
}
|
|
110
|
-
|
|
111
86
|
// Converts a query property into a knex `raw` instance.
|
|
112
87
|
function queryPropToKnexRaw(queryProp, builder) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
88
|
+
if (!queryProp) {
|
|
89
|
+
return queryProp;
|
|
90
|
+
}
|
|
91
|
+
if (queryProp.isObjectionQueryBuilderBase) {
|
|
92
|
+
return buildObjectionQueryBuilder(queryProp, builder);
|
|
93
|
+
}
|
|
94
|
+
else if (isKnexRawConvertable(queryProp)) {
|
|
95
|
+
return buildKnexRawConvertable(queryProp, builder);
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
return queryProp;
|
|
99
|
+
}
|
|
124
100
|
}
|
|
125
|
-
|
|
126
101
|
function buildObjectionQueryBuilder(builder, parentBuilder) {
|
|
127
|
-
|
|
102
|
+
return builder.subqueryOf(parentBuilder).toKnexQuery();
|
|
128
103
|
}
|
|
129
|
-
|
|
130
104
|
function buildKnexRawConvertable(convertable, builder) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return convertable.toKnexRaw(builder);
|
|
105
|
+
if (!builder) {
|
|
106
|
+
throw new Error('toDatabaseJson called without a query builder instance for a model with query properties');
|
|
107
|
+
}
|
|
108
|
+
return convertable.toKnexRaw(builder);
|
|
138
109
|
}
|
|
139
|
-
|
|
140
110
|
function isKnexRawConvertable(queryProp) {
|
|
141
|
-
|
|
111
|
+
return isFunction(queryProp.toKnexRaw);
|
|
142
112
|
}
|
|
143
|
-
|
|
144
113
|
module.exports = {
|
|
145
|
-
|
|
146
|
-
|
|
114
|
+
splitQueryProps,
|
|
115
|
+
mergeQueryProps,
|
|
147
116
|
};
|
package/lib/model/modelSet.js
CHANGED
|
@@ -1,136 +1,116 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const { isInternalProp } = require('../utils/internalPropUtils');
|
|
4
3
|
const { splitQueryProps } = require('./modelQueryProps');
|
|
5
4
|
const { isFunction, isString, isPlainObject } = require('../utils/objectUtils');
|
|
6
5
|
const { parseRelationsIntoModelInstances } = require('./modelParseRelations');
|
|
7
|
-
|
|
8
6
|
function setJson(model, json, options) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
model.$set(json);
|
|
31
|
-
|
|
32
|
-
if (!options.skipParseRelations) {
|
|
33
|
-
parseRelationsIntoModelInstances(model, json, options);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return model;
|
|
7
|
+
json = json || {};
|
|
8
|
+
options = options || {};
|
|
9
|
+
if (Object.prototype.toString.call(json) !== '[object Object]') {
|
|
10
|
+
throw new Error('You should only pass objects to $setJson method. ' +
|
|
11
|
+
'$setJson method was given an invalid value ' +
|
|
12
|
+
json);
|
|
13
|
+
}
|
|
14
|
+
if (!json.$isObjectionModel) {
|
|
15
|
+
// json can contain "query properties" like `raw` instances, query builders etc.
|
|
16
|
+
// We take them out of `json` and store them to a hidden property $$queryProps
|
|
17
|
+
// in the model instance for later use.
|
|
18
|
+
json = splitQueryProps(model, json);
|
|
19
|
+
}
|
|
20
|
+
json = model.$parseJson(json, options);
|
|
21
|
+
json = model.$validate(json, options);
|
|
22
|
+
model.$set(json);
|
|
23
|
+
if (!options.skipParseRelations) {
|
|
24
|
+
parseRelationsIntoModelInstances(model, json, options);
|
|
25
|
+
}
|
|
26
|
+
return model;
|
|
37
27
|
}
|
|
38
|
-
|
|
39
28
|
function setDatabaseJson(model, json) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
model[key] = json[key];
|
|
29
|
+
json = model.$parseDatabaseJson(json);
|
|
30
|
+
if (json) {
|
|
31
|
+
const keys = Object.keys(json);
|
|
32
|
+
for (let i = 0, l = keys.length; i < l; ++i) {
|
|
33
|
+
const key = keys[i];
|
|
34
|
+
model[key] = json[key];
|
|
35
|
+
}
|
|
48
36
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return model;
|
|
37
|
+
return model;
|
|
52
38
|
}
|
|
53
|
-
|
|
54
39
|
function setFast(model, obj) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
) {
|
|
72
|
-
model[key] = value;
|
|
73
|
-
}
|
|
40
|
+
if (obj) {
|
|
41
|
+
// Don't try to set read-only properties. They can easily get here
|
|
42
|
+
// through `fromJson` when parsing an object that was previously
|
|
43
|
+
// serialized from a model instance.
|
|
44
|
+
const readOnlyAttr = model.constructor.getReadOnlyAttributes();
|
|
45
|
+
const keys = Object.keys(obj);
|
|
46
|
+
for (let i = 0, l = keys.length; i < l; ++i) {
|
|
47
|
+
const key = keys[i];
|
|
48
|
+
const value = obj[key];
|
|
49
|
+
if ((value !== undefined || isPlainObject(obj)) &&
|
|
50
|
+
!isInternalProp(key) &&
|
|
51
|
+
!isFunction(value) &&
|
|
52
|
+
!readOnlyAttr.includes(key)) {
|
|
53
|
+
model[key] = value;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
74
56
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return model;
|
|
57
|
+
return model;
|
|
78
58
|
}
|
|
79
|
-
|
|
80
59
|
function setRelated(model, relation, models) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
60
|
+
relation = ensureRelation(model, relation);
|
|
61
|
+
if (relation.isOneToOne()) {
|
|
62
|
+
if (Array.isArray(models)) {
|
|
63
|
+
if (models.length === 0) {
|
|
64
|
+
model[relation.name] = null;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
model[relation.name] = models[0] || null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
model[relation.name] = models || null;
|
|
72
|
+
}
|
|
92
73
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
74
|
+
else {
|
|
75
|
+
if (!models) {
|
|
76
|
+
model[relation.name] = [];
|
|
77
|
+
}
|
|
78
|
+
else if (Array.isArray(models)) {
|
|
79
|
+
model[relation.name] = models;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
model[relation.name] = [models];
|
|
83
|
+
}
|
|
100
84
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
return model;
|
|
85
|
+
return model;
|
|
104
86
|
}
|
|
105
|
-
|
|
106
87
|
function appendRelated(model, relation, models) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return model.$setRelated(relation, models);
|
|
111
|
-
} else {
|
|
112
|
-
if (Array.isArray(models)) {
|
|
113
|
-
models.forEach((it) => model[relation.name].push(it));
|
|
114
|
-
} else if (models) {
|
|
115
|
-
model[relation.name].push(models);
|
|
88
|
+
relation = ensureRelation(model, relation);
|
|
89
|
+
if (!model[relation.name] || relation.isOneToOne()) {
|
|
90
|
+
return model.$setRelated(relation, models);
|
|
116
91
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
92
|
+
else {
|
|
93
|
+
if (Array.isArray(models)) {
|
|
94
|
+
models.forEach((it) => model[relation.name].push(it));
|
|
95
|
+
}
|
|
96
|
+
else if (models) {
|
|
97
|
+
model[relation.name].push(models);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return model;
|
|
120
101
|
}
|
|
121
|
-
|
|
122
102
|
function ensureRelation(model, relation) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
103
|
+
if (isString(relation)) {
|
|
104
|
+
return model.constructor.getRelation(relation);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
return relation;
|
|
108
|
+
}
|
|
128
109
|
}
|
|
129
|
-
|
|
130
110
|
module.exports = {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
111
|
+
setFast,
|
|
112
|
+
setJson,
|
|
113
|
+
setDatabaseJson,
|
|
114
|
+
setRelated,
|
|
115
|
+
appendRelated,
|
|
136
116
|
};
|