Skip to content

Commit 18ca41d

Browse files
committed
feat(read-preference): add transaction to inheritance rules
1 parent 7530405 commit 18ca41d

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lib/utils.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,15 @@ function resolveReadPreference(options, sources) {
511511
const db = sources.db;
512512
const coll = sources.collection;
513513
const defaultReadPreference = sources.default;
514+
const session = options.session;
514515

515516
let readPreference;
516517
if (options.readPreference) {
517518
readPreference = options.readPreference;
519+
} else if (session && session.inTransaction() && session.transaction.options.readPreference) {
520+
// From transactions spec: If the user supplies an explicit readConcern via a method
521+
// option, however, drivers MUST apply the readConcern...
522+
readPreference = session.transaction.options.readPreference;
518523
} else if (coll && coll.s.readPreference) {
519524
readPreference = coll.s.readPreference;
520525
} else if (db && db.s.readPreference) {
@@ -583,20 +588,18 @@ function decorateWithCollation(command, target, options) {
583588
}
584589
}
585590

586-
function decorateWithReadConcern(command, coll, options) {
591+
/**
592+
* Applies a read concern to a given command.
593+
*
594+
* @param {object} command the command on which to apply the read concern
595+
* @param {Collection} [target] the parent collection of the operation calling this method
596+
*/
597+
function decorateWithReadConcern(command, coll) {
587598
let readConcern = Object.assign({}, command.readConcern || {});
588599
if (coll.s.readConcern) {
589600
Object.assign(readConcern, coll.s.readConcern);
590601
}
591602

592-
if (
593-
options.session &&
594-
options.session.supports.causalConsistency &&
595-
options.session.operationTime
596-
) {
597-
Object.assign(readConcern, { afterClusterTime: options.session.operationTime });
598-
}
599-
600603
if (Object.keys(readConcern).length > 0) {
601604
Object.assign(command, { readConcern: readConcern });
602605
}

0 commit comments

Comments
 (0)