Merge pull request #15719 from fabiob/mongoose-save-options

@types/mongoose: added save options
This commit is contained in:
Ryan Cavanaugh
2017-04-17 18:08:44 -07:00
committed by GitHub
2 changed files with 29 additions and 1 deletions
+16
View File
@@ -2566,6 +2566,7 @@ declare module "mongoose" {
* @param options.validateBeforeSave set to false to save without validating.
* @param fn optional callback
*/
save(options?: SaveOptions, fn?: (err: any, product: this, numAffected: number) => void): Promise<this>;
save(fn?: (err: any, product: this, numAffected: number) => void): Promise<this>;
/**
@@ -2575,6 +2576,21 @@ declare module "mongoose" {
__v?: number;
}
interface SaveOptions {
safe?: boolean | WriteConcern;
validateBeforeSave?: boolean;
}
interface WriteConcern {
j?: boolean;
w?: number | 'majority' | TagSet;
wtimeout?: number;
}
interface TagSet {
[k: string]: string;
}
interface ModelProperties {
/** Base Mongoose instance the model uses. */
base: typeof mongoose;
+13 -1
View File
@@ -1234,8 +1234,20 @@ MongoModel.find({}).$where('indexOf("val") !== -1').exec(function (err, docs) {
docs[0].__v;
});
MongoModel.findById(999, function (err, doc) {
var handleSave = function(err: Error, product: mongoose.Document, numAffected: number) {};
doc.increment();
doc.save(cb).then(cb).catch(cb);
doc.save(handleSave).then(cb).catch(cb);
doc.save({ validateBeforeSave: false }, handleSave).then(cb).catch(cb);
doc.save({ safe: true }, handleSave).then(cb).catch(cb);
doc.save({ safe: { w: 2, j: true } }, handleSave).then(cb).catch(cb);
doc.save({ safe: { w: 'majority', wtimeout: 10000 } }, handleSave).then(cb).catch(cb);
// test if Typescript can infer the types of (err, product, numAffected)
doc.save(function(err, product, numAffected) { product.save(); })
.then(function(p) { p.save() }).catch(cb);
doc.save({ validateBeforeSave: false }, function(err, product, numAffected) {
product.save();
}).then(function(p) { p.save() }).catch(cb);
});
MongoModel = (new MongoModel()).model('MongoModel');
var mongoModel = new MongoModel();