Skip to content

Commit

Permalink
fix(NODE-4186): accept ReadPreferenceLike in TransactionOptions type (#…
Browse files Browse the repository at this point in the history
…3425)

Co-authored-by: Neal Beeken <neal.beeken@mongodb.com>
  • Loading branch information
noahsilas and nbbeeken committed Oct 3, 2022
1 parent 82b4a23 commit dc62bcb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/transactions.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions test/types/community/transaction.test-d.ts
Expand Up @@ -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' }
});
Expand Down
28 changes: 28 additions & 0 deletions 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');
});
});
});

0 comments on commit dc62bcb

Please sign in to comment.