Skip to content

Commit f5ce422

Browse files
s/replaceOne/replace
1 parent 5391174 commit f5ce422

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

index.d.ts

+33-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ declare module "monk" {
77
FindOneOptions,
88
UpdateQuery,
99
UpdateOneOptions,
10+
UpdateManyOptions,
11+
ReplaceOneOptions,
1012
CollectionInsertOneOptions,
1113
Cursor,
1214
FindOneAndDeleteOption,
@@ -238,24 +240,24 @@ declare module "monk" {
238240
findOneAndUpdate(
239241
query: FilterQuery<T>,
240242
update: UpdateQuery<T> | Partial<T>,
241-
options?: FindOneAndUpdateOption<T> & { replaceOne?: false }
243+
options?: FindOneAndUpdateOption<T> & { replace?: false }
242244
): Promise<FindOneResult<T>>;
243245
findOneAndUpdate(
244246
query: FilterQuery<T>,
245247
update: UpdateQuery<T> | Partial<T>,
246-
options?: FindOneAndUpdateOption<T> & { replaceOne?: false },
248+
options?: FindOneAndUpdateOption<T> & { replace?: false },
247249
callback?: Callback<FindOneResult<T>>
248250
): void;
249251
// Replace
250252
findOneAndUpdate(
251253
query: FilterQuery<T>,
252254
update: T,
253-
options?: FindOneAndReplaceOption<T> & { replaceOne: true }
255+
options?: FindOneAndReplaceOption<T> & { replace: true }
254256
): Promise<FindOneResult<T>>;
255257
findOneAndUpdate(
256258
query: FilterQuery<T>,
257259
update: T,
258-
options: FindOneAndReplaceOption<T> & { replaceOne: true },
260+
options: FindOneAndReplaceOption<T> & { replace: true },
259261
callback: Callback<FindOneResult<T>>
260262
): void;
261263

@@ -330,15 +332,40 @@ declare module "monk" {
330332
stats(options?: StatsOptions): Promise<CollStats>;
331333
stats(options: StatsOptions, callback: Callback<CollStats>): void;
332334

335+
// single
333336
update(
334337
query: FilterQuery<T>,
335338
update: UpdateQuery<T> | Partial<T>,
336-
options?: UpdateOneOptions
339+
options?: UpdateOneOptions & { single?: true, multi?: false, replace?: false}
337340
): Promise<UpdateWriteOpResult>;
338341
update(
339342
query: FilterQuery<T>,
340343
update: UpdateQuery<T> | Partial<T>,
341-
options: UpdateOneOptions,
344+
options: UpdateOneOptions & { single?: true, multi?: false, replace?: false},
345+
callback: Callback<UpdateWriteOpResult>
346+
): void;
347+
// multi
348+
update(
349+
query: FilterQuery<T>,
350+
update: UpdateQuery<T> | Partial<T>,
351+
options?: UpdateManyOptions & ({ single?: false, multi: true, replace?: false} | { single: false, multi?: true, replace?: false})
352+
): Promise<UpdateWriteOpResult>;
353+
update(
354+
query: FilterQuery<T>,
355+
update: UpdateQuery<T> | Partial<T>,
356+
options: UpdateOneOptions & ({ single?: false, multi: true, replace?: false} | { single: false, multi?: true, replace?: false}),
357+
callback: Callback<UpdateWriteOpResult>
358+
): void;
359+
// replace
360+
update(
361+
query: FilterQuery<T>,
362+
update: UpdateQuery<T> | Partial<T>,
363+
options?: ReplaceOneOptions & { single?: true, multi?: false, replace: true}
364+
): Promise<UpdateWriteOpResult>;
365+
update(
366+
query: FilterQuery<T>,
367+
update: UpdateQuery<T> | Partial<T>,
368+
options: ReplaceOneOptions & { single?: true, multi?: false, replace: true},
342369
callback: Callback<UpdateWriteOpResult>
343370
): void;
344371
}

lib/collection.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ Collection.prototype.findOneAndUpdate = function (query, update, opts, fn) {
272272
if (typeof (args.options || {}).returnOriginal === 'undefined') {
273273
args.options.returnOriginal = false
274274
}
275-
if (args.options.replaceOne) {
275+
if (args.options.replaceOne | args.options.replace) {
276276
method = 'findOneAndReplace'
277277
}
278278
return args.col[method](args.query, args.update, args.options)
@@ -381,7 +381,10 @@ Collection.prototype.update = function (query, update, opts, fn) {
381381
return this._dispatch(function update (args) {
382382
var options = args.options || {}
383383
var method = options.multi || options.single === false ? 'updateMany' : 'updateOne'
384-
if (options.replaceOne) {
384+
if (options.replace || options.replaceOne) {
385+
if (options.multi || options.single === false) {
386+
throw new Error('The `replace` option is only available for single updates.')
387+
}
385388
method = 'replaceOne'
386389
}
387390
return args.col[method](args.query, args.update, args.options).then(function (doc) {

0 commit comments

Comments
 (0)