forked from InfoCompass/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect_db.mjs
29 lines (23 loc) · 909 Bytes
/
connect_db.mjs
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
import { default as mongodb } from 'mongodb'
const { MongoClient } = mongodb
export async function getLocalDB(host, port, name, user, pass){
if(!port) throw "getLocalDB: missing port"
if(!name) throw "getLocalDB: missing name"
if(!host) throw "getLocalDB: missing host"
if(!user) throw "getLocalDB: missing user"
if(!pass) throw "getLocalDB: missing pass"
const connectionString = `mongodb://${user}:${pass}@${host}:${port}/${name}`
const connectionStringWithPasswordHidden = `mongodb://${user}:********@${host}:${port}/${name}`
process.stdout.write(`Connecting to: ${connectionStringWithPasswordHidden} ...`)
return await MongoClient.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(
client => {
process.stdout.write('[ok] \n\n')
return client.db(name)
},
console.log
)
}