Skip to content

Commit 7686d15

Browse files
committed
feat(NODE-5274): deprecate write concern options
1 parent ccc5e30 commit 7686d15

File tree

3 files changed

+170
-17
lines changed

3 files changed

+170
-17
lines changed

src/operations/command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export abstract class CommandOperation<T> extends AbstractCallbackOperation<T> {
135135
}
136136

137137
if (this.writeConcern && this.hasAspect(Aspect.WRITE_OPERATION) && !inTransaction) {
138-
Object.assign(cmd, { writeConcern: this.writeConcern });
138+
WriteConcern.apply(cmd, this.writeConcern);
139139
}
140140

141141
if (

src/write_concern.ts

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Document } from "bson";
2+
13
/** @public */
24
export type W = number | 'majority';
35

@@ -17,16 +19,36 @@ export interface WriteConcernSettings {
1719
journal?: boolean;
1820

1921
// legacy options
20-
/** The journal write concern */
22+
/**
23+
* The journal write concern.
24+
* @deprecated Will be removed in the next major version. Please use the journal option.
25+
*/
2126
j?: boolean;
22-
/** The write concern timeout */
27+
/**
28+
* The write concern timeout.
29+
* @deprecated Will be removed in the next major version. Please use the wtimeoutMS option.
30+
*/
2331
wtimeout?: number;
24-
/** The file sync write concern */
32+
/**
33+
* The file sync write concern.
34+
* @deprecated Will be removed in the next major version. Please use the journal option.
35+
*/
2536
fsync?: boolean | 1;
2637
}
2738

2839
export const WRITE_CONCERN_KEYS = ['w', 'wtimeout', 'j', 'journal', 'fsync'];
2940

41+
interface CommandWriteConcernOptions {
42+
/** The write concern */
43+
w?: W;
44+
/** The journal write concern. */
45+
j?: boolean;
46+
/** The write concern timeout. */
47+
wtimeout?: number;
48+
/** The file sync write concern. */
49+
fsync?: boolean | 1;
50+
}
51+
3052
/**
3153
* A MongoDB WriteConcern, which describes the level of acknowledgement
3254
* requested from MongoDB for write operations.
@@ -35,41 +57,66 @@ export const WRITE_CONCERN_KEYS = ['w', 'wtimeout', 'j', 'journal', 'fsync'];
3557
* @see https://www.mongodb.com/docs/manual/reference/write-concern/
3658
*/
3759
export class WriteConcern {
38-
/** request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags. */
39-
w?: W;
40-
/** specify a time limit to prevent write operations from blocking indefinitely */
60+
/** Request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags. */
61+
readonly w?: W;
62+
/** Request acknowledgment that the write operation has been written to the on-disk journal */
63+
readonly journal?: boolean;
64+
/** Specify a time limit to prevent write operations from blocking indefinitely */
65+
readonly wtimeoutMS?: number;
66+
/**
67+
* Specify a time limit to prevent write operations from blocking indefinitely.
68+
* @deprecated Will be removed in the next major version. Please use wtimeoutMS.
69+
*/
4170
wtimeout?: number;
42-
/** request acknowledgment that the write operation has been written to the on-disk journal */
71+
/**
72+
* Request acknowledgment that the write operation has been written to the on-disk journal.
73+
* @deprecated Will be removed in the next major version. Please use journal.
74+
*/
4375
j?: boolean;
44-
/** equivalent to the j option */
76+
/**
77+
* Equivalent to the j option.
78+
* @deprecated Will be removed in the next major version. Please use journal.
79+
*/
4580
fsync?: boolean | 1;
4681

4782
/**
4883
* Constructs a WriteConcern from the write concern properties.
4984
* @param w - request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags.
50-
* @param wtimeout - specify a time limit to prevent write operations from blocking indefinitely
51-
* @param j - request acknowledgment that the write operation has been written to the on-disk journal
85+
* @param wtimeoutMS - specify a time limit to prevent write operations from blocking indefinitely
86+
* @param journal - request acknowledgment that the write operation has been written to the on-disk journal
5287
* @param fsync - equivalent to the j option
5388
*/
54-
constructor(w?: W, wtimeout?: number, j?: boolean, fsync?: boolean | 1) {
89+
constructor(w?: W, wtimeoutMS?: number, journal?: boolean, fsync?: boolean | 1) {
5590
if (w != null) {
5691
if (!Number.isNaN(Number(w))) {
5792
this.w = Number(w);
5893
} else {
5994
this.w = w;
6095
}
6196
}
62-
if (wtimeout != null) {
63-
this.wtimeout = wtimeout;
97+
if (wtimeoutMS != null) {
98+
this.wtimeoutMS = this.wtimeout = wtimeoutMS;
6499
}
65-
if (j != null) {
66-
this.j = j;
100+
if (journal != null) {
101+
this.journal = this.j = journal;
67102
}
68103
if (fsync != null) {
69-
this.fsync = fsync;
104+
this.journal = this.j = fsync ? true : false;
70105
}
71106
}
72107

108+
/**
109+
* Apply a write concern to a command document.
110+
*/
111+
static apply(command: Document, writeConcern: WriteConcern): Document {
112+
const wc: CommandWriteConcernOptions = {};
113+
// The write concern document sent to the server has w/wtimeout/j fields.
114+
if (writeConcern.w != null) wc.w = writeConcern.w;
115+
if (writeConcern.wtimeoutMS != null) wc.wtimeout = writeConcern.wtimeoutMS;
116+
if (writeConcern.journal != null) wc.j = writeConcern.j;
117+
return Object.assign(command, { writeConcern: wc });
118+
}
119+
73120
/** Construct a WriteConcern given an options object. */
74121
static fromOptions(
75122
options?: WriteConcernOptions | WriteConcern | W,

test/unit/write_concern.test.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { expect } from 'chai';
2+
3+
import { WriteConcern } from '../mongodb';
4+
5+
describe('WriteConcern', function () {
6+
describe('#constructor', function () {
7+
context('when w is provided', function () {
8+
const writeConcern = new WriteConcern(1);
9+
10+
it('sets the w property', function () {
11+
expect(writeConcern.w).to.equal(1);
12+
});
13+
});
14+
15+
context('when wtimeoutMS is provided', function () {
16+
const writeConcern = new WriteConcern(1, 50);
17+
18+
it('sets the wtimeoutMS property', function () {
19+
expect(writeConcern.wtimeoutMS).to.equal(50);
20+
});
21+
22+
it('sets the wtimeout property', function () {
23+
expect(writeConcern.wtimeout).to.equal(50);
24+
});
25+
});
26+
27+
context('when journal is provided', function () {
28+
const writeConcern = new WriteConcern(1, 50, true);
29+
30+
it('sets the journal property', function () {
31+
expect(writeConcern.journal).to.be.true;
32+
});
33+
34+
it('sets the j property', function () {
35+
expect(writeConcern.j).to.be.true;
36+
});
37+
});
38+
39+
context('when fsync is provided', function () {
40+
const writeConcern = new WriteConcern(1, 50, false, true);
41+
42+
it('sets the journal property', function () {
43+
expect(writeConcern.journal).to.be.true;
44+
});
45+
46+
it('sets the j property', function () {
47+
expect(writeConcern.j).to.be.true;
48+
});
49+
});
50+
});
51+
52+
describe('.apply', function () {
53+
context('when no options are set', function () {
54+
const document = {};
55+
const writeConcern = new WriteConcern();
56+
57+
it('returns an empty write concern', function () {
58+
expect(WriteConcern.apply(document, writeConcern)).to.deep.equal({ writeConcern: {} });
59+
});
60+
});
61+
62+
context('when w is in the write concern', function () {
63+
const document = {};
64+
const writeConcern = new WriteConcern(2);
65+
66+
it('adds w to the write concern document', function () {
67+
expect(WriteConcern.apply(document, writeConcern)).to.deep.equal({
68+
writeConcern: { w: 2 }
69+
});
70+
});
71+
});
72+
73+
context('when wtimeoutMS is in the write concern', function () {
74+
const document = {};
75+
const writeConcern = new WriteConcern(2, 30);
76+
77+
it('adds wtimeout to the write concern document', function () {
78+
expect(WriteConcern.apply(document, writeConcern)).to.deep.equal({
79+
writeConcern: { w: 2, wtimeout: 30 }
80+
});
81+
});
82+
});
83+
84+
context('when journal is in the write concern', function () {
85+
const document = {};
86+
const writeConcern = new WriteConcern(2, 30, true);
87+
88+
it('adds j to the write concern document', function () {
89+
expect(WriteConcern.apply(document, writeConcern)).to.deep.equal({
90+
writeConcern: { w: 2, wtimeout: 30, j: true }
91+
});
92+
});
93+
});
94+
95+
context('when fsync is in the write concern', function () {
96+
const document = {};
97+
const writeConcern = new WriteConcern(2, 30, true, false);
98+
99+
it('overrites j to the write concern document', function () {
100+
expect(WriteConcern.apply(document, writeConcern)).to.deep.equal({
101+
writeConcern: { w: 2, wtimeout: 30, j: false }
102+
});
103+
});
104+
});
105+
});
106+
});

0 commit comments

Comments
 (0)