Skip to content

Commit 9698a76

Browse files
committed
fix(transactions): use options helper to resolve read preference
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
1 parent b8a7ef4 commit 9698a76

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

lib/core/transactions.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22
const MongoError = require('./error').MongoError;
3+
const ReadPreference = require('./topologies/read_preference');
4+
const ReadConcern = require('../read_concern');
5+
const WriteConcern = require('../write_concern');
36

47
let TxnState;
58
let stateMachine;
@@ -90,18 +93,26 @@ class Transaction {
9093
this.state = TxnState.NO_TRANSACTION;
9194
this.options = {};
9295

93-
if (options.writeConcern || typeof options.w !== 'undefined') {
94-
const w = options.writeConcern ? options.writeConcern.w : options.w;
95-
if (w <= 0) {
96+
const writeConcern = WriteConcern.fromOptions(options);
97+
if (writeConcern) {
98+
if (writeConcern.w <= 0) {
9699
throw new MongoError('Transactions do not support unacknowledged write concern');
97100
}
98101

99-
this.options.writeConcern = options.writeConcern ? options.writeConcern : { w: options.w };
102+
this.options.writeConcern = writeConcern;
100103
}
101104

102-
if (options.readConcern) this.options.readConcern = options.readConcern;
103-
if (options.readPreference) this.options.readPreference = options.readPreference;
104-
if (options.maxCommitTimeMS) this.options.maxTimeMS = options.maxCommitTimeMS;
105+
if (options.readConcern) {
106+
this.options.readConcern = ReadConcern.fromOptions(options);
107+
}
108+
109+
if (options.readPreference) {
110+
this.options.readPreference = ReadPreference.fromOptions(options);
111+
}
112+
113+
if (options.maxCommitTimeMS) {
114+
this.options.maxTimeMS = options.maxCommitTimeMS;
115+
}
105116

106117
// TODO: This isn't technically necessary
107118
this._pinnedServer = undefined;

0 commit comments

Comments
 (0)