Skip to content

Commit

Permalink
feat(pagination): Revert "fix: Merge pull request #26 from screwdrive…
Browse files Browse the repository at this point in the history
…r-cd/revert-25-pagination" (#27)

BREAKING CHANGE: This reverts commit 1344a9e.
  • Loading branch information
philipjscott authored Mar 5, 2018
1 parent 1344a9e commit ea1b525
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,14 @@ class Squeakquel extends Datastore {
/**
* Scan records in the datastore
* @method scan
* @param {Object} config Configuration object
* @param {String} config.table Table name
* @param {Object} [config.params] index => values to query on
* @param {String} [config.sort] Sorting option based on GSI range key. Ascending or descending.
* @return {Promise} Resolves to an array of records
* @param {Object} config Configuration object
* @param {String} config.table Table name
* @param {Object} [config.paginate] Pagination parameters
* @param {Number} [config.paginate.count] Number of items per page
* @param {Number} [config.paginate.page] Specific page of the set to return
* @param {Object} [config.params] index => values to query on
* @param {String} [config.sort] Sorting option based on GSI range key. Ascending or descending.
* @return {Promise} Resolves to an array of records
*/
_scan(config) {
const table = this.tables[config.table];
Expand All @@ -320,6 +323,11 @@ class Squeakquel extends Datastore {
return Promise.reject(new Error(`Invalid table name "${config.table}"`));
}

if (config.paginate) {
findParams.limit = config.paginate.count;
findParams.offset = findParams.limit * (config.paginate.page - 1);
}

if (config.params && Object.keys(config.params).length > 0) {
Object.keys(config.params).forEach((paramName) => {
const paramValue = config.params[paramName];
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"chai": "^4.0.2",
"eslint": "^3.19.0",
"eslint-config-screwdriver": "^2.1.3",
"eslint-plugin-import": "^2.9.0",
"jenkins-mocha": "^4.1.2",
"joi": "^10.5.2",
"mockery": "^2.0.0",
Expand Down
33 changes: 33 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,39 @@ describe('index test', function () {
});
});

it('scans a page of data', () => {
const dummyData = [];

for (let i = 1; i <= 30; i += 1) {
dummyData.push({
id: `data${i}`,
key: `value${i}`
});
}

const testData = dummyData.slice(11, 21);
const testInternal = testData.map(data => ({
toJSON: sinon.stub().returns(data)
}));

testParams.paginate = {
count: 10,
page: 2
};

sequelizeTableMock.findAll.resolves(testInternal);

return datastore.scan(testParams).then((data) => {
assert.deepEqual(data, testData);
assert.calledWith(sequelizeTableMock.findAll, {
where: {},
order: [['id', 'DESC']],
limit: 10,
offset: 10
});
});
});

it('scans for some data with params', () => {
const testData = [
{
Expand Down

0 comments on commit ea1b525

Please sign in to comment.