Skip to content

Commit

Permalink
move into options
Browse files Browse the repository at this point in the history
  • Loading branch information
emadum committed Feb 9, 2021
1 parent a4920d5 commit 294f901
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
12 changes: 11 additions & 1 deletion src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
MongoClient,
MongoClientOptions,
MongoOptions,
PkFactory
PkFactory,
ServerApi
} from './mongo_client';
import { MongoCredentials } from './cmap/auth/mongo_credentials';
import type { TagSet } from './sdam/server_description';
Expand Down Expand Up @@ -572,6 +573,15 @@ export const OPTIONS = {
autoEncryption: {
type: 'record'
},
serverApi: {
target: 'serverApi',
transform({ values: [version] }): ServerApi {
if (typeof version === 'string') {
return { version };
}
return version as ServerApi;
}
},
checkKeys: {
type: 'boolean'
},
Expand Down
19 changes: 8 additions & 11 deletions src/mongo_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
logger?: Logger;
/** Enable command monitoring for this client */
monitorCommands?: boolean;
/** Server API version */
serverApi?: ServerApiVersion | ServerApi;
/** Optionally enable client side auto encryption */
autoEncryption?: AutoEncryptionOptions;
/** Allows a wrapping driver to amend the client metadata generated by the driver to include information about the wrapping driver */
Expand All @@ -243,13 +245,13 @@ export interface MongoClientPrivate {
readConcern?: ReadConcern;
writeConcern?: WriteConcern;
readPreference: ReadPreference;
serverApi: ServerApi;
bsonOptions: BSONSerializeOptions;
namespace: MongoDBNamespace;
logger: Logger;
}

const kOptions = Symbol('options');
const kServerApi = Symbol('serverApi');

/**
* The **MongoClient** class is a class that allows for making Connections to MongoDB.
Expand Down Expand Up @@ -302,24 +304,17 @@ export class MongoClient extends EventEmitter {
*/
[kOptions]: MongoOptions;

/**
* The MongoDB Server API version
* @internal
* */
[kServerApi]: ServerApi;

// debugging
originalUri;
originalOptions;

constructor(url: string, options?: MongoClientOptions, serverApi?: ServerApi) {
constructor(url: string, options?: MongoClientOptions) {
super();

this.originalUri = url;
this.originalOptions = options;

this[kOptions] = parseOptions(url, this, options);
this[kServerApi] = Object.freeze({ version: ServerApiVersion.v1, ...serverApi });

// The internal state
this.s = {
Expand All @@ -329,6 +324,7 @@ export class MongoClient extends EventEmitter {
readConcern: this[kOptions].readConcern,
writeConcern: this[kOptions].writeConcern,
readPreference: this[kOptions].readPreference,
serverApi: this[kOptions].serverApi,
bsonOptions: resolveBSONOptions(this[kOptions]),
namespace: ns('admin'),
logger: this[kOptions].logger
Expand All @@ -339,8 +335,8 @@ export class MongoClient extends EventEmitter {
return Object.freeze({ ...this[kOptions] });
}

get serverApi(): Readonly<ServerApi> {
return this[kServerApi];
get serverApi(): Readonly<ServerApi | undefined> {
return this[kOptions].serverApi && Object.freeze({ ...this[kOptions].serverApi });
}

get autoEncrypter(): AutoEncrypter | undefined {
Expand Down Expand Up @@ -652,6 +648,7 @@ export interface MongoOptions
credentials?: MongoCredentials;
readPreference: ReadPreference;
readConcern: ReadConcern;
serverApi: ServerApi;
writeConcern: WriteConcern;
dbName: string;
metadata: ClientMetadata;
Expand Down
6 changes: 5 additions & 1 deletion test/functional/unified-spec-runner/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export class UnifiedMongoClient extends MongoClient {
} as const;

constructor(url: string, description: ClientEntity) {
super(url, { monitorCommands: true, ...description.uriOptions }, description.serverApi);
super(url, {
monitorCommands: true,
...description.uriOptions,
serverApi: description.serverApi
});
this.events = [];
this.failPoints = [];
this.ignoredEvents = [
Expand Down
17 changes: 9 additions & 8 deletions test/tools/runner/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ function convertToConnStringMap(obj) {
}

class TestConfiguration {
constructor(uri, context, serverApiVersion) {
constructor(uri, context) {
const { url, hosts } = parseURI(uri);
const hostAddresses = hosts.map(HostAddress.fromString);
this.topologyType = context.topologyType;
this.version = context.version;
this.serverApiVersion = serverApiVersion;
this.clientSideEncryption = context.clientSideEncryption;
this.serverApi = context.serverApi;
this.parameters = undefined;
this.options = {
hosts,
Expand Down Expand Up @@ -97,17 +97,17 @@ class TestConfiguration {
}

newClient(dbOptions, serverOptions) {
const defaultOptions = { minHeartbeatFrequencyMS: 100 };
if (this.serverApi) {
Object.assign(defaultOptions, { serverApi: this.serverApi });
}
// support MongoClient constructor form (url, options) for `newClient`
if (typeof dbOptions === 'string') {
return new MongoClient(
dbOptions,
Object.assign({ minHeartbeatFrequencyMS: 100 }, serverOptions),
{ version: this.serverApiVersion }
);
return new MongoClient(dbOptions, Object.assign(defaultOptions, serverOptions));
}

dbOptions = dbOptions || {};
serverOptions = Object.assign({}, { minHeartbeatFrequencyMS: 100 }, serverOptions);
serverOptions = Object.assign({}, defaultOptions, serverOptions);

// Fall back
let dbHost = (serverOptions && serverOptions.host) || this.options.host;
Expand Down Expand Up @@ -166,6 +166,7 @@ class TestConfiguration {
if (Reflect.has(serverOptions, 'host') || Reflect.has(serverOptions, 'port')) {
throw new Error(`Cannot use options to specify host/port, must be in ${connectionString}`);
}

return new MongoClient(connectionString, serverOptions);
}

Expand Down
8 changes: 6 additions & 2 deletions test/tools/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ before(function (_done) {
// )} topology`
// );

const client = new MongoClient(MONGODB_URI);
const options = MONGODB_API_VERSION ? { serverApi: MONGODB_API_VERSION } : {};
const client = new MongoClient(MONGODB_URI, options);
const done = err => client.close(err2 => _done(err || err2));

client.connect(err => {
Expand All @@ -71,14 +72,17 @@ before(function (_done) {
}

initializeFilters(client, (err, context) => {
if (MONGODB_API_VERSION) {
Object.assign(context, { serverApi: MONGODB_API_VERSION });
}
if (err) {
done(err);
return;
}

// replace this when mocha supports dynamic skipping with `afterEach`
filterOutTests(this._runnable.parent);
this.configuration = new TestConfiguration(MONGODB_URI, context, MONGODB_API_VERSION);
this.configuration = new TestConfiguration(MONGODB_URI, context);
done();
});
});
Expand Down

0 comments on commit 294f901

Please sign in to comment.