Skip to content

Sequelize 分页与计数

Updated: at 09:12 AMSuggest Changes

Sequelize 分页并计数

Post.findAndCountAll({
    where: {...},
    order: [...],
    limit: 5,
    offset: 0,
}).then(function (result) {
    res.render(...);
});

完整写法

const paginate = (query, { page, pageSize }) => {
  const offset = page * pageSize;
  const limit = pageSize;

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


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

参考文章


Previous Post
Sequelize 关联查询详解
Next Post
JS 可拖拽调整窗口大小的库