Add helper to check the existence of a document

This commit is contained in:
Blade Barringer
2015-10-31 10:55:28 -05:00
parent 6a65711c2f
commit 421b402948
2 changed files with 21 additions and 6 deletions

View File

@@ -41,6 +41,23 @@ export function translate(key, variables) {
return translatedString;
};
// Useful for checking things that have been deleted,
// but you no longer have access to,
// like private parties or users
export function checkExistence(collectionName, id) {
return new Promise((resolve, reject) => {
mongo.connect('mongodb://localhost/habitrpg_test', (err, db) => {
if (err) return reject(err);
let collection = db.collection(collectionName);
collection.find({_id: id}, {_id: 1}).limit(1).toArray((err, docs) => {
let exists = docs.length > 0;
db.close();
resolve(exists);
});
});
});
}
// Creates a new user and returns it
// If you need the user to have specific requirements,
// such as a balance > 0, just pass in the adjustment