mongoose-paginate-v2
Advanced tools
Comparing version 1.3.0 to 1.3.1
"use strict"; | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
@@ -58,3 +52,3 @@ | ||
function paginate(query, options, callback) { | ||
options = _objectSpread({}, defaultOptions, paginate.options, options); | ||
options = _objectSpread({}, defaultOptions, {}, paginate.options, {}, options); | ||
query = query || {}; | ||
@@ -70,3 +64,3 @@ var _options = options, | ||
var customLabels = _objectSpread({}, defaultOptions.customLabels, options.customLabels); | ||
var customLabels = _objectSpread({}, defaultOptions.customLabels, {}, options.customLabels); | ||
@@ -104,6 +98,7 @@ var limit = parseInt(options.limit, 10) || 0; | ||
skip = offset; | ||
} | ||
} // const countPromise = this.countDocuments(query).exec(); | ||
var countPromise = this.countDocuments(query).exec(); | ||
var countPromise = this.find(query).exec(); | ||
if (limit) { | ||
@@ -139,6 +134,5 @@ var mQuery = this.find(query, projection, findOptions); | ||
return Promise.all([countPromise, docsPromise]).then(function (values) { | ||
var _values = _slicedToArray(values, 2), | ||
count = _values[0], | ||
docs = _values[1]; | ||
// const [count, docs] = values; | ||
var count = values[0].length; | ||
var docs = values[1]; | ||
var meta = { | ||
@@ -145,0 +139,0 @@ [labelTotal]: count, |
{ | ||
"name": "mongoose-paginate-v2", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "A cursor based custom pagination library for Mongoose with customizable labels.", | ||
@@ -68,2 +68,2 @@ "main": "dist/index.js", | ||
} | ||
} | ||
} |
@@ -104,3 +104,4 @@ /** | ||
const countPromise = this.countDocuments(query).exec(); | ||
// const countPromise = this.countDocuments(query).exec(); | ||
const countPromise = this.find(query).exec(); | ||
@@ -139,3 +140,8 @@ if (limit) { | ||
.then((values) => { | ||
const [count, docs] = values; | ||
// const [count, docs] = values; | ||
const count = values[0].length; | ||
const docs = values[1]; | ||
const meta = { | ||
@@ -142,0 +148,0 @@ [labelTotal]: count, |
@@ -21,5 +21,10 @@ 'use strict'; | ||
ref: 'Author' | ||
} | ||
}, | ||
loc: Object | ||
}); | ||
BookSchema.index({ | ||
loc: "2dsphere" | ||
}); | ||
BookSchema.plugin(mongoosePaginate); | ||
@@ -54,6 +59,11 @@ | ||
date: new Date(date.getTime() + i), | ||
author: author._id | ||
author: author._id, | ||
loc: { | ||
type: "Point", | ||
coordinates: [-10.97, 20.77] | ||
}, | ||
}); | ||
books.push(book); | ||
} | ||
return Book.create(books); | ||
@@ -69,2 +79,3 @@ }); | ||
it('promise return test', function () { | ||
let promise = Book.paginate(); | ||
@@ -145,40 +156,41 @@ expect(promise.then).to.be.an.instanceof(Function); | ||
it('with empty custom labels', function () { | ||
var query = { | ||
title: { | ||
$in: [/Book/i] | ||
} | ||
}; | ||
it('with empty custom labels', function () { | ||
var query = { | ||
title: { | ||
$in: [/Book/i] | ||
} | ||
}; | ||
const myCustomLabels = { | ||
nextPage: false, | ||
prevPage: '', | ||
}; | ||
const myCustomLabels = { | ||
nextPage: false, | ||
prevPage: '', | ||
}; | ||
var options = { | ||
sort: { | ||
_id: 1 | ||
}, | ||
limit: 10, | ||
page: 5, | ||
select: { | ||
title: 1, | ||
price: 1 | ||
}, | ||
customLabels: myCustomLabels | ||
}; | ||
return Book.paginate(query, options).then((result) => { | ||
expect(result.docs).to.have.length(10); | ||
expect(result.docs[0].title).to.equal('Book #41'); | ||
expect(result.totalDocs).to.equal(100); | ||
expect(result.limit).to.equal(10); | ||
expect(result.page).to.equal(5); | ||
expect(result.pagingCounter).to.equal(41); | ||
expect(result.hasPrevPage).to.equal(true); | ||
expect(result.hasNextPage).to.equal(true); | ||
expect(result.totalPages).to.equal(10); | ||
expect(result.prevPage).to.equal(undefined); | ||
expect(result.nextPage).to.equal(undefined); | ||
var options = { | ||
sort: { | ||
_id: 1 | ||
}, | ||
limit: 10, | ||
page: 5, | ||
select: { | ||
title: 1, | ||
price: 1 | ||
}, | ||
customLabels: myCustomLabels | ||
}; | ||
return Book.paginate(query, options).then((result) => { | ||
expect(result.docs).to.have.length(10); | ||
expect(result.docs[0].title).to.equal('Book #41'); | ||
expect(result.totalDocs).to.equal(100); | ||
expect(result.limit).to.equal(10); | ||
expect(result.page).to.equal(5); | ||
expect(result.pagingCounter).to.equal(41); | ||
expect(result.hasPrevPage).to.equal(true); | ||
expect(result.hasNextPage).to.equal(true); | ||
expect(result.totalPages).to.equal(10); | ||
expect(result.prevPage).to.equal(undefined); | ||
expect(result.nextPage).to.equal(undefined); | ||
}); | ||
}); | ||
}); | ||
@@ -266,2 +278,38 @@ it('with custom labels', function () { | ||
it('2dsphere', function () { | ||
var query = { | ||
loc: { | ||
$near: { | ||
$geometry: { | ||
type: "Point", | ||
coordinates: [-10, 20] | ||
}, | ||
$maxDistance: 999999 | ||
} | ||
} | ||
}; | ||
const myCustomLabels = { | ||
meta: 'meta', | ||
docs: 'itemsList', | ||
totalDocs: 'total' | ||
}; | ||
var options = { | ||
sort: { | ||
_id: 1 | ||
}, | ||
limit: 10, | ||
page: 5, | ||
select: { | ||
title: 1, | ||
price: 1 | ||
}, | ||
customLabels: myCustomLabels | ||
}; | ||
return Book.paginate(query, options).then((result) => { | ||
expect(result.meta.total).to.equal(100); | ||
}); | ||
}); | ||
after(function (done) { | ||
@@ -268,0 +316,0 @@ mongoose.connection.db.dropDatabase(done); |
36107
620