Skip to content

fix(types): allow Op.not and Op.col in WhereOptions - #14087

Closed
remcohaszing wants to merge 1 commit into
sequelize:mainfrom
remcohaszing:where-type-fixes
Closed

remcohaszing wants to merge 1 commit into
sequelize:mainfrom
remcohaszing:where-type-fixes

Conversation

@remcohaszing

Copy link
Copy Markdown

Pull Request Checklist

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

  • Have you added new tests to prevent regressions?
  • Does yarn test or yarn 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 Of Change

Define Op.not and Op.col in WhereOptions.

Todos

  • N/A

@WikiRik
WikiRik requested a review from ephys February 10, 2022 16:10
@WikiRik WikiRik added the typescript For issues and PRs. Things that involve typescript, such as typings and intellisense. label Feb 10, 2022
@ephys ephys mentioned this pull request Feb 10, 2022
4 of 6 tasks
@ephys

ephys commented Feb 10, 2022

Copy link
Copy Markdown
Member

Thank you for the PR!

We do have typings for Op.not but they're incomplete since they should be allowed on the same level as Op.and & Op.or

I'd very much like to deprecate Op.col in favor of Sequelize.col, but as long as it's still a thing we might as well properly type it.

Current fix is incomplete,
Op.col should be accepted everywhere Col is.
I think there are more places where Op.not should be accepted.

I actually have an active PR that tries to fully fix WhereOptions, and it was missing your changes.
I'd like to incorporate changes from this PR in that PR. Op.not could be added to AcceptOrAnd (AcceptOrAndNot)

Would you be willing to join efforts and try to come up with WhereOptions that are failing in types but should be valid (and vice-versa)?

@remcohaszing

Copy link
Copy Markdown
Author

My initial attempt to fix the type errors was to replace all occurrences of { [Op.col]: foo } in our code base with col(foo), but that broke several tests. So I don’t think it’s safe to say these two are always interchangeable.

This is the code base I’m working on where I’m actively using Sequelize. It’s a monorepo with a package server where Sequelize is used.

You can discover the Sequelize types related issues I’m running into by running:

git clone https://gitlab.com/appsemble/appsemble.git
cd appsemble
yarn
yarn add typescript@latest
cd packages/server
yarn tsc

There are some edge cases not fixed by this PR, but I could resolve them by rewriting the queries.

@ephys

ephys commented Feb 10, 2022 •

Copy link
Copy Markdown
Member

My initial attempt to fix the type errors was to replace all occurrences of { [Op.col]: foo } in our code base with col(foo), but that broke several tests. So I don’t think it’s safe to say these two are always interchangeable.

They should be! If you have a query where Sequelize.col fails but Op.col does not, could you send it to me so I include a fix in #14020?

This is the code base I’m working on where I’m actively using Sequelize. It’s a monorepo with a package server where Sequelize is used.

Thanks! I'll test #14022 against it and include tests for the failing cases

@ephys

ephys commented Feb 10, 2022

Copy link
Copy Markdown
Member

I followed your steps but tsc didn't detect any issue (sequelize 6.16.1), are they in a different branch?

@remcohaszing

Copy link
Copy Markdown
Author

My bad! Because yarn add is ran from a monorepo root, the -W flag is needed.

git clone https://gitlab.com/appsemble/appsemble.git
cd appsemble
yarn
yarn add -W typescript@latest
cd packages/server
yarn tsc

It’s getting late here, so I’m going offline now. I’ll provide an example where col() and Op.col are not interchangeable tomorrow!

@remcohaszing

Copy link
Copy Markdown
Author

These are the steps to reproduce the issue about col I described earlier:

  1. Clone and install the Appsemble repository
    git clone https://gitlab.com/appsemble/appsemble.git
    cd appsemble
    yarn
  2. Spin up a Postgres database using docker-compose:
    docker-compose up -d
  3. For convenience, make jest only run this test in packages/server/src/controllers/organizations.test.ts and enable verbose logging, so the queries generated by Sequelize are logged
      describe('createOrganization', () => {
    -   it('should create a new organization', async () => {
    +   it.only('should create a new organization', async () => {
    +     const { setLogLevel } = await import('@appsemble/node-utils');
    +     setLogLevel('silly');
          authorizeStudio();
  4. Run the test. This should run fine.
    jest packages/server/src/controllers/organizations.test.ts
  5. Now modify a query in using Op.col in packages/server/src/controllers/organizations.ts
        await user.reload({
          attributes: ['primaryEmail', 'name'],
          include: [
            {
              required: false,
              model: EmailAuthorization,
              attributes: ['verified'],
              where: {
    -            email: { [Op.col]: 'User.primaryEmail' },
    +            email: col('User.primaryEmail'),
              },
            },
          ],
        });   
  6. Rerun the test.
    jest packages/server/src/controllers/organizations.test.ts
    This test now:
    • Generates a different query
    • The generated query is invalid
    • Hangs, because Sequelize hangs indefinitely on invalid queries.
  7. Exit jest by pressing Ctrl + C

appsemble-bot pushed a commit to appsemble/appsemble that referenced this pull request Feb 15, 2022
The new TypeScript version uncovers some issues in Sequelize that need
to be worked around.

See sequelize/sequelize#14022 and
sequelize/sequelize#14087 for details.
@ephys

ephys commented Feb 18, 2022

Copy link
Copy Markdown
Member

I'm back after a few days away, I'll try to take a look at this today

@ephys ephys self-assigned this Feb 20, 2022
@ephys

ephys commented Feb 21, 2022

Copy link
Copy Markdown
Member

I've managed to reproduce the issue with col() not behaving as expected. I've added a disabled unit test in #14022
I've also started work on fixing typings for Op.not & Op.col over there

@ephys

ephys commented Mar 9, 2022

Copy link
Copy Markdown
Member

#14022 is ready, just waiting for reviews before merging :)

There are typing tests for Op.not and Op.col in test/unit/sql/where.test.ts

@ephys ephys closed this in #14022 Mar 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

3 participants