mongo callback refactor

v1.18.x
barisusakli 11 years ago
parent 4f72513f19
commit 7b409c095c

@ -121,6 +121,13 @@
return value.toString(); return value.toString();
} }
function done(cb) {
return function(err, result) {
if (typeof cb === 'function') {
cb(err, result);
}
};
}
// //
// Exported functions // Exported functions
@ -158,19 +165,11 @@
}; };
module.searchRemove = function(key, id, callback) { module.searchRemove = function(key, id, callback) {
db.collection('search').remove({id:id, key:key}, function(err, result) { db.collection('search').remove({id:id, key:key}, done(callback));
if (typeof callback === 'function') {
callback(err);
}
});
}; };
module.flushdb = function(callback) { module.flushdb = function(callback) {
db.dropDatabase(function(err, result) { db.dropDatabase(done(callback));
if (typeof callback === 'function') {
callback(err);
}
});
}; };
module.info = function(callback) { module.info = function(callback) {
@ -198,11 +197,7 @@
}; };
module.delete = function(key, callback) { module.delete = function(key, callback) {
db.collection('objects').remove({_key:key}, function(err, result) { db.collection('objects').remove({_key:key}, done(callback));
if(typeof callback === 'function') {
callback(err, result);
}
});
}; };
module.get = function(key, callback) { module.get = function(key, callback) {
@ -215,11 +210,7 @@
}; };
module.rename = function(oldKey, newKey, callback) { module.rename = function(oldKey, newKey, callback) {
db.collection('objects').update({_key: oldKey}, {$set:{_key: newKey}}, function(err, result) { db.collection('objects').update({_key: oldKey}, {$set:{_key: newKey}}, done(callback));
if(typeof callback === 'function') {
callback(err, result);
}
});
}; };
module.expire = function(key, seconds, callback) { module.expire = function(key, seconds, callback) {
@ -233,11 +224,7 @@
//hashes //hashes
module.setObject = function(key, data, callback) { module.setObject = function(key, data, callback) {
data._key = key; data._key = key;
db.collection('objects').update({_key:key}, {$set:data}, {upsert:true, w: 1}, function(err, result) { db.collection('objects').update({_key:key}, {$set:data}, {upsert:true, w: 1}, done(callback));
if(typeof callback === 'function') {
callback(err, result);
}
});
}; };
module.setObjectField = function(key, field, value, callback) { module.setObjectField = function(key, field, value, callback) {
@ -272,11 +259,7 @@
module.getObjectField = function(key, field, callback) { module.getObjectField = function(key, field, callback) {
field = fieldToString(field); field = fieldToString(field);
module.getObjectFields(key, [field], function(err, data) { module.getObjectFields(key, [field], function(err, data) {
if(err) { callback(err, data ? data[field] : null);
return callback(err);
}
callback(null, data ? data[field] : null);
}); });
}; };
@ -338,15 +321,7 @@
module.getObjectKeys = function(key, callback) { module.getObjectKeys = function(key, callback) {
module.getObject(key, function(err, data) { module.getObject(key, function(err, data) {
if(err) { callback(err, data ? Object.keys(data) : []);
return callback(err);
}
if(data) {
callback(null, Object.keys(data));
} else {
callback(null, []);
}
}); });
}; };
@ -371,9 +346,6 @@
field = fieldToString(field); field = fieldToString(field);
data[field] = ''; data[field] = '';
db.collection('objects').findOne({_key:key}, {fields:data}, function(err, item) { db.collection('objects').findOne({_key:key}, {fields:data}, function(err, item) {
if(err) {
return callback(err);
}
callback(err, !!item && item[field] !== undefined && item[field] !== null); callback(err, !!item && item[field] !== undefined && item[field] !== null);
}); });
}; };
@ -382,11 +354,7 @@
var data = {}; var data = {};
field = fieldToString(field); field = fieldToString(field);
data[field] = ''; data[field] = '';
db.collection('objects').update({_key:key}, {$unset : data}, function(err, result) { db.collection('objects').update({_key:key}, {$unset : data}, done(callback));
if(typeof callback === 'function') {
callback(err, result);
}
});
}; };
module.incrObjectField = function(key, field, callback) { module.incrObjectField = function(key, field, callback) {
@ -421,11 +389,18 @@
array[index] = toString(element); array[index] = toString(element);
}); });
db.collection('objects').update({_key: key}, { $addToSet: { members: { $each: value } } }, { upsert: true, w: 1 }, function(err, result) { db.collection('objects').update({
if(typeof callback === 'function') { _key: key
callback(err, result); }, {
$addToSet: {
members: {
$each: value
}
} }
}); }, {
upsert: true,
w: 1
}, done(callback));
}; };
module.setRemove = function(key, value, callback) { module.setRemove = function(key, value, callback) {
@ -437,11 +412,7 @@
array[index] = toString(element); array[index] = toString(element);
}); });
db.collection('objects').update({_key: key}, {$pullAll: {members: value}}, function(err, result) { db.collection('objects').update({_key: key}, {$pullAll: {members: value}}, done(callback));
if(typeof callback === 'function') {
callback(err, result);
}
});
}; };
module.isSetMember = function(key, value, callback) { module.isSetMember = function(key, value, callback) {
@ -493,36 +464,21 @@
module.getSetMembers = function(key, callback) { module.getSetMembers = function(key, callback) {
db.collection('objects').findOne({_key:key}, {members:1}, function(err, data) { db.collection('objects').findOne({_key:key}, {members:1}, function(err, data) {
if (err) { callback(err, data ? data.members : []);
return callback(err);
}
callback(null, data ? data.members : []);
}); });
}; };
module.setCount = function(key, callback) { module.setCount = function(key, callback) {
db.collection('objects').findOne({_key:key}, function(err, data) { db.collection('objects').findOne({_key:key}, function(err, data) {
if (err) { return callback(err, data ? data.members.length : 0);
return callback(err);
}
return callback(null, data ? data.members.length : 0);
}); });
}; };
module.setRemoveRandom = function(key, callback) { module.setRemoveRandom = function(key, callback) {
db.collection('objects').findOne({_key:key}, function(err, data) { db.collection('objects').findOne({_key:key}, function(err, data) {
if(err) { if(err || !data) {
if(typeof callback === 'function') { if(typeof callback === 'function') {
callback(err); callback(err, 0);
}
return;
}
if(!data) {
if(typeof callback === 'function') {
callback(null, 0);
} }
return; return;
} }
@ -547,21 +503,13 @@
value: value value: value
}; };
db.collection('objects').update({_key:key, value:value}, {$set:data}, {upsert:true, w: 1}, function(err, result) { db.collection('objects').update({_key:key, value:value}, {$set:data}, {upsert:true, w: 1}, done(callback));
if(typeof callback === 'function') {
callback(err, result);
}
});
}; };
module.sortedSetRemove = function(key, value, callback) { module.sortedSetRemove = function(key, value, callback) {
value = toString(value); value = toString(value);
db.collection('objects').remove({_key:key, value:value}, function(err, result) { db.collection('objects').remove({_key:key, value:value}, done(callback));
if(typeof callback === 'function') {
callback(err, result);
}
});
}; };
function getSortedSetRange(key, start, stop, sort, callback) { function getSortedSetRange(key, start, stop, sort, callback) {
@ -570,12 +518,8 @@
.skip(start) .skip(start)
.sort({score: sort}) .sort({score: sort})
.toArray(function(err, data) { .toArray(function(err, data) {
if (err) { if (err || !data) {
return callback(err); return callback(err, null);
}
if (!data) {
return callback(null, null);
} }
data = data.map(function(item) { data = data.map(function(item) {
@ -695,17 +639,11 @@
if(err) { if(err) {
if(typeof callback === 'function') { if(typeof callback === 'function') {
return callback(err); return callback(err);
} else {
return winston.error(err.message);
} }
} }
if(exists) { if(exists) {
db.collection('objects').update({_key:key}, {'$set': {'array.-1': value}}, {upsert:true, w:1 }, function(err, result) { db.collection('objects').update({_key:key}, {'$set': {'array.-1': value}}, {upsert:true, w:1 }, done(callback));
if(typeof callback === 'function') {
callback(err, result);
}
});
} else { } else {
module.listAppend(key, value, callback); module.listAppend(key, value, callback);
} }
@ -714,11 +652,7 @@
module.listAppend = function(key, value, callback) { module.listAppend = function(key, value, callback) {
value = toString(value); value = toString(value);
db.collection('objects').update({ _key: key }, { $push: { array: value } }, {upsert:true, w:1}, function(err, result) { db.collection('objects').update({ _key: key }, { $push: { array: value } }, {upsert:true, w:1}, done(callback));
if(typeof callback === 'function') {
callback(err, result);
}
});
}; };
module.listRemoveLast = function(key, callback) { module.listRemoveLast = function(key, callback) {
@ -741,11 +675,7 @@
module.listRemoveAll = function(key, value, callback) { module.listRemoveAll = function(key, value, callback) {
value = toString(value); value = toString(value);
db.collection('objects').update({_key: key }, { $pull: { array: value } }, function(err, result) { db.collection('objects').update({_key: key }, { $pull: { array: value } }, done(callback));
if(typeof callback === 'function') {
callback(err, result);
}
});
}; };
module.getListRange = function(key, start, stop, callback) { module.getListRange = function(key, start, stop, callback) {
@ -763,12 +693,8 @@
} }
db.collection('objects').findOne({_key:key}, { array: { $slice: [skip, limit] }}, function(err, data) { db.collection('objects').findOne({_key:key}, { array: { $slice: [skip, limit] }}, function(err, data) {
if(err) { if(err || !(data && data.array)) {
return callback(err); return callback(err, []);
}
if(!(data && data.array)) {
return callback(null, []);
} }
if(splice) { if(splice) {

Loading…
Cancel
Save