-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.js
66 lines (55 loc) · 1.7 KB
/
database.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const MongoClient = require('mongodb').MongoClient;
const dbUrl = require('./db.js');
const config = require('./config.json');
const bole = require('bole');
const log = bole('dataBase');
bole.output({
level: config.logging,
stream: process.stdout,
});
function findUser(twitterId, success) {
MongoClient.connect(dbUrl, function (err, db) {
if (err) {
log.error(err);
throw err;
}
// We can look at the first result since there should only ever be one entry per user
db.collection('whispers').find({ "twitterId" : twitterId}).limit(1).next(function (err, doc) {
if (err) {
log.error(err);
throw err;
}
// Found a user.
success(doc);
// Close connection.
db.close();
});
});
}
function upsertUser(twitterId, success) {
MongoClient.connect(dbUrl, function (err, db) {
if (err) {
log.error(err);
throw err;
}
db.collection('whispers')
.updateOne({ "twitterId" : twitterId },
{
"twitterId" : twitterId,
"lastTweetDate" : new Date(),
},
{ upsert : true },
function (err, result) {
if (err) {
log.error(err);
throw err;
}
success();
db.close()
});
});
}
module.exports = {
findUser : findUser,
upsertUser : upsertUser,
}