Created
December 15, 2012 06:17
-
-
Save aponxi/4291691 to your computer and use it in GitHub Desktop.
Fixed async issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var DB = require('../../lib/db.js').DB; | |
function methods() { | |
//query object | |
var Q = {}; | |
//will act as our private variables to workaround asynchronous functions. | |
//will delete non-required ones when done -- we don't have to, but just for continuity. | |
var privates = {}; | |
//add tag to collection | |
Q.add = function(tag) { | |
if (typeof tag === "string") { | |
//maybe we are adding a tag by name | |
var obj = { | |
name: tag | |
}; | |
} else if (typeof tag === "object" && tag.name) { | |
//maybe the tag object was specified, and tag's name was provided | |
var obj = tag; | |
} | |
require('mongodb').connect(DB.mongo_url, function(err, db) { | |
db.collection('tags', function(err, coll) { | |
coll.insert(obj, { | |
safe: true | |
}, function(err, result) { | |
//console.log(result); | |
}); | |
}); | |
}); | |
} | |
var callback = { | |
_set: function(key, val) { | |
privates[key] = val; | |
//console.log(JSON.stringify(privates)); | |
}, | |
_get: function(key) { | |
//console.log("get: "+privates[key]); | |
//console.log(JSON.stringify(privates)); | |
return privates[key]; | |
}, | |
_unset: function(key) { | |
//delete privates[key]; | |
} | |
} | |
var if_exists = function(query, where, callback) { | |
require('mongodb').connect(DB.mongo_url, function(err, db) { | |
db.collection(where, function(err, coll) { | |
coll.findOne(query, function(e, r) { | |
//console.log(r); | |
if (r === null) { | |
callback(e, false); | |
//console.log("set : "+ callback._get("does_exist")); | |
} else { | |
callback(e, true); | |
//console.log("set : "+ callback._get("does_exist")); | |
} | |
}); | |
}); | |
//console.log("final set : "+ callback._get("does_exist")); | |
}); | |
//console.log("last set : "+ callback._get("does_exist")); | |
//var result = callback._get("does_exist"); | |
// delete privates.does_exist; | |
//return result; | |
} | |
Q.if_exists = if_exists; | |
/*Q.if_exists = function(query, where) { | |
var res; | |
if_exists(query, where, function(error, result) { | |
console.log('the result: '+result); | |
res = result; | |
}); | |
console.log('res '+res); | |
return res; | |
}*/ | |
return Q; | |
} | |
exports.query = methods(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var query = require('./query.js').query; | |
console.log("running unit tests..."); | |
console.log("add query test"); | |
query.add("os"); | |
query.add({ | |
name: "rhcp" | |
}); | |
console.log("exists tests:"); | |
var result = query.if_exists({ | |
name: "os" | |
}, "tags", function(error, result) { | |
console.log("should be true: " + result); | |
}); | |
var result = query.if_exists({ | |
name: "ossuruk" | |
}, "tags", function(error, result) { | |
console.log("should be false:" + result) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment