-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
54 lines (39 loc) · 1.16 KB
/
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
53
54
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const uri = 'mongodb://localhost:27017/';
class MongoDB {
constructor() {
this._client = null;
}
connect() {
return new Promise((resolve, reject) => {
if (this._client) {
resolve();
} else {
MongoClient.connect(uri, { useNewUrlParser: true })
.then((client) => {
console.log('Connected successfully to server');
this._client = client;
resolve();
})
.catch((err) => {
console.log(`Error connecting: ${err.message}`);
reject(err.message);
});
}
});
}
disconnect() {
this._client && this._client.close();
}
insertOne(artist) {
return this.artists.insertOne(artist);
}
insertMany(artists) {
return this.artists.insertMany(artists);
}
get artists() {
return this._client.db('discogs').collection('artists');
}
}
module.exports = MongoDB;