more mongo stuff

v1.18.x
Baris Soner Usakli 11 years ago
parent 5c6a7d4b94
commit 0471a192ab

@ -55,25 +55,37 @@
// key // key
module.exists = function(key, callback) { module.exists = function(key, callback) {
module.isObjectField('global', key, callback); db.collection('objects').findOne({_key:key}, function(err, item) {
callback(err, item !== undefined && item !== null);
});
} }
module.delete = function(key, callback) { module.delete = function(key, callback) {
module.deleteObjectField('global', key, callback); db.collection('objects').remove({_key:key}, function(err, result) {
if(err) {
return callback(err);
}
if(result === 0) {
db.collection('objects').remove({setName:key}, function(err, result) {
callback(err, result);
});
} else {
callback(null, result);
}
});
} }
module.get = function(key, callback) { module.get = function(key, callback) {
module.getObjectField('global', key, callback); module.getObjectField(key, 'value', callback);
} }
module.set = function(key, value, callback) { module.set = function(key, value, callback) {
var data = {}; var data = {value:value};
data[key] = value; module.setObject(key, data, callback);
module.setObject('global', data, callback);
} }
module.keys = function(key, callback) { module.keys = function(key, callback) {
db.collection.find( { _key: { $regex: key /*, $options: 'i'*/ } } ); db.collection('objects').find( { _key: { $regex: key /*, $options: 'i'*/ } } );
} }
//hashes //hashes
@ -200,15 +212,25 @@
// sets // sets
module.setAdd = function(key, value, callback) { module.setAdd = function(key, value, callback) {
throw new Error('not-implemented');
var data = {
value:value
};
data.setName = key;
module.setObject(key + ':' + value, data, callback);
} }
module.setRemove = function(key, value, callback) { module.setRemove = function(key, value, callback) {
throw new Error('not-implemented'); db.collection('objects').remove({setName:key, value:value}, function(err, result) {
callback(err, result);
});
} }
module.isSetMember = function(key, value, callback) { module.isSetMember = function(key, value, callback) {
throw new Error('not-implemented'); db.collection('objects').findOne({setName:key, value:value}, function(err, item) {
callback(err, item !== null && item !== undefined);
});
} }
module.isMemberOfSets = function(sets, value, callback) { module.isMemberOfSets = function(sets, value, callback) {
@ -216,28 +238,51 @@
} }
module.getSetMembers = function(key, callback) { module.getSetMembers = function(key, callback) {
console.log('GETTING SET MEMBERS', key); db.collection('objects').find({setName:key}).toArray(function(err, data) {
db.collection('sets').findOne({_key:key}, function(err, data) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
if(!data) { if(!data) {
console.log('GOT SET MEMBERS', []);
callback(null, []); callback(null, []);
} else { } else {
console.log('GOT SET MEMBERS', data); data = data.map(function(item) {
return item.value;
});
callback(null, data); callback(null, data);
} }
}); });
} }
module.setCount = function(key, callback) { module.setCount = function(key, callback) {
throw new Error('not-implemented'); db.collection('objects').count({setName:key}, function(err, count) {
if(err) {
return callback(err);
}
if(!count) {
return callback(null, 0);
}
callback(null,count);
});
} }
module.setRemoveRandom = function(key, callback) { module.setRemoveRandom = function(key, callback) {
throw new Error('not-implemented'); db.collection('objects').find({setName:key}).toArray(function(err, data) {
if(err) {
return callback(err);
}
if(!data) {
callback(null, 0);
} else {
var randomIndex = Math.floor(Math.random() * data.length);
var item = data[randomIndex];
module.setRemove(item.setName, item.value, function(err, result) {
callback(err, item.value);
});
}
});
} }
@ -316,7 +361,16 @@
} }
module.sortedSetCount = function(key, min, max, callback) { module.sortedSetCount = function(key, min, max, callback) {
throw new Error('not-implemented'); db.collection('objects').count({setName:key, score: {$gt:min, $lt:max}}, function(err, count) {
if(err) {
return callback(err);
}
if(!count) {
return callback(null, 0);
}
callback(null,count);
});
} }
// lists // lists

@ -233,6 +233,43 @@ var DebugRoute = function(app) {
} }
function setAdd(callback) {
db.setAdd('myTestSet', 5, function(err, data) {
console.log('setAdd return', data);
callback(err, {'setAdd': data});
});
}
function setRemove(callback) {
db.setRemove('myTestSet', 11, function(err, data) {
console.log('setRemove return', data);
callback(err, {'setRemove': data});
});
}
function getSetMembers(callback) {
db.getSetMembers('myTestSet', function(err, data) {
console.log('getSetMembers return', data);
callback(err, {'getSetMembers': data});
});
}
function isSetMember(callback) {
db.isSetMember('myTestSet', 5, function(err, data) {
console.log('isSetMember return', data);
callback(err, {'isSetMember': data});
});
}
function setRemoveRandom(callback) {
db.setRemoveRandom('myTestSet', function(err, data) {
console.log('setRemoveRandom return', data);
callback(err, {'setRemoveRandom': data});
});
}
var objectTasks = [ var objectTasks = [
//createUser, //createUser,
getUser, getUser,
@ -273,11 +310,26 @@ var DebugRoute = function(app) {
get, get,
exists, exists,
deleteKey, deleteKey,
deleteKey,
get, get,
exists exists
]; ];
require('async').series(keyTasks, function(err, results) { var setTasks = [
getSetMembers,
// setRemove,
setAdd,
setAdd,
getSetMembers,
isSetMember,
// setRemove,
isSetMember,
getSetMembers,
setRemoveRandom,
getSetMembers
];
require('async').series(setTasks, function(err, results) {
if(err) { if(err) {
console.log(err); console.log(err);
res.send(err.message); res.send(err.message);

Loading…
Cancel
Save