Skip to content

Commit 6991e10

Browse files
committed
refactor(NODE-1812)!: Replace returnOriginal with returnDocument in find and modify options
1 parent 29512da commit 6991e10

13 files changed

+79
-65
lines changed

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export { BatchType } from './bulk/common';
6565
export { AuthMechanism } from './cmap/auth/defaultAuthProviders';
6666
export { CURSOR_FLAGS } from './cursor/abstract_cursor';
6767
export { Compressor } from './cmap/wire_protocol/compression';
68+
export { ReturnDocument } from './operations/find_and_modify';
6869
export { ExplainVerbosity } from './explain';
6970
export { ReadConcernLevel } from './read_concern';
7071
export { ReadPreferenceMode } from './read_preference';

src/operations/find_and_modify.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ import { Sort, SortForCmd, formatSort } from '../sort';
1010
import type { ClientSession } from '../sessions';
1111
import type { WriteConcern, WriteConcernSettings } from '../write_concern';
1212

13+
/** @public */
14+
export const ReturnDocument = Object.freeze({
15+
BEFORE: 'Before',
16+
AFTER: 'After'
17+
} as const);
18+
19+
/** @public */
20+
export type ReturnDocument = typeof ReturnDocument[keyof typeof ReturnDocument];
21+
1322
/** @public */
1423
export interface FindOneAndDeleteOptions extends CommandOperationOptions {
1524
/** An optional hint for query optimization. See the {@link https://docs.mongodb.com/manual/reference/command/update/#update-command-hint|update command} reference for more information.*/
@@ -28,8 +37,8 @@ export interface FindOneAndReplaceOptions extends CommandOperationOptions {
2837
hint?: Document;
2938
/** Limits the fields to return for all matching documents. */
3039
projection?: Document;
31-
/** When false, returns the updated document rather than the original. The default is true. */
32-
returnOriginal?: boolean;
40+
/** When set to 'After', returns the updated document rather than the original. The default is 'Before'. */
41+
returnDocument?: ReturnDocument;
3342
/** Determines which document the operation modifies if the query selects multiple documents. */
3443
sort?: Sort;
3544
/** Upsert the document if it does not exist. */
@@ -46,16 +55,14 @@ export interface FindOneAndUpdateOptions extends CommandOperationOptions {
4655
hint?: Document;
4756
/** Limits the fields to return for all matching documents. */
4857
projection?: Document;
49-
/** When false, returns the updated document rather than the original. The default is true. */
50-
returnOriginal?: boolean;
58+
/** When set to 'After', returns the updated document rather than the original. The default is 'Before'. */
59+
returnDocument?: ReturnDocument;
5160
/** Determines which document the operation modifies if the query selects multiple documents. */
5261
sort?: Sort;
5362
/** Upsert the document if it does not exist. */
5463
upsert?: boolean;
5564
}
5665

57-
// TODO: NODE-1812 to deprecate returnOriginal for returnDocument
58-
5966
/** @internal */
6067
interface FindAndModifyCmdBase {
6168
remove: boolean;
@@ -74,7 +81,7 @@ function configureFindAndModifyCmdBaseUpdateOpts(
7481
cmdBase: FindAndModifyCmdBase,
7582
options: FindOneAndReplaceOptions | FindOneAndUpdateOptions
7683
): FindAndModifyCmdBase {
77-
cmdBase.new = options.returnOriginal === false;
84+
cmdBase.new = options.returnDocument === ReturnDocument.AFTER;
7885
cmdBase.upsert = options.upsert === true;
7986

8087
if (options.bypassDocumentValidation === true) {

test/functional/crud_api.test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const test = require('./shared').assert;
33
const { expect } = require('chai');
4+
const { ReturnDocument } = require('../../src');
45
const setupDatabase = require('./shared').setupDatabase;
56

67
// instanceof cannot be use reliably to detect the new models in js due to scoping and new
@@ -784,7 +785,7 @@ describe('CRUD API', function () {
784785
{
785786
projection: { b: 1, c: 1 },
786787
sort: { a: 1 },
787-
returnOriginal: false,
788+
returnDocument: ReturnDocument.AFTER,
788789
upsert: true
789790
},
790791
function (err, r) {
@@ -816,7 +817,7 @@ describe('CRUD API', function () {
816817
{
817818
projection: { b: 1, d: 1 },
818819
sort: { a: 1 },
819-
returnOriginal: false,
820+
returnDocument: ReturnDocument.AFTER,
820821
upsert: true
821822
},
822823
function (err, r) {

test/functional/crud_spec.test.js

-4
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,10 @@ describe('CRUD spec', function () {
320320
const filter = args.filter;
321321
const second = args.update || args.replacement;
322322
const options = Object.assign({}, args);
323-
if (options.returnDocument) {
324-
options.returnOriginal = options.returnDocument === 'After' ? false : true;
325-
}
326323

327324
delete options.filter;
328325
delete options.update;
329326
delete options.replacement;
330-
delete options.returnDocument;
331327

332328
const opName = scenarioTest.operation.name;
333329
const findPromise =

test/functional/find.test.js

+19-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const test = require('./shared').assert;
33
const { setupDatabase, withMonitoredClient } = require('./shared');
44
const { expect } = require('chai');
55
const sinon = require('sinon');
6-
const { Code, ObjectId, Long, Binary } = require('../../src');
6+
const { Code, ObjectId, Long, Binary, ReturnDocument } = require('../../src');
77

88
describe('Find', function () {
99
before(function () {
@@ -810,7 +810,7 @@ describe('Find', function () {
810810
collection.findOneAndUpdate(
811811
{ a: 1 },
812812
{ $set: { b: 3 } },
813-
{ returnOriginal: false },
813+
{ returnDocument: ReturnDocument.AFTER },
814814
function (err, updated_doc) {
815815
test.equal(1, updated_doc.value.a);
816816
test.equal(3, updated_doc.value.b);
@@ -846,7 +846,7 @@ describe('Find', function () {
846846
collection.findOneAndUpdate(
847847
{ a: 4 },
848848
{ $set: { b: 3 } },
849-
{ returnOriginal: false, upsert: true },
849+
{ returnDocument: ReturnDocument.AFTER, upsert: true },
850850
function (err, updated_doc) {
851851
test.equal(4, updated_doc.value.a);
852852
test.equal(3, updated_doc.value.b);
@@ -861,7 +861,10 @@ describe('Find', function () {
861861
collection.findOneAndUpdate(
862862
{ a: 100 },
863863
{ $set: { b: 5 } },
864-
{ returnOriginal: false, projection: { b: 1 } },
864+
{
865+
returnDocument: ReturnDocument.AFTER,
866+
projection: { b: 1 }
867+
},
865868
function (err, updated_doc) {
866869
test.equal(2, Object.keys(updated_doc.value).length);
867870
test.equal(
@@ -913,7 +916,7 @@ describe('Find', function () {
913916
collection.findOneAndUpdate(
914917
{ a: 1 },
915918
{ $set: { b: 3 } },
916-
{ returnOriginal: false, projection: { a: 1 } },
919+
{ returnDocument: ReturnDocument.AFTER, projection: { a: 1 } },
917920
function (err, updated_doc) {
918921
test.equal(2, Object.keys(updated_doc.value).length);
919922
test.equal(1, updated_doc.value.a);
@@ -1160,7 +1163,7 @@ describe('Find', function () {
11601163
collection.findOneAndUpdate(
11611164
{ a: 2 },
11621165
{ $set: { b: 3 } },
1163-
{ returnOriginal: false },
1166+
{ returnDocument: ReturnDocument.AFTER },
11641167
function (err, result) {
11651168
test.equal(2, result.value.a);
11661169
test.equal(3, result.value.b);
@@ -1252,7 +1255,7 @@ describe('Find', function () {
12521255
collection.findOneAndUpdate(
12531256
{ _id: id },
12541257
{ $set: { 'c.c': 100 } },
1255-
{ returnOriginal: false },
1258+
{ returnDocument: ReturnDocument.AFTER },
12561259
function (err, item) {
12571260
test.equal(doc._id.toString(), item.value._id.toString());
12581261
test.equal(doc.a, item.value.a);
@@ -1289,7 +1292,11 @@ describe('Find', function () {
12891292
collection.findOneAndUpdate(
12901293
{ _id: self._id, 'plays.uuid': _uuid },
12911294
{ $set: { 'plays.$.active': true } },
1292-
{ returnOriginal: false, projection: { plays: 0, results: 0 }, safe: true },
1295+
{
1296+
returnDocument: ReturnDocument.AFTER,
1297+
projection: { plays: 0, results: 0 },
1298+
safe: true
1299+
},
12931300
function (err) {
12941301
expect(err).to.not.exist;
12951302
client.close(done);
@@ -1437,7 +1444,7 @@ describe('Find', function () {
14371444
collection.findOneAndUpdate(
14381445
{ a: 1 },
14391446
{ $set: { b: 3 } },
1440-
{ returnOriginal: false },
1447+
{ returnDocument: ReturnDocument.AFTER },
14411448
function (err, updated_doc) {
14421449
expect(err).to.not.exist;
14431450
expect(updated_doc.value).to.not.exist;
@@ -1500,7 +1507,7 @@ describe('Find', function () {
15001507
'transactions.id': { $ne: transaction.transactionId }
15011508
},
15021509
{ $push: { transactions: transaction } },
1503-
{ returnOriginal: false, safe: true },
1510+
{ returnDocument: ReturnDocument.AFTER, safe: true },
15041511
function (err) {
15051512
expect(err).to.not.exist;
15061513
client.close(done);
@@ -1587,7 +1594,7 @@ describe('Find', function () {
15871594
function (err, collection) {
15881595
var q = { x: 1 };
15891596
var set = { y: 2, _id: new ObjectId() };
1590-
var opts = { returnOriginal: false, upsert: true };
1597+
var opts = { returnDocument: ReturnDocument.AFTER, upsert: true };
15911598
// Original doc
15921599
var doc = { _id: new ObjectId(), x: 1 };
15931600

@@ -2316,7 +2323,7 @@ describe('Find', function () {
23162323
collection.findOneAndUpdate(
23172324
{ a: 1 },
23182325
{ $set: { b: 3 } },
2319-
{ returnOriginal: false },
2326+
{ returnDocument: ReturnDocument.AFTER },
23202327
function (err, updated_doc) {
23212328
test.equal(1, updated_doc.value.a);
23222329
test.equal(3, updated_doc.value.b);

test/functional/insert.test.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const {
1616
MinKey,
1717
MaxKey,
1818
Code,
19-
MongoBulkWriteError
19+
MongoBulkWriteError,
20+
ReturnDocument
2021
} = require('../../src');
2122

2223
/**
@@ -967,7 +968,11 @@ describe('Insert', function () {
967968
collection.findOneAndUpdate(
968969
{ str: 'String' },
969970
{ $set: { f: function () {} } },
970-
{ returnOriginal: false, safe: true, serializeFunctions: true },
971+
{
972+
returnDocument: ReturnDocument.AFTER,
973+
safe: true,
974+
serializeFunctions: true
975+
},
971976
function (err, result) {
972977
test.ok(result.value.f._bsontype === 'Code');
973978
client.close(done);

test/functional/multiple_db.test.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
var test = require('./shared').assert;
33
var setupDatabase = require('./shared').setupDatabase;
4+
const { ReturnDocument } = require('../../src');
45
const { expect } = require('chai');
56

67
describe('Multiple Databases', function () {
@@ -78,12 +79,15 @@ describe('Multiple Databases', function () {
7879

7980
db_instance.collection('counters', function (err, collection) {
8081
expect(err).to.not.exist;
81-
collection.findOneAndUpdate({}, { $inc: { db: 1 } }, { returnOriginal: false }, function (
82-
err
83-
) {
84-
expect(err).to.not.exist;
85-
client.close(done);
86-
});
82+
collection.findOneAndUpdate(
83+
{},
84+
{ $inc: { db: 1 } },
85+
{ returnDocument: ReturnDocument.AFTER },
86+
function (err) {
87+
expect(err).to.not.exist;
88+
client.close(done);
89+
}
90+
);
8791
});
8892
});
8993
}

test/functional/operation_example.test.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { assert: test } = require('./shared');
33
const { setupDatabase } = require('./shared');
44
const { format: f } = require('util');
55
const { Topology } = require('../../src/sdam/topology');
6-
const { Code, ObjectId } = require('../../src');
6+
const { Code, ObjectId, ReturnDocument } = require('../../src');
77

88
const chai = require('chai');
99
const expect = chai.expect;
@@ -1532,7 +1532,7 @@ describe('Operation Examples', function () {
15321532
collection.findOneAndUpdate(
15331533
{ a: 1 },
15341534
{ $set: { b1: 1 } },
1535-
{ returnOriginal: false },
1535+
{ returnDocument: ReturnDocument.AFTER },
15361536
function (err, doc) {
15371537
expect(err).to.not.exist;
15381538
test.equal(1, doc.value.a);
@@ -1558,7 +1558,11 @@ describe('Operation Examples', function () {
15581558
collection.findOneAndUpdate(
15591559
{ d: 1 },
15601560
{ $set: { d: 1, f: 1 } },
1561-
{ returnOriginal: false, upsert: true, writeConcern: { w: 1 } },
1561+
{
1562+
returnDocument: ReturnDocument.AFTER,
1563+
upsert: true,
1564+
writeConcern: { w: 1 }
1565+
},
15621566
function (err, doc) {
15631567
expect(err).to.not.exist;
15641568
test.equal(1, doc.value.d);
@@ -6406,7 +6410,7 @@ describe('Operation Examples', function () {
64066410
{
64076411
projection: { b: 1, c: 1 },
64086412
sort: { a: 1 },
6409-
returnOriginal: false,
6413+
returnDocument: ReturnDocument.AFTER,
64106414
upsert: true
64116415
},
64126416
function (err, r) {
@@ -6462,7 +6466,7 @@ describe('Operation Examples', function () {
64626466
{
64636467
projection: { b: 1, d: 1 },
64646468
sort: { a: 1 },
6465-
returnOriginal: false,
6469+
returnDocument: ReturnDocument.AFTER,
64666470
upsert: true
64676471
},
64686472
function (err, r) {

test/functional/operation_generators_example.test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
var test = require('./shared').assert;
33
var setupDatabase = require('./shared').setupDatabase;
4-
const { Code } = require('../../src');
4+
const { Code, ReturnDocument } = require('../../src');
55
const { expect } = require('chai');
66

77
/**************************************************************************
@@ -906,7 +906,7 @@ describe('Operation (Generators)', function () {
906906
var doc = yield collection.findOneAndUpdate(
907907
{ a: 1 },
908908
{ $set: { b1: 1 } },
909-
{ returnOriginal: false }
909+
{ returnDocument: ReturnDocument.AFTER }
910910
);
911911
test.equal(1, doc.value.a);
912912
test.equal(1, doc.value.b1);
@@ -924,7 +924,7 @@ describe('Operation (Generators)', function () {
924924
doc = yield collection.findOneAndUpdate(
925925
{ d: 1 },
926926
{ $set: { d: 1, f: 1 } },
927-
{ returnOriginal: false, upsert: true, writeConcern: { w: 1 } }
927+
{ returnDocument: ReturnDocument.AFTER, upsert: true, writeConcern: { w: 1 } }
928928
);
929929
test.equal(1, doc.value.d);
930930
test.equal(1, doc.value.f);
@@ -4375,7 +4375,7 @@ describe('Operation (Generators)', function () {
43754375
{
43764376
projection: { b: 1, c: 1 },
43774377
sort: { a: 1 },
4378-
returnOriginal: false,
4378+
returnDocument: ReturnDocument.AFTER,
43794379
upsert: true
43804380
}
43814381
);
@@ -4430,7 +4430,7 @@ describe('Operation (Generators)', function () {
44304430
{
44314431
projection: { b: 1, d: 1 },
44324432
sort: { a: 1 },
4433-
returnOriginal: false,
4433+
returnDocument: ReturnDocument.AFTER,
44344434
upsert: true
44354435
}
44364436
);

0 commit comments

Comments
 (0)