Skip to content

feat: rewrite the part of QueryGenerator responsible for WHERE - #14020

Closed
ephys wants to merge 17 commits into
mainfrom
ephys/new-where-builder
Closed

ephys wants to merge 17 commits into
mainfrom
ephys/new-where-builder

Conversation

@ephys

@ephys ephys commented Jan 26, 2022 •

Copy link
Copy Markdown
Member

Large WIP PR to completely rewrite how WHERE is generated.

This PR is dependent on #14022, as unit tests & types must be improved first to ensure this PR doesn't break anything.

It's definitely not ready yet. A lot of tests are still failing.

The bulk of changes are in https://github.com/sequelize/sequelize/blob/ephys/new-where-builder/lib/dialects/abstract/where-sql-builder.ts

The new system is much more type-safe, with each operator being typed individually.

With this new system, it's also easier for dialects to override how an operator is built. They can override the method specific to that operator without needing to modify the other.

Closes #13602
Closes #13753

Goals of this PR:

  • Migrate the implementation of where to TypeScript
  • Remove all differences between the implementations of Sequelize.where() and pojo where objects (i.e. where: { firstName: 'zoé' }). Fixes Op.not operator is ignored when used with Sequelize.where() #13602
  • Remove behavior differences between { attribute: value } and { attribute: { [Op.eq]: value } }
  • Prepare the groundwork for a follow-up PR that implements Sequelize.attr as a attributeName-aware alternative to Sequelize.col
  • Prepare the groundwork for a follow-up PR that implements bind parameters in Sequelize.literal (eg. Sequelize.literal('name = ?name', { name: 'my name' }))
  • Accept all iterables in operators that do not explicitely need an array (eg. { attribute: { [Op.in]: new Set([1,2,3]) } })
  • Be more strict: An invalid where object should throw with a message describing what is invalid, instead of crashing with an obscure message.
  • Support using or inside another operator: { id: { [Op.eq]: or(1, 2) } } should function

TODO

@ephys ephys self-assigned this Jan 26, 2022
@ephys ephys mentioned this pull request Feb 9, 2022
4 of 6 tasks
@ephys

ephys commented Feb 10, 2022

Copy link
Copy Markdown
Member Author

If there are other open issues related to WHERE being incorrectly generated, don't hesitate to post them here so I can include them in the new implementation

@ephys
ephys force-pushed the ephys/new-where-builder branch from f6c4702 to 011e387 Compare March 14, 2022 15:49
@WikiRik

WikiRik commented Nov 11, 2022

Copy link
Copy Markdown
Member

Looking at the goals of this PR I don't really see any breaking changes. Is that correct? Since I do not think we'll land this in the initial release of v7 but postponing it to v8 would also be a waste

@ephys

ephys commented Nov 11, 2022 •

Copy link
Copy Markdown
Member Author

There is one breaking change: Writing the following is no longer supported

import { where, col } from '@sequelize/core';

User.findAll({
  where: where(col("a"), '=', col("b"))
})

instead users need to write the following:

import { where, col, Op } from '@sequelize/core';

User.findAll({
  where: where(col("a"), Op.eq, col("b"))
});

This is because where needs to know which operator is used to build the query properly. The left and right operands are not always serialized in exactly the same way, depending on the operator.

If the user needs to use a custom operator, they can do this instead:

import { sql, col } from '@sequelize/core';

User.findAll({
  where: sql`${col("a")} = ${col("b")}}`
});

Well... once I get to it #14299

I wouldn't worry about whether this gets postponed to v8. It's such a massive amount of work, I haven't been able to finish it in the past year. I think I'm better equipped to handle it now but I still have so many other things in progress to complete first that it may not be ready until it's time to consider releasing v8 :(

@WikiRik

WikiRik commented Nov 11, 2022

Copy link
Copy Markdown
Member

No worries about working on it for v8, v7 already has some nice new and improved features that this can wait a bit. Thanks for the information!

@ephys

ephys commented Jan 25, 2023

Copy link
Copy Markdown
Member Author

Replaced by #15598

@ephys ephys closed this Jan 25, 2023
@ephys
ephys deleted the ephys/new-where-builder branch June 8, 2023 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Problem on sequelize.where function with correctly placing % operators. Op.not operator is ignored when used with Sequelize.where()

2 participants