0%

Sequelize 分页并计数

Sequelize 分页并计数

1
2
3
4
5
6
7
8
Post.findAndCountAll({
where: {...},
order: [...],
limit: 5,
offset: 0,
}).then(function (result) {
res.render(...);
});

完整写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const paginate = (query, { page, pageSize }) => {
const offset = page * pageSize;
const limit = pageSize;

return {
...query,
offset,
limit,
};
};


model.findAll(
paginate(
{
where: {}, // conditions
},
{ page, pageSize },
),
);

参考文章