✨ finish and fix .delete() for one-to-many relations
This commit is contained in:
parent
ce09e01c21
commit
0247df3e42
@ -83,7 +83,7 @@ export class BaseClassWithoutId extends BaseEntity {
|
|||||||
if (!criteria) throw new Error("You need to specify delete criteria");
|
if (!criteria) throw new Error("You need to specify delete criteria");
|
||||||
|
|
||||||
const repository = this.getRepository();
|
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 (x.orphanedRowAction !== "delete") return;
|
||||||
if (typeof x.type === "string") 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}`
|
`Foreign key not found for entity ${repository.metadata.name} in relation ${x.propertyName}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
console.log(foreignKey);
|
|
||||||
const id = (criteria as any)[foreignKey.referencedColumnNames[0]];
|
const id = (criteria as any)[foreignKey.referencedColumnNames[0]];
|
||||||
if (!id) throw new Error("id missing in criteria options");
|
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()
|
return getConnection()
|
||||||
.createQueryBuilder()
|
.createQueryBuilder()
|
||||||
.relation(this, x.propertyName)
|
.relation(this, x.propertyName)
|
||||||
.of(id)
|
.of(id)
|
||||||
.remove({ [foreignKey.columnNames[0]]: 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()
|
return getConnection()
|
||||||
.createQueryBuilder()
|
.getRepository(x.inverseEntityMetadata.target)
|
||||||
.from(x.inverseEntityMetadata, "user")
|
.delete({ [foreignKey.columnNames[0]]: id });
|
||||||
.of(id)
|
|
||||||
.remove({ [foreignKey.name]: id });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user