I am trying to get one single mongodb connection shared with rest of the js scripts in the node.js application. I have been using the mongoose code try and work with sharing the connection but having trouble with it. The code below runs at the startup of the application. Then this script gets imported into other modules wherever it needs to work on the database.My code so far for database.js:require('dotenv').config();import {MongoClient as mongo} from 'mongodb';class Connection { constructor() { const MONGODB_URI = process.env.MONGODB_URI; console.log("Establish new connection with database."); mongo.connect(MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => { if (err) return console.log(err) console.log('Connected to database'); dbconnection = client.db('redone'); }) }}export default new Connection();
Submitted November 06, 2019 at 07:55PM by Redwood_11
No comments:
Post a Comment