Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change behaviour of update and add a replace method #88

Closed
lroal opened this issue May 24, 2024 · 2 comments · Fixed by #90
Closed

Change behaviour of update and add a replace method #88

lroal opened this issue May 24, 2024 · 2 comments · Fixed by #90

Comments

@lroal
Copy link
Member

lroal commented May 24, 2024

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)
})
@andreasbhansen
Copy link

That's a great change proposal 🎉

@lroal
Copy link
Member Author

lroal commented May 28, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants