Skip to content

Commit e330c20

Browse files
committed
improve models associate per file
1 parent deccceb commit e330c20

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

Diff for: src/models/index.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ export interface FilterQueryAttributes {
1919
sorted: string
2020
}
2121

22-
export default {
22+
const models = {
2323
Role,
2424
User,
2525
UserRole,
2626
}
2727

28-
/*
29-
Models Association
30-
*/
28+
export default models
3129

32-
User.belongsToMany(Role, { through: UserRole })
30+
export type MyModels = typeof models
31+
32+
Object.entries(models).map(([, model]) => {
33+
if (model?.associate) {
34+
model.associate(models)
35+
}
36+
return model
37+
})

Diff for: src/models/user.ts

+4
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,8 @@ User.prototype.comparePassword = function (candidatePassword: string) {
9393
})
9494
}
9595

96+
User.associate = (models) => {
97+
User.belongsToMany(models.Role, { through: models.UserRole })
98+
}
99+
96100
export default User

0 commit comments

Comments
 (0)