data icon indicating copy to clipboard operation
data copied to clipboard

Support "upsert" and "upsertMany" model method

Open kettanaito opened this issue 5 years ago • 5 comments

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'
  }
})

kettanaito avatar Dec 11 '20 08:12 kettanaito

When is upsertMany applicable? Seems chaotic to update multiple entities during which some of them could also be created.

kettanaito avatar Dec 11 '20 20:12 kettanaito

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

marcosvega91 avatar Dec 11 '20 21:12 marcosvega91

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.

kettanaito avatar Dec 12 '20 16:12 kettanaito

I don't know if is very useful here. We can evaluate then

marcosvega91 avatar Dec 15 '20 13:12 marcosvega91

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)

kettanaito avatar Dec 22 '20 15:12 kettanaito