node-auth/api/src/db.js

20 lines
483 B
JavaScript
Raw Normal View History

2021-03-03 19:26:05 +00:00
import mongo from 'mongodb'
const { MongoClient } = mongo
const url = process.env.MONGO_URL
export const client = new MongoClient(url, { useNetUrlParser: true })
export async function connectDb() {
try {
await client.connect()
// Confirm connection
await client.db("admin").command({ ping: 1 })
console.log("🗄️ Connected to DB Success");
} catch(e) {
console.error(e)
// If there is a problem close connection to db
await client.close()
}
}