From 6c69b7d51d98cf858607b0fdcad41d26691f97db Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Mon, 1 Aug 2022 15:31:36 -0300 Subject: [PATCH] fix(NODE-4467): Add back support for `oplogReplay` option as deprecated (#3337) --- src/operations/find.ts | 9 ++++ test/unit/operations/find.test.ts | 69 +++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 test/unit/operations/find.test.ts diff --git a/src/operations/find.ts b/src/operations/find.ts index c52aa2cd92..ba2e8cf741 100644 --- a/src/operations/find.ts +++ b/src/operations/find.ts @@ -63,6 +63,11 @@ export interface FindOptions extends Comman showRecordId?: boolean; /** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */ let?: Document; + /** + * Option to enable an optimized code path for queries looking for a particular range of `ts` values in the oplog. Requires `tailable` to be true. + * @deprecated Starting from MongoDB 4.4 this flag is not needed and will be ignored. + */ + oplogReplay?: boolean; } const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5; @@ -242,6 +247,10 @@ function makeFindCommand(ns: MongoDBNamespace, filter: Document, options: FindOp findCommand.tailable = options.tailable; } + if (typeof options.oplogReplay === 'boolean') { + findCommand.oplogReplay = options.oplogReplay; + } + if (typeof options.timeout === 'boolean') { findCommand.noCursorTimeout = !options.timeout; } else if (typeof options.noCursorTimeout === 'boolean') { diff --git a/test/unit/operations/find.test.ts b/test/unit/operations/find.test.ts new file mode 100644 index 0000000000..f4031bcfbd --- /dev/null +++ b/test/unit/operations/find.test.ts @@ -0,0 +1,69 @@ +import { expect } from 'chai'; +import * as sinon from 'sinon'; +import { promisify } from 'util'; + +import { FindOperation } from '../../../src/operations/find'; +import { Server } from '../../../src/sdam/server'; +import { ServerDescription } from '../../../src/sdam/server_description'; +import { Topology } from '../../../src/sdam/topology'; +import { ns } from '../../../src/utils'; + +describe('FindOperation', function () { + const namespace = ns('db.coll'); + const options = { + batchSize: 100 + }; + const filter = { + ts: { $gt: new Date() } + }; + + afterEach(function () { + sinon.restore(); + }); + + describe('#constructor', function () { + const operation = new FindOperation(undefined, namespace, filter, options); + + it('sets the namespace', function () { + expect(operation.ns).to.equal(namespace); + }); + + it('sets options', function () { + expect(operation.options).to.equal(options); + }); + + it('sets filter', function () { + expect(operation.filter).to.equal(filter); + }); + }); + + describe('#execute', function () { + context('command construction', () => { + const namespace = ns('db.collection'); + const server = new Server(new Topology([], {} as any), new ServerDescription(''), {} as any); + + it('should build basic find command with filter', async () => { + const findOperation = new FindOperation(undefined, namespace, filter); + const stub = sinon.stub(server, 'command').yieldsRight(); + await promisify(findOperation.execute.bind(findOperation))(server, undefined); + expect(stub).to.have.been.calledOnceWith(namespace, { + find: namespace.collection, + filter + }); + }); + + it('should build find command with oplogReplay', async () => { + const options = { + oplogReplay: true + }; + const findOperation = new FindOperation(undefined, namespace, {}, options); + const stub = sinon.stub(server, 'command').yieldsRight(); + await promisify(findOperation.execute.bind(findOperation))(server, undefined); + expect(stub).to.have.been.calledOnceWith( + namespace, + sinon.match.has('oplogReplay', options.oplogReplay) + ); + }); + }); + }); +});