Repository navigation
fix(types): make WhereOptions more accurate - #14022
Conversation
|
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? |
|
If you mean in the parameters, because we can put both and & or inside another and & or :)
But with my yet-to-be-pushed draft, these two types don't exist anymore. Turns out that the issues goes much deeper |
|
I've added as many variations of valid & invalid More tests welcome In the new version, In v7, I'd like to type it based on what the attribute says its type is. |
|
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: |
|
I've tested the updated version against a large codebase and it resolved all existing query issues related to 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 Edit: that is actually not valid. It throws. It'd be nice if |
71ae139 to
50eece2
Compare
50eece2 to
ff0caa1
Compare
|
Apparently, this works: User.findAll({
where: {
id: or(1, 2),
},
});but this doesn't, it generates 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 |
…ize into ephys/fix-and-or-typing
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 And Looks like support for |
|
Can confirm that doing something like 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: 2IMO 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; |
|
Changed the typings of the two-arguments overload of 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 I'm starting to really need this PR for the other 2 PRs I have in progress. |
|
I'll take a look a the additional changes tomorrow afternoon. Any new TODOs from those commits are for v7, correct? |
Yes, I'm hoping to handle them all in #14020 |
|
🎉 This PR is included in version 7.0.0-alpha.11 🎉 The release is available on: Your semantic-release bot 📦🚀 |



v7 version of the PR, see #14368 for v6
Pull Request Checklist
Please make sure to review and check all of these items:
npm run testornpm run test-DIALECTpass with this change (including linting)?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 acceptedOp.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.fn(),col(),literal(),cast(),where(),json()and{ [Op.col] }can be used.::castingsyntax, andjson.pathsyntax.DataTypes.RANGEmust be typed when using TypeScript. There was a newline type change in that file.Op.adjacentThis 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:

