-
Notifications
You must be signed in to change notification settings - Fork 0
/
message-store.js
47 lines (39 loc) · 1.41 KB
/
message-store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var mongo = require('mongodb');
var monk = require('monk');
var util = require('util');
var MessageStore = function (dbhost, dbport, dbname, dbusername, dbpassword) {
var self = this;
self.dbhost = dbhost;
self.dbport = dbport;
self.dbname = dbname;
self.dbusername = dbusername;
self.dbpassword = dbpassword;
self.dbcreds = self.dbusername ? (self.dbusername + ':' + self.dbpassword + '@') : '';
self.dbConnectionString = self.dbcreds + self.dbhost + ':' + self.dbport + '/' + self.dbname;
self.db = monk(self.dbConnectionString);
self.saveMessage = function(message) {
console.error("Saving: " + util.inspect(message));
var messages = self.db.get('messages');
if (message._id) {
return messages.updateById(message._id, message).then(function() { return message; });
} else {
return messages.insert(message);
}
};
self.getById = function(id) {
return self.db.get('messages').findById(id);
};
self.getPendingMessages = function () {
return self.db.get('messages').find({
$or: [
{ status: { $exists: false } },
{ status: 'Pending' }
]
});
};
self.getAllMessages = function () {
return self.db.get('messages').find({});
};
return self;
};
var exports = module.exports = MessageStore;