Mongoose Promise with bluebird and typescript

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

I just read an article from mongoose's documentation on how to connect an external promise library , and it is very simple:

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

It works great. But I would like to expand / override the type of return promise.

Here is an example function:

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

This function returns _mongoose.Promise<MyModel>, and I would like to return bluebird Promise<MyModel>, because I know that this is the promise of bluebird.

Is there a way to change / expand / override the mongoose request type returned? Should I write a custom definition file for my application? Any other suggestions would be appreciated.

Thank!

+4
4

mongoose , , MongoosePromise<T>.

.d.ts :

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

, Mongoose Bluebird Promise!

promises.

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

.

0

Promise mongoose, mongoose , Promise .

  • q lib.
    • npm install --save q @types/q. tsc version >= 2.0.
    • (<any>mongoose).Promise = Q.Promise;
  • bluebird lib, .
    • import Bluebird = require("bluebird");
    • (<any>mongoose).Promise = Bluebird;
+5

. Promise .

+1

, , typings install mongoose , Specificaly mpromise, typings\modules\mongoose\index.d.ts mpromise module .

Until this problem is resolved, my solution is to remove the module definition for ~mongoose~mpromiseand replace the following line in the file index.d.ts:

import * as Promise from '~mongoose~mpromise';

with this:

import Promise = require( "bluebird");

This is admittedly a temporary stop because another call typings install mongoosewill return this fix. For the curious, my current setup includes: typings (version 2.0.0), npm (3.10.9), and node (v6.9.2)

0
source

Source: https://habr.com/ru/post/1648329/


All Articles