Skip to content

Commit

Permalink
Ability to set a skip when querying jobs.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjolif committed Dec 19, 2019
1 parent 10c9773 commit 6a71880
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -487,12 +487,12 @@ console.log('Job successfully saved');
## Managing Jobs


### jobs(mongodb-native query, mongodb-native sort, mongodb-native limit)
### jobs(mongodb-native query, mongodb-native sort, mongodb-native limit, mongodb-native skip)

Lets you query (then sort and limit the result) all of the jobs in the agenda job's database. These are full [mongodb-native](https://github.com/mongodb/node-mongodb-native) `find`, `sort` and `limit` commands. See mongodb-native's documentation for details.
Lets you query (then sort, limit and kip the result) all of the jobs in the agenda job's database. These are full [mongodb-native](https://github.com/mongodb/node-mongodb-native) `find`, `sort`, `limit` and `skip` commands. See mongodb-native's documentation for details.

```js
const jobs = await agenda.jobs({name: 'printAnalyticsReport'}, {data:-1}, 3);
const jobs = await agenda.jobs({name: 'printAnalyticsReport'}, {data:-1}, 3, 1);
// Work with jobs (see below)
```

Expand Down
4 changes: 3 additions & 1 deletion lib/agenda/jobs.js
Expand Up @@ -8,13 +8,15 @@ const {createJob} = require('../utils');
* @param {Object} query object for MongoDB
* @param {Object} sort object for MongoDB
* @param {Number} limit number of documents to return from MongoDB
* @param {Number} number of documents to skip in MongoDB
* @returns {Promise} resolves when fails or passes
*/
module.exports = async function(query = {}, sort = {}, limit = 0) {
module.exports = async function(query = {}, sort = {}, limit = 0, skip = 0) {
const result = await this._collection
.find(query)
.sort(sort)
.limit(limit)
.skip(skip)
.toArray();

return result.map(job => createJob(this, job));
Expand Down
5 changes: 5 additions & 0 deletions test/agenda.js
Expand Up @@ -554,6 +554,11 @@ describe('Agenda', function() { // eslint-disable-line prefer-arrow-callback
expect(results).to.have.length(2);
});

it('should skip jobs', async() => {
const results = await jobs.jobs({name: 'jobA'}, {}, 2, 2);
expect(results).to.have.length(1);
});

it('should sort jobs', async() => {
const results = await jobs.jobs({name: 'jobA'}, {data: -1});

Expand Down

0 comments on commit 6a71880

Please sign in to comment.