Skip to content

feat!: lock down registered Sequelize models - #17077

Merged
ephys merged 9 commits into
mainfrom
ephys/clean-model-manager
Feb 11, 2024
Merged

ephys merged 9 commits into
mainfrom
ephys/clean-model-manager

Conversation

@ephys

@ephys ephys commented Feb 11, 2024 •

Copy link
Copy Markdown
Member

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?
  • 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 Change

Closes #16483

The goal of this PR is to:

  • Unify sequelize.models with sequelize.modelManager.models to have a single source of truth
    • The concept of "ModelManager" is dropped. It is now a read-only Set view over the internal model set that only the sequelize instance can mutate. This class still has extra model-specific methods, but is basically a specialized extension of SetView
  • Prevent users from mutating the list of models directly
  • Disconnect a model from the sequelize instance if the model is removed from sequelize.models
  • Deduplicate methods. Instead of sequelize.isDefined, use sequelize.models.has or sequelize.models.hasByName. Instead of sequelize.model, use sequelize.models.get or sequelize.models.getOrThrow
  • A big change in this PR is that once a model is removed from the Sequelize instance, its model definition is deleted. This means it's possible to re-init a model with a different sequelize instance. I would not recommend doing so yet, as there is still some state present in model.js that has not yet been migrated to ModelDefinition.
    • This means I had to rework many of our tests as the models were initialized too soon and were being removed from the Sequelize instance.

Misc details:

  • sequelize.connectionManager is now a getter instead of a copy of sequelize.dialect.connectionManager
  • Deprecated sequelize.getDialect: It returns the dialect name, but sequelize.dialect is the dialect class. This is confusing and users should use sequelize.dialect.name if they want the name.
  • Deprecated sequelize.getQueryInterface in favor of sequelize.queryInterface to unify methods again
  • This cleans up our sqlite database tests a little bit more. Hopefully this should lead to less sqlite test failures

Breaking changes

  • It is not possible to directly mutate Sequelize's list of models anymore. Only Model.init, sequelize.define and sequelize.addModels can add models to that set. Only sequelize.removeAllModels can remove a model from that set.
  • sequelize.modelManager was removed in favor of sequelize.models
  • sequelize.models is not a plain object anymore, but a Set View with extra methods for model search that previously were available on sequelize.modelManager.

TODO

  • Document the breaking changes

@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.

Nice that we're getting closer to re-init a model.
The updates to the tests are also nice. I think after these it should be easier to migrate test suites to TypeScript but that's for future PRs.

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

Labels

None yet

Projects

Status: To document

Development

Successfully merging this pull request may close these issues.

Sequelize 7 should allow models to be registered on another instance when the original instance has been closed

2 participants