为 egg-mysql 提供分页功能
$ npm i egg-mysql-paginator --save
egg-mysql-paginator 版本 | egg 2.x |
---|---|
2.x | 😁 |
1.x | ❌ |
- mysql
// config/plugin.js
exports.mysqlPaginator = {
enable: true,
package: 'egg-mysql-paginator',
};
// 查询
const results = await app.mysqlPaginator.query('posts',{
where: { status: 'draft' },
orders: [['created_at','desc'], ['id','desc']],
page: 1,
size: 10
});
// 结果
{
pagination: {
total: 21,
size: 10,
currentPage: 1,
lastPage: 3,
from: 0,
to: 10
},
data: [...]
}
const results = await app.mysqlPaginator.query('posts',{
where: 'WHERE name LIKE %name%',
orders: [['created_at','desc'], ['id','desc']],
page: 1,
size: 10
});
const results = await app.mysqlPaginator.query('posts',{
where: 'WHERE name LIKE %name%',
orders: 'rank+',
page: 1,
size: 10
});