-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
31 lines (27 loc) · 1.02 KB
/
db.js
File metadata and controls
31 lines (27 loc) · 1.02 KB
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
const MongoClient = require("mongodb").MongoClient;
const ObjectId = require("mongodb").ObjectID;
const CONNECTION_URL = process.env.ROE_DB_CONNECTION
const DATABASE_NAME = process.env.ROE_DB_NAME
const COLLECTIONS = process.env.ROE_COLLECTIONS
console.log('COLLECTIONS', COLLECTIONS)
const COLLECTION_LIST = []
module.exports = {
connect: () => {
init_colls = (db) => {
COLLECTIONS.split(':').map(coll => {
const c = { name: coll, ref: db.collection(coll) }
COLLECTION_LIST.push(c)
})
}
MongoClient.connect(CONNECTION_URL, { useNewUrlParser: true }, (error, client) => {
if(error) { throw error; }
database = client.db(DATABASE_NAME);
init_colls(database)
collection = database.collection(COLLECTION_LIST[0].name)
console.log("Connected to database: " + DATABASE_NAME);
})
},
coll: (name) => {
return COLLECTION_LIST.find(coll => { return coll.name === name } ).ref
}
}