Replies: 5 comments
-
You need to use QB for upserts, there you have the on conflict support. |
Beta Was this translation helpful? Give feedback.
-
Is it possible to batch QB requests? otherwise for an array of 500 items, I would be awaiting each qb.execute |
Beta Was this translation helpful? Give feedback.
-
Or, how can I solve the original problem using existing EntityManager methods? |
Beta Was this translation helpful? Give feedback.
-
You can use batch insert with QB. There is no way via UoW other than trying to load the entities first, using explicit transaction so the select is done in the same transaction. I have no plans to support native upserts via UoW, if you want those, QB is the only way. |
Beta Was this translation helpful? Give feedback.
-
Check the QB tests for examples, for multiinsert just pass array of objects into qb.insert() https://github.com/mikro-orm/mikro-orm/blob/master/tests/QueryBuilder.test.ts |
Beta Was this translation helpful? Give feedback.
-
Is your feature request related to a problem? Please describe.
Suppose I have an array of plain javascript objects from outside input.
I want to insert any that are missing in the table and update any that already exists (upsert).
I have tried converting the javascript objects to Entities using assign, but when flushing they aren't saved to db.
I have also tried using persist() but then for the items that already exist results in duplicate pk error.
I also tried using merge() .. not sure if it is relevant but then for new items they don't get inserted.
Currently it seems I have to know whether each one already exists or not and call persist or assign accordingly.
TypeORM allows me to just call save on the whole array.
Maybe this is already possible but I just don't know how?
Describe the solution you'd like
An easy way to upsert an array of javascript objects or Entities.
For postgresql could use on conflict to handle the upsert.
Beta Was this translation helpful? Give feedback.
All reactions