I am using Feathersjs backend framework with Sequelize as ORM. Using Sequelize join tables of n:m relations can be generated 'automatically' (from hereon called 'Approach A') with the belongsToMany
association, however I followed an also advocated alternative approach ('Approach B'), i.e. explicitly defining the join table with 2 belongsTo
associations and using hasMany
associations from the two tables you want to join to the joint table.
This works fine. However my output looks like this:
{
"id": 1,
"user_id": 2,
"group_id": 1,
"Venues": [
{
"id": 1,
"capacity": "400",
"venue_name": "Club Lotte",
"venue_description": "Club in Rotterdam",
"EmployeeVenues": {
"employee_id": 1,
"venue_id": 1
}
},
{
"id": 2,
"capacity": "400",
"venue_name": "Club Dino",
"venue_description": "Club in Rotterdam",
"EmployeeVenues": {
"employee_id": 1,
"venue_id": 2
}
}
]
}
And I would like it to look like this:
{
"id": 1,
"user_id": 2,
"group_id": 1,
"Venues": [
{
"id": 1,
"capacity": "400",
"venue_name": "Club Lotte",
"venue_description": "Club in Rotterdam"
},
{
"id": 2,
"capacity": "400",
"venue_name": "Club Dino",
"venue_description": "Club in Rotterdam"
}
]
}
When using Approach A this exact same issue can be solved using the through: { attributes: [] }
in the query. Can this also be achieved using Approach B?
Approach B uses an intermediate model and uses belongsTo
. There are no features from sequelize (kind of implicated in the question) that support to leave out the output of the intermediate model (the join table). I browsed through the sequelize code and did not find 'such thing'. The instruction videos of Eric Katz (I found 3 of them on m:n with sequelize) also do not show such a method. He simply processes the output with forEach statements to get the output he wants. See at the bottom of this picture.
So in short: no such feature in sequelize for Approach B.