Support "upsert" and "upsertMany" model method
An upsert method updates an entity matching a query or creates a new entity if there was none. upsertMany behaves the same with the difference that it operates on multiple entities.
const newOrUpdatedUser = db.user.upsert({
query: {
which: { id: { equals: 'abc-123 } }
},
data: {
firstName: 'John'
}
})
When is upsertMany applicable? Seems chaotic to update multiple entities during which some of them could also be created.
I could be useful to update/create based on the primary key. Instead of get the entity, check if exist and if not then create, you can instead insertOrUpdate it
That's true, @marcosvega91. But I was wondering when it's beneficial with a *Many query? It looks quite unpredictable if I want to update multiple resources, some of which won't exist and the library would have to create them.
I don't know if is very useful here. We can evaluate then
What should be created if the query for upsert is given a range of values?
db.user.upsert({
which: { id: { in: [1, 2, 3] } }
})
- Should it create an entity with any of the given values in a range?
- Should it forbid certain query comparators (i.e. array) on
upsert? (that would be rather unfortunate)