From ebf9f312594981863e6031a2643c64261b522d8f Mon Sep 17 00:00:00 2001 From: Philip Andersson Date: Fri, 4 May 2018 22:49:48 +0200 Subject: [PATCH] Mongoose model init (#25530) * Adding init method declaration on Model interface * Test init method on Model --- types/mongoose/index.d.ts | 10 ++++++++++ types/mongoose/mongoose-tests.ts | 1 + 2 files changed, 11 insertions(+) diff --git a/types/mongoose/index.d.ts b/types/mongoose/index.d.ts index 5723a9a881..362f549e67 100644 --- a/types/mongoose/index.d.ts +++ b/types/mongoose/index.d.ts @@ -2714,6 +2714,16 @@ declare module "mongoose" { insertMany(doc: any, callback?: (error: any, doc: T) => void): Promise; insertMany(doc: any, options?: { ordered?: boolean, rawResult?: boolean }, callback?: (error: any, doc: T) => void): Promise; + /** + * Performs any async initialization of this model against MongoDB. + * This function is called automatically, so you don't need to call it. + * This function is also idempotent, so you may call it to get back a promise + * that will resolve when your indexes are finished building as an alternative + * to `MyModel.on('index')` + * @param callback optional + */ + init(callback?: (err: any) => void): Promise; + /** * Executes a mapReduce command. * @param o an object specifying map-reduce options diff --git a/types/mongoose/mongoose-tests.ts b/types/mongoose/mongoose-tests.ts index a6fe9f406a..95f50a6e6f 100644 --- a/types/mongoose/mongoose-tests.ts +++ b/types/mongoose/mongoose-tests.ts @@ -1420,6 +1420,7 @@ var MongoModel = mongoose.model('MongoModel', new mongoose.Schema({ required: true } }), 'myCollection', true); +MongoModel.init().then(cb); MongoModel.find({}).$where('indexOf("val") !== -1').exec(function (err, docs) { docs[0].save(); docs[0].__v;