@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,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
- constructor(tableName) {
9
- super();
10
- this.name = this.constructor.name;
11
- this.tableName = tableName;
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
- constructor() {
17
- super();
18
- this.name = this.constructor.name;
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
- // references must be a reference string like `Table.column:maybe.some.json[1].path`.
30
- // or an array of such references (composite key).
31
- //
32
- // modelClassResolver must be a function that takes a table name
33
- // and returns a model class.
34
- constructor(references, modelClassResolver) {
35
- const refs = createRefs(asArray(references));
36
- const paths = createPaths(refs, modelClassResolver);
37
- const modelClass = resolveModelClass(paths);
38
-
39
- this._refs = refs.map((ref) => ref.model(modelClass));
40
- this._modelClass = modelClass;
41
- this._props = paths.map((it) => it.path[0]);
42
- this._cols = refs.map((it) => it.column);
43
- this._propGetters = paths.map((it) => createGetter(it.path));
44
- this._propSetters = paths.map((it) => createSetter(it.path));
45
- this._patchers = refs.map((it, i) => createPatcher(it, paths[i].path));
46
- }
47
-
48
- static get ModelNotFoundError() {
49
- return ModelNotFoundError;
50
- }
51
-
52
- static get InvalidReferenceError() {
53
- return InvalidReferenceError;
54
- }
55
-
56
- // The number of columns.
57
- get size() {
58
- return this._refs.length;
59
- }
60
-
61
- // The model class that owns the property.
62
- get modelClass() {
63
- return this._modelClass;
64
- }
65
-
66
- // An array of property names. Contains multiple values in case of composite key.
67
- // This may be different from `cols` if the model class has some kind of conversion
68
- // between database and "external" formats, for example a snake_case to camelCase
69
- // conversion.
70
- get props() {
71
- return this._props;
72
- }
73
-
74
- // An array of column names. Contains multiple values in case of composite key.
75
- // This may be different from `props` if the model class has some kind of conversion
76
- // between database and "external" formats, for example a snake_case to camelCase
77
- // conversion.
78
- get cols() {
79
- return this._cols;
80
- }
81
-
82
- forEach(callback) {
83
- for (let i = 0, l = this.size; i < l; ++i) {
84
- callback(i);
85
- }
86
- }
87
-
88
- // Creates a concatenated string from the property values of the given object.
89
- propKey(obj) {
90
- const size = this.size;
91
- let key = PROP_KEY_PREFIX;
92
-
93
- for (let i = 0; i < size; ++i) {
94
- key += propToStr(this.getProp(obj, i));
95
-
96
- if (i !== size - 1) {
97
- key += ',';
98
- }
99
- }
100
-
101
- return key;
102
- }
103
-
104
- // Returns the property values of the given object as an array.
105
- getProps(obj) {
106
- const size = this.size;
107
- const props = new Array(size);
108
-
109
- for (let i = 0; i < size; ++i) {
110
- props[i] = this.getProp(obj, i);
111
- }
112
-
113
- return props;
114
- }
115
-
116
- // Returns true if the given object has a non-null value in all properties.
117
- hasProps(obj) {
118
- const size = this.size;
119
-
120
- for (let i = 0; i < size; ++i) {
121
- const prop = this.getProp(obj, i);
122
-
123
- if (prop === null || prop === undefined) {
124
- return false;
125
- }
126
- }
127
-
128
- return true;
129
- }
130
-
131
- // Returns the index:th property value of the given object.
132
- getProp(obj, index) {
133
- return this._propGetters[index](obj);
134
- }
135
-
136
- // Sets the index:th property value of the given object.
137
- setProp(obj, index, value) {
138
- return this._propSetters[index](obj, value);
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
- try {
173
- return refs.map((it) => {
174
- if (!isObject(it) || !it.isObjectionReferenceBuilder) {
175
- return createRef(it);
176
- } else {
177
- return it;
178
- }
179
- });
180
- } catch (err) {
181
- throw new InvalidReferenceError();
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
- return refs.map((ref) => {
187
- if (!ref.tableName) {
188
- throw new InvalidReferenceError();
189
- }
190
-
191
- const modelClass = modelClassResolver(ref.tableName);
192
-
193
- if (!modelClass) {
194
- throw new ModelNotFoundError(ref.tableName);
195
- }
196
-
197
- const prop = modelClass.columnNameToPropertyName(ref.column);
198
- const jsonPath = ref.parsedExpr.access.map((it) => it.ref);
199
-
200
- return {
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
- const modelClasses = paths.map((it) => it.modelClass);
209
- const uniqueModelClasses = uniqBy(modelClasses);
210
-
211
- if (uniqueModelClasses.length !== 1) {
212
- throw new InvalidReferenceError();
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
- if (path.length === 1) {
220
- const prop = path[0];
221
- return (obj) => obj[prop];
222
- } else {
223
- return (obj) => get(obj, path);
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
- if (path.length === 1) {
229
- const prop = path[0];
230
- return (obj, value) => (obj[prop] = value);
231
- } else {
232
- return (obj, value) => set(obj, path, value);
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
- if (ref.isPlainColumnRef) {
238
- return (patch, value) => (patch[path[0]] = value);
239
- } else {
240
- // Objection `patch`, `update` etc. methods understand field expressions.
241
- return (patch, value) => (patch[ref.expression] = value);
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
- RelationProperty,
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
- constructor(name, opt) {
8
- super(name, opt);
9
-
10
- this.relation = opt.relation;
11
- this.owner = opt.owner;
12
- }
13
-
14
- onBuild(builder) {
15
- super.onBuild(builder);
16
-
17
- this.relation.findQuery(builder, this.owner);
18
- }
19
-
20
- toFindOperation() {
21
- return new RelationFindOperation('find', {
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
- RelationUpdateOperation,
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
- onAfter1(_, result) {
7
- if (this.owner.isModels) {
8
- const ownerProp = this.relation.ownerProp;
9
-
10
- for (const owner of this.owner.modelArray) {
11
- ownerProp.forEach((i) => {
12
- ownerProp.setProp(owner, i, null);
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
- BelongsToOneDeleteOperation,
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
- onAdd(builder, args) {
8
- const retVal = super.onAdd(builder, args);
9
-
10
- if (this.models.length > 1) {
11
- throw this.relation.createError('can only insert one model to a BelongsToOneRelation');
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
- return retVal;
15
- }
16
-
17
- async onAfter1(builder, ret) {
18
- const inserted = await super.onAfter1(builder, ret);
19
-
20
- if (!builder.isExecutable()) {
21
- return inserted;
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
- BelongsToOneInsertOperation,
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
- onAdd(_, args) {
8
- this.input = args[0];
9
- this.ids = normalizeIds(args[0], this.relation.relatedProp, { arrayOutput: true });
10
-
11
- assertOneId(this.ids);
12
- return true;
13
- }
14
-
15
- queryExecutor(builder) {
16
- const patch = {};
17
- const ownerProp = this.relation.ownerProp;
18
-
19
- ownerProp.forEach((i) => {
20
- const relatedValue = this.ids[0][i];
21
-
22
- if (this.owner.isModels) {
23
- for (const owner of this.owner.modelArray) {
24
- ownerProp.setProp(owner, i, relatedValue);
25
- }
26
- }
27
-
28
- ownerProp.patch(patch, i, relatedValue);
29
- });
30
-
31
- const ownerIdProp = this.relation.ownerModelClass.getIdRelationProperty();
32
- const ownerIds = this.owner.getProps(this.relation, ownerIdProp);
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
- if (ids.length > 1) {
44
- throw new Error('can only relate one model to a BelongsToOneRelation');
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
- BelongsToOneRelateOperation,
38
+ BelongsToOneRelateOperation,
50
39
  };