Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var Model = mongoose.model('Model', schema); // Model.paginate()
- `[leanWithId=true]` {Boolean} - If `lean` and `leanWithId` are `true`, adds `id` field with string representation of `_id` to every document
- `[offset=0]` {Number} - Use `offset` or `page` to set skip position
- `[page=1]` {Number}
- `[limit=10]` {Number}
- `[limit=Number.MAX_SAFE_INTEGER]` {Number}
* `[callback(err, result)]` - If specified the callback is called once pagination results are retrieved or when an error has occurred

**Return value**
Expand Down Expand Up @@ -107,19 +107,6 @@ Book.paginate(query, options).then(function(result) {
});
```

#### Zero limit

You can use `limit=0` to get only metadata:

```js
Model.paginate({}, { offset: 100, limit: 0 }).then(function(result) {
// result.docs - empty array
// result.total
// result.limit - 0
// result.offset - 100
});
```

#### Set custom default options for all queries

config.js:
Expand Down
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function paginate(query, options, callback) {
let populate = options.populate;
let lean = options.lean || false;
let leanWithId = options.leanWithId ? options.leanWithId : true;
let limit = options.limit ? options.limit : 10;
let limit = options.limit || Number.MAX_SAFE_INTEGER;
let page, offset, skip, promises;
if (options.offset) {
offset = options.offset;
Expand Down Expand Up @@ -65,23 +65,23 @@ function paginate(query, options, callback) {
promises = Object.keys(promises).map((x) => promises[x]);
return Promise.all(promises).then((data) => {
let result = {
docs: data.docs,
total: data.count,
docs: data[0],
total: data[1],
limit: limit
};
if (offset !== undefined) {
result.offset = offset;
}
if (page !== undefined) {
result.page = page;
result.pages = Math.ceil(data.count / limit) || 1;
result.pages = Math.ceil(result.total / limit) || 1;
}
if (typeof callback === 'function') {
return callback(null, result);
}
let promise = new Promise();
promise.resolve(result);
return promise;
return new Promise((resolve, reject) => {
resolve(result);
});
});
}

Expand Down
Loading