/
has-many-through.repository.ts
258 lines (243 loc) · 7.69 KB
/
has-many-through.repository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright IBM Corp. 2019,2020. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {
constrainDataObject,
constrainFilter,
constrainWhere,
constrainWhereOr,
Count,
DataObject,
Entity,
EntityCrudRepository,
Filter,
Getter,
Options,
Where,
} from '../..';
/**
* CRUD operations for a target repository of a HasManyThrough relation
*
* EXPERIMENTAL: This interface is not stable and may change in the near future.
* Backwards-incompatible changes may be introduced in semver-minor versions.
*/
export interface HasManyThroughRepository<
Target extends Entity,
TargetID,
Through extends Entity
> {
/**
* Create a target model instance
* @param targetModelData - The target model data
* @param options - Options for the operation
* @returns A promise which resolves to the newly created target model instance
*/
create(
targetModelData: DataObject<Target>,
options?: Options & {
throughData?: DataObject<Through>;
throughOptions?: Options;
},
): Promise<Target>;
/**
* Find target model instance(s)
* @param filter - A filter object for where, order, limit, etc.
* @param options - Options for the operation
* @returns A promise which resolves with the found target instance(s)
*/
find(
filter?: Filter<Target>,
options?: Options & {
throughOptions?: Options;
},
): Promise<Target[]>;
/**
* Delete multiple target model instances
* @param where - Instances within the where scope are deleted
* @param options
* @returns A promise which resolves the deleted target model instances
*/
delete(
where?: Where<Target>,
options?: Options & {
throughOptions?: Options;
},
): Promise<Count>;
/**
* Patch multiple target model instances
* @param dataObject - The fields and their new values to patch
* @param where - Instances within the where scope are patched
* @param options
* @returns A promise which resolves the patched target model instances
*/
patch(
dataObject: DataObject<Target>,
where?: Where<Target>,
options?: Options & {
throughOptions?: Options;
},
): Promise<Count>;
/**
* Creates a new many-to-many association to an existing target model instance
* @param targetModelId - The target model ID to link
* @param options
* @returns A promise which resolves to the linked target model instance
*/
link(
targetModelId: TargetID,
options?: Options & {
throughData?: DataObject<Through>;
throughOptions?: Options;
},
): Promise<Target>;
/**
* Removes an association to an existing target model instance
* @param targetModelId - The target model to unlink
* @param options
* @returns A promise which resolves to null
*/
unlink(
targetModelId: TargetID,
options?: Options & {
throughOptions?: Options;
},
): Promise<void>;
}
/**
* a class for CRUD operations for hasManyThrough relation.
*
* Warning: The hasManyThrough interface is experimental and is subject to change.
* If backwards-incompatible changes are made, a new major version may not be
* released.
*/
export class DefaultHasManyThroughRepository<
TargetEntity extends Entity,
TargetID,
TargetRepository extends EntityCrudRepository<TargetEntity, TargetID>,
ThroughEntity extends Entity,
ThroughID,
ThroughRepository extends EntityCrudRepository<ThroughEntity, ThroughID>
> implements HasManyThroughRepository<TargetEntity, TargetID, ThroughEntity> {
constructor(
public getTargetRepository: Getter<TargetRepository>,
public getThroughRepository: Getter<ThroughRepository>,
public getTargetConstraint: (
throughInstances: ThroughEntity | ThroughEntity[],
) => DataObject<TargetEntity>,
public getThroughConstraint: () => DataObject<ThroughEntity>,
public getThroughFkConstraint: (
targetInstance: TargetEntity,
) => DataObject<ThroughEntity>,
) {}
async create(
targetModelData: DataObject<TargetEntity>,
options?: Options & {
throughData?: DataObject<ThroughEntity>;
throughOptions?: Options;
},
): Promise<TargetEntity> {
const targetRepository = await this.getTargetRepository();
const throughRepository = await this.getThroughRepository();
const targetInstance = await targetRepository.create(
targetModelData,
options,
);
const targetConstraint = this.getThroughFkConstraint(targetInstance);
const throughConstraint = this.getThroughConstraint();
const constraints = {...targetConstraint, ...throughConstraint};
await throughRepository.create(
constrainDataObject(
options?.throughData ?? {},
constraints as DataObject<ThroughEntity>,
),
options?.throughOptions,
);
return targetInstance;
}
async find(
filter?: Filter<TargetEntity>,
options?: Options & {
throughOptions?: Options;
},
): Promise<TargetEntity[]> {
const targetRepository = await this.getTargetRepository();
const throughRepository = await this.getThroughRepository();
const throughConstraint = this.getThroughConstraint();
const throughInstances = await throughRepository.find(
constrainFilter(undefined, throughConstraint),
options?.throughOptions,
);
const targetConstraint = this.getTargetConstraint(throughInstances);
return targetRepository.find(
constrainFilter(filter, targetConstraint),
options,
);
}
async delete(
where?: Where<TargetEntity>,
options?: Options & {
throughOptions?: Options;
},
): Promise<Count> {
const targetRepository = await this.getTargetRepository();
const throughRepository = await this.getThroughRepository();
const throughConstraint = this.getThroughConstraint();
const throughInstances = await throughRepository.find(
constrainFilter(undefined, throughConstraint),
options?.throughOptions,
);
const targetConstraint = this.getTargetConstraint(throughInstances);
// delete throughs that have the targets that are going to be deleted
const throughFkConstraint = this.getThroughFkConstraint(
targetConstraint as TargetEntity,
);
await throughRepository.deleteAll(
constrainWhereOr({}, [throughConstraint, throughFkConstraint]),
);
// delete target(s)
return targetRepository.deleteAll(
constrainWhere(where, targetConstraint as Where<TargetEntity>),
options,
);
}
// only allows patch target instances for now
async patch(
dataObject: DataObject<TargetEntity>,
where?: Where<TargetEntity>,
options?: Options & {
throughOptions?: Options;
},
): Promise<Count> {
const targetRepository = await this.getTargetRepository();
const throughRepository = await this.getThroughRepository();
const throughConstraint = this.getThroughConstraint();
const throughInstances = await throughRepository.find(
constrainFilter(undefined, throughConstraint),
options?.throughOptions,
);
const targetConstraint = this.getTargetConstraint(throughInstances);
return targetRepository.updateAll(
constrainDataObject(dataObject, targetConstraint),
constrainWhere(where, targetConstraint as Where<TargetEntity>),
options,
);
}
async link(
targetModelId: TargetID,
options?: Options & {
throughData?: DataObject<ThroughEntity>;
throughOptions?: Options;
},
): Promise<TargetEntity> {
throw new Error('Method not implemented.');
}
async unlink(
targetModelId: TargetID,
options?: Options & {
throughOptions?: Options;
},
): Promise<void> {
throw new Error('Method not implemented.');
}
}