In my project, there are 2 models called User
and Role
, and they have a many-to-many
relationship. To manage this connection, I introduced a third model named UserRole
.
The issue arises when the UserRole
is also retrieved in the query below:
async getUser(conditionals: object) {
return await this.userRepository.findOne({
where: { ...conditionals },
include: [{ model: Role, attributes: 'value' } ]
});
}
Here's an example of the response (please disregard the format, as the data was encoded and decoded from a jwt
):
{
"userId": "7a145aea-72c8-4b96-a25b-b404f9b1bf5a",
"username": "bl4drnnr",
"roles": [
{
"value": "USER",
"UserRole": {
"id": "e9d2afe8-ddd1-4311-ab14-6638b79d0d80",
"userId": "7a145aea-72c8-4b96-a25b-b404f9b1bf5a",
"roleId": "123e4567-e89b-12d3-a456-426614174001"
}
}
],
"type": "access",
"iat": 1659878703,
"exp": 1659879603
}
To achieve the desired output without the UserRole
model, it should look like this:
{
"userId": "7a145aea-72c8-4b96-a25b-b404f9b1bf5a",
"username": "bl4drnnr",
"roles": [ { "value": "USER" } ],
"type": "access",
"iat": 1659878703,
"exp": 1659879603
}
I attempted to use the exclude
property in Sequelize, but it seems to only work with fields, not models. So, how can I exclude the UserRole
model from the database request?