Thomas Thomas - 1 year ago 464
Node.js Question

Mongoose Promise with bluebird and typescript

I am developing an application with nodejs/typescript and a mongodb database.
To query the database, I am using mongoose.

I've just read an article from the mongoose documentation on how to plug in an external promise library and it is very simple:

import mongoose = require("mongoose");
import Promise = require("bluebird");
mongoose.Promise = Promise;

Doing this is working fine. But I'd like to extend/override the type of the promise that is returned.

Here is an example of a function:

public getModel= () => {
return MyModel.findOne().exec();

This function returns a
and I'd like to return a bluebird
because I know that is a bluebird promise.

Is there is anyway to change/extend/override the return type of mongoose query ?
Should I write a custom definition file for my app ? Any others suggestions would be appreciated.

Thanks !

Answer Source

The mongoose team updated the definition file and you can now plug in and use your own promise library by assigning the MongoosePromise<T>.

Create a main .d.ts file for your application and add this:

declare module "mongoose" {
    import Bluebird = require("bluebird");
    type MongoosePromise<T> = Bluebird<T>;

Reference this file in your project and now Mongoose returns Bluebird Promise !

This also works for others promises library.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download