Skip to content

Commit 294f901

Browse files
committed
move into options
1 parent a4920d5 commit 294f901

File tree

5 files changed

+39
-23
lines changed

5 files changed

+39
-23
lines changed

src/connection_string.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import {
2121
MongoClient,
2222
MongoClientOptions,
2323
MongoOptions,
24-
PkFactory
24+
PkFactory,
25+
ServerApi
2526
} from './mongo_client';
2627
import { MongoCredentials } from './cmap/auth/mongo_credentials';
2728
import type { TagSet } from './sdam/server_description';
@@ -572,6 +573,15 @@ export const OPTIONS = {
572573
autoEncryption: {
573574
type: 'record'
574575
},
576+
serverApi: {
577+
target: 'serverApi',
578+
transform({ values: [version] }): ServerApi {
579+
if (typeof version === 'string') {
580+
return { version };
581+
}
582+
return version as ServerApi;
583+
}
584+
},
575585
checkKeys: {
576586
type: 'boolean'
577587
},

src/mongo_client.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
222222
logger?: Logger;
223223
/** Enable command monitoring for this client */
224224
monitorCommands?: boolean;
225+
/** Server API version */
226+
serverApi?: ServerApiVersion | ServerApi;
225227
/** Optionally enable client side auto encryption */
226228
autoEncryption?: AutoEncryptionOptions;
227229
/** Allows a wrapping driver to amend the client metadata generated by the driver to include information about the wrapping driver */
@@ -243,13 +245,13 @@ export interface MongoClientPrivate {
243245
readConcern?: ReadConcern;
244246
writeConcern?: WriteConcern;
245247
readPreference: ReadPreference;
248+
serverApi: ServerApi;
246249
bsonOptions: BSONSerializeOptions;
247250
namespace: MongoDBNamespace;
248251
logger: Logger;
249252
}
250253

251254
const kOptions = Symbol('options');
252-
const kServerApi = Symbol('serverApi');
253255

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

305-
/**
306-
* The MongoDB Server API version
307-
* @internal
308-
* */
309-
[kServerApi]: ServerApi;
310-
311307
// debugging
312308
originalUri;
313309
originalOptions;
314310

315-
constructor(url: string, options?: MongoClientOptions, serverApi?: ServerApi) {
311+
constructor(url: string, options?: MongoClientOptions) {
316312
super();
317313

318314
this.originalUri = url;
319315
this.originalOptions = options;
320316

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

324319
// The internal state
325320
this.s = {
@@ -329,6 +324,7 @@ export class MongoClient extends EventEmitter {
329324
readConcern: this[kOptions].readConcern,
330325
writeConcern: this[kOptions].writeConcern,
331326
readPreference: this[kOptions].readPreference,
327+
serverApi: this[kOptions].serverApi,
332328
bsonOptions: resolveBSONOptions(this[kOptions]),
333329
namespace: ns('admin'),
334330
logger: this[kOptions].logger
@@ -339,8 +335,8 @@ export class MongoClient extends EventEmitter {
339335
return Object.freeze({ ...this[kOptions] });
340336
}
341337

342-
get serverApi(): Readonly<ServerApi> {
343-
return this[kServerApi];
338+
get serverApi(): Readonly<ServerApi | undefined> {
339+
return this[kOptions].serverApi && Object.freeze({ ...this[kOptions].serverApi });
344340
}
345341

346342
get autoEncrypter(): AutoEncrypter | undefined {
@@ -652,6 +648,7 @@ export interface MongoOptions
652648
credentials?: MongoCredentials;
653649
readPreference: ReadPreference;
654650
readConcern: ReadConcern;
651+
serverApi: ServerApi;
655652
writeConcern: WriteConcern;
656653
dbName: string;
657654
metadata: ClientMetadata;

test/functional/unified-spec-runner/entities.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ export class UnifiedMongoClient extends MongoClient {
3737
} as const;
3838

3939
constructor(url: string, description: ClientEntity) {
40-
super(url, { monitorCommands: true, ...description.uriOptions }, description.serverApi);
40+
super(url, {
41+
monitorCommands: true,
42+
...description.uriOptions,
43+
serverApi: description.serverApi
44+
});
4145
this.events = [];
4246
this.failPoints = [];
4347
this.ignoredEvents = [

test/tools/runner/config.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ function convertToConnStringMap(obj) {
3434
}
3535

3636
class TestConfiguration {
37-
constructor(uri, context, serverApiVersion) {
37+
constructor(uri, context) {
3838
const { url, hosts } = parseURI(uri);
3939
const hostAddresses = hosts.map(HostAddress.fromString);
4040
this.topologyType = context.topologyType;
4141
this.version = context.version;
42-
this.serverApiVersion = serverApiVersion;
4342
this.clientSideEncryption = context.clientSideEncryption;
43+
this.serverApi = context.serverApi;
4444
this.parameters = undefined;
4545
this.options = {
4646
hosts,
@@ -97,17 +97,17 @@ class TestConfiguration {
9797
}
9898

9999
newClient(dbOptions, serverOptions) {
100+
const defaultOptions = { minHeartbeatFrequencyMS: 100 };
101+
if (this.serverApi) {
102+
Object.assign(defaultOptions, { serverApi: this.serverApi });
103+
}
100104
// support MongoClient constructor form (url, options) for `newClient`
101105
if (typeof dbOptions === 'string') {
102-
return new MongoClient(
103-
dbOptions,
104-
Object.assign({ minHeartbeatFrequencyMS: 100 }, serverOptions),
105-
{ version: this.serverApiVersion }
106-
);
106+
return new MongoClient(dbOptions, Object.assign(defaultOptions, serverOptions));
107107
}
108108

109109
dbOptions = dbOptions || {};
110-
serverOptions = Object.assign({}, { minHeartbeatFrequencyMS: 100 }, serverOptions);
110+
serverOptions = Object.assign({}, defaultOptions, serverOptions);
111111

112112
// Fall back
113113
let dbHost = (serverOptions && serverOptions.host) || this.options.host;
@@ -166,6 +166,7 @@ class TestConfiguration {
166166
if (Reflect.has(serverOptions, 'host') || Reflect.has(serverOptions, 'port')) {
167167
throw new Error(`Cannot use options to specify host/port, must be in ${connectionString}`);
168168
}
169+
169170
return new MongoClient(connectionString, serverOptions);
170171
}
171172

test/tools/runner/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ before(function (_done) {
6161
// )} topology`
6262
// );
6363

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

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

7374
initializeFilters(client, (err, context) => {
75+
if (MONGODB_API_VERSION) {
76+
Object.assign(context, { serverApi: MONGODB_API_VERSION });
77+
}
7478
if (err) {
7579
done(err);
7680
return;
7781
}
7882

7983
// replace this when mocha supports dynamic skipping with `afterEach`
8084
filterOutTests(this._runnable.parent);
81-
this.configuration = new TestConfiguration(MONGODB_URI, context, MONGODB_API_VERSION);
85+
this.configuration = new TestConfiguration(MONGODB_URI, context);
8286
done();
8387
});
8488
});

0 commit comments

Comments
 (0)