Closed YashdalfTheGray closed 9 years ago
Hmm I use Mongo with little to no problems and I don't think I need promises. This would be a better conversation in person ugh, but alas.
Maybe you can explain what you need and I can share my experiences of Mongo from that. I think I accomplish everything I need via callback functions. I'd have to look at what I've implemented though. On Jul 7, 2015 10:11 PM, "Yash" notifications@github.com wrote:
So I started implementing Pouch (all gung ho and shit) and I realized that not only do you need a specific version of python to get leveldown (which pouchdb-server depends on) installed but you also have to have a specific version of this and that and this other Microsoft Visual C++ Express x64 2010 Redistributable package installer file setup bullshit...I digress. Anyway, I started looking into mongo but it doesn't seem like they have promise support which makes me a sad panda.
So I'm officially looking for suggestions on what backend to use other than firebase.
P.S.: I know you could wrap the async calls from MongoDB and return promises but that's...yucky.
— Reply to this email directly or view it on GitHub https://github.com/YashdalfTheGray/flintandsteel/issues/71.
So the normal way to handle async in JS has been callbacks and you can easily get into this callback hell situation where you're passing a method to another method which in turn is passed into another method and I don't like that. The new way to handle it (and what's going to become standard come ES6 or maybe ES7) are promises.
The difference is syntactic sugar but it makes it way more human readable.
This is how you write a promise (using angular's $q but most promise implementations work this way),
function asyncMethod () {
var def = $q.defer();
// perform some async stuff here
if (asyncResponse === 'OK') {
def.resolve(/*whatever data you want to pass back*/);
}
else {
def.reject(/*pass error because things didn't go well*/);
}
return def.promise;
}
// And then you call it like this,
asyncMethod().then(function(result) {
// handle the good case here
}).catch(function(error) {
// handle the error case here
});
Ok, sure. I've used promises before. I guess I just didn't think we'd be nesting so many calls within a DB related call. I figured we'd receive a request, call a DB related operation, get our result, pass it to the callback, and send it back to the requester.
Node also has Q which can help, though I know you didn't want to wrap promises around the async calls.
Is there a node driver for Mongo that supports promises? I know MongoDB has a native driver, then there's Mongoose. Not sure if there's another or if one of those supports promises naively.
I've only used Mongo though, so I have no suggestions off the top of my head beyond that. On Jul 7, 2015 10:28 PM, "Yash" notifications@github.com wrote:
So the normal way to handle async in JS has been callbacks and you can easily get into this callback hell situation where you're passing a method to another method which in turn is passed into another method and I don't like that. The new way to handle it (and what's going to become standard come ES6 or maybe ES7) are promises.
The difference is syntactic sugar but it makes it way more human readable.
This is how you write a promise (using angular's $q but most promise implementations work this way),
function asyncMethod () { var def = $q.defer(); // perform some async stuff here if (asyncResponse === 'OK') { def.resolve(/whatever data you want to pass back/); } else { def.reject(/pass error because things didn't go well/); } return def.promise; } // And then you call it like this, asyncMethod().then(function(result) { // handle the good case here }).catch(function(error) { // handle the error case here });
— Reply to this email directly or view it on GitHub https://github.com/YashdalfTheGray/flintandsteel/issues/71#issuecomment-119401534 .
Yeah, but I figured since it's about to become standard, might as well avoid old "best practices" and go with the new patterns. $q is being used in production so at least I know that it's not half-baked or anything.
I'll keep looking for something that uses mongo but has those async calls wrapped in promises. I might also see how much of an install step CouchDb is and see if I can set that up instead.
I messed with this for like 15 minutes a few weeks ago so take that with a grain of salt.
http://redis.io/ On Jul 7, 2015 10:37 PM, "Yash" notifications@github.com wrote:
Yeah, but I figured since it's about to become standard, might as well avoid old "best practices" and go with the new patterns. $q is being used in production so at least I know that it's not half-baked or anything.
I'll keep looking for something that uses mongo but has those async calls wrapped in promises. I might also see how much of an install step CouchDb is and see if I can set that up instead.
— Reply to this email directly or view it on GitHub https://github.com/YashdalfTheGray/flintandsteel/issues/71#issuecomment-119402635 .
Redis, forgot about redis. I'll look at this too.
mongodb@2.0.36 node_modules\mongodb ├── es6-promise@2.1.1 ├── readable-stream@1.0.31 (string_decoder@0.10.31, isarray@0.0.1, inherits@2.0.1, core-util-is@1.0.1) └── mongodb-core@1.2.4 (bson@0.4.7, kerberos@0.0.12)
FYI, Looks like the mongodb driver has es6-promise as a dependency
Fux yeah! Well, that pretty much seals that deal. Any pitfalls that I should know about?
Yash Kulshrestha yash.kulshrestha@gmail.com (614)563-1153 Sent from my Nexus 6 On Jul 9, 2015 8:25 PM, "Mctalian" notifications@github.com wrote:
mongodb@2.0.36 node_modules\mongodb ├── es6-promise@2.1.1 ├── readable-stream@1.0.31 (string_decoder@0.10.31, isarray@0.0.1, inherits@2.0.1, core-util-is@1.0.1) └── mongodb-core@1.2.4 (bson@0.4.7, kerberos@0.0.12)
FYI, Looks like the mongodb driver has es6-promise as a dependency
— Reply to this email directly or view it on GitHub https://github.com/YashdalfTheGray/flintandsteel/issues/71#issuecomment-120193820 .
Not sure, just starting to mess with it myself. I was using the MongoLabs web API for a while, but I'm trying to shift to using a driver.
But just because it uses es6-promise as a dependency doesn't necessarily mean that it returns a promise right? I guess a little more research is needed but at least now there's a place to start.
Eh... sorry for the excitement-
Check out the comments to this blog post:
https://www.mongodb.com/blog/post/introducing-nodejs-mongodb-20-driver
You're right, but something is promise based in Mongo and we just have to hope that they expose it somehow.
Yash Kulshrestha yash.kulshrestha@gmail.com (614)563-1153 Sent from my Nexus 6 On Jul 9, 2015 8:32 PM, "Mctalian" notifications@github.com wrote:
Not sure, just starting to mess with it myself. I was using the MongoLabs web API for a while, but I'm trying to shift to using a driver.
But just because it uses es6-promise as a dependency doesn't necessarily mean that it returns a promise right? I guess a little more research is needed but at least now there's a place to start.
Eh... sorry for the excitement-
Check out the comments to this blog post:
https://www.mongodb.com/blog/post/introducing-nodejs-mongodb-20-driver
— Reply to this email directly or view it on GitHub https://github.com/YashdalfTheGray/flintandsteel/issues/71#issuecomment-120194939 .
So I just started to install mongodb on my windows computer and part of the npm install just failed. The server installs fine but I run into the same issue as pouch where the native parts of the mongodb driver don't compile right on Windows but they work fine on macs.
I think I'm going to just use the HTTP API through node.js. FUCK IT ALL!
@Mctalian, how do you interface with mongo?
I used to use the MongoLabs REST API, but they recommend against it in favor of the driver.
I am using the driver mongodb
version 2.0.36
In my db driver for MongoLabs, I do the following:
var mongodb = require('mongodb').MongoClient;
...
mongodb.connect(uri, function(err, db) {
if (err !== null) {
callback(err);
}
var collection = db.collection(collectionName);
collection.find({}).toArray(function(err,docs) {
if (err !== null) {
callback(err);
}
callback(null, docs);
});
});
I don't really remember if the NPM install completed successfully or not. But it obviously completed "successfully enough" because this code works. I just tested it last weekend and I was able to pull data from my MongoLabs databases.
I think, for now, we should use MongoDB. Matt and I were in a discussion at the end of the recap meeting today and we really need to start utilizing "branch by abstraction" where we have an interface for services (DB is a perfect example) and we can change out the implementation with ease. We can start with MongoDB to get something that "just works" for now and the "callback hell" will be limited to the MongoDB-specific implementation. If we find something that uses promises, we can create a specific implementation for that and use configuration information to choose the correct DB implementation.
So I started implementing Pouch (all gung ho and shit) and I realized that not only do you need a specific version of python to get leveldown (which pouchdb-server depends on) installed but you also have to have a specific version of this and that and this other Microsoft Visual C++ Express x64 2010 Redistributable package installer file setup bullshit...I digress. Anyway, I started looking into mongo but it doesn't seem like they have promise support which makes me a sad panda.
So I'm officially looking for suggestions on what backend to use other than firebase.
P.S.: I know you could wrap the async calls from MongoDB and return promises but that's...yucky.