Skip to content

Commit 60ea0bc

Browse files
HanaPearlmannbbeeken
authored andcommitted
remove top-level wc options
1 parent 43c94b6 commit 60ea0bc

File tree

6 files changed

+48
-43
lines changed

6 files changed

+48
-43
lines changed

src/cmap/wire_protocol/command.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import type { Document, BSONSerializeOptions } from '../../bson';
88
import type { Server } from '../../sdam/server';
99
import type { Topology } from '../../sdam/topology';
1010
import type { ReadPreferenceLike } from '../../read_preference';
11-
import type { WriteConcernOptions, WriteConcern, W } from '../../write_concern';
11+
import type { WriteConcernOptions } from '../../write_concern';
1212
import type { WriteCommandOptions } from './write_command';
1313

1414
/** @internal */
15-
export interface CommandOptions extends BSONSerializeOptions {
15+
// FIXME: NODE-2781
16+
export interface CommandOptions extends BSONSerializeOptions, WriteConcernOptions {
1617
command?: boolean;
1718
slaveOk?: boolean;
1819
/** Specify read preference if command supports it */
@@ -28,9 +29,6 @@ export interface CommandOptions extends BSONSerializeOptions {
2829

2930
// FIXME: NODE-2802
3031
willRetryWrite?: boolean;
31-
32-
// FIXME: NODE-2781
33-
writeConcern?: WriteConcernOptions | WriteConcern | W;
3432
}
3533

3634
function isClientEncryptionEnabled(server: Server) {

src/db.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ import type { Admin } from './admin';
6060

6161
// Allowed parameters
6262
const legalOptionNames = [
63-
'w',
64-
'wtimeout',
65-
'fsync',
66-
'j',
63+
'writeConcern',
6764
'readPreference',
6865
'readPreferenceTags',
6966
'native_parser',

src/gridfs-stream/upload.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,9 @@ function doWrite(
515515
function getWriteOptions(stream: GridFSBucketWriteStream): WriteConcernOptions {
516516
const obj: WriteConcernOptions = {};
517517
if (stream.writeConcern) {
518-
obj.w = stream.writeConcern.w;
519-
obj.wtimeout = stream.writeConcern.wtimeout;
520-
obj.j = stream.writeConcern.j;
518+
obj.writeConcern = { w: stream.writeConcern.w };
519+
obj.writeConcern.wtimeout = stream.writeConcern.wtimeout;
520+
obj.writeConcern.j = stream.writeConcern.j;
521521
}
522522
return obj;
523523
}

src/mongo_client.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { EventEmitter } from 'events';
33
import { ChangeStream, ChangeStreamOptions } from './change_stream';
44
import { ReadPreference, ReadPreferenceMode } from './read_preference';
55
import { MongoError, AnyError } from './error';
6-
import { WriteConcern, WriteConcernOptions } from './write_concern';
6+
import { WriteConcern, WriteConcernOptions, W } from './write_concern';
77
import { maybePromise, MongoDBNamespace, Callback, resolveOptions } from './utils';
88
import { deprecate } from 'util';
99
import { connect, validOptions } from './operations/connect';
@@ -58,7 +58,7 @@ type CleanUpHandlerFunction = (err?: AnyError, result?: any, opts?: any) => Prom
5858
* @public
5959
* @see https://docs.mongodb.com/manual/reference/connection-string
6060
*/
61-
export interface MongoURIOptions extends Pick<WriteConcernOptions, 'journal' | 'w' | 'wtimeoutMS'> {
61+
export interface MongoURIOptions {
6262
/** Specifies the name of the replica set, if the mongod is a member of a replica set. */
6363
replicaSet?: string;
6464
/** Enables or disables TLS/SSL for the connection. */
@@ -131,6 +131,13 @@ export interface MongoURIOptions extends Pick<WriteConcernOptions, 'journal' | '
131131
retryWrites?: boolean;
132132
/** Allow a driver to force a Single topology type with a connection string containing one host */
133133
directConnection?: boolean;
134+
// TODO: but now mongo client options accept these at the top level...
135+
/** The journal write concern */
136+
journal?: boolean;
137+
/** The write concern */
138+
w?: W;
139+
/** The write concern timeout */
140+
wtimeoutMS?: number;
134141
}
135142

136143
/** @public */

src/operations/connect.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ const validOptionNames = [
5151
'acceptableLatencyMS',
5252
'connectWithNoPrimary',
5353
'authSource',
54-
'w',
55-
'wtimeout',
56-
'j',
54+
'writeConcern',
5755
'forceServerObjectId',
5856
'serializeFunctions',
5957
'ignoreUndefined',
@@ -78,7 +76,6 @@ const validOptionNames = [
7876
'password',
7977
'authMechanism',
8078
'compression',
81-
'fsync',
8279
'readPreferenceTags',
8380
'numberOfRetries',
8481
'auto_reconnect',
@@ -226,12 +223,6 @@ export function connect(
226223
delete finalOptions.db_options.auth;
227224
}
228225

229-
// `journal` should be translated to `j` for the driver
230-
if (finalOptions.journal != null) {
231-
finalOptions.j = finalOptions.journal;
232-
finalOptions.journal = undefined;
233-
}
234-
235226
// resolve tls options if needed
236227
resolveTLSOptions(finalOptions);
237228

@@ -422,7 +413,9 @@ function createUnifiedOptions(finalOptions: any, options: any) {
422413
const noMerge = ['readconcern', 'compression', 'autoencryption'];
423414

424415
for (const name in options) {
425-
if (noMerge.indexOf(name.toLowerCase()) !== -1) {
416+
if (name === 'writeConcern') {
417+
finalOptions[name] = { ...finalOptions[name], ...options[name] };
418+
} else if (noMerge.indexOf(name.toLowerCase()) !== -1) {
426419
finalOptions[name] = options[name];
427420
} else if (childOptions.indexOf(name.toLowerCase()) !== -1) {
428421
finalOptions = mergeOptions(finalOptions, options[name], false);
@@ -559,14 +552,23 @@ function transformUrlOptions(connStrOptions: any) {
559552
connStrOpts.readConcern = new ReadConcern(connStrOpts.readConcernLevel);
560553
}
561554

562-
if (connStrOpts.wTimeoutMS) {
563-
connStrOpts.wtimeout = connStrOpts.wTimeoutMS;
564-
}
565-
566555
if (connStrOptions.srvHost) {
567556
connStrOpts.srvHost = connStrOptions.srvHost;
568557
}
569558

559+
const wc_keys = ['w', 'j', 'journal', 'wtimeout', 'wtimeoutMS', 'fsync'];
560+
const writeConcern = connStrOpts.writeConcern ?? {};
561+
for (const key of wc_keys) {
562+
if (connStrOpts[key] !== undefined) {
563+
writeConcern[key] = connStrOpts[key];
564+
connStrOpts[key] = undefined;
565+
}
566+
}
567+
connStrOpts.writeConcern = writeConcern;
568+
569+
if (connStrOpts.writeConcern.wTimeoutMS) {
570+
connStrOpts.writrConcern.wtimeout = connStrOpts.writeConcern.wTimeoutMS;
571+
}
570572
return connStrOpts;
571573
}
572574

src/write_concern.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ export type W = number | 'majority';
33

44
/** @public */
55
export interface WriteConcernOptions {
6+
/** Write Concern as an object */
7+
writeConcern?: WriteConcern | WriteConcernSettings;
8+
}
9+
10+
/** @public */
11+
export interface WriteConcernSettings {
612
/** The write concern */
713
w?: W;
814
/** The write concern timeout */
@@ -15,12 +21,8 @@ export interface WriteConcernOptions {
1521
journal?: boolean;
1622
/** The file sync write concern */
1723
fsync?: boolean | 1;
18-
/** Write Concern as an object */
19-
writeConcern?: WriteConcernOptions | WriteConcern | W;
2024
}
2125

22-
export const writeConcernKeys = ['w', 'j', 'wtimeout', 'fsync'];
23-
2426
/**
2527
* A MongoDB WriteConcern, which describes the level of acknowledgement
2628
* requested from MongoDB for write operations.
@@ -65,19 +67,17 @@ export class WriteConcern {
6567

6668
/** Construct a WriteConcern given an options object. */
6769
static fromOptions(
68-
options?: WriteConcernOptions | WriteConcern | W,
70+
options?: WriteConcernOptions | WriteConcern,
6971
inherit?: WriteConcernOptions | WriteConcern
7072
): WriteConcern | undefined {
71-
const { fromOptions } = WriteConcern;
7273
if (typeof options === 'undefined') return undefined;
73-
if (typeof options === 'number') return fromOptions({ ...inherit, w: options });
74-
if (typeof options === 'string') return fromOptions({ ...inherit, w: options });
75-
if (options instanceof WriteConcern) return fromOptions({ ...inherit, ...options });
76-
if (options.writeConcern) {
77-
const { writeConcern, ...viable } = { ...inherit, ...options };
78-
return fromOptions(writeConcern, viable);
79-
}
80-
const { w, wtimeout, j, fsync, journal, wtimeoutMS } = { ...inherit, ...options };
74+
inherit = inherit ?? {};
75+
const opts: WriteConcern | WriteConcernSettings | undefined =
76+
options instanceof WriteConcern ? options : options.writeConcern;
77+
const parentOpts: WriteConcern | WriteConcernSettings | undefined =
78+
inherit instanceof WriteConcern ? inherit : inherit.writeConcern;
79+
80+
const { w, wtimeout, j, fsync, journal, wtimeoutMS } = { ...parentOpts, ...opts };
8181
if (
8282
w != null ||
8383
wtimeout != null ||
@@ -88,6 +88,7 @@ export class WriteConcern {
8888
) {
8989
return new WriteConcern(w, wtimeout ?? wtimeoutMS, j ?? journal, fsync);
9090
}
91+
9192
return undefined;
9293
}
9394
}

0 commit comments

Comments
 (0)