Skip to content

feat: migrate model definitions to TypeScript - #15431

Merged
ephys merged 22 commits into
mainfrom
ephys/model-definition-ts
Dec 18, 2022
Merged

ephys merged 22 commits into
mainfrom
ephys/model-definition-ts

Conversation

@ephys

@ephys ephys commented Dec 10, 2022 •

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

Sorry for yet another big PR 😓

⚠️: breaking change

This PR migrates the entire model definition to TypeScript. It also lays the first stone to be able to add support for Data Mapper.

TypeScript and static properties don't play nice, so this PR takes a different, backwards compatible, approach:

  • All parts of Model that contributed to its Model Definition, such as attributes, indexes, hooks, validation, etc, have been moved to a new class called ModelDefinition.
    • This new class is very protective of its data. Modifications are very controlled to ensure the model remains as stable as can be.
  • Each Active Record Model class has exactly one ModelDefinition instance, accessible through MyModel.modelDefinition.
    • In the future, each Data Mapper Model will have exactly one ModelDefinition instance per Sequelize instance. This is because, unlike Active Record Model, Data Mapper Models can be used with multiple Sequelize instances.
  • I added ModelRepository. It does not currently do anything but it will serve the same purposes as ModelDefinition but for methods that interact with a database. That's the file we'll progressively move model methods to as we migrate to TypeScript.
  • ⚠️ Model.rawAttributes has been removed because it served an ambiguous dual purpose. It has been split in two properties:
    • ModelDefinition.rawAttributes: Attributes as they were configured by the user. This object is mutable, but should only be used if the user needs to modify the definition.
    • ModelDefinition.attributes: Attributes that have been normalized by Sequelize. This object is immutable but should be used the user needs to read the stable definition.

Misc Changes

  • ✨✨ Fixed the bug with Class Properties overshadowing attribute getter & setters (How to fix the incompatibility with Public Class Fields #14300).
  • ✨✨ SQLITE: Indexes get properly copied over when altering a table now
  • ⚠️ ModelAttributeColumnOptions has been renamed to AttributeOptions to be less verbose
  • DEPRECATED: Model.tableName and Model.getTableName are both deprecated in favor of Model.table
    • Model.tableName was just the table without schema
    • Model.getTableName added a noise toString method we needed to delete
    • Model.table works like Model.getTableName but is clean and its output is immutable.
  • I added a bunch of TODOs, they're not new issues added by this PR but problems that this PR uncovered and which should be fixed in future PRs.
  • Hooks added by multiple @Table decorators are merged
  • Added beforeDefinitionRefresh and afterDefinitionRefresh which are triggered when refreshAttributes is called
  • ⚠️ Removed setterMethods, getterMethods (they were deprecated in v6). They throw an error if used to guide the user.
  • I renamed many "field" variables into either "columnName" or "attributeName" depending on what they represent
  • field and fieldName in attribute definitions are deprecated in favor of columnName and attributeName respectively (Clarify Column VS Attribute Terminology #14301)
  • IndexType can be either fully lowercase or fully uppercase, since we accepted both anyway!
  • Added the ability to declare an index on an attribute (side effect of index cleanup)
  • ⚠️ references.model (the attribute option) now always references a model. references.table has been added to reference tables instead.
    • When normalized, it always becomes references.table
  • ⚠️ Models throw if the user tries to declare an attribute that conflicts with the timestamp or version attributes that Sequelize needs to add
  • DEPRECATED: Model.primaryKeyAttribute, Model.primaryKeyField: They're not compatible with composite primary keys.
  • DEPRECATED: Model.primaryKeyAttributes, Model.fieldRawAttributesMap, Model.tableAttributes, Model.fieldAttributeMap, Model.refreshAttributes: Use Model.modelDefinition instead
  • ⚠️ Removed Model#validators, use Model.modelDefinition.attributes instead

Breaking changes

Don't forget the blank line in the commit message body


BREAKING CHANGE:

@ephys
ephys marked this pull request as draft December 10, 2022 21:42
Comment thread src/sequelize.js Resolved
@ephys
ephys marked this pull request as ready for review December 11, 2022 19:32
@ephys

ephys commented Dec 12, 2022

Copy link
Copy Markdown
Member Author

Something else I'm considering: remove the uniqueKeys property on models. It's never been reliable since it only listed unique indexes created by the unique options on attributes, instead of all indexes.

@ephys ephys mentioned this pull request Dec 12, 2022
2 of 5 tasks

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

Big PR indeed. I still have to look at the big changes to the model code but it sounds like a good approach for the future. Just a few comments from the files I've already viewed, it's great work as always

Comment thread test/unit/model/init.test.ts Resolved
Comment thread test/unit/hooks.test.js Resolved
Comment thread src/dialects/db2/query-interface.js Resolved
Comment thread src/utils/object.ts Resolved
@WikiRik

WikiRik commented Dec 12, 2022

Copy link
Copy Markdown
Member

Something else I'm considering: remove the uniqueKeys property on models. It's never been reliable since it only listed unique indexes created by the unique options on attributes, instead of all indexes.

That might not be a bad idea, but can we do that in a separate PR?

@ephys

ephys commented Dec 16, 2022

Copy link
Copy Markdown
Member Author

That might not be a bad idea, but can we do that in a separate PR?

Yep

@ephys
ephys requested a review from WikiRik December 17, 2022 12:56
WikiRik
WikiRik previously approved these changes Dec 17, 2022

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

Few minor remarks, but looking good!

Comment thread src/model-typescript.ts Resolved
Comment thread src/model-definition.ts Resolved
Comment thread src/model.js Resolved
@ephys
ephys requested a review from WikiRik December 18, 2022 11:54

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

Let's go!

@ephys

ephys commented Dec 18, 2022

Copy link
Copy Markdown
Member Author

I'll document the list of breaking changes & merge

@ephys
ephys merged commit f57e5a0 into main Dec 18, 2022
@ephys
ephys deleted the ephys/model-definition-ts branch December 18, 2022 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants