Mongoose Promise with bluebird and typescript
Promise
export as a variable in mongoose, so you can convert mongoose
namespace as any first, and then set Promise
to others.
- if you are using
q
lib.- install
npm install --save q @types/q
first.tsc
version >= 2.0. - then add
(<any>mongoose).Promise = Q.Promise;
- install
- using
bluebird
lib, add code below.import Bluebird = require("bluebird");
(<any>mongoose).Promise = Bluebird;
Should I write a custom definition file for my app
Yes. It will mostly be a find and replace of Promise in the mongoose definition.
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.
EDIT latest typings version
declare module "mongoose" { import Bluebird = require("bluebird"); type Promise<T> = Bluebird<T>;}
See documentation here