I have a typeorm library using nestjs and a mysql database which has a table user, user_roles (many to many) and roles. I also have a user who already has the super_admin role.
const users = await this.userRepository.find({
relations: ['roles.role'],
});
The following comes in response:
[
{
"id": "1",
"email": "[email protected]",
"password": "Parol123",
"created_at": "2024-07-17T10:10:49.000Z",
"updated_at": "2024-07-17T10:10:49.000Z",
"roles": [
{
"id": "1",
"role": {
"id": "1",
"name": "super_admin",
"label": "Super Admin",
"description": "https://wordpress.org/documentation/article/roles-and-capabilities/"
}
}
]
}
]
I need to change the incoming roles value so that it only displays the role name, for example:
roles: ["super_admin"]
I tried using Query Builder, but it didn’t work for me either.
Dmitriy Novikov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Once you get the information need you can use the map
function and format the response yourself
const users = await this.userRepository.find({
relations: ['roles.role'],
});
return users.map(user=>{
return {
...user,
roles: user.roles.map(role=>role.role.name)
}
})