I am trying to conditionally select specific attributes to retrieve from the included Model in a findOne() Sequelize function, based on values from the 'parent' Model.
Does anyone know if this is possible in a single query?
Here's my attempt so far -
let article = await Article.findOne({
where: { slug, published: true },
include: [{
model: User,
as: 'author',
attributes: {
$col: Article.usepseudonym
? ['pseudonym']
: ['id', 'name', 'avatarUrl']
}
}]
})
The Sequelize function is not throwing an error and is returning all of the User attributes when I only require specific fields.