-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (38 loc) · 1.04 KB
/
index.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
const MongoClient = require('mongodb').MongoClient
let MONGODB_USER = process.env['MONGODB_USER']
let MONGODB_PASSWORD = process.env['MONGODB_PASSWORD']
let MONGODB_HOST = process.env['MONGODB_HOST']
let clientOptions = {
keepAlive: true,
connectTimeoutMS: 5000,
reconnectTries: 3,
reconnectInterval: 1000,
useNewUrlParser: true,
}
function connectionUrl() {
return `mongodb+srv://${MONGODB_USER}:${MONGODB_PASSWORD}@${MONGODB_HOST}/admin`
}
async function connect() {
const connectUrl = connectionUrl()
console.log('Connecting... ')
try {
const client = await MongoClient.connect(
connectUrl,
clientOptions
)
const db = client.db('moztechspeakers')
return db
}
catch (e) {
console.log(e)
throw e
}
}
function configure(cfg) {
if ('MONGODB_USER' in cfg) MONGODB_USER = cfg['MONGODB_USER']
if ('MONGODB_PASSWORD' in cfg) MONGODB_PASSWORD = cfg['MONGODB_PASSWORD']
if ('MONGODB_HOST' in cfg) MONGODB_HOST = cfg['MONGODB_HOST']
}
module.exports = {
connect, connectionUrl, configure,
}