Comments (8)
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.
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.
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.
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.
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.
@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.
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.
@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)
- Bug with empty fields after using 'Model.embeds()'
- Bug when setting any field after using 'Model.embeds()'
- Bulk insert HOT 3
- How would I handle gridFS with mongorito? HOT 1
- Mass assigment (fillable & guarded fields) HOT 1
- Access document's fields without using .get/.set HOT 3
- outdated website information HOT 1
- How to use inherited(?) mquery findOneAndUpdate method HOT 3
- toJS method HOT 4
- Type definition for intellisense HOT 1
- TypeError: this.Model.query(...).then is not a function HOT 3
- Alterative to old middleware `create` hooks example HOT 1
- Database connect on hold
- support for hashed indexes, and compound indexes HOT 2
- found 1 low severity vulnerability HOT 1
- How to use findOneAndUpdate with option upsert : true ?
- Enormous difference between Github docs and Site docs HOT 1
- abandoned? HOT 1
- How to check the current state of the connection with the database?
- can update the internal mongodb driver
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 mongorito.