-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDB.js
67 lines (61 loc) · 1.79 KB
/
MongoDB.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
67
const MongoClient = require('mongodb').MongoClient;
const dotenv = require('dotenv').config()
let DEBUG_MODE = false;
const winston = require('winston');
// Logging
const loggerOptions = {
file: {
level: 'debug',
filename: `./index.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 2,
colorize: false,
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
}
};
const logger = winston.createLogger({transports: [new winston.transports.File(loggerOptions.file), new winston.transports.Console(loggerOptions.console)], exitOnError: false});
if (!process.env.ENVIRONMENT) { logger.error("No ENV file, quitting."); process.exit(); }
if (process.env.ENVIRONMENT == 'dev' || process.env.ENVIRONMENT == 'test') DEBUG_MODE = true;
class MongoDB {
/* this.client
this.db
*/
constructor(url, dbName) {
this.url = url;
this.client = new MongoClient(url, { useUnifiedTopology: true });
this.dbName = dbName;
}
connect() {
if (this.isConnected()) { return; };
DEBUG_MODE && logger.debug("MongoDB connect()");
this.client.connect((err, db) => {
if (err) {
logger.error('MongoClient.connect() error: ' + err);
}
this.db = this.client.db(this.dbName);
});
}
close() {
this.client.close();
}
isConnected() {
return !!this.client && !!this.client.topology && this.client.topology.isConnected();
}
insertOne(collName, row) {
if (!this.isConnected()) { return "Not connected to Mongo."; };
DEBUG_MODE && logger.debug("MongoDB insertOne()");
this.db.collection(collName).insertOne(row, (err, res) => {
if (err) {
logger.error('insertOne() failed for '+collName+': ' + err);
}
});
}
}
module.exports = MongoDB;