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

fix(NODE-4186): accept ReadPreferenceLike in TransactionOptions type #3425

Merged
merged 2 commits into from Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
});
});
});