sequelize-paginate
sequelize-paginate copied to clipboard
Add option to whitelist attribute includes in count query
This PR adds a "keepAttrs" option, making possible to conserve one or several attributes include in the count query.
I can be useful when your select query contains a HAVING condition based on a subquery value. Here is my use case:
Contenu.paginate({
attributes: {
include: [
[sequelize.literal(`(
SELECT
COUNT(*)
FROM Tags t
WHERE
t.nom IN (${tagsStr})
AND
t.id IN (SELECT tc.tag FROM TagsContenus tc WHERE tc.contenu = Contenu.id)
)`), 'score'],
]
},
page: 1,
paginate: 20,
keepAttrs: ['score'],
having: sequelize.literal('score > 0'),
order: ['score', 'DESC']
});