-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.js
55 lines (47 loc) · 1.06 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
'use strict'
const MongoClient = require('mongodb')
class Database {
constructor (uri) {
this.uri = uri
this.db = {}
return this
}
connect () {
return new Promise((resolve, reject) => {
MongoClient.connect(this.uri, (err, db) => {
if (err) reject(err)
this.db = db
resolve(this)
})
})
}
addReport (domain) {
return new Promise((resolve, reject) => {
this.db.collection('domains').findAndModify(
{ domain: domain }
, {}
, { $inc: { reported: 1 } }
, { new: true, upsert: true }
, (err, data) => {
if (err) reject(err)
resolve(data)
})
})
}
findReport (domain) {
return new Promise((resolve, reject) => {
this.db.collection('domains').findOne(
{ domain: domain }
, { _id: false, reported: true }
, (err, data) => {
if (err) reject(err)
if (data) {
resolve(data.reported)
} else {
resolve(0)
}
})
})
}
}
module.exports = Database