Skip to content

Commit

Permalink
fix(transactions): use options helper to resolve read preference
Browse files Browse the repository at this point in the history
ReadPreference can be expressed a number of ways through options,
so we need to ensure that the read preference is properly resolved
when a user provides options. This change also includes fixes for
`readConcern` and `writeConcern`.

NODE-2401
  • Loading branch information
mbroadst committed Jan 15, 2020
1 parent b8a7ef4 commit 9698a76
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/core/transactions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';
const MongoError = require('./error').MongoError;
const ReadPreference = require('./topologies/read_preference');
const ReadConcern = require('../read_concern');
const WriteConcern = require('../write_concern');

let TxnState;
let stateMachine;
Expand Down Expand Up @@ -90,18 +93,26 @@ class Transaction {
this.state = TxnState.NO_TRANSACTION;
this.options = {};

if (options.writeConcern || typeof options.w !== 'undefined') {
const w = options.writeConcern ? options.writeConcern.w : options.w;
if (w <= 0) {
const writeConcern = WriteConcern.fromOptions(options);
if (writeConcern) {
if (writeConcern.w <= 0) {
throw new MongoError('Transactions do not support unacknowledged write concern');
}

this.options.writeConcern = options.writeConcern ? options.writeConcern : { w: options.w };
this.options.writeConcern = writeConcern;
}

if (options.readConcern) this.options.readConcern = options.readConcern;
if (options.readPreference) this.options.readPreference = options.readPreference;
if (options.maxCommitTimeMS) this.options.maxTimeMS = options.maxCommitTimeMS;
if (options.readConcern) {
this.options.readConcern = ReadConcern.fromOptions(options);
}

if (options.readPreference) {
this.options.readPreference = ReadPreference.fromOptions(options);
}

if (options.maxCommitTimeMS) {
this.options.maxTimeMS = options.maxCommitTimeMS;
}

// TODO: This isn't technically necessary
this._pinnedServer = undefined;
Expand Down

0 comments on commit 9698a76

Please sign in to comment.