sequelize-paginate icon indicating copy to clipboard operation
sequelize-paginate copied to clipboard

Add option to whitelist attribute includes in count query

Open gaetanlegac opened this issue 5 years ago • 0 comments

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']
});

gaetanlegac avatar Jun 21 '19 16:06 gaetanlegac