M.A.K. Ripon
M.A.K. Ripon

Reputation: 2148

Sequelize set limit to max row count

From front end I'm sending 2 type of param to Sequelize

1st Param : {}

2nd Param: {page: '1', start: '0', limit: '30' }

My Sequelize query:

myTable.findAndCountAll({
    .....
    offset: (req.query.start) ? parseInt(req.query.start) : 0,
    limit: (req.query.limit) ? parseInt(req.query.limit) : 30
.........

For 1st param by default it's setting limit to 30

And for the 2nd param it's setting limit as the param sending.

My problem here is, I need to set the default limit for the 1st param to return all row.

Tried to set null for default like limit: (req.query.limit) ? parseInt(req.query.limit) : null which setting default limit to 10000000000000, what I don't want.

If there any specific way to do this might help.

Upvotes: 0

Views: 5760

Answers (1)

Vivek Doshi
Vivek Doshi

Reputation: 58593

As Sequelize doesn't provide inbuilt feature for this , One of the solution to the issue is :

let limits = {};
if(req.query.limit){
    limits = {
        offset: (req.query.start) ? parseInt(req.query.start) : 0,
        limit: parseInt(req.query.limit)
    }
}

myTable.findAndCountAll({
    .....
    ...limits , // <---- here three dots are ES6 syntax , so don't ignore that)
    .....
});

For more detail : READ

Upvotes: 5

Related Questions