Skip to content

Commit

Permalink
Tracking bookshelf-page (fix bookshelf-page/bookshelf#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
anyong committed Apr 12, 2016
1 parent 8aa1729 commit 99fea87
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/plugins/pagination.js
Expand Up @@ -167,8 +167,11 @@ module.exports = function paginationPlugin (bookshelf) {
// Remove grouping and ordering. Ordering is unnecessary
// for a count, and grouping returns the entire result set
// What we want instead is to use `DISTINCT`
_remove(qb._statements, statement => {
return (notNeededQueries.indexOf(statement.type) > -1) ||
statement.grouping === 'columns';
});
qb.countDistinct.apply(qb, [`${tableName}.${idAttribute}`]);
_remove(qb._statements, statement => notNeededQueries.indexOf(statement.type) > -1);
})

.fetchAll()
Expand Down
9 changes: 9 additions & 0 deletions test/integration/plugins/pagination.js
Expand Up @@ -66,6 +66,15 @@ module.exports = function (bookshelf) {
expect(parseInt(m[1].get('id'))).to.equal(4);
})
})

it('fetches a page when other columns are specified on the original query', function () {
return Models.Customer.forge().query(function (qb) {
qb.column.apply(qb, ['name'])
}).fetchPage().then(function (results) {
var md = results.pagination;
expect(md.rowCount).to.equal(4);
})
})
})

describe('Model static fetchPage', function () {
Expand Down

0 comments on commit 99fea87

Please sign in to comment.