Files
NodeBB/src/database/mongo/main.js

139 lines
3.5 KiB
JavaScript
Raw Normal View History

2014-04-11 15:44:53 -04:00
"use strict";
var winston = require('winston');
module.exports = function(db, module) {
var helpers = module.helpers.mongo;
2014-04-14 13:51:45 -04:00
2014-06-24 09:15:39 -04:00
module.searchIndex = function(key, content, id, callback) {
callback = callback || function() {};
2014-04-11 15:44:53 -04:00
var data = {
2014-06-24 09:15:39 -04:00
id: id,
key: key,
content: content
2014-04-11 15:44:53 -04:00
};
db.collection('search').update({key:key, id:id}, {$set:data}, {upsert:true, w: 1}, function(err) {
2014-04-11 15:44:53 -04:00
if(err) {
winston.error('Error indexing ' + err.message);
}
2014-06-24 09:15:39 -04:00
callback(err);
2014-04-11 15:44:53 -04:00
});
};
module.search = function(key, term, limit, callback) {
2014-07-21 14:18:42 -04:00
db.collection('search').find({ $text: { $search: term }, key: key}, {limit: limit}).toArray(function(err, results) {
2014-04-11 15:44:53 -04:00
if(err) {
return callback(err);
}
2014-07-21 14:18:42 -04:00
if(!results || !results.length) {
2014-04-11 15:44:53 -04:00
return callback(null, []);
}
2014-07-21 14:18:42 -04:00
var data = results.map(function(item) {
return item.id;
2014-04-11 15:44:53 -04:00
});
callback(null, data);
});
};
module.searchRemove = function(key, id, callback) {
2014-09-03 01:13:28 -04:00
callback = callback || helpers.noop;
db.collection('search').remove({key:key, id:id}, callback);
2014-04-11 15:44:53 -04:00
};
module.flushdb = function(callback) {
2014-09-03 01:13:28 -04:00
callback = callback || helpers.noop;
db.dropDatabase(callback);
2014-04-11 15:44:53 -04:00
};
module.info = function(callback) {
db.stats({scale:1024}, function(err, stats) {
if(err) {
return callback(err);
}
stats.avgObjSize = (stats.avgObjSize / 1024).toFixed(2);
2014-09-18 22:24:14 -04:00
stats.dataSize = (stats.dataSize / 1024).toFixed(2);
stats.storageSize = (stats.storageSize / 1024).toFixed(2);
stats.fileSize = (stats.fileSize / 1024).toFixed(2);
stats.indexSize = (stats.indexSize / 1024).toFixed(2);
2014-04-11 15:44:53 -04:00
stats.raw = JSON.stringify(stats, null, 4);
stats.mongo = true;
callback(null, stats);
});
};
module.exists = function(key, callback) {
2014-09-21 13:30:20 -04:00
if (!key) {
return callback();
}
db.collection('objects').findOne({_key: key}, function(err, item) {
2014-04-11 15:44:53 -04:00
callback(err, item !== undefined && item !== null);
});
};
module.delete = function(key, callback) {
2014-09-03 01:13:28 -04:00
callback = callback || helpers.noop;
2014-09-21 13:30:20 -04:00
if (!key) {
return callback();
}
db.collection('objects').remove({_key: key}, callback);
2014-04-11 15:44:53 -04:00
};
module.deleteAll = function(keys, callback) {
callback = callback || helpers.noop;
2014-09-21 13:30:20 -04:00
if (!Array.isArray(keys) || !keys.length) {
return callback();
}
db.collection('objects').remove({_key: {$in: keys}}, callback);
};
2014-04-11 15:44:53 -04:00
module.get = function(key, callback) {
2014-09-21 13:30:20 -04:00
if (!key) {
return callback();
}
2014-04-11 15:44:53 -04:00
module.getObjectField(key, 'value', callback);
};
module.set = function(key, value, callback) {
2014-09-21 13:30:20 -04:00
callback = callback || helpers.noop;
if (!key) {
return callback();
}
var data = {value: value};
2014-04-11 15:44:53 -04:00
module.setObject(key, data, callback);
};
2014-05-11 11:45:20 -04:00
module.increment = function(key, callback) {
2014-09-03 01:13:28 -04:00
callback = callback || helpers.noop;
2014-09-21 13:30:20 -04:00
if (!key) {
return callback();
}
2014-09-03 01:13:28 -04:00
db.collection('objects').update({_key: key}, { $inc: { value: 1 } }, callback);
2014-05-11 11:45:20 -04:00
};
2014-04-11 15:44:53 -04:00
module.rename = function(oldKey, newKey, callback) {
2014-09-03 01:13:28 -04:00
callback = callback || helpers.noop;
db.collection('objects').update({_key: oldKey}, {$set:{_key: newKey}}, callback);
2014-04-11 15:44:53 -04:00
};
module.expire = function(key, seconds, callback) {
module.expireAt(key, Math.round(Date.now() / 1000) + seconds, callback);
};
module.expireAt = function(key, timestamp, callback) {
module.setObjectField(key, 'expireAt', new Date(timestamp * 1000), callback);
};
module.pexpire = function(key, ms, callback) {
module.expireAt(key, Date.now() + parseInt(ms, 10), callback);
};
module.pexpireAt = function(key, timestamp, callback) {
module.setObjectField(key, 'expireAt', new Date(timestamp), callback);
};
2014-04-11 15:44:53 -04:00
};