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

Is Iterate over findAll result and update records possible? #17251

Closed
4 of 8 tasks
akshay-vishnoi opened this issue Apr 5, 2024 · 3 comments
Closed
4 of 8 tasks

Is Iterate over findAll result and update records possible? #17251

akshay-vishnoi opened this issue Apr 5, 2024 · 3 comments
Labels
pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet type: feature For issues and PRs. For new features. Never breaking changes.

Comments

@akshay-vishnoi
Copy link

akshay-vishnoi commented Apr 5, 2024

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Feature Description

Describe the feature you'd like to see implemented

When I use findAll function to find the records, I should be able to iterate over them and update them. For e.g.

users = await User.findAll({
    where: {
    name: "Akshay",
  }
})

for (const user of users) {
  if (condition) { // some logic goes here
    user.status = 'not-working'
    await user.save
  }
}

I don't want to use udpate:where because I have a logic in the if block which is dependent on other factors.

Right now, it doesn't save the individual user object, instead it fires a query on user.save

 UPDATE `users` SET `updatedAt`=? WHERE `id` = ?

Describe why you would like this feature to be added to Sequelize

We should be able to treat the result of finders as the set of model objects and perform further operations as per the need. Not sure if this is already available in sequelize and I am missing something. Please let me know if you need more details.

Is this feature dialect-specific?

  • No. This feature is relevant to Sequelize as a whole.
  • Yes. This feature only applies to the following dialect(s):

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in implementing my feature.

Indicate your interest in the addition of this feature by adding the 👍 reaction. Comments such as "+1" will be removed.

@akshay-vishnoi akshay-vishnoi added pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet type: feature For issues and PRs. For new features. Never breaking changes. labels Apr 5, 2024
@ephys
Copy link
Member

ephys commented Apr 5, 2024

That is already possible, and is pretty much what you did except that findAll and save must be awaited, and save was not called

If it's not working, there may be a bug in your code. Don't hesitate to post a reproducible example that fails. With the current information, it's not possible for us to say what is wrong with the code

@ephys ephys closed this as not planned Won't fix, can't repro, duplicate, stale Apr 5, 2024
@akshay-vishnoi
Copy link
Author

@ephys - My bad, I had used await in my original code but didn't put it in the description. It will be great if you can open the issue again.

I will try to create an example as soon as I can. I appreciate your responses. Thanks

@ephys
Copy link
Member

ephys commented Apr 7, 2024

Don't worry we still get notified of changes on closed issues. We can reopen once we have the info we need to determine if there is an issue :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet type: feature For issues and PRs. For new features. Never breaking changes.
Projects
None yet
Development

No branches or pull requests

2 participants