Basically I have two models:
- User
- Tournament
they have a Many-to-Many relationship between them through a third model called UserTournament which also have an extra field that track the user points.
and basically for me to grab the data of the users in the tournaments I just write this.
const ladder = await WeeklyLadder.findByPk(req.params.id);
const users = await ladder.getUsers({
attributes: ['displayName'],
});
but now I'm trying to order it by the user points that are in the 3rd model but I can not seem to do it at all!
I've read in the docs of sequelize that you can add extra fields to the 3rd model and add to them with 'through'
example:
const amidala = User.create({ username: 'p4dm3', points: 1000 });
const queen = Profile.create({ name: 'Queen' });
await amidala.addProfile(queen, { through: { selfGranted: false } });
const result = await User.findOne({
where: { username: 'p4dm3' },
include: Profile
});
console.log(result);
but they never explained how to order by this data and its confusing me a lot.