From bf97b0491c0472eaf632826ec6fddd960a0be068 Mon Sep 17 00:00:00 2001 From: Aaron Queen Date: Sat, 2 Sep 2023 13:48:01 -0600 Subject: [PATCH 1/3] Support MongoDB driver 6.0 driver 6.0 no longer includes metadata on findOneAndUpdate... calls, so it returns the document at the root, rather than having to reach into .value to get the document. This commit provides backwards compatibility by passing the option includeResultMetadata: true to retain the old behavior. --- src/JobDbRepository.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/JobDbRepository.ts b/src/JobDbRepository.ts index 3f216c7..9b1055c 100644 --- a/src/JobDbRepository.ts +++ b/src/JobDbRepository.ts @@ -18,6 +18,11 @@ import { hasMongoProtocol } from './utils/hasMongoProtocol'; const log = debug('agenda:db'); +const findOneAndUpdateCommonOptions = { + includeResultMetadata: true, // mongodb driver 6.0 default is false, so we use true for backwards compatibility + returnDocument: 'after' +}; + /** * @class */ @@ -107,8 +112,8 @@ export class JobDbRepository { // Update / options for the MongoDB query const update: UpdateFilter = { $set: { lockedAt: new Date() } }; const options: FindOneAndUpdateOptions = { - returnDocument: 'after', - sort: this.connectOptions.sort + sort: this.connectOptions.sort, + ...findOneAndUpdateCommonOptions }; // Lock the job in MongoDB! @@ -154,8 +159,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 @@ -313,7 +318,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); } @@ -352,7 +357,7 @@ export class JobDbRepository { update, { upsert: true, - returnDocument: 'after' + ...findOneAndUpdateCommonOptions } ); log( From 4e7449eec208cd8818512b9ad788b85f64007606 Mon Sep 17 00:00:00 2001 From: Aaron Queen Date: Sat, 2 Sep 2023 14:04:12 -0600 Subject: [PATCH 2/3] extend type FindOneAndUpdateOptions --- src/JobDbRepository.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/JobDbRepository.ts b/src/JobDbRepository.ts index 9b1055c..b7b9682 100644 --- a/src/JobDbRepository.ts +++ b/src/JobDbRepository.ts @@ -3,7 +3,7 @@ import { Collection, Db, Filter, - FindOneAndUpdateOptions, + FindOneAndUpdateOptions as FindOneAndUpdateOptions_orig, MongoClient, MongoClientOptions, ObjectId, @@ -18,6 +18,10 @@ 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' From 4c7abce94f3723e7da50dae15f6cd531d64b57b2 Mon Sep 17 00:00:00 2001 From: Aaron Queen Date: Sat, 2 Sep 2023 14:16:02 -0600 Subject: [PATCH 3/3] extend type FindOneAndUpdateOptions --- src/JobDbRepository.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JobDbRepository.ts b/src/JobDbRepository.ts index b7b9682..31b9b04 100644 --- a/src/JobDbRepository.ts +++ b/src/JobDbRepository.ts @@ -25,7 +25,7 @@ interface FindOneAndUpdateOptions extends FindOneAndUpdateOptions_orig { const findOneAndUpdateCommonOptions = { includeResultMetadata: true, // mongodb driver 6.0 default is false, so we use true for backwards compatibility returnDocument: 'after' -}; +} as FindOneAndUpdateOptions; /** * @class