Skip to content

Commit 6acced0

Browse files
author
Thomas Reggi
authoredOct 12, 2020
fix: use options for readPreference in client
NODE-2807
1 parent 4955a52 commit 6acced0

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed
 

‎lib/mongo_client.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ function MongoClient(url, options) {
169169
dbCache: new Map(),
170170
sessions: new Set(),
171171
writeConcern: WriteConcern.fromOptions(options),
172+
readPreference: ReadPreference.fromOptions(options) || ReadPreference.primary,
172173
namespace: new MongoDBNamespace('admin')
173174
};
174175
}
@@ -188,7 +189,7 @@ Object.defineProperty(MongoClient.prototype, 'writeConcern', {
188189
Object.defineProperty(MongoClient.prototype, 'readPreference', {
189190
enumerable: true,
190191
get: function() {
191-
return ReadPreference.primary;
192+
return this.s.readPreference;
192193
}
193194
});
194195

‎test/functional/mongo_client.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var f = require('util').format;
44
var test = require('./shared').assert;
55
var setupDatabase = require('./shared').setupDatabase;
6+
const ReadPreference = require('../../lib/core/topologies/read_preference');
67
const Db = require('../../lib/db');
78
const expect = require('chai').expect;
89

@@ -831,4 +832,10 @@ describe('MongoClient', function() {
831832
}
832833
});
833834
});
835+
836+
it('should cache a resolved readPreference from options', function() {
837+
const client = this.configuration.newClient({}, { readPreference: ReadPreference.SECONDARY });
838+
expect(client.readPreference).to.be.instanceOf(ReadPreference);
839+
expect(client.readPreference).to.have.property('mode', ReadPreference.SECONDARY);
840+
});
834841
});

0 commit comments

Comments
 (0)
Please sign in to comment.