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