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

Support MongoDB driver 6.0 #46

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 16 additions & 7 deletions src/JobDbRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Collection,
Db,
Filter,
FindOneAndUpdateOptions,
FindOneAndUpdateOptions as FindOneAndUpdateOptions_orig,
MongoClient,
MongoClientOptions,
ObjectId,
Expand All @@ -18,6 +18,15 @@ import { hasMongoProtocol } from './utils/hasMongoProtocol';

const log = debug('agenda:db');

interface FindOneAndUpdateOptions extends FindOneAndUpdateOptions_orig {
includeResultMetadata: boolean; // support mongodb driver 6.0
}

const findOneAndUpdateCommonOptions = {
includeResultMetadata: true, // mongodb driver 6.0 default is false, so we use true for backwards compatibility
returnDocument: 'after'
} as FindOneAndUpdateOptions;

/**
* @class
*/
Expand Down Expand Up @@ -107,8 +116,8 @@ export class JobDbRepository {
// Update / options for the MongoDB query
const update: UpdateFilter<IJobParameters> = { $set: { lockedAt: new Date() } };
const options: FindOneAndUpdateOptions = {
returnDocument: 'after',
sort: this.connectOptions.sort
sort: this.connectOptions.sort,
...findOneAndUpdateCommonOptions
};

// Lock the job in MongoDB!
Expand Down Expand Up @@ -154,8 +163,8 @@ export class JobDbRepository {
* Query used to affect what gets returned
*/
const JOB_RETURN_QUERY: FindOneAndUpdateOptions = {
returnDocument: 'after',
sort: this.connectOptions.sort
sort: this.connectOptions.sort,
...findOneAndUpdateCommonOptions
};

// Find ONE and ONLY ONE job and set the 'lockedAt' time so that job begins to be processed
Expand Down Expand Up @@ -313,7 +322,7 @@ export class JobDbRepository {
const result = await this.collection.findOneAndUpdate(
{ _id: id, name: props.name },
update,
{ returnDocument: 'after' }
{ ...findOneAndUpdateCommonOptions }
);
return this.processDbResult(job, result.value as IJobParameters<DATA>);
}
Expand Down Expand Up @@ -352,7 +361,7 @@ export class JobDbRepository {
update,
{
upsert: true,
returnDocument: 'after'
...findOneAndUpdateCommonOptions
}
);
log(
Expand Down