-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
52 lines (44 loc) · 991 Bytes
/
db.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
48
49
50
51
52
const mongoose = require("mongoose");
const settings = require("./settings").settings;
const winston = require("winston");
if (!settings.mongodb) {
return;
}
winston.cli();
mongoose.connect(settings.mongodb, function (err, res) {
if (err) {
winston.error('ERROR connecting to: ' + settings.mongodb + '. ' + err);
} else {
winston.info('Succeeded connected to: ' + settings.mongodb);
}
});
const messageSchema = new mongoose.Schema({
user: {
name: String,
id: Number
},
message: {
chat_id: String,
id: String,
text: String
},
timestamp: String
});
const Message = mongoose.model('Messages', messageSchema);
module.exports = {
getLogs(cb) {
Message.find({}).exec(function (err, result) {
cb(result);
});
},
clearLogs(cb) {
Message.remove({}, cb);
},
addLog(user, message, cb) {
dbmsg = new Message({
user: user,
message: message,
timestamp: new Date().toISOString()
}).save(cb);
}
};