Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set a skip when querying jobs. #898

Merged
merged 1 commit into from Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 skip 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