@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.
Files changed (192) hide show
  1. package/README.md +103 -67
  2. package/lib/index.mjs +6 -0
  3. package/lib/initialize.js +6 -9
  4. package/lib/model/AjvValidator.js +235 -282
  5. package/lib/model/Model.js +653 -823
  6. package/lib/model/ModifierNotFoundError.js +5 -7
  7. package/lib/model/NotFoundError.js +14 -18
  8. package/lib/model/RelationDoesNotExistError.js +6 -9
  9. package/lib/model/ValidationError.js +33 -41
  10. package/lib/model/Validator.js +15 -21
  11. package/lib/model/getModel.js +1 -3
  12. package/lib/model/graph/ModelGraph.js +44 -64
  13. package/lib/model/graph/ModelGraphBuilder.js +164 -217
  14. package/lib/model/graph/ModelGraphEdge.js +31 -43
  15. package/lib/model/graph/ModelGraphNode.js +192 -231
  16. package/lib/model/inheritModel.js +9 -18
  17. package/lib/model/modelBindKnex.js +48 -62
  18. package/lib/model/modelClone.js +79 -98
  19. package/lib/model/modelColPropMap.js +16 -26
  20. package/lib/model/modelConcurrency.js +24 -0
  21. package/lib/model/modelId.js +44 -49
  22. package/lib/model/modelJsonAttributes.js +56 -76
  23. package/lib/model/modelParseRelations.js +68 -67
  24. package/lib/model/modelQueryProps.js +76 -107
  25. package/lib/model/modelSet.js +92 -112
  26. package/lib/model/modelTableMetadata.js +60 -72
  27. package/lib/model/modelToJson.js +120 -150
  28. package/lib/model/modelUtils.js +30 -37
  29. package/lib/model/modelValidate.js +34 -42
  30. package/lib/model/modelValues.js +78 -91
  31. package/lib/model/modelVisitor.js +22 -29
  32. package/lib/objection.js +42 -67
  33. package/lib/queryBuilder/FunctionBuilder.js +231 -279
  34. package/lib/queryBuilder/InternalOptions.js +19 -24
  35. package/lib/queryBuilder/JoinBuilder.js +116 -149
  36. package/lib/queryBuilder/QueryBuilder.js +1009 -1271
  37. package/lib/queryBuilder/QueryBuilderBase.js +647 -865
  38. package/lib/queryBuilder/QueryBuilderContext.js +14 -21
  39. package/lib/queryBuilder/QueryBuilderContextBase.js +20 -29
  40. package/lib/queryBuilder/QueryBuilderOperationSupport.js +462 -536
  41. package/lib/queryBuilder/QueryBuilderUserContext.js +19 -25
  42. package/lib/queryBuilder/RawBuilder.js +58 -73
  43. package/lib/queryBuilder/ReferenceBuilder.js +232 -270
  44. package/lib/queryBuilder/RelationExpression.js +322 -380
  45. package/lib/queryBuilder/StaticHookArguments.js +94 -106
  46. package/lib/queryBuilder/ValueBuilder.js +100 -112
  47. package/lib/queryBuilder/expressions.js +86 -116
  48. package/lib/queryBuilder/functions/mysql.js +642 -697
  49. package/lib/queryBuilder/graph/GraphAction.js +44 -59
  50. package/lib/queryBuilder/graph/GraphData.js +7 -9
  51. package/lib/queryBuilder/graph/GraphFetcher.js +115 -151
  52. package/lib/queryBuilder/graph/GraphNodeDbExistence.js +114 -152
  53. package/lib/queryBuilder/graph/GraphOperation.js +16 -22
  54. package/lib/queryBuilder/graph/GraphOptions.js +143 -185
  55. package/lib/queryBuilder/graph/GraphUpsert.js +167 -241
  56. package/lib/queryBuilder/graph/delete/GraphDelete.js +8 -13
  57. package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +44 -62
  58. package/lib/queryBuilder/graph/insert/GraphInsert.js +99 -128
  59. package/lib/queryBuilder/graph/insert/GraphInsertAction.js +151 -193
  60. package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +57 -79
  61. package/lib/queryBuilder/graph/patch/GraphPatch.js +8 -13
  62. package/lib/queryBuilder/graph/patch/GraphPatchAction.js +181 -234
  63. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +16 -21
  64. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +80 -110
  65. package/lib/queryBuilder/join/JoinResultColumn.js +14 -18
  66. package/lib/queryBuilder/join/JoinResultParser.js +88 -101
  67. package/lib/queryBuilder/join/RelationJoiner.js +205 -288
  68. package/lib/queryBuilder/join/TableNode.js +117 -144
  69. package/lib/queryBuilder/join/TableTree.js +60 -76
  70. package/lib/queryBuilder/join/utils.js +10 -16
  71. package/lib/queryBuilder/operations/DelegateOperation.js +93 -133
  72. package/lib/queryBuilder/operations/DeleteOperation.js +33 -37
  73. package/lib/queryBuilder/operations/FindByIdOperation.js +20 -28
  74. package/lib/queryBuilder/operations/FindByIdsOperation.js +17 -23
  75. package/lib/queryBuilder/operations/FindOperation.js +114 -131
  76. package/lib/queryBuilder/operations/FirstOperation.js +14 -19
  77. package/lib/queryBuilder/operations/FromOperation.js +69 -88
  78. package/lib/queryBuilder/operations/InsertAndFetchOperation.js +28 -39
  79. package/lib/queryBuilder/operations/InsertGraphAndFetchOperation.js +25 -35
  80. package/lib/queryBuilder/operations/InsertGraphOperation.js +77 -98
  81. package/lib/queryBuilder/operations/InsertOperation.js +132 -165
  82. package/lib/queryBuilder/operations/InstanceDeleteOperation.js +27 -36
  83. package/lib/queryBuilder/operations/InstanceFindOperation.js +9 -13
  84. package/lib/queryBuilder/operations/InstanceInsertOperation.js +13 -17
  85. package/lib/queryBuilder/operations/InstanceUpdateOperation.js +32 -46
  86. package/lib/queryBuilder/operations/JoinRelatedOperation.js +66 -86
  87. package/lib/queryBuilder/operations/KnexOperation.js +4 -7
  88. package/lib/queryBuilder/operations/MergeOperation.js +32 -45
  89. package/lib/queryBuilder/operations/ObjectionToKnexConvertingOperation.js +131 -150
  90. package/lib/queryBuilder/operations/OnBuildKnexOperation.js +17 -23
  91. package/lib/queryBuilder/operations/OnBuildOperation.js +17 -23
  92. package/lib/queryBuilder/operations/OnErrorOperation.js +17 -23
  93. package/lib/queryBuilder/operations/QueryBuilderOperation.js +231 -270
  94. package/lib/queryBuilder/operations/RangeOperation.js +30 -40
  95. package/lib/queryBuilder/operations/RelateOperation.js +14 -21
  96. package/lib/queryBuilder/operations/ReturningOperation.js +12 -18
  97. package/lib/queryBuilder/operations/RunAfterOperation.js +17 -23
  98. package/lib/queryBuilder/operations/RunBeforeOperation.js +17 -23
  99. package/lib/queryBuilder/operations/UnrelateOperation.js +12 -17
  100. package/lib/queryBuilder/operations/UpdateAndFetchOperation.js +48 -65
  101. package/lib/queryBuilder/operations/UpdateOperation.js +104 -129
  102. package/lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js +25 -35
  103. package/lib/queryBuilder/operations/UpsertGraphOperation.js +36 -55
  104. package/lib/queryBuilder/operations/WhereCompositeOperation.js +30 -39
  105. package/lib/queryBuilder/operations/WhereInOperation.js +34 -36
  106. package/lib/queryBuilder/operations/eager/EagerOperation.js +37 -52
  107. package/lib/queryBuilder/operations/eager/JoinEagerOperation.js +34 -44
  108. package/lib/queryBuilder/operations/eager/NaiveEagerOperation.js +4 -7
  109. package/lib/queryBuilder/operations/eager/WhereInEagerOperation.js +193 -251
  110. package/lib/queryBuilder/operations/jsonApi/WhereJsonHasOperation.js +18 -25
  111. package/lib/queryBuilder/operations/jsonApi/WhereJsonNotObjectOperation.js +25 -41
  112. package/lib/queryBuilder/operations/jsonApi/WhereJsonOperation.js +19 -20
  113. package/lib/queryBuilder/operations/jsonApi/WhereJsonOverlapsOperation.js +23 -21
  114. package/lib/queryBuilder/operations/jsonApi/mysqlJsonApi.js +106 -118
  115. package/lib/queryBuilder/operations/jsonApi/postgresJsonApi.js +79 -108
  116. package/lib/queryBuilder/operations/select/SelectOperation.js +37 -53
  117. package/lib/queryBuilder/operations/select/Selection.js +94 -105
  118. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeMsSqlOperation.js +68 -101
  119. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeOperation.js +65 -75
  120. package/lib/queryBuilder/operations/whereInComposite/WhereInCompositeSqliteOperation.js +75 -84
  121. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.js +760 -859
  122. package/lib/queryBuilder/parsers/relationExpressionParser.js +840 -952
  123. package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +10 -15
  124. package/lib/queryBuilder/transformations/QueryTransformation.js +4 -6
  125. package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +35 -49
  126. package/lib/queryBuilder/transformations/index.js +3 -9
  127. package/lib/queryBuilder/whereScopes.js +158 -186
  128. package/lib/relations/Relation.js +279 -363
  129. package/lib/relations/RelationDeleteOperation.js +16 -22
  130. package/lib/relations/RelationFindOperation.js +100 -133
  131. package/lib/relations/RelationInsertOperation.js +19 -27
  132. package/lib/relations/RelationOwner.js +167 -204
  133. package/lib/relations/RelationProperty.js +177 -217
  134. package/lib/relations/RelationUpdateOperation.js +16 -23
  135. package/lib/relations/belongsToOne/BelongsToOneDeleteOperation.js +11 -16
  136. package/lib/relations/belongsToOne/BelongsToOneInsertOperation.js +24 -35
  137. package/lib/relations/belongsToOne/BelongsToOneRelateOperation.js +30 -41
  138. package/lib/relations/belongsToOne/BelongsToOneRelation.js +33 -41
  139. package/lib/relations/belongsToOne/BelongsToOneUnrelateOperation.js +38 -53
  140. package/lib/relations/hasMany/HasManyInsertOperation.js +35 -51
  141. package/lib/relations/hasMany/HasManyRelateOperation.js +38 -54
  142. package/lib/relations/hasMany/HasManyRelation.js +30 -38
  143. package/lib/relations/hasMany/HasManyUnrelateOperation.js +19 -26
  144. package/lib/relations/hasOne/HasOneRelation.js +4 -7
  145. package/lib/relations/hasOneThrough/HasOneThroughRelation.js +4 -7
  146. package/lib/relations/manyToMany/ManyToManyModifyMixin.js +67 -90
  147. package/lib/relations/manyToMany/ManyToManyRelation.js +349 -451
  148. package/lib/relations/manyToMany/ManyToManySqliteModifyMixin.js +19 -26
  149. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperation.js +3 -5
  150. package/lib/relations/manyToMany/delete/ManyToManyDeleteOperationBase.js +14 -20
  151. package/lib/relations/manyToMany/delete/ManyToManyDeleteSqliteOperation.js +3 -7
  152. package/lib/relations/manyToMany/find/ManyToManyFindOperation.js +71 -94
  153. package/lib/relations/manyToMany/insert/ManyToManyInsertOperation.js +31 -43
  154. package/lib/relations/manyToMany/relate/ManyToManyRelateOperation.js +50 -67
  155. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperation.js +4 -7
  156. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateOperationBase.js +20 -27
  157. package/lib/relations/manyToMany/unrelate/ManyToManyUnrelateSqliteOperation.js +5 -10
  158. package/lib/relations/manyToMany/update/ManyToManyUpdateOperation.js +3 -5
  159. package/lib/relations/manyToMany/update/ManyToManyUpdateOperationBase.js +55 -72
  160. package/lib/relations/manyToMany/update/ManyToManyUpdateSqliteOperation.js +3 -7
  161. package/lib/transaction.js +53 -83
  162. package/lib/utils/assert.js +15 -21
  163. package/lib/utils/buildUtils.js +17 -19
  164. package/lib/utils/classUtils.js +7 -11
  165. package/lib/utils/cloneDeep.js +365 -0
  166. package/lib/utils/createModifier.js +36 -41
  167. package/lib/utils/deprecate.js +8 -12
  168. package/lib/utils/identifierMapping.js +153 -192
  169. package/lib/utils/internalPropUtils.js +4 -8
  170. package/lib/utils/knexUtils.js +61 -56
  171. package/lib/utils/mixin.js +16 -23
  172. package/lib/utils/mysqlJsonPath.js +55 -40
  173. package/lib/utils/normalizeIds.js +110 -127
  174. package/lib/utils/objectUtils.js +236 -338
  175. package/lib/utils/parseFieldExpression.js +66 -44
  176. package/lib/utils/postgresJsonPath.js +48 -62
  177. package/lib/utils/promiseUtils/after.js +10 -13
  178. package/lib/utils/promiseUtils/afterReturn.js +10 -13
  179. package/lib/utils/promiseUtils/index.js +15 -17
  180. package/lib/utils/promiseUtils/isPromise.js +6 -9
  181. package/lib/utils/promiseUtils/map.js +75 -89
  182. package/lib/utils/promiseUtils/mapAfterAllReturn.js +17 -23
  183. package/lib/utils/promiseUtils/try.js +15 -18
  184. package/lib/utils/resolveModel.js +91 -94
  185. package/lib/utils/tmpColumnUtils.js +4 -13
  186. package/package.json +50 -23
  187. package/typings/objection/index.d.mts +362 -0
  188. package/typings/objection/index.d.ts +36 -9
  189. package/lib/.eslintrc.json +0 -8
  190. package/lib/queryBuilder/parsers/jsonFieldExpressionParser.pegjs +0 -97
  191. package/lib/queryBuilder/parsers/relationExpressionParser.pegjs +0 -141
  192. package/lib/utils/clone.js +0 -2329
@@ -1,121 +1,102 @@
1
1
  'use strict';
2
-
3
2
  const { isObject, cloneDeep } = require('../utils/objectUtils');
4
3
  const { hiddenProps } = require('./modelUtils');
5
4
  const { defineNonEnumerableProperty } = require('./modelUtils');
6
5
  const { isInternalProp } = require('../utils/internalPropUtils');
7
-
8
6
  function clone(model, shallow, stripInternal) {
9
- let clone = null;
10
-
11
- const omitFromJson = model.$omitFromJson();
12
- const omitFromDatabaseJson = model.$omitFromDatabaseJson();
13
-
14
- if (!shallow && !stripInternal) {
15
- clone = cloneSimple(model);
16
- } else {
17
- clone = cloneWithOpt(model, shallow, stripInternal);
18
- }
19
-
20
- if (omitFromJson) {
21
- clone.$omitFromJson(omitFromJson);
22
- }
23
-
24
- if (omitFromDatabaseJson) {
25
- clone.$omitFromDatabaseJson(omitFromDatabaseJson);
26
- }
27
-
28
- clone = copyHiddenProps(model, clone);
29
- return clone;
7
+ let clone = null;
8
+ const omitFromJson = model.$omitFromJson();
9
+ const omitFromDatabaseJson = model.$omitFromDatabaseJson();
10
+ if (!shallow && !stripInternal) {
11
+ clone = cloneSimple(model);
12
+ }
13
+ else {
14
+ clone = cloneWithOpt(model, shallow, stripInternal);
15
+ }
16
+ if (omitFromJson) {
17
+ clone.$omitFromJson(omitFromJson);
18
+ }
19
+ if (omitFromDatabaseJson) {
20
+ clone.$omitFromDatabaseJson(omitFromDatabaseJson);
21
+ }
22
+ clone = copyHiddenProps(model, clone);
23
+ return clone;
30
24
  }
31
-
32
25
  function cloneSimple(model) {
33
- const clone = new model.constructor();
34
- const keys = Object.keys(model);
35
-
36
- for (let i = 0, l = keys.length; i < l; ++i) {
37
- const key = keys[i];
38
- const value = model[key];
39
-
40
- if (isObject(value)) {
41
- clone[key] = cloneObject(value);
42
- } else {
43
- clone[key] = value;
26
+ const clone = new model.constructor();
27
+ const keys = Object.keys(model);
28
+ for (let i = 0, l = keys.length; i < l; ++i) {
29
+ const key = keys[i];
30
+ const value = model[key];
31
+ if (isObject(value)) {
32
+ clone[key] = cloneObject(value);
33
+ }
34
+ else {
35
+ clone[key] = value;
36
+ }
44
37
  }
45
- }
46
-
47
- return clone;
38
+ return clone;
48
39
  }
49
-
50
40
  function cloneWithOpt(model, shallow, stripInternal) {
51
- const clone = new model.constructor();
52
- const keys = Object.keys(model);
53
- const relationNames = model.constructor.getRelationNames();
54
-
55
- for (let i = 0, l = keys.length; i < l; ++i) {
56
- const key = keys[i];
57
- const value = model[key];
58
-
59
- if (shallow && relationNames.includes(key)) {
60
- // The constructor may have given default values for relations.
61
- delete clone[key];
62
- continue;
63
- }
64
-
65
- if (stripInternal && isInternalProp(key)) {
66
- continue;
41
+ const clone = new model.constructor();
42
+ const keys = Object.keys(model);
43
+ const relationNames = model.constructor.getRelationNames();
44
+ for (let i = 0, l = keys.length; i < l; ++i) {
45
+ const key = keys[i];
46
+ const value = model[key];
47
+ if (shallow && relationNames.includes(key)) {
48
+ // The constructor may have given default values for relations.
49
+ delete clone[key];
50
+ continue;
51
+ }
52
+ if (stripInternal && isInternalProp(key)) {
53
+ continue;
54
+ }
55
+ if (isObject(value)) {
56
+ clone[key] = cloneObject(value);
57
+ }
58
+ else {
59
+ clone[key] = value;
60
+ }
67
61
  }
68
-
69
- if (isObject(value)) {
70
- clone[key] = cloneObject(value);
71
- } else {
72
- clone[key] = value;
73
- }
74
- }
75
-
76
- return clone;
62
+ return clone;
77
63
  }
78
-
79
64
  function cloneObject(value) {
80
- if (Array.isArray(value)) {
81
- return cloneArray(value);
82
- } else if (value.$isObjectionModel) {
83
- return clone(value, false, false);
84
- } else if (Buffer.isBuffer(value)) {
85
- return new Buffer(value);
86
- } else {
87
- return cloneDeep(value);
88
- }
65
+ if (Array.isArray(value)) {
66
+ return cloneArray(value);
67
+ }
68
+ else if (value.$isObjectionModel) {
69
+ return clone(value, false, false);
70
+ }
71
+ else if (Buffer.isBuffer(value)) {
72
+ return Buffer.from(value);
73
+ }
74
+ else {
75
+ return cloneDeep(value);
76
+ }
89
77
  }
90
-
91
78
  function cloneArray(value) {
92
- const ret = new Array(value.length);
93
-
94
- for (let i = 0, l = ret.length; i < l; ++i) {
95
- const item = value[i];
96
-
97
- if (isObject(item)) {
98
- ret[i] = cloneObject(item);
99
- } else {
100
- ret[i] = item;
79
+ const ret = new Array(value.length);
80
+ for (let i = 0, l = ret.length; i < l; ++i) {
81
+ const item = value[i];
82
+ if (isObject(item)) {
83
+ ret[i] = cloneObject(item);
84
+ }
85
+ else {
86
+ ret[i] = item;
87
+ }
101
88
  }
102
- }
103
-
104
- return ret;
89
+ return ret;
105
90
  }
106
-
107
91
  function copyHiddenProps(model, clone) {
108
- for (let i = 0, l = hiddenProps.length; i < l; ++i) {
109
- const prop = hiddenProps[i];
110
-
111
- if (model.hasOwnProperty(prop)) {
112
- defineNonEnumerableProperty(clone, prop, model[prop]);
92
+ for (let i = 0, l = hiddenProps.length; i < l; ++i) {
93
+ const prop = hiddenProps[i];
94
+ if (Object.hasOwn(model, prop)) {
95
+ defineNonEnumerableProperty(clone, prop, model[prop]);
96
+ }
113
97
  }
114
- }
115
-
116
- return clone;
98
+ return clone;
117
99
  }
118
-
119
100
  module.exports = {
120
- clone,
101
+ clone,
121
102
  };
@@ -1,34 +1,24 @@
1
1
  'use strict';
2
-
3
2
  const { difference } = require('../utils/objectUtils');
4
-
5
3
  function columnNameToPropertyName(modelClass, columnName) {
6
- const model = new modelClass();
7
- const addedProps = Object.keys(model.$parseDatabaseJson({}));
8
-
9
- const row = {};
10
- row[columnName] = null;
11
-
12
- const props = Object.keys(model.$parseDatabaseJson(row));
13
- const propertyName = difference(props, addedProps)[0];
14
-
15
- return propertyName || columnName;
4
+ const model = new modelClass();
5
+ const addedProps = Object.keys(model.$parseDatabaseJson({}));
6
+ const row = {};
7
+ row[columnName] = null;
8
+ const props = Object.keys(model.$parseDatabaseJson(row));
9
+ const propertyName = difference(props, addedProps)[0];
10
+ return propertyName || columnName;
16
11
  }
17
-
18
12
  function propertyNameToColumnName(modelClass, propertyName) {
19
- const model = new modelClass();
20
- const addedCols = Object.keys(model.$formatDatabaseJson({}));
21
-
22
- const obj = {};
23
- obj[propertyName] = null;
24
-
25
- const cols = Object.keys(model.$formatDatabaseJson(obj));
26
- const columnName = difference(cols, addedCols)[0];
27
-
28
- return columnName || propertyName;
13
+ const model = new modelClass();
14
+ const addedCols = Object.keys(model.$formatDatabaseJson({}));
15
+ const obj = {};
16
+ obj[propertyName] = null;
17
+ const cols = Object.keys(model.$formatDatabaseJson(obj));
18
+ const columnName = difference(cols, addedCols)[0];
19
+ return columnName || propertyName;
29
20
  }
30
-
31
21
  module.exports = {
32
- columnNameToPropertyName,
33
- propertyNameToColumnName,
22
+ columnNameToPropertyName,
23
+ propertyNameToColumnName,
34
24
  };
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+ const { isMsSql } = require('../utils/knexUtils');
3
+ const DEFAULT_CONCURRENCY = 4;
4
+ let defaultGetConcurrency = null;
5
+ // Called by Model.js with Model.getConcurrency.
6
+ function setDefaultGetConcurrency(getConcurrency) {
7
+ defaultGetConcurrency = getConcurrency;
8
+ }
9
+ // The concurrency of the queries that only read, like the queries of
10
+ // `withGraphFetched()`. By default `getConcurrency()` is 1 on MySQL, because
11
+ // concurrent graph updates and deletes deadlock there, but reads don't. The
12
+ // concurrency a model sets with `concurrency` or `getConcurrency()` is used as
13
+ // is.
14
+ function getReadConcurrency(modelClass, knex) {
15
+ if (modelClass.concurrency !== null || modelClass.getConcurrency !== defaultGetConcurrency) {
16
+ return modelClass.getConcurrency(knex);
17
+ }
18
+ // The mssql driver can't run concurrent queries.
19
+ return knex && isMsSql(knex) ? 1 : DEFAULT_CONCURRENCY;
20
+ }
21
+ module.exports = {
22
+ setDefaultGetConcurrency,
23
+ getReadConcurrency,
24
+ };
@@ -1,62 +1,57 @@
1
1
  'use strict';
2
-
3
2
  function getSetId(model, maybeId) {
4
- if (maybeId !== undefined) {
5
- return setId(model, maybeId);
6
- } else {
7
- return getId(model);
8
- }
3
+ if (maybeId !== undefined) {
4
+ return setId(model, maybeId);
5
+ }
6
+ else {
7
+ return getId(model);
8
+ }
9
9
  }
10
-
11
10
  function hasId(model) {
12
- return model.$hasProps(model.constructor.getIdPropertyArray());
11
+ return model.$hasProps(model.constructor.getIdPropertyArray());
13
12
  }
14
-
15
13
  function setId(model, id) {
16
- const idProp = model.constructor.getIdProperty();
17
- const isCompositeId = Array.isArray(idProp);
18
-
19
- if (Array.isArray(id)) {
20
- if (isCompositeId) {
21
- if (id.length !== idProp.length) {
22
- throw new Error('trying to set an invalid identifier for a model');
23
- }
24
-
25
- for (let i = 0; i < id.length; ++i) {
26
- model[idProp[i]] = id[i];
27
- }
28
- } else {
29
- if (id.length !== 1) {
30
- throw new Error('trying to set an invalid identifier for a model');
31
- }
32
-
33
- model[idProp] = id[0];
14
+ const idProp = model.constructor.getIdProperty();
15
+ const isCompositeId = Array.isArray(idProp);
16
+ if (Array.isArray(id)) {
17
+ if (isCompositeId) {
18
+ if (id.length !== idProp.length) {
19
+ throw new Error('trying to set an invalid identifier for a model');
20
+ }
21
+ for (let i = 0; i < id.length; ++i) {
22
+ model[idProp[i]] = id[i];
23
+ }
24
+ }
25
+ else {
26
+ if (id.length !== 1) {
27
+ throw new Error('trying to set an invalid identifier for a model');
28
+ }
29
+ model[idProp] = id[0];
30
+ }
34
31
  }
35
- } else {
36
- if (isCompositeId) {
37
- if (idProp.length > 1) {
38
- throw new Error('trying to set an invalid identifier for a model');
39
- }
40
-
41
- model[idProp[0]] = id;
42
- } else {
43
- model[idProp] = id;
32
+ else {
33
+ if (isCompositeId) {
34
+ if (idProp.length > 1) {
35
+ throw new Error('trying to set an invalid identifier for a model');
36
+ }
37
+ model[idProp[0]] = id;
38
+ }
39
+ else {
40
+ model[idProp] = id;
41
+ }
44
42
  }
45
- }
46
43
  }
47
-
48
44
  function getId(model) {
49
- const idProp = model.constructor.getIdProperty();
50
- const isCompositeId = Array.isArray(idProp);
51
-
52
- if (isCompositeId) {
53
- return model.$values(idProp);
54
- } else {
55
- return model[idProp];
56
- }
45
+ const idProp = model.constructor.getIdProperty();
46
+ const isCompositeId = Array.isArray(idProp);
47
+ if (isCompositeId) {
48
+ return model.$values(idProp);
49
+ }
50
+ else {
51
+ return model[idProp];
52
+ }
57
53
  }
58
-
59
54
  module.exports = {
60
- getSetId,
61
- hasId,
55
+ getSetId,
56
+ hasId,
62
57
  };
@@ -1,92 +1,72 @@
1
1
  'use strict';
2
-
3
2
  const { asArray, isObject, flatten, isString } = require('../utils/objectUtils');
4
-
5
3
  function parseJsonAttributes(json, modelClass) {
6
- const jsonAttr = modelClass.getJsonAttributes();
7
-
8
- if (jsonAttr.length) {
9
- // JSON attributes may be returned as strings depending on the database and
10
- // the database client. Convert them to objects here.
11
- for (let i = 0, l = jsonAttr.length; i < l; ++i) {
12
- const attr = jsonAttr[i];
13
- const value = json[attr];
14
-
15
- if (isString(value)) {
16
- const parsed = tryParseJson(value);
17
-
18
- // tryParseJson returns undefined if parsing failed.
19
- if (parsed !== undefined) {
20
- json[attr] = parsed;
4
+ const jsonAttr = modelClass.getJsonAttributes();
5
+ if (jsonAttr.length) {
6
+ // JSON attributes may be returned as strings depending on the database and
7
+ // the database client. Convert them to objects here.
8
+ for (let i = 0, l = jsonAttr.length; i < l; ++i) {
9
+ const attr = jsonAttr[i];
10
+ const value = json[attr];
11
+ if (isString(value)) {
12
+ const parsed = tryParseJson(value);
13
+ // tryParseJson returns undefined if parsing failed.
14
+ if (parsed !== undefined) {
15
+ json[attr] = parsed;
16
+ }
17
+ }
21
18
  }
22
- }
23
19
  }
24
- }
25
-
26
- return json;
20
+ return json;
27
21
  }
28
-
29
22
  function formatJsonAttributes(json, modelClass) {
30
- const jsonAttr = modelClass.getJsonAttributes();
31
-
32
- if (jsonAttr.length) {
33
- // All database clients want JSON columns as strings. Do the conversion here.
34
- for (let i = 0, l = jsonAttr.length; i < l; ++i) {
35
- const attr = jsonAttr[i];
36
- const value = json[attr];
37
-
38
- if (value != null) {
39
- json[attr] = JSON.stringify(value);
40
- }
23
+ const jsonAttr = modelClass.getJsonAttributes();
24
+ if (jsonAttr.length) {
25
+ // All database clients want JSON columns as strings. Do the conversion here.
26
+ for (let i = 0, l = jsonAttr.length; i < l; ++i) {
27
+ const attr = jsonAttr[i];
28
+ const value = json[attr];
29
+ if (value != null) {
30
+ json[attr] = JSON.stringify(value);
31
+ }
32
+ }
41
33
  }
42
- }
43
-
44
- return json;
34
+ return json;
45
35
  }
46
-
47
36
  function getJsonAttributes(modelClass) {
48
- let jsonAttributes = modelClass.jsonAttributes;
49
-
50
- if (Array.isArray(jsonAttributes)) {
51
- return jsonAttributes;
52
- }
53
-
54
- jsonAttributes = [];
55
-
56
- if (modelClass.getJsonSchema()) {
57
- const props = modelClass.getJsonSchema().properties || {};
58
-
59
- for (const propName of Object.keys(props)) {
60
- const prop = props[propName];
61
- let types = asArray(prop.type).filter((it) => !!it);
62
-
63
- if (types.length === 0 && Array.isArray(prop.anyOf)) {
64
- types = flatten(prop.anyOf.map((it) => it.type));
65
- }
66
-
67
- if (types.length === 0 && Array.isArray(prop.oneOf)) {
68
- types = flatten(prop.oneOf.map((it) => it.type));
69
- }
70
-
71
- if (types.indexOf('object') !== -1 || types.indexOf('array') !== -1) {
72
- jsonAttributes.push(propName);
73
- }
37
+ let jsonAttributes = modelClass.jsonAttributes;
38
+ if (Array.isArray(jsonAttributes)) {
39
+ return jsonAttributes;
40
+ }
41
+ jsonAttributes = [];
42
+ if (modelClass.getJsonSchema()) {
43
+ const props = modelClass.getJsonSchema().properties || {};
44
+ for (const propName of Object.keys(props)) {
45
+ const prop = props[propName];
46
+ let types = asArray(prop.type).filter((it) => !!it);
47
+ if (types.length === 0 && Array.isArray(prop.anyOf)) {
48
+ types = flatten(prop.anyOf.map((it) => it.type));
49
+ }
50
+ if (types.length === 0 && Array.isArray(prop.oneOf)) {
51
+ types = flatten(prop.oneOf.map((it) => it.type));
52
+ }
53
+ if (types.indexOf('object') !== -1 || types.indexOf('array') !== -1) {
54
+ jsonAttributes.push(propName);
55
+ }
56
+ }
74
57
  }
75
- }
76
-
77
- return jsonAttributes;
58
+ return jsonAttributes;
78
59
  }
79
-
80
60
  function tryParseJson(maybeJsonStr) {
81
- try {
82
- return JSON.parse(maybeJsonStr);
83
- } catch (err) {
84
- return undefined;
85
- }
61
+ try {
62
+ return JSON.parse(maybeJsonStr);
63
+ }
64
+ catch (err) {
65
+ return undefined;
66
+ }
86
67
  }
87
-
88
68
  module.exports = {
89
- parseJsonAttributes,
90
- formatJsonAttributes,
91
- getJsonAttributes,
69
+ parseJsonAttributes,
70
+ formatJsonAttributes,
71
+ getJsonAttributes,
92
72
  };