forked from AmbireTech/adex-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
29 lines (23 loc) · 766 Bytes
/
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
const { MongoClient } = require('mongodb')
const redis = require('redis')
const url = process.env.DB_MONGO_URL || 'mongodb://localhost:27017'
const dbName = process.env.DB_MONGO_NAME || 'adexValidator'
const redisUrl = process.env.REDIS_URL || 'redis://127.0.0.1:6379'
let mongoClient = null
let redisClient = null
function connect() {
return MongoClient.connect(url, { useNewUrlParser: true }).then(function(client) {
mongoClient = client
})
}
function getMongo() {
if (mongoClient) return mongoClient.db(dbName)
throw new Error('db.connect() needs to be invoked before using getMongo()')
}
function getRedis() {
if (!redisClient) {
redisClient = redis.createClient(redisUrl)
}
return redisClient
}
module.exports = { connect, getMongo, getRedis }