summary refs log tree commit diff
path: root/util/src
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-20 18:02:57 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-20 18:02:57 +0200
commit7809148867c7e30a5bab43fa46b506f5a67f89ba (patch)
tree5d9b706de44296bc6554ec51002248f5698652e6 /util/src
parent:construction: auto delete relations (diff)
downloadserver-7809148867c7e30a5bab43fa46b506f5a67f89ba.tar.xz
:sparkles: finish and fix .delete() for one-to-many relations
Diffstat (limited to 'util/src')
-rw-r--r--util/src/entities/BaseClass.ts17
1 files changed, 9 insertions, 8 deletions
diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts
index 2a621f40..8a0b7a26 100644
--- a/util/src/entities/BaseClass.ts
+++ b/util/src/entities/BaseClass.ts
@@ -83,7 +83,7 @@ export class BaseClassWithoutId extends BaseEntity {
 		if (!criteria) throw new Error("You need to specify delete criteria");
 
 		const repository = this.getRepository();
-		const promises = repository.metadata.relations.map((x) => {
+		const promises = repository.metadata.relations.map(async (x) => {
 			if (x.orphanedRowAction !== "delete") return;
 			if (typeof x.type === "string") return;
 
@@ -95,22 +95,23 @@ export class BaseClassWithoutId extends BaseEntity {
 					`Foreign key not found for entity ${repository.metadata.name} in relation ${x.propertyName}`
 				);
 			}
-			console.log(foreignKey);
 			const id = (criteria as any)[foreignKey.referencedColumnNames[0]];
 			if (!id) throw new Error("id missing in criteria options");
 
-			if (x.relationType === "many-to-many" || x.relationType === "one-to-many") {
+			if (x.relationType === "many-to-many") {
 				return getConnection()
 					.createQueryBuilder()
 					.relation(this, x.propertyName)
 					.of(id)
 					.remove({ [foreignKey.columnNames[0]]: id });
-			} else if (x.relationType === "one-to-one" || x.relationType === "many-to-one") {
+			} else if (
+				x.relationType === "one-to-one" ||
+				x.relationType === "many-to-one" ||
+				x.relationType === "one-to-many"
+			) {
 				return getConnection()
-					.createQueryBuilder()
-					.from(x.inverseEntityMetadata, "user")
-					.of(id)
-					.remove({ [foreignKey.name]: id });
+					.getRepository(x.inverseEntityMetadata.target)
+					.delete({ [foreignKey.columnNames[0]]: id });
 			}
 		});
 		await Promise.all(promises);