Skip to content

Commit bde24aa

Browse files
committed
refactor: readded delete method
1 parent d8a7c94 commit bde24aa

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/polymorphic.repository.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,6 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
241241

242242
const metadata = this.getPolymorphicMetadata();
243243

244-
// TODO find if it has a parent metadata
245-
// TODO set the columns
246-
247244
metadata.map((options: PolymorphicOptionsType) => {
248245
if (this.isParent(options)) {
249246
(Array.isArray(entityOrEntities)
@@ -286,6 +283,39 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
286283
// );
287284
}
288285

286+
private async deletePolymorphs(
287+
entity: E,
288+
options: PolymorphicMetadataInterface[],
289+
): Promise<void | never> {
290+
await Promise.all(
291+
options.map(
292+
(option: PolymorphicMetadataInterface) =>
293+
new Promise(resolve => {
294+
if (!option.deleteBeforeUpdate) {
295+
return Promise.resolve();
296+
}
297+
298+
const entityTypes = Array.isArray(option.classType)
299+
? option.classType
300+
: [option.classType];
301+
302+
resolve(
303+
Promise.all(
304+
entityTypes.map((type: () => Function | Function[]) => {
305+
const repository = this.findRepository(type);
306+
307+
repository.delete({
308+
[entityTypeColumn(option)]: type,
309+
[entityIdColumn(option)]: entity[PrimaryColumn(option)],
310+
});
311+
}),
312+
),
313+
);
314+
}),
315+
),
316+
);
317+
}
318+
289319
find(options?: FindManyOptions<E>): Promise<E[]>;
290320

291321
find(conditions?: FindConditions<E>): Promise<E[]>;
@@ -388,9 +418,5 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
388418
return entity;
389419
}
390420

391-
// TODO add save, update etc
392-
393421
/// TODO implement remove and have an option to delete children/parent
394-
395-
// TODO implement method to prevent hydrating parent search to stop circular
396422
}

0 commit comments

Comments
 (0)