-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
31 lines (28 loc) · 875 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
30
31
const mongodb = require('mongodb');
async function initCollection(client, dbName, collectionName) {
try {
const collection = await client.db(dbName).collection(collectionName);
return collection;
} catch (err) {
console.error(`ERROR: could not initialize collection: ${err}`)
client.close();
process.exit(1);
}
}
async function initDatabase(data) {
const { dbURI, dbName, collectionName } = data;
try {
const client_options = {
maxPoolSize: 50,
wtimeoutMS: 2500
};
const client = await mongodb.MongoClient.connect(dbURI, client_options);
const collection = initCollection(client, dbName, collectionName);
if (collection) return collection;
else throw new Error();
} catch (err) {
console.error(`ERROR: could not connect to mongodb: ${err}`)
process.exit(1);
}
}
module.exports = initDatabase;