Comments (11)
I haven't used promises much (if at all) in my work. I should probably dig into them a bit more. IIRC hapi 8 has promise support. I may be interested in supporting both, but I don't think I'd want to switch out for promises.
Any examples or details would be much appreciated. Could you show me an example of some code that returns a promise instead?
from hapi-mongo-models.
Some libraries support both and basically when callback is passed, it calls the callback, otherwise it returns promise.
var Promise = require('bluebird');
BaseModel.findByIdAndRemove = function (id, callback) {
var collection = BaseModel.db.collection(this._collection);
var query = { _id: this._idClass(id) };
return new Promise(function (resolve, reject) {
collection.remove(query,function (err, res) {
if (err) {
reject(err)
}
else {
resolve(res)
}
})
}).nodeify(callback);
};
This would be the example how to support both. findByIdAndRemove
returns promise and nodeify
calls the callback if the callback is function.
It could be used like
var promise = model.findByIdAndRemove(id);
promise.then(function(){
...
})
But as I had chance to think about it more, not sure if this good idea for now. hapi-mongo-models is great because of its simplicity.. and it would make it more complex.
Some BaseModel functions have optional parameters, which would require to check if last parameter is function. Also it would require more tests..
Alternative strategy that should work (I have not tried it yet) using bluebird's promisifyAll on baseModel, which creates functions with Async suffix that return promises (findByIdAndRemoveAsync
...).
Closing for now, if anyone have a great idea how to integrate Promises with minimal hassle.. it can be considered again I guess..
Btw have not noticed Async.auto until today.. Its probably not the fastest, but definitely convenient :-)
from hapi-mongo-models.
Thanks so much for the detailed response. It's helpful to see these examples.
I'm not a huge fan of adding another dependency (bluebird
) or the added code complexity. But if a decent number of people wanted it and were willing to contribute the code and tests, I'd be open to that.
from hapi-mongo-models.
+1. promises definitely make the code easier to read about. I have used bluebird and its pretty awesome. the nodeify, denodeify functions maybe exactly what you need.
from hapi-mongo-models.
+1 - definitely the break or make feature for myself though I am consideirng promisyfing it until then
from hapi-mongo-models.
Thanks for commenting @cyberwombat. @jardakotesovec mentioned some important things once he dug into it:
But as I had chance to think about it more, not sure if this good idea for now. hapi-mongo-models is great because of its simplicity.. and it would make it more complex.
Some BaseModel functions have optional parameters, which would require to check if last parameter is function. Also it would require more tests..
I really don't see promises adding enough value to justify the overhead in dev time and dependency footprint.
from hapi-mongo-models.
I just found out, that the native mongodb driver already support promises. So you don't need to add another dependency. Just add a return statement at the end of each method.
For example:
BaseModel.insertOne = function () {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
var collection = BaseModel.db.collection(this._collection);
var callback = this.resultFactory.bind(this, args.pop());
args.push(callback);
return collection.insertOne.apply(collection, args); // <--- adding return statement here
};
See http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#insertOne
I'm not quite sure what the resultFactory
call does, but it seems not to work with it. I just started working with mongodb and I'm still figuring out what to use.
from hapi-mongo-models.
@SteffenGorenflo thanks for sharing this. If it's as easy as adding return statements, I think we should go for it.
The result factor is a way for us to get the results back from a query and turn the data into the correct model instances. Would you mind creating a new issue about this and include your code sample above?
from hapi-mongo-models.
Just curious if there's been any internal progress on Promises support.
from hapi-mongo-models.
@prashaantt not from me since I almost never use promises. If it's as simple as mentioned above and doesn't disturb the existing API, a PR is welcomed.
from hapi-mongo-models.
📣 Async/await support is on the menu now. Tracked via: #74 and help is very much appreciated.
from hapi-mongo-models.
Related Issues (20)
- Can't use Mongo Native method Aggregate HOT 3
- Not authorized on dbname to execute command HOT 1
- QUESTION: Does this plug-in support associations? HOT 2
- Issue with updateOne & update Many HOT 4
- server.start() to include Mongo models? HOT 3
- v6.0.0 release notes
- Add a modelsDirectory plugin option
- using aggregate function HOT 2
- Not expected results in v6.0.0 responses HOT 3
- Multiple db support HOT 1
- MongoDB custom logger HOT 1
- Unable to get the instantiated db reference. HOT 1
- Hapi v17 support HOT 1
- Migrate callbacks pattern to async/await (hapi v17)
- Support for ssh tunnel ? HOT 6
- models "path/to/customer" might be off? HOT 2
- Migration help HOT 1
- Plugin example for 7.x.x? HOT 3
- $elemMatch returns all subjects HOT 1
- If connection.db is optional, why is there an assertion error if it isn't provided? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hapi-mongo-models.