Skip to content

Commit 780ec71

Browse files
committed
refactor: Make find and modify new/upsert parameter assignments in a uniform way
1 parent df16dec commit 780ec71

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/operations/find_and_modify.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ interface FindAndModifyOptions extends CommandOperationOptions {
7474

7575
// NOTE: These types are a misuse of options, can we think of a way to remove them?
7676
remove?: boolean;
77-
new?: boolean;
7877
}
7978

8079
/** @internal */
@@ -121,9 +120,15 @@ export class FindAndModifyOperation extends CommandOperation<Document> {
121120
cmd.sort = sort;
122121
}
123122

124-
cmd.new = options.new ? true : false;
123+
if (!options.remove) {
124+
cmd.new = typeof options.returnOriginal === 'boolean' ? !options.returnOriginal : false;
125+
cmd.upsert = typeof options.upsert === 'boolean' ? options.upsert : false;
126+
if (doc) {
127+
cmd.update = doc;
128+
}
129+
}
130+
125131
cmd.remove = options.remove ? true : false;
126-
cmd.upsert = options.upsert ? true : false;
127132

128133
if (options.projection) {
129134
cmd.fields = options.projection;
@@ -133,10 +138,6 @@ export class FindAndModifyOperation extends CommandOperation<Document> {
133138
cmd.arrayFilters = options.arrayFilters;
134139
}
135140

136-
if (doc && !options.remove) {
137-
cmd.update = doc;
138-
}
139-
140141
if (options.maxTimeMS) {
141142
cmd.maxTimeMS = options.maxTimeMS;
142143
}
@@ -212,8 +213,6 @@ export class FindOneAndReplaceOperation extends FindAndModifyOperation {
212213
) {
213214
// Final options
214215
const finalOptions = Object.assign({}, options);
215-
finalOptions.new = options.returnOriginal !== void 0 ? !options.returnOriginal : false;
216-
finalOptions.upsert = options.upsert !== void 0 ? !!options.upsert : false;
217216

218217
if (filter == null || typeof filter !== 'object') {
219218
throw new TypeError('Filter parameter must be an object');
@@ -241,9 +240,6 @@ export class FindOneAndUpdateOperation extends FindAndModifyOperation {
241240
) {
242241
// Final options
243242
const finalOptions = Object.assign({}, options);
244-
finalOptions.new =
245-
typeof options.returnOriginal === 'boolean' ? !options.returnOriginal : false;
246-
finalOptions.upsert = typeof options.upsert === 'boolean' ? options.upsert : false;
247243

248244
if (filter == null || typeof filter !== 'object') {
249245
throw new TypeError('Filter parameter must be an object');

0 commit comments

Comments
 (0)