alfateam/orange-orm

Change behaviour of update and add a replace method

lroal opened this issue · 2 comments

Feature Description

Currently, the update method will replace the entire row - even for columns not specified in payload. Also, it is based on primary key being present.
I would be better to move the update method to a replace method. This reflects better what it actually does.
Then change the behaviour of update to do a batch update - allowing to pass in a filter.
This feels more natural and is in line with other ORMS.
Something like this:

db.user.update({
  is_banned: true
}, {
  where: x => x.id.eq(user_id)
})

That's a great change proposal 🎉

I am pretty much done, https://github.com/alfateam/orange-orm/tree/replace
Not sure I wanna do a major version bump - even though it is a breaking change.

If you include a fetching strategy, the affected rows and their related data will be returned; otherwise, no data is returned.

import map from './map';
const db = map.sqlite('demo.db');

update();

async function update() {

  const propsToBeModified = {
    orderDate: new Date(),
    customerId: 2,
    lines: [
      { id: 1, product: 'Bicycle', amount: 250 }, //already existing line
      { id: 2, product: 'Small guitar', amount: 150 }, //already existing line
      { product: 'Piano', amount: 800 } //the new line to be inserted
    ]
  };

  const optionalFetchingStrategy = {customer: true, deliveryAddress: true, lines: true};
  const orders = await db.order.update(propsToBeModified, { where: x => x.id.eq(1) }, optionalFetchingStrategy);
}