Skip to content

feat: query builder #17937

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

Open
wants to merge 24 commits into
base: v6
Choose a base branch
from
Open

feat: query builder #17937

wants to merge 24 commits into from

Conversation

yagodorea
Copy link

@yagodorea yagodorea commented Jul 17, 2025

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • If a documentation update is necessary, have you opened a PR to the documentation repository? docs: query builder website#804
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Does the name of your PR follow our conventions?

Description of Changes

Adds a simple query builder to sequelize static models. Basic functionality:

  • Model.select() -> will instantiate the query builder for chaining
  • .attributes(['a', 'b']) -> select attributes
  • .where({c: true, d: 1}) -> specify where conditions
  • .getQuery() -> return plain SQL
  • Add an .execute() method that will run the raw sql instead of returning it
  • Support .limit() and .offset()
  • Support .orderBy([['a', 'DESC'], ['b', 'ASC']])
  • Support .groupBy(['a', 'b', 1, 2])
  • Support .having()
  • Support .andHaving()
  • Support .include() with custom join logic

Future improvements/additions:

  • Support returning the Model objects instead of raw result
  • Support .andWhere using Sequelize.where(col, op, value), Sequelize.literal() and WhereBrackets as per proposal
  • Support .orWhere, block simultaneous usage with .andWhere
  • Allow QueryBuilder on right-hand side of where options as a subquery/literal (DynamicValues?)
  • Support CTEs

Disclaimer: history is kinda dirty because I accidentally merged the PR to the wrong branch, had to revert it and recreate it, but hopefully we can squash merge it 🙏

@WikiRik
Copy link
Member

WikiRik commented Jul 19, 2025

Hi! Thanks for willing to work on sequelize. Unfortunately we won't be merging this to the v6 branch, we're not really accepting new features on there anymore. If you have a PR for the main branch we're happy to review it

@yagodorea
Copy link
Author

Hey @WikiRik, is there any way this could be an exception? This is something my team (and likely many others) need dearly, and to have to migrate to sequelize v7 (which is still in alpha) to get it would be a much more abrasive process. Having it only for v7 would also diminish a thousand times the impact of this feature.

Regardless, if it's really a deal breaker, I can work on the v7 PR, as I was already going to do anyway.

@yagodorea
Copy link
Author

PR for v7 -> #17958

@WikiRik
Copy link
Member

WikiRik commented Aug 1, 2025

@yagodorea I didn't respond to your message yet. I think for v6 it might be better to publish a fork, since we have limited time to work on sequelize and I'd rather have that we spend the time to promote v7 to beta/stable and review open PRs for that than to also review v6 PRs.
I've started the review on the v7 PR, I hope to be able to finish it this weekend

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

Successfully merging this pull request may close these issues.

2 participants