diff --git a/src/transactions.ts b/src/transactions.ts index 34d84403c7..bff2df1983 100644 --- a/src/transactions.ts +++ b/src/transactions.ts @@ -2,6 +2,7 @@ import type { Document } from './bson'; import { MongoRuntimeError, MongoTransactionError } from './error'; import type { CommandOperationOptions } from './operations/command'; import { ReadConcern, ReadConcernLike } from './read_concern'; +import type { ReadPreferenceLike } from './read_preference'; import { ReadPreference } from './read_preference'; import type { Server } from './sdam/server'; import { WriteConcern } from './write_concern'; @@ -67,7 +68,7 @@ export interface TransactionOptions extends CommandOperationOptions { /** A default writeConcern for commands in this transaction */ writeConcern?: WriteConcern; /** A default read preference for commands in this transaction */ - readPreference?: ReadPreference; + readPreference?: ReadPreferenceLike; /** Specifies the maximum amount of time to allow a commit action on a transaction to run in milliseconds */ maxCommitTimeMS?: number; } diff --git a/test/types/community/transaction.test-d.ts b/test/types/community/transaction.test-d.ts index 259d76114d..b6dd488dd6 100644 --- a/test/types/community/transaction.test-d.ts +++ b/test/types/community/transaction.test-d.ts @@ -48,6 +48,7 @@ async function runTransactionWithRetry( async function updateEmployeeInfo(client: MongoClient, session: ClientSession) { session.startTransaction({ + readPreference: 'primary', readConcern: new ReadConcern('available'), // NODE-3297 writeConcern: { w: 'majority' } }); diff --git a/test/unit/transactions.test.ts b/test/unit/transactions.test.ts new file mode 100644 index 0000000000..0c34f70ad5 --- /dev/null +++ b/test/unit/transactions.test.ts @@ -0,0 +1,28 @@ +import { expect } from 'chai'; + +import { ReadPreference } from '../../src'; +import { Transaction } from '../../src/transactions'; + +describe('class Transaction', () => { + describe('constructor()', () => { + it('uses ReadPreference instance', () => { + const transaction = new Transaction({ + readPreference: ReadPreference.nearest + }); + expect(transaction.options) + .to.have.property('readPreference') + .that.is.instanceOf(ReadPreference) + .that.has.property('mode', 'nearest'); + }); + + it('transforms ReadPreferenceLike string', () => { + const transaction = new Transaction({ + readPreference: 'nearest' + }); + expect(transaction.options) + .to.have.property('readPreference') + .that.is.instanceOf(ReadPreference) + .that.has.property('mode', 'nearest'); + }); + }); +});