Skip to content

Commit

Permalink
s/replaceOne/replace
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieudutour committed Oct 13, 2020
1 parent 5391174 commit f5ce422
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
39 changes: 33 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ declare module "monk" {
FindOneOptions,
UpdateQuery,
UpdateOneOptions,
UpdateManyOptions,
ReplaceOneOptions,
CollectionInsertOneOptions,
Cursor,
FindOneAndDeleteOption,
Expand Down Expand Up @@ -238,24 +240,24 @@ declare module "monk" {
findOneAndUpdate(
query: FilterQuery<T>,
update: UpdateQuery<T> | Partial<T>,
options?: FindOneAndUpdateOption<T> & { replaceOne?: false }
options?: FindOneAndUpdateOption<T> & { replace?: false }
): Promise<FindOneResult<T>>;
findOneAndUpdate(
query: FilterQuery<T>,
update: UpdateQuery<T> | Partial<T>,
options?: FindOneAndUpdateOption<T> & { replaceOne?: false },
options?: FindOneAndUpdateOption<T> & { replace?: false },
callback?: Callback<FindOneResult<T>>
): void;
// Replace
findOneAndUpdate(
query: FilterQuery<T>,
update: T,
options?: FindOneAndReplaceOption<T> & { replaceOne: true }
options?: FindOneAndReplaceOption<T> & { replace: true }
): Promise<FindOneResult<T>>;
findOneAndUpdate(
query: FilterQuery<T>,
update: T,
options: FindOneAndReplaceOption<T> & { replaceOne: true },
options: FindOneAndReplaceOption<T> & { replace: true },
callback: Callback<FindOneResult<T>>
): void;

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

// single
update(
query: FilterQuery<T>,
update: UpdateQuery<T> | Partial<T>,
options?: UpdateOneOptions
options?: UpdateOneOptions & { single?: true, multi?: false, replace?: false}
): Promise<UpdateWriteOpResult>;
update(
query: FilterQuery<T>,
update: UpdateQuery<T> | Partial<T>,
options: UpdateOneOptions,
options: UpdateOneOptions & { single?: true, multi?: false, replace?: false},
callback: Callback<UpdateWriteOpResult>
): void;
// multi
update(
query: FilterQuery<T>,
update: UpdateQuery<T> | Partial<T>,
options?: UpdateManyOptions & ({ single?: false, multi: true, replace?: false} | { single: false, multi?: true, replace?: false})
): Promise<UpdateWriteOpResult>;
update(
query: FilterQuery<T>,
update: UpdateQuery<T> | Partial<T>,
options: UpdateOneOptions & ({ single?: false, multi: true, replace?: false} | { single: false, multi?: true, replace?: false}),
callback: Callback<UpdateWriteOpResult>
): void;
// replace
update(
query: FilterQuery<T>,
update: UpdateQuery<T> | Partial<T>,
options?: ReplaceOneOptions & { single?: true, multi?: false, replace: true}
): Promise<UpdateWriteOpResult>;
update(
query: FilterQuery<T>,
update: UpdateQuery<T> | Partial<T>,
options: ReplaceOneOptions & { single?: true, multi?: false, replace: true},
callback: Callback<UpdateWriteOpResult>
): void;
}
Expand Down
7 changes: 5 additions & 2 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ Collection.prototype.findOneAndUpdate = function (query, update, opts, fn) {
if (typeof (args.options || {}).returnOriginal === 'undefined') {
args.options.returnOriginal = false
}
if (args.options.replaceOne) {
if (args.options.replaceOne | args.options.replace) {
method = 'findOneAndReplace'
}
return args.col[method](args.query, args.update, args.options)
Expand Down Expand Up @@ -381,7 +381,10 @@ Collection.prototype.update = function (query, update, opts, fn) {
return this._dispatch(function update (args) {
var options = args.options || {}
var method = options.multi || options.single === false ? 'updateMany' : 'updateOne'
if (options.replaceOne) {
if (options.replace || options.replaceOne) {
if (options.multi || options.single === false) {
throw new Error('The `replace` option is only available for single updates.')
}
method = 'replaceOne'
}
return args.col[method](args.query, args.update, args.options).then(function (doc) {
Expand Down

0 comments on commit f5ce422

Please sign in to comment.