sequelizejs-cursor-pagination
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -9,3 +9,3 @@ const test = require('ava'); | ||
const Test = sequelize.define('test', { | ||
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, | ||
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, | ||
counter: Sequelize.INTEGER, | ||
@@ -42,4 +42,4 @@ }); | ||
pagination = await Test.paginate({ limit: 2, after: pagination.cursors.after }); | ||
pagination = await Test.paginate({ limit: 2, after: pagination.cursors.after }); | ||
t.is(pagination.results[0].id, 3); | ||
@@ -50,3 +50,3 @@ t.is(pagination.results[1].id, 4); | ||
pagination = await Test.paginate({ limit: 2, before: pagination.cursors.before }); | ||
pagination = await Test.paginate({ limit: 2, before: pagination.cursors.before }); | ||
@@ -61,14 +61,14 @@ t.is(pagination.results[0].id, 1); | ||
let pagination = await Test.paginate({ limit: 2, desc: true }); | ||
t.is(pagination.results[0].id, 5); | ||
t.is(pagination.results[1].id, 4); | ||
t.is(pagination.cursors.hasNext, true); | ||
t.is(pagination.cursors.hasPrevious, false); | ||
pagination = await Test.paginate({ limit: 2, after: pagination.cursors.after, desc: true }); | ||
t.is(pagination.results[0].id, 3); | ||
t.is(pagination.results[1].id, 2); | ||
t.is(pagination.cursors.hasNext, true); | ||
t.is(pagination.cursors.hasPrevious, true); | ||
t.is(pagination.results[0].id, 5); | ||
t.is(pagination.results[1].id, 4); | ||
t.is(pagination.cursors.hasNext, true); | ||
t.is(pagination.cursors.hasPrevious, false); | ||
pagination = await Test.paginate({ limit: 2, after: pagination.cursors.after, desc: true }); | ||
t.is(pagination.results[0].id, 3); | ||
t.is(pagination.results[1].id, 2); | ||
t.is(pagination.cursors.hasNext, true); | ||
t.is(pagination.cursors.hasPrevious, true); | ||
}); | ||
@@ -78,24 +78,24 @@ | ||
const data = await generateTestData(); | ||
let pagination = await Test.paginate({ limit: 2, paginationField: 'counter' }); | ||
t.is(pagination.results[0].counter, 1); | ||
t.is(pagination.results[1].counter, 2); | ||
t.is(pagination.cursors.hasNext, true); | ||
t.is(pagination.cursors.hasPrevious, false); | ||
pagination = await Test.paginate({ limit: 2, paginationField: 'counter', after: pagination.cursors.after }); | ||
t.is(pagination.results[0].counter, 3); | ||
t.is(pagination.results[1].counter, 4); | ||
t.is(pagination.results[1].id, 1) | ||
t.is(pagination.cursors.hasNext, true); | ||
t.is(pagination.cursors.hasPrevious, true); | ||
pagination = await Test.paginate({ limit: 2, paginationField: 'counter', after: pagination.cursors.after }); | ||
let pagination = await Test.paginate({ limit: 2, paginationField: 'counter' }); | ||
t.is(pagination.results[0].counter, 4); | ||
t.is(pagination.results[0].id, 2) | ||
t.is(pagination.cursors.hasNext, false); | ||
t.is(pagination.cursors.hasPrevious, true); | ||
t.is(pagination.results[0].counter, 1); | ||
t.is(pagination.results[1].counter, 2); | ||
t.is(pagination.cursors.hasNext, true); | ||
t.is(pagination.cursors.hasPrevious, false); | ||
pagination = await Test.paginate({ limit: 2, paginationField: 'counter', after: pagination.cursors.after }); | ||
t.is(pagination.results[0].counter, 3); | ||
t.is(pagination.results[1].counter, 4); | ||
t.is(pagination.results[1].id, 1) | ||
t.is(pagination.cursors.hasNext, true); | ||
t.is(pagination.cursors.hasPrevious, true); | ||
pagination = await Test.paginate({ limit: 2, paginationField: 'counter', after: pagination.cursors.after }); | ||
t.is(pagination.results[0].counter, 4); | ||
t.is(pagination.results[0].id, 2) | ||
t.is(pagination.cursors.hasNext, false); | ||
t.is(pagination.cursors.hasPrevious, true); | ||
}); | ||
@@ -106,4 +106,36 @@ | ||
let pagination = await Test.paginate({ limit: 2, attributes: ['id'], paginationField: 'counter' }); | ||
t.is(pagination.results[0].counter, undefined); | ||
let pagination = await Test.paginate({ limit: 2, attributes: ['id'], paginationField: 'counter' }); | ||
t.is(pagination.results[0].id, 3); | ||
t.is(pagination.results[0].counter, undefined); | ||
}); | ||
test('use findAndCountAll and get count if rowCount parameter is true', async t => { | ||
const data = await generateTestData(); | ||
let pagination = await Test.paginate({ | ||
limit: 2, | ||
attributes: ['id'], | ||
paginationField: 'counter', | ||
where: { | ||
id: { | ||
$lte: 3, | ||
} | ||
}, | ||
rowCount: true, | ||
}); | ||
t.is(pagination.count, 3); | ||
pagination = await Test.paginate({ | ||
limit: 2, | ||
attributes: ['id'], | ||
paginationField: 'counter', | ||
where: { | ||
id: { | ||
$lte: 3, | ||
} | ||
}, | ||
rowCount: false, | ||
}); | ||
t.is(pagination.count, null); | ||
}); |
@@ -7,3 +7,3 @@ { | ||
"description": "Sequelize model decorator which provides cursor based pagination queries", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"main": "./src/index.js", | ||
@@ -10,0 +10,0 @@ "license": "MIT", |
@@ -7,8 +7,14 @@ # Sequelize Cursor Pagination | ||
The original [repostory is here](https://github.com/Kaltsoon/sequelize-cursor-pagination), but it seems not maintained (not sure). | ||
## Install | ||
``` | ||
yarn add sequelize-cursor-pagination | ||
``` | ||
### npm | ||
`npm install sequelizejs-cursor-pagination` | ||
### yarn | ||
`yarn add sequelizejs-cursor-pagination` | ||
## How to use | ||
@@ -53,2 +59,3 @@ | ||
* **results**, the results of the query | ||
* **count**, number of rows that match the query condition, only if `rowCount` is set to `true`, or it will be null | ||
* **cursors**, object containing the cursors' related data | ||
@@ -66,6 +73,7 @@ * **cursors.before**, the first record in the result serialized | ||
* **limit**, limit the number of records returned | ||
* **rowCount**, If set to true, it will use [findAndCountAll](http://docs.sequelizejs.com/manual/tutorial/models-usage.html#-findandcountall-search-for-multiple-elements-in-the-database-returns-both-data-and-total-count) instead of [findAll](http://docs.sequelizejs.com/manual/tutorial/models-usage.html#-findall-search-for-multiple-elements-in-the-database). The default value is ``false``. | ||
* **desc**, whether to sort in descending order. The default value is ``false``. | ||
* **before**, the before cursor | ||
* **after**, the after curosr | ||
* **paginationField**, the field to be used for the pagination. The default value is the `primaryKeyField` option value. | ||
* **paginationField**, the field to be used for the pagination. The default value is the `primaryKeyField` option value. | ||
@@ -72,0 +80,0 @@ ## Run tests |
@@ -39,3 +39,3 @@ const base64 = require('base-64'); | ||
return model => { | ||
const paginate = ({ where = {}, attributes = [], include = [], limit, before, after, desc = false, paginationField = primaryKeyField }) => { | ||
const paginate = ({ where = {}, attributes = [], include = [], limit, before, after, desc = false, paginationField = primaryKeyField, rowCount = false }) => { | ||
const decodedBefore = !!before ? decodeCursor(before) : null; | ||
@@ -59,3 +59,4 @@ const decodedAfter = !!after ? decodeCursor(after) : null; | ||
return model.findAll({ | ||
// Dynamic load query method by rowCount condition. | ||
return model[rowCount ? 'findAndCountAll' : 'findAll']({ | ||
where: whereQuery, | ||
@@ -69,3 +70,4 @@ include, | ||
...(Array.isArray(attributes) && attributes.length) ? { attributes } : {}, | ||
}).then(results => { | ||
}).then(queryResults => { | ||
const results = (rowCount) ? queryResults.rows : queryResults; | ||
const hasMore = results.length > limit; | ||
@@ -99,2 +101,3 @@ | ||
results, | ||
...rowCount ? { count: queryResults.count } : { count: null }, | ||
cursors: { | ||
@@ -101,0 +104,0 @@ hasNext, |
116319
198
82