Skip to content

Commit

Permalink
fix: use options for readPreference in client
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Reggi authored Oct 12, 2020
1 parent 4955a52 commit 6acced0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ function MongoClient(url, options) {
dbCache: new Map(),
sessions: new Set(),
writeConcern: WriteConcern.fromOptions(options),
readPreference: ReadPreference.fromOptions(options) || ReadPreference.primary,
namespace: new MongoDBNamespace('admin')
};
}
Expand All @@ -188,7 +189,7 @@ Object.defineProperty(MongoClient.prototype, 'writeConcern', {
Object.defineProperty(MongoClient.prototype, 'readPreference', {
enumerable: true,
get: function() {
return ReadPreference.primary;
return this.s.readPreference;
}
});

Expand Down
7 changes: 7 additions & 0 deletions test/functional/mongo_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var f = require('util').format;
var test = require('./shared').assert;
var setupDatabase = require('./shared').setupDatabase;
const ReadPreference = require('../../lib/core/topologies/read_preference');
const Db = require('../../lib/db');
const expect = require('chai').expect;

Expand Down Expand Up @@ -831,4 +832,10 @@ describe('MongoClient', function() {
}
});
});

it('should cache a resolved readPreference from options', function() {
const client = this.configuration.newClient({}, { readPreference: ReadPreference.SECONDARY });
expect(client.readPreference).to.be.instanceOf(ReadPreference);
expect(client.readPreference).to.have.property('mode', ReadPreference.SECONDARY);
});
});

0 comments on commit 6acced0

Please sign in to comment.