check keys

v1.18.x
barisusakli 10 years ago
parent 0914004e12
commit 5f2a66295c

@ -7,6 +7,9 @@ module.exports = function(db, module) {
module.setObject = function(key, data, callback) {
callback = callback || helpers.noop;
if (!key) {
return callback();
}
data._key = key;
db.collection('objects').update({_key:key}, {$set:data}, {upsert:true, w: 1}, function(err) {
callback(err);
@ -21,10 +24,16 @@ module.exports = function(db, module) {
};
module.getObject = function(key, callback) {
db.collection('objects').findOne({_key:key}, {_id:0, _key:0}, callback);
if (!key) {
return callback();
}
db.collection('objects').findOne({_key: key}, {_id:0, _key:0}, callback);
};
module.getObjects = function(keys, callback) {
if (!Array.isArray(keys) || !keys.length) {
return callback();
}
db.collection('objects').find({_key: {$in: keys}}, {_id: 0}).toArray(function(err, data) {
if(err) {
return callback(err);
@ -42,19 +51,52 @@ module.exports = function(db, module) {
};
module.getObjectField = function(key, field, callback) {
if (!key) {
return callback();
}
field = helpers.fieldToString(field);
module.getObjectFields(key, [field], function(err, data) {
callback(err, data ? data[field] : null);
var _fields = {
_id: 0
};
_fields[field] = 1;
db.collection('objects').findOne({_key: key}, _fields, function(err, item) {
if (err || !item) {
return callback(err, null);
}
callback(null, item[field] || null);
});
};
module.getObjectFields = function(key, fields, callback) {
module.getObjectsFields([key], fields, function(err, items) {
callback(err, items ? items[0] : null);
if (!key) {
return callback();
}
var _fields = {
_id: 0
};
for(var i=0; i<fields.length; ++i) {
fields[i] = helpers.fieldToString(fields[i]);
_fields[fields[i]] = 1;
}
db.collection('objects').findOne({_key: key}, _fields, function(err, item) {
if (err) {
return callback(err);
}
item = item || {};
var result = {};
for(i=0; i<fields.length; ++i) {
result[fields[i]] = item[fields[i]] || null;
}
callback(null, result);
});
};
module.getObjectsFields = function(keys, fields, callback) {
if (!Array.isArray(keys) || !keys.length) {
return callback(null, []);
}
var _fields = {
_id: 0,
_key: 1
@ -80,10 +122,10 @@ module.exports = function(db, module) {
item;
for (var i=0; i<keys.length; ++i) {
var item = map[keys[i]] || {};
item = map[keys[i]] || {};
for (var k=0; k<fields.length; ++k) {
if (item[fields[k]] === null || item[fields[k]] === undefined) {
if (item[fields[k]] === undefined) {
item[fields[k]] = null;
}
}
@ -117,20 +159,26 @@ module.exports = function(db, module) {
};
module.isObjectField = function(key, field, callback) {
if (!key) {
return callback();
}
var data = {};
field = helpers.fieldToString(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) {
callback(err, !!item && item[field] !== undefined && item[field] !== null);
});
};
module.deleteObjectField = function(key, field, callback) {
callback = callback || helpers.noop;
if (!key) {
return callback();
}
var data = {};
field = helpers.fieldToString(field);
data[field] = '';
db.collection('objects').update({_key:key}, {$unset : data}, callback);
db.collection('objects').update({_key: key}, {$unset : data}, callback);
};
module.incrObjectField = function(key, field, callback) {
@ -142,12 +190,15 @@ module.exports = function(db, module) {
};
module.incrObjectFieldBy = function(key, field, value, callback) {
callback = callback || function() {};
callback = callback || helpers.noop;
if (!key) {
return callback();
}
var data = {};
field = helpers.fieldToString(field);
data[field] = value;
db.collection('objects').findAndModify({_key:key}, {}, {$inc: data}, {new:true, upsert:true}, function(err, result) {
db.collection('objects').findAndModify({_key: key}, {}, {$inc: data}, {new:true, upsert:true}, function(err, result) {
callback(err, result ? result[field] : null);
});
};

@ -4,7 +4,12 @@ module.exports = function(db, module) {
var helpers = module.helpers.mongo;
module.listPrepend = function(key, value, callback) {
callback = callback || function() {};
callback = callback || helpers.noop;
if (!key) {
return callback();
}
value = helpers.valueToString(value);
module.isObjectField(key, 'array', function(err, exists) {
@ -22,12 +27,18 @@ module.exports = function(db, module) {
module.listAppend = function(key, value, callback) {
callback = callback || helpers.noop;
if (!key) {
return callback();
}
value = helpers.valueToString(value);
db.collection('objects').update({ _key: key }, { $push: { array: value } }, {upsert:true, w:1}, callback);
};
module.listRemoveLast = function(key, callback) {
callback = callback || function() {};
callback = callback || helpers.noop;
if (!key) {
return callback();
}
module.getListRange(key, -1, 0, function(err, value) {
if (err) {
return callback(err);
@ -40,7 +51,10 @@ module.exports = function(db, module) {
};
module.listTrim = function(key, start, stop, callback) {
callback = callback || function() {};
callback = callback || helpers.noop;
if (!key) {
return callback();
}
module.getListRange(key, start, stop, function(err, value) {
if (err) {
return callback(err);
@ -52,13 +66,18 @@ module.exports = function(db, module) {
module.listRemoveAll = function(key, value, callback) {
callback = callback || helpers.noop;
if (!key) {
return callback();
}
value = helpers.valueToString(value);
db.collection('objects').update({_key: key }, { $pull: { array: value } }, callback);
};
module.getListRange = function(key, start, stop, callback) {
if (!key) {
return callback();
}
var skip = start,
limit = stop - start + 1,
splice = false;

@ -68,32 +68,51 @@ module.exports = function(db, module) {
};
module.exists = function(key, callback) {
db.collection('objects').findOne({_key:key}, function(err, item) {
if (!key) {
return callback();
}
db.collection('objects').findOne({_key: key}, function(err, item) {
callback(err, item !== undefined && item !== null);
});
};
module.delete = function(key, callback) {
callback = callback || helpers.noop;
db.collection('objects').remove({_key:key}, callback);
if (!key) {
return callback();
}
db.collection('objects').remove({_key: key}, callback);
};
module.deleteAll = function(keys, callback) {
callback = callback || helpers.noop;
if (!Array.isArray(keys) || !keys.length) {
return callback();
}
db.collection('objects').remove({_key: {$in: keys}}, callback);
};
module.get = function(key, callback) {
if (!key) {
return callback();
}
module.getObjectField(key, 'value', callback);
};
module.set = function(key, value, callback) {
var data = {value:value};
callback = callback || helpers.noop;
if (!key) {
return callback();
}
var data = {value: value};
module.setObject(key, data, callback);
};
module.increment = function(key, callback) {
callback = callback || helpers.noop;
if (!key) {
return callback();
}
db.collection('objects').update({_key: key}, { $inc: { value: 1 } }, callback);
};

@ -94,16 +94,21 @@ module.exports = function(db, module) {
});
};
module.isSetMember = function(key, value, callback) {
if (!key) {
return callback();
}
value = helpers.valueToString(value);
db.collection('objects').findOne({_key:key, members: value}, {_id: 0, members: 0},function(err, item) {
db.collection('objects').findOne({_key: key, members: value}, {_id: 0, members: 0},function(err, item) {
callback(err, item !== null && item !== undefined);
});
};
module.isSetMembers = function(key, values, callback) {
if (!key) {
return callback();
}
for (var i=0; i<values.length; ++i) {
values[i] = helpers.valueToString(values[i]);
}
@ -122,11 +127,13 @@ module.exports = function(db, module) {
};
module.isMemberOfSets = function(sets, value, callback) {
if (!Array.isArray(sets) || !sets.length) {
return callback();
}
value = helpers.valueToString(value);
db.collection('objects').find({_key: {$in : sets}, members: value}, {_id:0, members: 0}).toArray(function(err, result) {
if(err) {
if (err) {
return callback(err);
}
@ -143,12 +150,18 @@ module.exports = function(db, module) {
};
module.getSetMembers = function(key, callback) {
if (!key) {
return callback();
}
db.collection('objects').findOne({_key: key}, {members: 1}, {_id: 0, _key: 0}, function(err, data) {
callback(err, data ? data.members : []);
});
};
module.getSetsMembers = function(keys, callback) {
if (!Array.isArray(keys) || !keys.length) {
return callback();
}
db.collection('objects').find({_key: {$in: keys}}, {_id: 0, _key: 1, members: 1}).toArray(function(err, data) {
if (err) {
return callback(err);
@ -168,6 +181,9 @@ module.exports = function(db, module) {
};
module.setCount = function(key, callback) {
if (!key) {
return callback(null, 0);
}
db.collection('objects').findOne({_key: key}, {_id: 0}, function(err, data) {
return callback(err, data ? data.members.length : 0);
});

@ -7,6 +7,9 @@ module.exports = function(db, module) {
module.sortedSetAdd = function(key, score, value, callback) {
callback = callback || helpers.noop;
if (!key) {
return callback();
}
if (Array.isArray(score) && Array.isArray(value)) {
return sortedSetAddBulk(key, score, value, callback);
}
@ -42,7 +45,9 @@ module.exports = function(db, module) {
module.sortedSetsAdd = function(keys, score, value, callback) {
callback = callback || helpers.noop;
if (!Array.isArray(keys) || !keys.length) {
return callback();
}
value = helpers.valueToString(value);
var data = {
score: parseInt(score, 10),
@ -62,6 +67,9 @@ module.exports = function(db, module) {
module.sortedSetRemove = function(key, value, callback) {
callback = callback || helpers.noop;
if (!key) {
return callback();
}
if (!Array.isArray(value)) {
value = [value];
}
@ -74,6 +82,9 @@ module.exports = function(db, module) {
module.sortedSetsRemove = function(keys, value, callback) {
callback = callback || helpers.noop;
if (!Array.isArray(keys) || !keys.length) {
return callback();
}
value = helpers.valueToString(value);
db.collection('objects').remove({_key: {$in: keys}, value: value}, callback);
@ -81,12 +92,18 @@ module.exports = function(db, module) {
module.sortedSetsRemoveRangeByScore = function(keys, min, max, callback) {
callback = callback || helpers.noop;
if (!Array.isArray(keys) || !keys.length) {
return callback();
}
db.collection('objects').remove({_key: {$in: keys}, score: {$lte: max, $gte: min}}, function(err) {
callback(err);
});
};
function getSortedSetRange(key, start, stop, sort, withScores, callback) {
if (!key) {
return callback();
}
db.collection('objects').find({_key:key}, {fields: {_id: 0, value: 1, score: 1}})
.limit(stop - start + 1)
.skip(start)
@ -127,6 +144,9 @@ module.exports = function(db, module) {
};
function getSortedSetRangeByScore(key, start, count, min, max, sort, callback) {
if (!key) {
return callback();
}
if(parseInt(count, 10) === -1) {
count = 0;
}
@ -157,19 +177,28 @@ module.exports = function(db, module) {
}
module.sortedSetCount = function(key, min, max, callback) {
db.collection('objects').count({_key:key, score: {$gte:min, $lte:max}}, function(err, count) {
if (!key) {
return callback();
}
db.collection('objects').count({_key: key, score: {$gte: min, $lte: max}}, function(err, count) {
callback(err, count ? count : 0);
});
};
module.sortedSetCard = function(key, callback) {
db.collection('objects').count({_key:key}, function(err, count) {
if (!key) {
return callback();
}
db.collection('objects').count({_key: key}, function(err, count) {
count = parseInt(count, 10);
callback(err, count ? count : 0);
});
};
module.sortedSetsCard = function(keys, callback) {
if (!Array.isArray(keys) || !keys.length) {
return callback();
}
var pipeline = [
{ $match : { _key : { $in: keys } } } ,
{ $group: { _id: {_key: '$_key'}, count: { $sum: 1 } } },
@ -207,6 +236,9 @@ module.exports = function(db, module) {
};
function getSortedSetRank(method, key, value, callback) {
if (!key) {
return callback();
}
value = helpers.valueToString(value);
method(key, 0, -1, function(err, result) {
if(err) {
@ -219,6 +251,9 @@ module.exports = function(db, module) {
}
module.sortedSetsRanks = function(keys, values, callback) {
if (!Array.isArray(keys) || !keys.length) {
return callback(null, []);
}
var data = new Array(values.length);
for (var i=0; i<values.length; ++i) {
data[i] = {key: keys[i], value: values[i]};
@ -230,13 +265,19 @@ module.exports = function(db, module) {
};
module.sortedSetScore = function(key, value, callback) {
if (!key) {
return callback();
}
value = helpers.valueToString(value);
db.collection('objects').findOne({_key:key, value: value}, {fields:{score:1}}, function(err, result) {
db.collection('objects').findOne({_key:key, value: value}, {fields:{score: 1}}, function(err, result) {
callback(err, result ? result.score : null);
});
};
module.sortedSetsScore = function(keys, value, callback) {
if (!Array.isArray(keys) || !keys.length) {
return callback();
}
value = helpers.valueToString(value);
db.collection('objects').find({_key:{$in:keys}, value: value}).toArray(function(err, result) {
if (err) {
@ -257,6 +298,9 @@ module.exports = function(db, module) {
};
module.sortedSetScores = function(key, values, callback) {
if (!key) {
return callback();
}
values = values.map(helpers.valueToString);
db.collection('objects').find({_key: key, value: {$in: values}}).toArray(function(err, result) {
if (err) {
@ -287,6 +331,9 @@ module.exports = function(db, module) {
};
module.isSortedSetMembers = function(key, values, callback) {
if (!key) {
return callback();
}
values = values.map(helpers.valueToString);
db.collection('objects').find({_key: key, value: {$in: values}}).toArray(function(err, results) {
if (err) {
@ -313,6 +360,9 @@ module.exports = function(db, module) {
};
function getSortedSetUnion(sets, sort, start, stop, callback) {
if (!Array.isArray(sets) || !sets.length) {
return callback();
}
var limit = stop - start + 1;
if (limit <= 0) {
limit = 0;

@ -527,6 +527,9 @@ var async = require('async'),
};
Posts.getPostIndices = function(posts, uid, callback) {
if (!Array.isArray(posts) || !posts.length) {
return callback(null, []);
}
user.getSettings(uid, function(err, settings) {
if (err) {
return callback(err);

Loading…
Cancel
Save