Repository navigation
Description
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBlocked (any)
Activity
ephys commentedon Dec 7, 2021
Update: After a quick test this would enable having smart typings for the
Model.associationsstatic property :)https://www.typescriptlang.org/play?#code/MYGwhgzhAEAKBOB7AVgU2AFwLKICapGgG8BfAKDNEhgFUJV4d9CizpoIMwMBLYaaomA9uPRADsIALmgAVAKIAPDPDCYAglCEjeEiAB46DJgQB8AbgrsAZj3icAcmAC2qAIQzO8HuIDml9nBHF3dPFR9-Nmgo+FQwXAkQAE9oAAckNEwID2gACUgsMHEk9QxwgCMAVwxUAOhY+MSU1LBY8QwcgCECCV8IWURSiurasnJKIJhNCG1RCSHvKpriKIB9VYwk1NRoAF5oAHJp2d1xBZ4l1APLcaooPIKikrLFkehUZVRxXCmtYTmzi8Lm9WOwAG5gECVULQRDlTIYADaAF1oAAfaCVb6oWziVC4Oq43DqEAgHIACgAlHtTMQSDcJtQHhBCsVjv9TnSKHcYN0QL1+oMgZd3p9vr8ZhyxIDhstQdAIVCYXCEejMdjcfjCT5cBTqbtaaQGYz7nyBQN2TppVyyJttnIlCo1BhLQCDCYQLT9vLEQBpVApHzQADWAcQ1mgHoEMA9foDqI+NXF0Fdp3OIoA-NB-SkZHiwQxkTJYzmE2KfszWc9ZTss-kWU9U9Kouxi3gCHGkmWkxWzX5Bem3lm+30LX8rRIW9A86gC-AbpYyEZGO2QAA6QRSvRr9IodAYCDmaAAemPlcb44BS-oK+YG8vpwgO9aXwwR9P0BHA4fzeXHvvkoTpIa62PYGBOK475nvmDDXsYq4ASc0pPkE4EhFB0AwfAQA
[-]RFC: association methods in v7[/-][+][RFC] Rework association methods for better TS support[/+]ephys commentedon Jan 9, 2022
This RFC would also allow us to resolve #7621, as pluralization is used in associations method names
ephys commentedon Jan 28, 2022
Note from https://gist.github.com/ephys/078ee6a2eb31dc209f0de6cea95316e7:
This would also simplify adding QueryBuilder support:
mmahalwy commentedon Mar 7, 2022
This would be amazing! Would be a game-changer to reducing developer mistakes.
ephys commentedon Mar 17, 2022
Caveat: It's not possible to infer the types of static class fields based on generics.
Model.rawAttributeshas been replaced withModel.getAttributes()for this reason.I think we could deprecate and replace
Model.associationswithModel.getAssociations(). And also addModel.getAssociation(associationName): Association- which would throw if the association doesn't exist.jthoward64 commentedon Nov 25, 2023
Is this planned to be held off until v8/beyond or would work on this be accepted?