@spinajs/orm 2.0.180 → 2.0.182

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 (97) hide show
  1. package/lib/cjs/builders.d.ts +643 -643
  2. package/lib/cjs/builders.js +1602 -1602
  3. package/lib/cjs/builders.js.map +1 -1
  4. package/lib/cjs/converters.d.ts +34 -34
  5. package/lib/cjs/converters.js +104 -104
  6. package/lib/cjs/decorators.d.ts +152 -152
  7. package/lib/cjs/decorators.js +449 -449
  8. package/lib/cjs/dehydrators.d.ts +10 -10
  9. package/lib/cjs/dehydrators.js +47 -47
  10. package/lib/cjs/driver.d.ts +82 -82
  11. package/lib/cjs/driver.js +102 -102
  12. package/lib/cjs/driver.js.map +1 -1
  13. package/lib/cjs/enums.d.ts +116 -116
  14. package/lib/cjs/enums.js +126 -126
  15. package/lib/cjs/enums.js.map +1 -1
  16. package/lib/cjs/exceptions.d.ts +6 -6
  17. package/lib/cjs/exceptions.js +10 -10
  18. package/lib/cjs/hydrators.d.ts +19 -19
  19. package/lib/cjs/hydrators.js +132 -132
  20. package/lib/cjs/hydrators.js.map +1 -1
  21. package/lib/cjs/index.d.ts +17 -17
  22. package/lib/cjs/index.js +33 -33
  23. package/lib/cjs/interfaces.d.ts +921 -919
  24. package/lib/cjs/interfaces.d.ts.map +1 -1
  25. package/lib/cjs/interfaces.js +279 -279
  26. package/lib/cjs/interfaces.js.map +1 -1
  27. package/lib/cjs/middlewares.d.ts +62 -62
  28. package/lib/cjs/middlewares.js +258 -258
  29. package/lib/cjs/model.d.ts +288 -284
  30. package/lib/cjs/model.d.ts.map +1 -1
  31. package/lib/cjs/model.js +826 -810
  32. package/lib/cjs/model.js.map +1 -1
  33. package/lib/cjs/orm.d.ts +61 -61
  34. package/lib/cjs/orm.js +333 -333
  35. package/lib/cjs/orm.js.map +1 -1
  36. package/lib/cjs/relation-objects.d.ts +108 -108
  37. package/lib/cjs/relation-objects.js +221 -221
  38. package/lib/cjs/relations.d.ts +61 -61
  39. package/lib/cjs/relations.js +194 -194
  40. package/lib/cjs/relations.js.map +1 -1
  41. package/lib/cjs/statements.d.ts +143 -143
  42. package/lib/cjs/statements.js +309 -309
  43. package/lib/cjs/statements.js.map +1 -1
  44. package/lib/cjs/types.d.ts +32 -32
  45. package/lib/cjs/types.js +2 -2
  46. package/lib/cjs/wrappers.d.ts +5 -5
  47. package/lib/cjs/wrappers.js +12 -12
  48. package/lib/mjs/builders.d.ts +643 -643
  49. package/lib/mjs/builders.js +1594 -1594
  50. package/lib/mjs/builders.js.map +1 -1
  51. package/lib/mjs/converters.d.ts +34 -34
  52. package/lib/mjs/converters.js +96 -96
  53. package/lib/mjs/decorators.d.ts +152 -152
  54. package/lib/mjs/decorators.js +422 -422
  55. package/lib/mjs/dehydrators.d.ts +10 -10
  56. package/lib/mjs/dehydrators.js +41 -41
  57. package/lib/mjs/driver.d.ts +82 -82
  58. package/lib/mjs/driver.js +98 -98
  59. package/lib/mjs/driver.js.map +1 -1
  60. package/lib/mjs/enums.d.ts +116 -116
  61. package/lib/mjs/enums.js +123 -123
  62. package/lib/mjs/enums.js.map +1 -1
  63. package/lib/mjs/exceptions.d.ts +6 -6
  64. package/lib/mjs/exceptions.js +6 -6
  65. package/lib/mjs/hydrators.d.ts +19 -19
  66. package/lib/mjs/hydrators.js +128 -128
  67. package/lib/mjs/hydrators.js.map +1 -1
  68. package/lib/mjs/index.d.ts +17 -17
  69. package/lib/mjs/index.js +17 -17
  70. package/lib/mjs/interfaces.d.ts +921 -919
  71. package/lib/mjs/interfaces.d.ts.map +1 -1
  72. package/lib/mjs/interfaces.js +267 -267
  73. package/lib/mjs/interfaces.js.map +1 -1
  74. package/lib/mjs/middlewares.d.ts +62 -62
  75. package/lib/mjs/middlewares.js +249 -249
  76. package/lib/mjs/model.d.ts +288 -284
  77. package/lib/mjs/model.d.ts.map +1 -1
  78. package/lib/mjs/model.js +816 -800
  79. package/lib/mjs/model.js.map +1 -1
  80. package/lib/mjs/orm.d.ts +61 -61
  81. package/lib/mjs/orm.js +326 -326
  82. package/lib/mjs/orm.js.map +1 -1
  83. package/lib/mjs/relation-objects.d.ts +108 -108
  84. package/lib/mjs/relation-objects.js +211 -211
  85. package/lib/mjs/relations.d.ts +61 -61
  86. package/lib/mjs/relations.js +191 -191
  87. package/lib/mjs/relations.js.map +1 -1
  88. package/lib/mjs/statements.d.ts +143 -143
  89. package/lib/mjs/statements.js +301 -301
  90. package/lib/mjs/statements.js.map +1 -1
  91. package/lib/mjs/types.d.ts +32 -32
  92. package/lib/mjs/types.js +1 -1
  93. package/lib/mjs/wrappers.d.ts +5 -5
  94. package/lib/mjs/wrappers.js +9 -9
  95. package/lib/tsconfig.cjs.tsbuildinfo +1 -1
  96. package/lib/tsconfig.mjs.tsbuildinfo +1 -1
  97. package/package.json +5 -5
@@ -1,63 +1,63 @@
1
- import { IRelationDescriptor, IModelDescriptor, IBuilderMiddleware, ISelectQueryBuilder } from './interfaces.js';
2
- import { ModelBase } from './model.js';
3
- import { BelongsToRelation } from './relations.js';
4
- export declare class HasManyRelationMiddleware implements IBuilderMiddleware {
5
- protected _relationQuery: ISelectQueryBuilder;
6
- protected _description: IRelationDescriptor;
7
- protected _path: string;
8
- constructor(_relationQuery: ISelectQueryBuilder, _description: IRelationDescriptor, _path: string);
9
- afterQuery(data: any[]): any[];
10
- modelCreation(_: any): ModelBase;
11
- afterHydration(data: ModelBase[]): Promise<any[]>;
12
- }
13
- export declare class BelongsToRelationRecursiveMiddleware implements IBuilderMiddleware {
14
- protected _relationQuery: ISelectQueryBuilder;
15
- protected _description: IRelationDescriptor;
16
- protected _targetModelDescriptor: IModelDescriptor;
17
- constructor(_relationQuery: ISelectQueryBuilder, _description: IRelationDescriptor, _targetModelDescriptor: IModelDescriptor);
18
- afterQuery(data: any[]): any[];
19
- modelCreation(_: any): ModelBase;
20
- afterHydration(data: ModelBase[]): Promise<any[]>;
21
- }
22
- export declare class HasManyToManyRelationMiddleware implements IBuilderMiddleware {
23
- protected _relationQuery: ISelectQueryBuilder;
24
- protected _description: IRelationDescriptor;
25
- protected _targetModelDescriptor: IModelDescriptor;
26
- constructor(_relationQuery: ISelectQueryBuilder, _description: IRelationDescriptor, _targetModelDescriptor: IModelDescriptor);
27
- afterQuery(data: any[]): any[];
28
- modelCreation(_: any): ModelBase;
29
- afterHydration(data: ModelBase[]): Promise<any[]>;
30
- private pickProps;
31
- }
32
- export declare class BelongsToPopulateDataMiddleware implements IBuilderMiddleware {
33
- protected _description: IRelationDescriptor;
34
- protected relation: BelongsToRelation;
35
- constructor(_description: IRelationDescriptor, relation: BelongsToRelation);
36
- afterQuery(data: any[]): any[];
37
- modelCreation(_: any): ModelBase<unknown>;
38
- afterHydration(data: ModelBase<unknown>[]): Promise<void | any[]>;
39
- }
40
- export declare class BelongsToRelationResultTransformMiddleware implements IBuilderMiddleware {
41
- afterQuery(data: any[]): any[];
42
- modelCreation(_: any): ModelBase;
43
- afterHydration(_data: Array<ModelBase>): Promise<void>;
44
- /**
45
- * Dynamically sets a deeply nested value in an object.
46
- * Optionally "bores" a path to it if its undefined.
47
- *
48
- * @param obj - The object which contains the value you want to change/set.
49
- * @param path - The array representation of path to the value you want to change/set.
50
- * @param value - The value you want to set it to.
51
- * @param setrecursively - If true, will set value of non-existing path as well.
52
- */
53
- protected setDeep(obj: any, path: any[], value: any, setrecursively?: boolean): void;
54
- protected keyTransform(key: string): string[];
55
- }
56
- export declare class DiscriminationMapMiddleware implements IBuilderMiddleware {
57
- protected _description: IModelDescriptor;
58
- constructor(_description: IModelDescriptor);
59
- afterQuery(data: any[]): any[];
60
- modelCreation(data: any): ModelBase;
61
- afterHydration(_data: ModelBase[]): Promise<void>;
62
- }
1
+ import { IRelationDescriptor, IModelDescriptor, IBuilderMiddleware, ISelectQueryBuilder } from './interfaces.js';
2
+ import { ModelBase } from './model.js';
3
+ import { BelongsToRelation } from './relations.js';
4
+ export declare class HasManyRelationMiddleware implements IBuilderMiddleware {
5
+ protected _relationQuery: ISelectQueryBuilder;
6
+ protected _description: IRelationDescriptor;
7
+ protected _path: string;
8
+ constructor(_relationQuery: ISelectQueryBuilder, _description: IRelationDescriptor, _path: string);
9
+ afterQuery(data: any[]): any[];
10
+ modelCreation(_: any): ModelBase;
11
+ afterHydration(data: ModelBase[]): Promise<any[]>;
12
+ }
13
+ export declare class BelongsToRelationRecursiveMiddleware implements IBuilderMiddleware {
14
+ protected _relationQuery: ISelectQueryBuilder;
15
+ protected _description: IRelationDescriptor;
16
+ protected _targetModelDescriptor: IModelDescriptor;
17
+ constructor(_relationQuery: ISelectQueryBuilder, _description: IRelationDescriptor, _targetModelDescriptor: IModelDescriptor);
18
+ afterQuery(data: any[]): any[];
19
+ modelCreation(_: any): ModelBase;
20
+ afterHydration(data: ModelBase[]): Promise<any[]>;
21
+ }
22
+ export declare class HasManyToManyRelationMiddleware implements IBuilderMiddleware {
23
+ protected _relationQuery: ISelectQueryBuilder;
24
+ protected _description: IRelationDescriptor;
25
+ protected _targetModelDescriptor: IModelDescriptor;
26
+ constructor(_relationQuery: ISelectQueryBuilder, _description: IRelationDescriptor, _targetModelDescriptor: IModelDescriptor);
27
+ afterQuery(data: any[]): any[];
28
+ modelCreation(_: any): ModelBase;
29
+ afterHydration(data: ModelBase[]): Promise<any[]>;
30
+ private pickProps;
31
+ }
32
+ export declare class BelongsToPopulateDataMiddleware implements IBuilderMiddleware {
33
+ protected _description: IRelationDescriptor;
34
+ protected relation: BelongsToRelation;
35
+ constructor(_description: IRelationDescriptor, relation: BelongsToRelation);
36
+ afterQuery(data: any[]): any[];
37
+ modelCreation(_: any): ModelBase<unknown>;
38
+ afterHydration(data: ModelBase<unknown>[]): Promise<void | any[]>;
39
+ }
40
+ export declare class BelongsToRelationResultTransformMiddleware implements IBuilderMiddleware {
41
+ afterQuery(data: any[]): any[];
42
+ modelCreation(_: any): ModelBase;
43
+ afterHydration(_data: Array<ModelBase>): Promise<void>;
44
+ /**
45
+ * Dynamically sets a deeply nested value in an object.
46
+ * Optionally "bores" a path to it if its undefined.
47
+ *
48
+ * @param obj - The object which contains the value you want to change/set.
49
+ * @param path - The array representation of path to the value you want to change/set.
50
+ * @param value - The value you want to set it to.
51
+ * @param setrecursively - If true, will set value of non-existing path as well.
52
+ */
53
+ protected setDeep(obj: any, path: any[], value: any, setrecursively?: boolean): void;
54
+ protected keyTransform(key: string): string[];
55
+ }
56
+ export declare class DiscriminationMapMiddleware implements IBuilderMiddleware {
57
+ protected _description: IModelDescriptor;
58
+ constructor(_description: IModelDescriptor);
59
+ afterQuery(data: any[]): any[];
60
+ modelCreation(data: any): ModelBase;
61
+ afterHydration(_data: ModelBase[]): Promise<void>;
62
+ }
63
63
  //# sourceMappingURL=middlewares.d.ts.map
@@ -1,259 +1,259 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DiscriminationMapMiddleware = exports.BelongsToRelationResultTransformMiddleware = exports.BelongsToPopulateDataMiddleware = exports.HasManyToManyRelationMiddleware = exports.BelongsToRelationRecursiveMiddleware = exports.HasManyRelationMiddleware = void 0;
4
- /* eslint-disable prettier/prettier */
5
- const interfaces_js_1 = require("./interfaces.js");
6
- const relation_objects_js_1 = require("./relation-objects.js");
7
- class HasManyRelationMiddleware {
8
- constructor(_relationQuery, _description, _path) {
9
- this._relationQuery = _relationQuery;
10
- this._description = _description;
11
- this._path = _path;
12
- }
13
- afterQuery(data) {
14
- return data;
15
- }
16
- modelCreation(_) {
17
- return null;
18
- }
19
- async afterHydration(data) {
20
- const self = this;
21
- const pks = data.map((d) => {
22
- return d[this._description.PrimaryKey];
23
- });
24
- const hydrateMiddleware = {
25
- afterQuery(data) {
26
- return data;
27
- },
28
- modelCreation() {
29
- return null;
30
- },
31
- async afterHydration(relationData) {
32
- relationData.forEach((d) => (d.__relationKey__ = self._description.Name));
33
- data.forEach((d) => {
34
- const relData = relationData.filter((rd) => {
35
- return d[self._description.PrimaryKey] === rd[self._description.ForeignKey];
36
- });
37
- d[self._description.Name] = new relation_objects_js_1.OneToManyRelationList(d, self._description.TargetModel, self._description, relData);
38
- });
39
- },
40
- };
41
- if (pks.length !== 0) {
42
- this._relationQuery.whereIn(this._description.ForeignKey, pks);
43
- this._relationQuery.middleware(hydrateMiddleware);
44
- return await this._relationQuery;
45
- }
46
- return [];
47
- }
48
- }
49
- exports.HasManyRelationMiddleware = HasManyRelationMiddleware;
50
- class BelongsToRelationRecursiveMiddleware {
51
- constructor(_relationQuery, _description, _targetModelDescriptor) {
52
- this._relationQuery = _relationQuery;
53
- this._description = _description;
54
- this._targetModelDescriptor = _targetModelDescriptor;
55
- }
56
- afterQuery(data) {
57
- return data;
58
- }
59
- modelCreation(_) {
60
- return null;
61
- }
62
- async afterHydration(data) {
63
- const self = this;
64
- const pks = data.map((d) => d[this._description.PrimaryKey]);
65
- const fKey = this._description.ForeignKey;
66
- const key = this._description.PrimaryKey;
67
- const name = this._description.Name;
68
- const hydrateMiddleware = {
69
- afterQuery(data) {
70
- return data;
71
- },
72
- modelCreation(_) {
73
- return null;
74
- },
75
- async afterHydration(relationData) {
76
- relationData.forEach((d) => (d.__relationKey__ = self._description.Name));
77
- function buildRelationTree(_d, parent) {
78
- const branch = [];
79
- _d.forEach((d) => {
80
- if (d[fKey] === parent) {
81
- const children = buildRelationTree(_d, d[key]);
82
- if (children) {
83
- // TODO:
84
- // implement RecursiveRelation list to allow for
85
- // manipulation of the recursive data
86
- d[name] = new relation_objects_js_1.OneToManyRelationList(d, d.Model, {
87
- Name: name,
88
- Type: interfaces_js_1.RelationType.Many,
89
- TargetModelType: d.Model,
90
- TargetModel: d.Model,
91
- SourceModel: d.Model,
92
- ForeignKey: fKey,
93
- PrimaryKey: key,
94
- Recursive: false,
95
- }, children);
96
- }
97
- branch.push(d);
98
- }
99
- });
100
- return branch;
101
- }
102
- const result = buildRelationTree(relationData, null);
103
- data.forEach((d) => {
104
- d[name] = result.find((r) => r[key] === d[key])[name];
105
- });
106
- },
107
- };
108
- this._relationQuery.whereIn(this._description.PrimaryKey, pks);
109
- this._relationQuery.middleware(new DiscriminationMapMiddleware(this._targetModelDescriptor));
110
- this._relationQuery.middleware(hydrateMiddleware);
111
- return await this._relationQuery;
112
- }
113
- }
114
- exports.BelongsToRelationRecursiveMiddleware = BelongsToRelationRecursiveMiddleware;
115
- class HasManyToManyRelationMiddleware {
116
- constructor(_relationQuery, _description, _targetModelDescriptor) {
117
- this._relationQuery = _relationQuery;
118
- this._description = _description;
119
- this._targetModelDescriptor = _targetModelDescriptor;
120
- }
121
- afterQuery(data) {
122
- return data;
123
- }
124
- modelCreation(_) {
125
- return null;
126
- }
127
- async afterHydration(data) {
128
- const self = this;
129
- const pks = data.map((d) => d[this._description.PrimaryKey]);
130
- const hydrateMiddleware = {
131
- afterQuery(data) {
132
- return data.map((d) => Object.assign({}, d[self._description.Name], { JunctionModel: self.pickProps(d, [self._description.Name]) }));
133
- },
134
- modelCreation(_) {
135
- return null;
136
- },
137
- async afterHydration(relationData) {
138
- relationData.forEach((d) => (d.__relationKey__ = self._description.Name));
139
- data.forEach((d) => {
140
- const relData = relationData.filter((rd) => rd.JunctionModel[self._description.ForeignKey] === d[self._description.PrimaryKey]);
141
- d[self._description.Name] = new relation_objects_js_1.ManyToManyRelationList(d, self._description.TargetModel, self._description, relData);
142
- });
143
- relationData.forEach((d) => delete d.JunctionModel);
144
- },
145
- };
146
- if (pks.length !== 0) {
147
- this._relationQuery.whereIn(this._description.ForeignKey, pks);
148
- this._relationQuery.middleware(new BelongsToRelationResultTransformMiddleware());
149
- this._relationQuery.middleware(new DiscriminationMapMiddleware(this._targetModelDescriptor));
150
- this._relationQuery.middleware(hydrateMiddleware);
151
- return await this._relationQuery;
152
- }
153
- return [];
154
- }
155
- pickProps(source, except) {
156
- const obj = {};
157
- for (const p in source) {
158
- if (except.indexOf(p) === -1) {
159
- obj[p] = source[p];
160
- }
161
- }
162
- return obj;
163
- }
164
- }
165
- exports.HasManyToManyRelationMiddleware = HasManyToManyRelationMiddleware;
166
- class BelongsToPopulateDataMiddleware {
167
- constructor(_description, relation) {
168
- this._description = _description;
169
- this.relation = relation;
170
- }
171
- afterQuery(data) {
172
- return data;
173
- }
174
- modelCreation(_) {
175
- return null;
176
- }
177
- afterHydration(data) {
178
- const relData = data.map((d) => d[this._description.Name].Value).filter((x) => x !== null && x !== undefined);
179
- const middlewares = this.relation._relationQuery.Relations
180
- .map((x) => {
181
- return x._query._middlewares;
182
- })
183
- .reduce((prev, current) => {
184
- return prev.concat(current);
185
- }, []);
186
- return Promise.all(middlewares.map((x) => {
187
- return x.afterHydration(relData);
188
- }));
189
- }
190
- }
191
- exports.BelongsToPopulateDataMiddleware = BelongsToPopulateDataMiddleware;
192
- class BelongsToRelationResultTransformMiddleware {
193
- afterQuery(data) {
194
- return data.map((d) => {
195
- const transformedData = Object.assign(d);
196
- for (const key in transformedData) {
197
- if (key.startsWith('$')) {
198
- this.setDeep(transformedData, this.keyTransform(key), d[key]);
199
- delete transformedData[key];
200
- }
201
- }
202
- return transformedData;
203
- });
204
- }
205
- modelCreation(_) {
206
- return null;
207
- }
208
- // tslint:disable-next-line: no-empty
209
- async afterHydration(_data) { }
210
- /**
211
- * Dynamically sets a deeply nested value in an object.
212
- * Optionally "bores" a path to it if its undefined.
213
- *
214
- * @param obj - The object which contains the value you want to change/set.
215
- * @param path - The array representation of path to the value you want to change/set.
216
- * @param value - The value you want to set it to.
217
- * @param setrecursively - If true, will set value of non-existing path as well.
218
- */
219
- setDeep(obj, path, value, setrecursively = true) {
220
- path.reduce((a, b, level) => {
221
- if (setrecursively && typeof a[b] === 'undefined' && level !== path.length - 1) {
222
- a[b] = {};
223
- return a[b];
224
- }
225
- if (level === path.length - 1) {
226
- a[b] = value;
227
- return value;
228
- }
229
- return a[b];
230
- }, obj);
231
- }
232
- keyTransform(key) {
233
- return key.replace(/\$+/g, '').split('.');
234
- }
235
- }
236
- exports.BelongsToRelationResultTransformMiddleware = BelongsToRelationResultTransformMiddleware;
237
- class DiscriminationMapMiddleware {
238
- constructor(_description) {
239
- this._description = _description;
240
- }
241
- afterQuery(data) {
242
- return data;
243
- }
244
- modelCreation(data) {
245
- if (this._description.DiscriminationMap && this._description.DiscriminationMap.Field) {
246
- const distValue = data[this._description.DiscriminationMap.Field];
247
- if (distValue && this._description.DiscriminationMap.Models.has(distValue)) {
248
- const result = new (this._description.DiscriminationMap.Models.get(distValue))();
249
- result.hydrate(data);
250
- return result;
251
- }
252
- }
253
- return null;
254
- }
255
- // tslint:disable-next-line: no-empty
256
- async afterHydration(_data) { }
257
- }
258
- exports.DiscriminationMapMiddleware = DiscriminationMapMiddleware;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DiscriminationMapMiddleware = exports.BelongsToRelationResultTransformMiddleware = exports.BelongsToPopulateDataMiddleware = exports.HasManyToManyRelationMiddleware = exports.BelongsToRelationRecursiveMiddleware = exports.HasManyRelationMiddleware = void 0;
4
+ /* eslint-disable prettier/prettier */
5
+ const interfaces_js_1 = require("./interfaces.js");
6
+ const relation_objects_js_1 = require("./relation-objects.js");
7
+ class HasManyRelationMiddleware {
8
+ constructor(_relationQuery, _description, _path) {
9
+ this._relationQuery = _relationQuery;
10
+ this._description = _description;
11
+ this._path = _path;
12
+ }
13
+ afterQuery(data) {
14
+ return data;
15
+ }
16
+ modelCreation(_) {
17
+ return null;
18
+ }
19
+ async afterHydration(data) {
20
+ const self = this;
21
+ const pks = data.map((d) => {
22
+ return d[this._description.PrimaryKey];
23
+ });
24
+ const hydrateMiddleware = {
25
+ afterQuery(data) {
26
+ return data;
27
+ },
28
+ modelCreation() {
29
+ return null;
30
+ },
31
+ async afterHydration(relationData) {
32
+ relationData.forEach((d) => (d.__relationKey__ = self._description.Name));
33
+ data.forEach((d) => {
34
+ const relData = relationData.filter((rd) => {
35
+ return d[self._description.PrimaryKey] === rd[self._description.ForeignKey];
36
+ });
37
+ d[self._description.Name] = new relation_objects_js_1.OneToManyRelationList(d, self._description.TargetModel, self._description, relData);
38
+ });
39
+ },
40
+ };
41
+ if (pks.length !== 0) {
42
+ this._relationQuery.whereIn(this._description.ForeignKey, pks);
43
+ this._relationQuery.middleware(hydrateMiddleware);
44
+ return await this._relationQuery;
45
+ }
46
+ return [];
47
+ }
48
+ }
49
+ exports.HasManyRelationMiddleware = HasManyRelationMiddleware;
50
+ class BelongsToRelationRecursiveMiddleware {
51
+ constructor(_relationQuery, _description, _targetModelDescriptor) {
52
+ this._relationQuery = _relationQuery;
53
+ this._description = _description;
54
+ this._targetModelDescriptor = _targetModelDescriptor;
55
+ }
56
+ afterQuery(data) {
57
+ return data;
58
+ }
59
+ modelCreation(_) {
60
+ return null;
61
+ }
62
+ async afterHydration(data) {
63
+ const self = this;
64
+ const pks = data.map((d) => d[this._description.PrimaryKey]);
65
+ const fKey = this._description.ForeignKey;
66
+ const key = this._description.PrimaryKey;
67
+ const name = this._description.Name;
68
+ const hydrateMiddleware = {
69
+ afterQuery(data) {
70
+ return data;
71
+ },
72
+ modelCreation(_) {
73
+ return null;
74
+ },
75
+ async afterHydration(relationData) {
76
+ relationData.forEach((d) => (d.__relationKey__ = self._description.Name));
77
+ function buildRelationTree(_d, parent) {
78
+ const branch = [];
79
+ _d.forEach((d) => {
80
+ if (d[fKey] === parent) {
81
+ const children = buildRelationTree(_d, d[key]);
82
+ if (children) {
83
+ // TODO:
84
+ // implement RecursiveRelation list to allow for
85
+ // manipulation of the recursive data
86
+ d[name] = new relation_objects_js_1.OneToManyRelationList(d, d.Model, {
87
+ Name: name,
88
+ Type: interfaces_js_1.RelationType.Many,
89
+ TargetModelType: d.Model,
90
+ TargetModel: d.Model,
91
+ SourceModel: d.Model,
92
+ ForeignKey: fKey,
93
+ PrimaryKey: key,
94
+ Recursive: false,
95
+ }, children);
96
+ }
97
+ branch.push(d);
98
+ }
99
+ });
100
+ return branch;
101
+ }
102
+ const result = buildRelationTree(relationData, null);
103
+ data.forEach((d) => {
104
+ d[name] = result.find((r) => r[key] === d[key])[name];
105
+ });
106
+ },
107
+ };
108
+ this._relationQuery.whereIn(this._description.PrimaryKey, pks);
109
+ this._relationQuery.middleware(new DiscriminationMapMiddleware(this._targetModelDescriptor));
110
+ this._relationQuery.middleware(hydrateMiddleware);
111
+ return await this._relationQuery;
112
+ }
113
+ }
114
+ exports.BelongsToRelationRecursiveMiddleware = BelongsToRelationRecursiveMiddleware;
115
+ class HasManyToManyRelationMiddleware {
116
+ constructor(_relationQuery, _description, _targetModelDescriptor) {
117
+ this._relationQuery = _relationQuery;
118
+ this._description = _description;
119
+ this._targetModelDescriptor = _targetModelDescriptor;
120
+ }
121
+ afterQuery(data) {
122
+ return data;
123
+ }
124
+ modelCreation(_) {
125
+ return null;
126
+ }
127
+ async afterHydration(data) {
128
+ const self = this;
129
+ const pks = data.map((d) => d[this._description.PrimaryKey]);
130
+ const hydrateMiddleware = {
131
+ afterQuery(data) {
132
+ return data.map((d) => Object.assign({}, d[self._description.Name], { JunctionModel: self.pickProps(d, [self._description.Name]) }));
133
+ },
134
+ modelCreation(_) {
135
+ return null;
136
+ },
137
+ async afterHydration(relationData) {
138
+ relationData.forEach((d) => (d.__relationKey__ = self._description.Name));
139
+ data.forEach((d) => {
140
+ const relData = relationData.filter((rd) => rd.JunctionModel[self._description.ForeignKey] === d[self._description.PrimaryKey]);
141
+ d[self._description.Name] = new relation_objects_js_1.ManyToManyRelationList(d, self._description.TargetModel, self._description, relData);
142
+ });
143
+ relationData.forEach((d) => delete d.JunctionModel);
144
+ },
145
+ };
146
+ if (pks.length !== 0) {
147
+ this._relationQuery.whereIn(this._description.ForeignKey, pks);
148
+ this._relationQuery.middleware(new BelongsToRelationResultTransformMiddleware());
149
+ this._relationQuery.middleware(new DiscriminationMapMiddleware(this._targetModelDescriptor));
150
+ this._relationQuery.middleware(hydrateMiddleware);
151
+ return await this._relationQuery;
152
+ }
153
+ return [];
154
+ }
155
+ pickProps(source, except) {
156
+ const obj = {};
157
+ for (const p in source) {
158
+ if (except.indexOf(p) === -1) {
159
+ obj[p] = source[p];
160
+ }
161
+ }
162
+ return obj;
163
+ }
164
+ }
165
+ exports.HasManyToManyRelationMiddleware = HasManyToManyRelationMiddleware;
166
+ class BelongsToPopulateDataMiddleware {
167
+ constructor(_description, relation) {
168
+ this._description = _description;
169
+ this.relation = relation;
170
+ }
171
+ afterQuery(data) {
172
+ return data;
173
+ }
174
+ modelCreation(_) {
175
+ return null;
176
+ }
177
+ afterHydration(data) {
178
+ const relData = data.map((d) => d[this._description.Name].Value).filter((x) => x !== null && x !== undefined);
179
+ const middlewares = this.relation._relationQuery.Relations
180
+ .map((x) => {
181
+ return x._query._middlewares;
182
+ })
183
+ .reduce((prev, current) => {
184
+ return prev.concat(current);
185
+ }, []);
186
+ return Promise.all(middlewares.map((x) => {
187
+ return x.afterHydration(relData);
188
+ }));
189
+ }
190
+ }
191
+ exports.BelongsToPopulateDataMiddleware = BelongsToPopulateDataMiddleware;
192
+ class BelongsToRelationResultTransformMiddleware {
193
+ afterQuery(data) {
194
+ return data.map((d) => {
195
+ const transformedData = Object.assign(d);
196
+ for (const key in transformedData) {
197
+ if (key.startsWith('$')) {
198
+ this.setDeep(transformedData, this.keyTransform(key), d[key]);
199
+ delete transformedData[key];
200
+ }
201
+ }
202
+ return transformedData;
203
+ });
204
+ }
205
+ modelCreation(_) {
206
+ return null;
207
+ }
208
+ // tslint:disable-next-line: no-empty
209
+ async afterHydration(_data) { }
210
+ /**
211
+ * Dynamically sets a deeply nested value in an object.
212
+ * Optionally "bores" a path to it if its undefined.
213
+ *
214
+ * @param obj - The object which contains the value you want to change/set.
215
+ * @param path - The array representation of path to the value you want to change/set.
216
+ * @param value - The value you want to set it to.
217
+ * @param setrecursively - If true, will set value of non-existing path as well.
218
+ */
219
+ setDeep(obj, path, value, setrecursively = true) {
220
+ path.reduce((a, b, level) => {
221
+ if (setrecursively && typeof a[b] === 'undefined' && level !== path.length - 1) {
222
+ a[b] = {};
223
+ return a[b];
224
+ }
225
+ if (level === path.length - 1) {
226
+ a[b] = value;
227
+ return value;
228
+ }
229
+ return a[b];
230
+ }, obj);
231
+ }
232
+ keyTransform(key) {
233
+ return key.replace(/\$+/g, '').split('.');
234
+ }
235
+ }
236
+ exports.BelongsToRelationResultTransformMiddleware = BelongsToRelationResultTransformMiddleware;
237
+ class DiscriminationMapMiddleware {
238
+ constructor(_description) {
239
+ this._description = _description;
240
+ }
241
+ afterQuery(data) {
242
+ return data;
243
+ }
244
+ modelCreation(data) {
245
+ if (this._description.DiscriminationMap && this._description.DiscriminationMap.Field) {
246
+ const distValue = data[this._description.DiscriminationMap.Field];
247
+ if (distValue && this._description.DiscriminationMap.Models.has(distValue)) {
248
+ const result = new (this._description.DiscriminationMap.Models.get(distValue))();
249
+ result.hydrate(data);
250
+ return result;
251
+ }
252
+ }
253
+ return null;
254
+ }
255
+ // tslint:disable-next-line: no-empty
256
+ async afterHydration(_data) { }
257
+ }
258
+ exports.DiscriminationMapMiddleware = DiscriminationMapMiddleware;
259
259
  //# sourceMappingURL=middlewares.js.map