1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-25 03:33:33 +01:00

finish and fix .delete() for one-to-many relations

This commit is contained in:
Flam3rboy 2021-09-20 18:02:57 +02:00
parent ce09e01c21
commit 0247df3e42

View File

@ -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);