I am using Nodejs, Express, Postgresql, and Sequelize
I have 4 Models
Account (has many books)
Book (has many parts)
- Part (has many chapters)
- Chapter
I'm returning a book and including both 'Part' and 'Chapter' with some specific ordering - This works fine.
The model 'Chapter' has a column 'topic' and I only want to include rows from the 'Chapter' model that have distinct 'topics'.
How do I apply distinct to this 'Chapter' model with this nested include structure?
Code snippet:
return Book
.findOne({
where: {accountid: req.body.accountid,
id: req.body.bookid},
include: [{
model: Part,
as: 'parts'
include: [
{
model: Chapter,
required: false,
as: 'chapters',
where: {
createdAt: {
$gte: moment().subtract(24,'hours').format()
}
}
}],
}],
order: [
[
{ model: Part, as: 'parts' },
'createdAt',
'DESC'
],
[
{ model: Part, as: 'parts' },
{ model: Chapter, as: 'chapters' },
'createdAt',
'DESC'
]
]
})
.then(book => res.status(200).send(book))
.catch(error => res.status(400).send(error.toString()));
}