'Pull in specific records/rows based on field in Sequelize
So a basic Sequelize query I'm working with (minus fluff for simplicity sake) looks like this:
{ model: Contact,
required: true,
attributes: ['FullName', 'Country'],
include: [
{
model: Rank,
required: false,
attributes: ['Year', 'Category', 'Rank'],
where: {
Year: 2021
}
}
]
The issue is, I only need to pull in the Top rank (Gold, Silver, Bronze, or no Rank/null) row, even though our contacts have multiple rows in the Rank table if they have multiple Categories
So using the logic above, I get data like below:
"FullName": "Bob",
"Country": 'USA',
"Rank": [
{
"Year": 2021,
"Category": "ExampleA",
"Rank": "Silver"
},
{
"Year": 2021,
"Category": "ExampleB",
"Rank": "Gold"
}
]
Is there a way to have Sequelize only pull in the "Gold" row in the above example, since that is the highest rank?
So desired result for that specific user would be:
"FullName": "Bob",
"Country": 'USA',
"Rank": [
{
{
"Year": 2021,
"Category": "ExampleB",
"Rank": "Gold"
}
]
If a user had Silver and Bronze, but no gold, desired result would be to have it only pull in Silver row.
Appreciate any insight at all, will give additional info if needed, have been pulling my hair out over this one.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
