Giter Site home page Giter Site logo

Promises about hapi-mongo-models HOT 11 CLOSED

jedireza avatar jedireza commented on May 18, 2024 1
Promises

from hapi-mongo-models.

Comments (11)

jedireza avatar jedireza commented on May 18, 2024

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.

jardakotesovec avatar jardakotesovec commented on May 18, 2024

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.

jedireza avatar jedireza commented on May 18, 2024

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.

k-sheth avatar k-sheth commented on May 18, 2024

+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.

cyberwombat avatar cyberwombat commented on May 18, 2024

+1 - definitely the break or make feature for myself though I am consideirng promisyfing it until then

from hapi-mongo-models.

jedireza avatar jedireza commented on May 18, 2024

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.

SteffenGorenflo avatar SteffenGorenflo commented on May 18, 2024

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.

jedireza avatar jedireza commented on May 18, 2024

@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.

prashaantt avatar prashaantt commented on May 18, 2024

Just curious if there's been any internal progress on Promises support.

from hapi-mongo-models.

jedireza avatar jedireza commented on May 18, 2024

@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.

jedireza avatar jedireza commented on May 18, 2024

📣 Async/await support is on the menu now. Tracked via: #74 and help is very much appreciated.

from hapi-mongo-models.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.