relocate and rename Storage.js. implement a simple, non-persistent in memory datastore for those who'd rather not bother with mongodb. Continue to default to previous values.

This commit is contained in:
ansuz
2015-10-25 19:35:25 -04:00
parent 05ce2695b2
commit 3928c89d35
5 changed files with 65 additions and 5 deletions

48
storage/amnesia.js Normal file
View File

@@ -0,0 +1,48 @@
console.log("Loading amnesiadb. This is a horrible idea in production, as data *will not* persist\n");
/*
As the comment says, this module does nothing to make your data persist
across sessions. If your process crashes for any reason, all pads will die.
This might be useful if you want to debug other parts of the codebase, if
you want to test out cryptpad without installing mongodb locally, or if
you don't want to rely on a remote db like the one at mongolab.com.
Maybe you just like the idea of a forgetful pad? To use this module, edit
config.js to include a directive `storage: './storage/amnesia'
Enjoy!
*/
var db=[],
index=0;
var insert = function(channelName, content, cb){
var val = {
id:index++,
chan: channelName,
msg: content,
time: new Date().getTime(),
};
db.push(val);
cb();
};
var getMessages = function(channelName, cb){
db.sort(function(a,b){
return a.id - b.id;
});
db.filter(function(val){
return val.chan == channelName;
}).forEach(function(doc){
console.log(doc);
cb(doc.msg);
});
};
module.exports.create = function(conf, cb){
cb({
message: insert,
getMessages: getMessages,
});
};

61
storage/mongo.js Normal file
View File

@@ -0,0 +1,61 @@
/*
* Copyright 2014 XWiki SAS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var MongoClient = require('mongodb').MongoClient;
var MONGO_URI = "mongodb://demo_user:demo_password@ds027769.mongolab.com:27769/demo_database";
var COLLECTION_NAME = 'cryptpad';
var insert = function (coll, channelName, content, cb) {
var val = {chan: channelName, msg:content, time: (new Date()).getTime()};
coll.insertOne(val, {}, function (err, r) {
console.log(r);
if (err || (r.insertedCount !== 1)) {
console.log('failed to insert ' + err);
return;
}
cb();
});
};
var getMessages = function (coll, channelName, cb) {
// find entries with a matching channelname
coll.find({chan:channelName})
// sort by _id, ascending
.sort( { _id: 1 } )
// iterate over entries
.forEach(function (doc) {
cb(doc.msg);
}, function (err) {
if (!err) { return; }
console.log('error ' + err);
});
};
module.exports.create = function (conf, cb) {
MongoClient.connect(conf.mongoUri, function(err, db) {
var coll = db.collection(conf.mongoCollectionName);
if (err) { throw err; }
cb({
message: function (channelName, content, cb) {
insert(coll, channelName, content, cb);
},
getMessages: function (channelName, msgHandler) {
getMessages(coll, channelName, msgHandler);
}
});
});
};