fix: upsertWithReplace should delete non-persist relationship fields … (#6960)
…before upsert
This commit is contained in:
1 parent
9ccd88d025
commit
a0005faa14
2 files changed
+42
No files matched your search
+32
@@ -385,6 +385,38 @@ describe("mikroOrmRepository", () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should clear the parent entity from the one-to-many relation", async () => {
|
||||||
|
const entity1 = {
|
||||||
|
id: "1",
|
||||||
|
title: "en1",
|
||||||
|
entity2: [{ title: "en2-1", entity1: null }],
|
||||||
|
}
|
||||||
|
|
||||||
|
await manager1().upsertWithReplace([entity1], {
|
||||||
|
relations: ["entity2"],
|
||||||
|
})
|
||||||
|
const listedEntities = await manager1().find({
|
||||||
|
where: { id: "1" },
|
||||||
|
options: { populate: ["entity2"] },
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(listedEntities).toHaveLength(1)
|
||||||
|
expect(listedEntities[0]).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
id: "1",
|
||||||
|
title: "en1",
|
||||||
|
})
|
||||||
|
)
|
||||||
|
expect(listedEntities[0].entity2.getItems()).toHaveLength(1)
|
||||||
|
expect(listedEntities[0].entity2.getItems()).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
title: "en2-1",
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
it("should only update the parent entity of a one-to-many if relation is not included", async () => {
|
it("should only update the parent entity of a one-to-many if relation is not included", async () => {
|
||||||
const entity1 = {
|
const entity1 = {
|
||||||
id: "1",
|
id: "1",
|
||||||
|
|||||||
@@ -640,6 +640,16 @@ export function mikroOrmBaseRepositoryFactory<T extends object = object>(
|
|||||||
Object.assign(normalizedDataItem, {
|
Object.assign(normalizedDataItem, {
|
||||||
...joinColumnsConstraints,
|
...joinColumnsConstraints,
|
||||||
})
|
})
|
||||||
|
// Non-persist relation columns should be removed before we do the upsert.
|
||||||
|
Object.entries(relation.targetMeta?.properties ?? {})
|
||||||
|
.filter(
|
||||||
|
([_, propDef]) =>
|
||||||
|
propDef.persist === false &&
|
||||||
|
propDef.reference === ReferenceType.MANY_TO_ONE
|
||||||
|
)
|
||||||
|
.forEach(([key]) => {
|
||||||
|
delete normalizedDataItem[key]
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
await this.upsertMany_(manager, relation.type, normalizedData)
|
await this.upsertMany_(manager, relation.type, normalizedData)
|
||||||
|
|||||||
Reference in new issue
Block a user