Skip to content

fix(types): make WhereOptions more accurate - #14022

Merged
ephys merged 85 commits into
mainfrom
ephys/fix-and-or-typing
Mar 22, 2022
Merged

ephys merged 85 commits into
mainfrom
ephys/fix-and-or-typing

Conversation

@ephys

@ephys ephys commented Jan 27, 2022 •

Copy link
Copy Markdown
Member

v7 version of the PR, see #14368 for v6

Pull Request Checklist

Please make sure to review and check all of these items:

  • Have you added new tests to prevent regressions?
  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • 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?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description

This PR fixes many typing errors with WhereOperators.

List of changes

  • and(), or(), { [Op.and] }, { [Op.or] }, { [Op.not] } are updated to properly reflect where they are actually accepted
  • Improved typing for Op.any, Op.all, Op.values. Almost all operators are supposed to support these operators. Actual support is spotty, but to be improved in a follow-up PR.
  • Updated what each operator actually accepts.
  • Updated where fn(), col(), literal(), cast(), where(), json() and { [Op.col] } can be used.
  • Improved support for PG Arrays.
  • Improved typing support for ::casting syntax, and json.path syntax.
  • Restrict comparisons to null & boolean: only nullable attributes, and boolean attributes respectively can be compared to those two types.
  • Documented how DataTypes.RANGE must be typed when using TypeScript. There was a newline type change in that file.
  • Added missing types for Op.adjacent

This PR adds many unit tests that are currently disabled. These are queries that failed even though they should not have.
As this PR is only a typing PR, I will be fixing these new unit tests in #14020

Closes #14087
Closes #11079

Examples of non-errors this PR tries to fix:
image
image

@ephys ephys self-assigned this Jan 27, 2022
@ephys ephys added the typescript For issues and PRs. Things that involve typescript, such as typings and intellisense. label Jan 27, 2022
@WikiRik

WikiRik commented Jan 27, 2022 •

Copy link
Copy Markdown
Member

Out of curiosity; why would we allow both AndOperator and OrOperator for and and or instead of only AndOperator for and and OrOperator for or?

@ephys

ephys commented Jan 27, 2022 •

Copy link
Copy Markdown
Member Author

If you mean in the parameters, because we can put both and & or inside another and & or :)

or(and(), and()) should be valid, and and(or(), or()) too

But with my yet-to-be-pushed draft, these two types don't exist anymore. Turns out that the issues goes much deeper

@ephys

ephys commented Jan 27, 2022

Copy link
Copy Markdown
Member Author

I've added as many variations of valid & invalid where as I could think of (here) & crafted the new typings in accordance.

More tests welcome

In the new version, AndOperator, OrOperator, and WhereValue are unused (pending removal in v7)

In v7, I'd like to type it based on what the attribute says its type is.
So { name: Number } would be an error if name has been declared as a string in the model's attributes.

@ephys

ephys commented Jan 28, 2022 •

Copy link
Copy Markdown
Member Author

I think this PR is ready but I'd like to merge my other TS PRs before undrafting so I can test a unified build against a real codebase and see if something is still missing.

Those are:

@ephys

ephys commented Feb 7, 2022 •

Copy link
Copy Markdown
Member Author

I've tested the updated version against a large codebase and it resolved all existing query issues related to and & or 🥳

I think it's ready

Edit: it would have been if I didn't make my changes in /dist instead of /types

Edit: Ok now it should work.

Edit: found new errors I should fix / investigate

image

Edit: that is actually not valid. It throws. It'd be nice if undefined values were just ignored imo. Makes building conditions easier :)
So anyway, this PR is ready

@ephys
ephys marked this pull request as ready for review February 7, 2022 14:30
@ephys
ephys force-pushed the ephys/fix-and-or-typing branch from 71ae139 to 50eece2 Compare February 7, 2022 14:53
@ephys
ephys force-pushed the ephys/fix-and-or-typing branch from 50eece2 to ff0caa1 Compare February 7, 2022 14:54
@ephys
ephys marked this pull request as draft February 7, 2022 14:59
@ephys
ephys marked this pull request as ready for review February 7, 2022 15:04
@ephys
ephys requested a review from a team February 7, 2022 15:16
@ephys ephys changed the title fix(types): broaden and & or typing fix(types): make and & or typings more accurate Feb 7, 2022
Comment thread types/lib/model.d.ts Resolved
Comment thread types/lib/model.d.ts Resolved
@ephys

ephys commented Feb 9, 2022 •

Copy link
Copy Markdown
Member Author

Apparently, this works:

User.findAll({
  where: {
    id: or(1, 2),
  },
});

but this doesn't, it generates id = [Object object]:

User.findAll({
  where: {
    id: { [Op.eq]: or(1, 2) },
  },
});

I'll reflect it in the typings but I'd like to fix it in #14020

Edit: fixed. Typings mark the first one as valid but the second one as invalid

WikiRik
WikiRik previously approved these changes Mar 15, 2022
@ephys

ephys commented Mar 15, 2022 •

Copy link
Copy Markdown
Member Author

I'm amazed about how many scenarios you come up with. I could not have come up with a824ed2

Thanks :) It's because I've been migrating query-generator to TypeScript and every time I see something unexpected I add a test to see how it's supposed to behave.

I have a new one: Apparently Sequelize.fn supports WhereOptions?

image

And Sequelize.col declares accepting Array input, but the part of the code that stringifies it does not support arrays:

image

Looks like support for col(['a', 'b']) and col('a', 'b') has been broken for so long with 0 complaints. We can just remove it.

@ephys

ephys commented Mar 15, 2022 •

Copy link
Copy Markdown
Member Author

Can confirm that doing something like fn('sum', { a: 1 }) } generates the SQL sum("a" = 1).

It was added on 2016-11-18 via PR #6685.

Apparently it's done to support doing something like this:

-- mysql / mariadb only
SELECT SUM(id IN (1,2)) FROM users;

-- out: 2

IMO this is the kind of edge-case that these functions should not try to resolve. This should be handled using improved literals because it's impossible to properly support everything:

import { sql, where } from 'sequelize';

// mysql
User.findOne({
  attributes: [
    sql`COUNT(${where({ id: 1 })})`,
  ]
})

// SELECT COUNT(id = 1) FROM users;
import { sql, where } from 'sequelize';

// postgres
User.findOne({
  attributes: [
    sql`COUNT(*) FILTER (WHERE ${where({ id: 1 })})`,
  ]
})

// SELECT COUNT(*) FILTER (WHERE i = 1) FROM users;

@ephys

ephys commented Mar 15, 2022

Copy link
Copy Markdown
Member Author

Changed the typings of the two-arguments overload of where() from:

function where(leftOperand: WhereLeftOperand, rightOperand: WhereOperators[Op.eq]<any>): Where;

to

function where(leftOperand: WhereLeftOperand, rightOperand: WhereAttributeHashValue<any>): Where;

which is more accurate.

The old version assumed it was equal to doing { leftOperand: { [Op.eq]: rightOperand } },
but in reality it's equal to doing { leftOperand: rightOperand } (they have slightly different behaviors. The second one accepts Op.{and,or,not}).


I'm starting to really need this PR for the other 2 PRs I have in progress.
I think we can merge this now. If I have more tests I want to add, I'll open a follow-up PR.

@ephys
ephys requested a review from WikiRik March 15, 2022 18:28
@WikiRik

WikiRik commented Mar 15, 2022

Copy link
Copy Markdown
Member

I'll take a look a the additional changes tomorrow afternoon. Any new TODOs from those commits are for v7, correct?

@WikiRik WikiRik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interested to see how you'll take this further in #14020

@ephys

ephys commented Mar 17, 2022

Copy link
Copy Markdown
Member Author

Any new TODOs from those commits are for v7, correct?

Yes, I'm hoping to handle them all in #14020

@ephys
ephys merged commit 81c3e50 into main Mar 22, 2022
@ephys
ephys deleted the ephys/fix-and-or-typing branch March 22, 2022 08:37
ephys added a commit that referenced this pull request Apr 12, 2022
ephys added a commit that referenced this pull request Apr 12, 2022
ephys added a commit that referenced this pull request Apr 12, 2022
ephys added a commit that referenced this pull request Apr 12, 2022
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 7.0.0-alpha.11 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

released on @v7 typescript For issues and PRs. Things that involve typescript, such as typings and intellisense.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

typescript definition: function inside operator inside where

5 participants