Skip to content

Commit

Permalink
Merge pull request #1914 from throrin19/patch-pagination-plugin
Browse files Browse the repository at this point in the history
pagination-plugin and fetch options on count fix
  • Loading branch information
ricardograca committed Dec 1, 2018
2 parents 629a914 + 0d95b64 commit bda57a3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/plugins/pagination.js
Expand Up @@ -107,7 +107,7 @@ module.exports = function paginationPlugin(bookshelf) {
const limit = options.limit;
const offset = options.offset;
const fetchOptions = _.omit(options, ['page', 'pageSize', 'limit', 'offset']);
const transacting = fetchOptions.transacting;
const countOptions = _.omit(fetchOptions, ['require', 'columns', 'withRelated', 'lock']);
const fetchMethodName = isModel ? 'fetchAll' : 'fetch';
const targetModel = isModel ? this.constructor : this.target || this.model;
const tableName = targetModel.prototype.tableName;
Expand Down Expand Up @@ -180,7 +180,7 @@ module.exports = function paginationPlugin(bookshelf) {

qb.countDistinct.apply(qb, groupColumns.length > 0 ? groupColumns : targetIdColumn);
})
[fetchMethodName]({transacting})
[fetchMethodName](countOptions)
.then((result) => {
const metadata = usingPageSize ? {page: _page, pageSize: _limit} : {offset: _offset, limit: _limit};

Expand Down
77 changes: 77 additions & 0 deletions test/integration/plugins/pagination.js
@@ -1,4 +1,5 @@
var expect = require('chai').expect;
var _ = require('lodash');

module.exports = function(bookshelf) {
describe('Pagination Plugin', function() {
Expand Down Expand Up @@ -180,5 +181,81 @@ module.exports = function(bookshelf) {
});
});
});

describe('with fetch Options', function() {
var Site = Models.Site;

afterEach(function() {
delete Site.prototype.initialize;
});

it('ignore standard options for count query', function() {
const allOptions = [];

Site.prototype.initialize = function() {
this.on('fetching:collection', function(collection, columns, options) {
allOptions.push(_.omit(options, 'query'));
});
};

var site = new Site();

return site
.fetchPage({
require: true,
withRelated: ['blogs'],
columns: 'name'
})
.then(function() {
expect(allOptions.length).equals(2);
expect(allOptions[1]).not.deep.equals(allOptions[0]);

const countOptions = allOptions.find(function(option) {
return !_.has(option, ['require', 'withRelated', 'columns']);
});
const fetchOptions = allOptions.find(function(option) {
return _.has(option, ['require', 'withRelated', 'columns']);
});

expect(countOptions).not.to.be.null;
expect(fetchOptions).not.to.be.null;
});
});

it('keep custom options for count query', function() {
const allOptions = [];

Site.prototype.initialize = function() {
this.on('fetching:collection', function(collection, columns, options) {
allOptions.push(_.omit(options, 'query'));
});
};

var site = new Site();

return site
.fetchPage({
withRelated: ['blogs'],
customOption: true
})
.then(function() {
expect(allOptions.length).equals(2);
expect(allOptions[1]).not.deep.equals(allOptions[0]);

const countOptions = allOptions.find(function(option) {
return !_.has(option, ['withRelated']);
});
const fetchOptions = allOptions.find(function(option) {
return _.has(option, ['withRelated']);
});

expect(countOptions).not.to.be.null;
expect(fetchOptions).not.to.be.null;
expect(countOptions.customOption).to.equal(true);
expect(fetchOptions.customOption).to.equal(true);
expect(countOptions.customOption).to.equal(fetchOptions.customOption);
});
});
});
});
};

0 comments on commit bda57a3

Please sign in to comment.