I've been using feathersjs/nodejs over postgres db via sequelize. In my db i have Users table and Events table. They are twice in relation:
events.belongsTo(models.users, {
foreignKey: {
name: 'creatorId',
allowNull: false
},
onDelete: 'CASCADE',
as: 'creator'
});
events.belongsToMany(models.users, {
through: 'event_participants',
as: 'participants',
foreignKey: 'eventId',
otherKey: 'userId'
});
models.users.belongsToMany(events, {
through: 'event_participants',
as: 'events'
});
Everything works just fine, table is created and with include im getting users inside of event as participants. Problem is querying by association. I'm trying to fetch events for current user, so I need events where creator is current user AND events where one of participants is current user. Problem is the second part 'querying where current user is one of participants'. I was expecting something like this
'api/events?$or[0][creatorId]=currUserId&$or[1][participants][$contains]=currUserId'
but its not working cause there is no such column as 'participants' its being included so i cant query it. So for now I'm just fetching all events and filtering them for current user in after hooks, but it just seems wrong. What is the right way to do this?
And yea I know I can get events where user is one of participants by including them into user and fetching him but problem with that is i cant sort that data all together, its being sorted separately and another problem is doin pagination on frontend.