Replies: 2 comments
-
Nothing out of box provided, mainly because you can't just say that entity with PK should be updated, while without it it should be inserted - that might work for your use case, but it is not a universal rule - PK can be client side generated, like UUID, or even auto-generated, but you want to provide the value manually (like in test fixtures). First of all you should do all this inside explicit transaction. Not tested, but this should put you on the right path: function updateOrCreate(data: A) {
return this.em.transactional(async em => {
const a = data.id ? await em.findOneOrFail(A, data.id, { populate: ['items.items'] }) : em.create(A, {});
const aItems = [];
em.assign(a, { text: data.text });
em.persist(a);
for (const item of data.items) {
const b = item.id ? await em.findOneOrFail(B, item.id, { populate: ['items'] }) : em.create(B, {});
const bItems = [];
em.assign(b, { text: item.text });
em.persist(b);
bItems.push(b);
for (const item2 of item.items) {
const c = item2.id ? await em.findOneOrFail(C, item2.id) : em.create(C, {});
em.assign(c, { text: item2.text });
em.persist(c);
bItems.push(c);
}
item.items.set(bItems);
}
a.items.set(aItems);
});
} This code can be then generalized to work with the ORM metadata (you can access entity metadata via |
Beta Was this translation helpful? Give feedback.
-
Thank you for the quick response. I will look into your approach. |
Beta Was this translation helpful? Give feedback.
-
Hi there.
Can you point out me more neat way to update/create nested entities?
I came up with a code below but it seems to me it
s messy) I am going to write loose versions of entities because it
s not essential here.And i want to update/create the entity "A".
Beta Was this translation helpful? Give feedback.
All reactions