Skip to content

[RFC] Rework association methods for better TS support #14302

Description

@ephys

Is your feature request related to a problem? Please describe.

The way association methods are generated today is cumbersome when using TypeScript.

sequelize-typescript solves this by adding $get, $count, etc methods.

Describe the solution you'd like

I think we could improve developer ergonomics by grouping generated methods in one object.

So instead of looking like this

import {
  Model,
  HasManyGetAssociationsMixin,
  HasManyAddAssociationMixin,
  HasManyHasAssociationMixin,
  HasManyCountAssociationsMixin,
  HasManyCreateAssociationMixin,
  Association,
} from "sequelize";

class User extends Model {
  declare getProjects: HasManyGetAssociationsMixin<Project>; // Note the null assertions!
  declare addProject: HasManyAddAssociationMixin<Project, number>;
  declare addProjects: HasManyAddAssociationsMixin<Project, number>;
  declare setProjects: HasManySetAssociationsMixin<Project, number>;
  declare removeProject: HasManyRemoveAssociationMixin<Project, number>;
  declare removeProjects: HasManyRemoveAssociationsMixin<Project, number>;
  declare hasProject: HasManyHasAssociationMixin<Project, number>;
  declare hasProjects: HasManyHasAssociationsMixin<Project, number>;
  declare countProjects: HasManyCountAssociationsMixin;
  declare createProject: HasManyCreateAssociationMixin<Project, 'ownerId'>;
  declare readonly projects?: Project[];

  declare static associations: {
    projects: Association<User, Project>;
  };
}

The association would look like this:

import { Model, HasManyAttribute } from "sequelize";

class User extends Model {
  declare readonly projects: HasManyAttribute<Project, number>;

  // static associations typings now smart enough, no need to set them :)
}

with HasManyAttribute looking like this

// HasManyAttribute definition (provided by sequelize)
class HasManyAttribute<Model, PrimaryKey> {
  value: Project[] | undefined; // when eager loaded through association. `undefined` when not loaded.
  findOne: HasManyGetAssociationMixin<Model>;
  findAll: HasManyGetAssociationsMixin<Model>;
  add: HasManyAddAssociationMixin<Model, PrimaryKey>;
  bulkAdd: HasManyAddAssociationsMixin<Model, PrimaryKey>;
  setAll: HasManySetAssociationsMixin<Model, PrimaryKey>;
  remove: HasManyRemoveAssociationMixin<Model, PrimaryKey>;
  bulkRemove: HasManyRemoveAssociationsMixin<Model, PrimaryKey>;
  has: HasManyHasAssociationMixin<Model, PrimaryKey>;
  hasMany: HasManyHasAssociationsMixin<Model, PrimaryKey>;
  count: HasManyCountAssociationsMixin;
  create: HasManyCreateAssociationMixin;
}

This would also open the door for a clean decorator support

import { Model, HasManyAttribute, HasMany } from "sequelize";

class User extends Model {
  @HasMany(Project)
  public readonly projects!: HasManyAttribute<Project, number>;
}

Why should this be in Sequelize

I think this way is less verbode, less error prone, and improves typescript users ergonomics.

This solution also enables us to add smart typings for the associations static property. Demo here

Note that this would be a pretty big breaking change.

Describe alternatives/workarounds you've considered

The way sequelize-typescript does it. But this solution lets us use different typings for each association type.

Feature Request Checklist

Is this feature dialect-specific?

  • No. This feature is relevant to Sequelize as a whole.

Would you be willing to implement this feature by submitting a Pull Request?

  • Yes, I have the time and I know how to start.

Activity

  1. changed the title [-]RFC: association methods in v7[/-] [+][RFC] Rework association methods for better TS support[/+] on Dec 23, 2021
  2. transferred this issue fromsequelize/sequelizeon Dec 23, 2021
  3. added
    RFC
    on Dec 23, 2021
  4. self-assigned this
    on Dec 23, 2021
  5. ephys commented on Jan 9, 2022

    @ephys
    MemberAuthor

    This RFC would also allow us to resolve #7621, as pluralization is used in associations method names

  6. ephys commented on Jan 28, 2022

    @ephys
    MemberAuthor

    Note from https://gist.github.com/ephys/078ee6a2eb31dc209f0de6cea95316e7:

    This would also simplify adding QueryBuilder support:

    // non qb version
    user.projects.findAll()
    
    // qb version
    user.projects.select()
      .orderBy('id', 'ASC')
      .findAll()
  7. mmahalwy commented on Mar 7, 2022

    @mmahalwy
    Contributor

    This would be amazing! Would be a game-changer to reducing developer mistakes.

  8. ephys commented on Mar 17, 2022

    @ephys
    MemberAuthor

    Caveat: It's not possible to infer the types of static class fields based on generics.

    Model.rawAttributes has been replaced with Model.getAttributes() for this reason.

    I think we could deprecate and replace Model.associations with Model.getAssociations(). And also add Model.getAssociation(associationName): Association - which would throw if the association doesn't exist.

  9. transferred this issue fromsequelize/meetingson Mar 30, 2022
  10. added
    typescript
    on Mar 31, 2022
  11. moved this to Blocked in Roadmapon Apr 21, 2022
  12. added
    topic: associations
    on Oct 6, 2022
  13. moved this from Blocked (v7) to Blocked (any) in Roadmapon Oct 20, 2022
  14. jthoward64 commented on Nov 25, 2023

    @jthoward64

    Is this planned to be held off until v8/beyond or would work on this be accepted?

  15. removed
    RFC
    on Mar 9, 2025
  16. added theissue type on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

topic: associationstypescript

Projects

  • Status
    Blocked (any)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions