Skip to content

feat: add support for model inheritance - #16095

Merged
ephys merged 7 commits into
mainfrom
ephys/inheritance
Jun 17, 2023
Merged

ephys merged 7 commits into
mainfrom
ephys/inheritance

Conversation

@ephys

@ephys ephys commented Jun 3, 2023 •

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

This PR makes extending a model inherit the relevant model options, attributes, and associatiosn

This feature is only available for the new decorator-based model declaration.

Abstract Models

the @Table.Abstract decorator can be used to declare abstract models that can only be used for inheritance.

These models are never initialized. They only exist to register metadata on them.

Model Option Inheritance

Options are inherited from parent models just like if you used the @Table decorator multiple times on the final model. In case of conflicting options, the options set on the descendant overwrite the options set on the parent instead of throwing an error.

The tableName, name and modelName options are never inherited.

Attribute Inheritance

Options are inherited from parent models just like if you used the @Attribute decorator multiple times on the final model. In case of conflicting options, the options set on the descendant overwrite the options set on the parent instead of throwing an error.

Association Inheritance

Only @BelongsTo without the inverse option can be inherited. All other association types, or using the inverse option will throw an error.

This limitation is put into place to prevent associations from declare conflicting associations on the target model.

This could be revisited in the future by providing a way to apply different options based on which child model is being initialized, like this:

class BaseUser extends Model<InferAttributes<BaseUser>> {
  @hasMany(() => Project, SourceModel => ({
    // this will declare a different foreign key on `Project` for each class extending BaseUser
    foreignKey: lowerFirst(SourceModel.name) + 'Id',
    inverse: {
      as: lowerFirst(SourceModel.name),
    },
  }))
  declare projects?: NonAttribute<Project[]>;
}

But that is currently out of scope.

Todos

  • Document model inheritance in documentation repository

@ephys
ephys marked this pull request as draft June 3, 2023 13:26
@ephys
ephys marked this pull request as ready for review June 3, 2023 14:24
@ephys
ephys requested a review from a team June 3, 2023 14:24

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

One compliment and one small question, but apart from that it looks great!

@ephys
ephys requested a review from WikiRik June 17, 2023 13:45
@ephys
ephys enabled auto-merge (squash) June 17, 2023 13:49
auto-merge was automatically disabled June 17, 2023 14:21

Merge queue setting changed

@ephys
ephys enabled auto-merge June 17, 2023 14:22
@ephys
ephys added this pull request to the merge queue Jun 17, 2023
Merged via the queue into main with commit 6c553a9 Jun 17, 2023
@ephys
ephys deleted the ephys/inheritance branch June 17, 2023 14:58
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