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

Add Model.exists() to allow efficient checking of at least one matching row #17300

Open
2 of 6 tasks
c-oreills opened this issue May 2, 2024 · 0 comments
Open
2 of 6 tasks
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

@c-oreills
Copy link

Feature Description

Describe the feature you'd like to see implemented

Add a new exists method on model, taking the same parameters as findOne, which returns a boolean of whether at least one matching row exists in the database.

Example usage:

await User.create({ name: 'Alice' });

await User.exists({ name: 'Alice' }); // true
await User.exists({ name: 'Bob' }); // false

Under the hood this would execute a count query limited to 1 and coerce to a boolean, so that it's efficient on the database and network.

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

This is a convenience method. I recently needed this to avoid O(NM) complexity in a check. I ended up using findOne instead and checking for a null, but didn't need the resulting model instance.

The limit of 1 is more efficient for large queries.

The API mirrors a Django's QuerySet.exists

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.

@c-oreills c-oreills 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 May 2, 2024
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

1 participant