Git Product home page Git Product logo

Comments (8)

vadimdemedes avatar vadimdemedes commented on July 2, 2024

I don't really understand the problem here or how this callback would help. The whole point is to eliminate callbacks at a maximum level. Can you provide some specific example?

from mongorito.

tracker1 avatar tracker1 commented on July 2, 2024

Lets say I want to provide a myapp-data module... and expos a method ".getUser" that returns a promise. And I have a mongodb connection object already...

//elsewhere
Mongrito.models.User = ...;
...

//in my data library - returns a promise that will resolve a user.
exports.authoriseUser = function(username, password) {
  var invalidUsername = new Error("Invalid Username");
  invalidUsername.code = 403;

  var invalidPassword = new Error("Invalid Password");
  invalidPassword.code = 403;

  if (!username) return Promise.reject(invalidUsername);
  if (!password) return Promise.reject(invalidPassword);

  return config.getNamedMongoConnection('user-db')
    .then(function(connection){
      //connection should match an instance of mongodb, or a mongo-spec url string.
      return Mongrito.use(connection, function *(db){
        var User = db.models.User; //context-bound clone of Mongrito.models.User
        var user = yield Users.findOne({ username: username.trim().toLowerCase() });
        if (user == null) throw invalidUsername;
        var valid = yield matchPassword(user, password); //promise
        if (!valid) throw invalidPassword;
        return user;
      });
    });
}

In the above case if Mongrito.use follows co convention, it can resolve a promise against the final user, or the promise chain will reject with the thrown error.

This is much cleaner than having to wrap the alternatives... With a clearly defined scope.

from mongorito.

vadimdemedes avatar vadimdemedes commented on July 2, 2024

Still, Mongorito is intended to be used in ES6-only environments, like with Koa framework, which is based on generators at its core. From your example, I assume that you want to execute some code after connection to mongo is established. With Mongorito it does not matter, all operations are buffered and will be executed once connection is there. So after Mongorito.connect, just write the code that operates on db. Imagine, that Mongorito.connect is a synchronous method. Regarding re-using existing mongodb connection, I will consider this for future releases. Immediate note here, it will be possible to re-use existing monk instance, but not a connection from other libraries.

from mongorito.

tracker1 avatar tracker1 commented on July 2, 2024

What I mean is, I have two issues...

First: You aren't using mongodb-style URLs for your connection.

Second: You cannot use Mongorito with multiple databases..

For example, say I have my User information on mongo://foo:2700/users and say, some other collections on mongo://bar:2701/baz ... There's no way to specify which one to use, is there?

The part related to the specific request, is that it would be nice, if you could wrap the code in a promise, so that results can be yielded/resolved out..

What I would like... - no boilerplate code

MyDataLibrary.foo = function(input) {
  return Mongorito.use(connection, function *(db){
    // *** implicit open *** - with context-bound db object

    //use db / db.models.Foo

    return finalResult; // *** implicit close ***
  });
}

What I would need to do with the current library - 13 lines of extr boilerplate

MyDataLibrary.foo = function(input) {
  try {
    Mongorito.open(...);
  } catch(err) {
    return Promise.reject(err);
  }

  //co can wrap this part
  return co(function *(){
    //do stuff - can't have connection to db1 and db2 for different models
    return finalResult;
  })
  .then(function(result){
    Mongorito.close(); //close here
    return result; //re-extend result
  })
  .catch(function(err){
    Mongorito.close(); //close here too
    throw err; //rethrow error
  });
}

elsewhere

MyDataLibrary.foo(someinput).then(function(finalResult){
  //use final result
});

co@4 will return a promise, I was thinking it would be nicer if your library could be used that way as well... where the open/close is implicit, IE: I don't have to call them.

from mongorito.

vadimdemedes avatar vadimdemedes commented on July 2, 2024
First: You aren't using mongodb-style URLs for your connection.

Second: You cannot use Mongorito with multiple databases.

Now I understand your issues and yes, those are valid points. I will fix those in the future releases. As for the second problem, I came up with a solution different then you mentioned, but it will achieve the same thing. Thank you for suggestions.

from mongorito.

tracker1 avatar tracker1 commented on July 2, 2024

@vdemedes Thanks... I'm sorry if my request came off with frustration... I do appreciate the work that you have done, was just wanting to offer some observations that could help adopters use your library.

from mongorito.

vadimdemedes avatar vadimdemedes commented on July 2, 2024

Absolutely no problem, I just did not catch the idea at first. Moreover, I appreciate that you took the time to create an issue and tell me about your thoughts and suggestions.

from mongorito.

vadimdemedes avatar vadimdemedes commented on July 2, 2024

@tracker1 Update, which enables support for mongo:// urls and multiple database connections is pushed to GitHub and NPM under version 0.4.6. Thank you!

from mongorito.

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.