Typescript example of M:N relationship (belongsToMany) with through table attributes
See original GitHub issueIssue Description
Examples of using Typescript with many-to-many relationships (i.e. belongsToMany
<-> belongsToMany
)
What was unclear/insufficient/not covered in the documentation
Only simple relationship examples exist: i.e hasMany
Additional context
I’ve looked through the type definitions and tests and I’m coming up empty-handed.
Simple example:
class Account extends Model { }
class User extends Model {
public addAccount!: ????
}
User.belongsToMany(Account, { through: AccountUser });
Account.belongsToMany(User, { through: AccountUser });
userInst.addAccount(accountInst, { through: { role: 'owner' } })
How do I declare the addAccount
method of class User
? My through table (AccountUser
) has a single attribute (role
). I’ve looked through the code (i.e. associations/belongs-to-many.d.ts
) but I’m stumped. It appears that BelongsToManyAddAssociationMixin
takes 3 arguments but when I express something like:
public addAccount!: BelongsToManyAddAssociationMixin<Account, string, { role: string }>;
it complains with Generic type 'BelongsToManyAddAssociationMixin' requires 2 type argument(s)
Some help and guidance would be greatly appreciated. I’d be happy to contribute to the Typescript documentation page to cover this use case.
Is this issue dialect-specific?
- No. This issue is relevant to Sequelize as a whole.
- Yes. This issue only applies to the following dialect(s): Typescript
- I don’t know.
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don’t know how to start, I would need guidance.
- No, I don’t have the time, although I believe I could do it if I had the time…
- No, I don’t have the time and I wouldn’t even know how to start.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:7 (3 by maintainers)
Hey @Crodaycat & others
I am also working on my first Sequelize / Typescript selfstudy project. I found this example to be pretty useful (though it is from 2 years ago)
https://github.com/ahmerb/typescript-sequelize-example
Nice!!
I agree 😬 Would you be willing to post how did you finally make it work?