@spinajs/orm 2.0.179 → 2.0.181

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