I\'m trying to develop a class on the top of the mongoose with my custom methods, so I extended the mongoose with my own class but when I invoke to create a new car method i
While the answer above is accurate and works, you have to account for the issue of Performance if you have a real, production Node app.
The solution above will use native ES6 promises - which are 4X slower than bluebird in the benchmarks I've shared below. This could dramatically affect the performance of an API written in Node and using MongoDB.
I recommend using Bluebird:
// Assuming you store the library in a var called mongoose
var mongoose = require('mongoose');
// Just add bluebird to your package.json, and then the following line should work
mongoose.Promise = require('bluebird');
Benchmark Results
Platform: (using latest Node at time of writing)
| file | time(ms) | memory(MB) |
|-------------------------------------------|----------|------------|
| callbacks-baseline.js | 114 | 25.09 |
| callbacks-suguru03-neo-async-waterfall.js | 152 | 32.98 |
| promises-bluebird-generator.js | 208 | 29.89 |
| promises-bluebird.js | 223 | 45.47 |
| promises-cujojs-when.js | 320 | 58.11 |
| promises-then-promise.js | 327 | 64.51 |
| promises-tildeio-rsvp.js | 387 | 85.17 |
| promises-lvivski-davy.js | 396 | 81.18 |
| callbacks-caolan-async-waterfall.js | 527 | 97.45 |
| promises-dfilatov-vow.js | 593 | 148.30 |
| promises-calvinmetcalf-lie.js | 666 | 122.78 |
| generators-tj-co.js | 885 | 121.71 |
| promises-obvious-kew.js | 920 | 216.08 |
| promises-ecmascript6-native.js | 931 | 184.90 |
| promises-medikoo-deferred.js | 1412 | 158.38 |
| streamline-generators.js | 1695 | 175.84 |
| observables-Reactive-Extensions-RxJS.js | 1739 | 218.96 |
| streamline-callbacks.js | 2668 | 248.61 |
| promises-kriskowal-q.js | 9889 | 410.96 |
| observables-baconjs-bacon.js.js | 21636 | 799.09 |
| observables-pozadi-kefir.js | 51601 | 151.29 |
| observables-caolan-highland.js | 134113 | 387.07 |