node-auth/api/src/db.js
2021-04-25 13:17:50 -07:00

20 lines
No EOL
483 B
JavaScript

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()
}
}