/egg-mysql-paginator

Paginator for egg-mysql

Primary LanguageJavaScriptMIT LicenseMIT

egg-mysql-paginator

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Paginator for egg-mysql

Install

$ npm i egg-mysql-paginator --save

Usage

// {app_root}/config/plugin.js
exports.mysqlPaginator = {
  enable: true,
  package: 'egg-mysql-paginator',
};

Example

// query
const results = await app.mysqlPaginator.query('posts',{
  where: { status: 'draft' },
  orders: [['created_at','desc'], ['id','desc']],
  page: 1,
  size: 10
});
// result
  {
    pagination: {
      total: 21,
      size: 10,
      currentPage: 1,
      lastPage: 3,
      from: 0,
      to: 10
    },
    data: [...]
  }

Fuzzy query

const results = await app.mysqlPaginator.query('posts',{
  where: 'WHERE name LIKE %name%',
  orders: [['created_at','desc'], ['id','desc']],
  page: 1,
  size: 10
});

Customized order by

const results = await app.mysqlPaginator.query('posts',{
  where: 'WHERE name LIKE %name%',
  orders: 'rank+',
  page: 1,
  size: 10
});

License

MIT