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 committed Oct 12, 2020
1 parent 7ed6dbf commit 800e71e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { AuthMechanism } from './cmap/auth/defaultAuthProviders';
import type { Topology } from './sdam/topology';
import type { ClientSession, ClientSessionOptions } from './sessions';
import type { OperationParent } from './operations/command';
import type { TagSet } from './sdam/server_description';

/** @public */
export enum LogLevel {
Expand Down Expand Up @@ -97,7 +98,7 @@ export interface MongoURIOptions extends Pick<WriteConcernOptions, 'journal' | '
/** Specifies, in seconds, how stale a secondary can be before the client stops using it for read operations. */
maxStalenessSeconds?: number;
/** Specifies the tags document as a comma-separated list of colon-separated key-value pairs. */
readPreferenceTags?: string;
readPreferenceTags?: TagSet[];
/** Specify the database name associated with the user’s credentials. */
authSource?: string;
/** Specify the authentication mechanism that MongoDB will use to authenticate the connection. */
Expand Down Expand Up @@ -220,6 +221,7 @@ export interface MongoClientPrivate {
sessions: Set<ClientSession>;
readConcern?: ReadConcern;
writeConcern?: WriteConcern;
readPreference: ReadPreference;
namespace: MongoDBNamespace;
logger: Logger;
}
Expand Down Expand Up @@ -281,6 +283,7 @@ export class MongoClient extends EventEmitter implements OperationParent {
sessions: new Set(),
readConcern: ReadConcern.fromOptions(options),
writeConcern: WriteConcern.fromOptions(options),
readPreference: ReadPreference.fromOptions(options) || ReadPreference.primary,
namespace: new MongoDBNamespace('admin'),
logger: options?.logger ?? new Logger('MongoClient')
};
Expand All @@ -295,7 +298,7 @@ export class MongoClient extends EventEmitter implements OperationParent {
}

get readPreference(): ReadPreference {
return ReadPreference.primary;
return this.s.readPreference;
}

get logger(): Logger {
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('../../src');
const { Db } = require('../../src/db');
const expect = require('chai').expect;

Expand Down Expand Up @@ -301,4 +302,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 800e71e

Please sign in to comment.