From 6e04535eff44c75863b547207e08edfe522af893 Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Wed, 29 Dec 2021 19:01:03 +0300 Subject: [PATCH 1/2] [NFC] Outline database connection into a separate file --- server.js | 14 ++++---------- server/db.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 server/db.js diff --git a/server.js b/server.js index 4c4cb6f63..7b87ae83e 100644 --- a/server.js +++ b/server.js @@ -77,16 +77,10 @@ const config = require('nconf') .file('environment', { file: `config/${process.env.NODE_ENV}.json` }) .file('defaults', { file: 'config/default.json' }); -//DB -const mongoose = require('mongoose'); -mongoose.connect(config.get('mongodb_uri') || config.get('mongolab_uri') || 'mongodb://localhost/naturalcrit', - { retryWrites: false }); -mongoose.connection.on('error', (err)=>{ - console.log('Error : Could not connect to a Mongo Database.'); - console.log(' If you are running locally, make sure mongodb.exe is running.'); - console.log(err); - throw 'Can not connect to Mongo'; -}); +// DB +const DB = require('./server/db.js'); +// FIXME: consider implementing error hanlding for a Promise returned by connect +DB.connect(config); //Account Middleware app.use((req, res, next)=>{ diff --git a/server/db.js b/server/db.js new file mode 100644 index 000000000..7adf3a58f --- /dev/null +++ b/server/db.js @@ -0,0 +1,37 @@ +const Mongoose = require('mongoose'); + +const getMongoDBURL = (config)=>{ + return config.get('mongodb_uri') || + config.get('mongolab_uri') || + 'mongodb://localhost/homebrewery'; +}; + +const disconnect = ()=>{ + return Mongoose.close(); +}; + +const connect = (config)=>{ + const resolver = (resolve, reject)=>{ + Mongoose.connect(getMongoDBURL(config), + { retryWrites: false }, + (error)=>{ + if(error) { + console.error('Could not connect to a Mongo Database.'); + console.log(error); + console.error('If you are running locally, make sure mongodb.exe is running.'); + // FIXME: do we need to pass 'error' to 'reject'? + return reject(); + } + + return resolve(); + }); + + }; + + return new Promise(resolver); +}; + +module.exports = { + connect : connect, + disconnect : disconnect +}; From e84cd4fe8b589f687cd29746910864fe80bc57d3 Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Mon, 10 Jan 2022 19:25:36 +0300 Subject: [PATCH 2/2] Apply review comments Switched from raw promises to async/await. Outlined error handling function to reduce amount of nested code. Added comment about the new file intent. --- server.js | 1 - server/db.js | 40 ++++++++++++++++++++-------------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/server.js b/server.js index 7b87ae83e..3bdb77b92 100644 --- a/server.js +++ b/server.js @@ -79,7 +79,6 @@ const config = require('nconf') // DB const DB = require('./server/db.js'); -// FIXME: consider implementing error hanlding for a Promise returned by connect DB.connect(config); //Account Middleware diff --git a/server/db.js b/server/db.js index 7adf3a58f..030d7f61b 100644 --- a/server/db.js +++ b/server/db.js @@ -1,3 +1,10 @@ +// The main purpose of this file is to provide an interface for database +// connection. Even though the code is quite simple and basically a tiny +// wrapper around mongoose package, it works as single point where +// database setup/config is performed and the interface provided here can be +// reused by both the main application and all tests which require database +// connection. + const Mongoose = require('mongoose'); const getMongoDBURL = (config)=>{ @@ -6,29 +13,22 @@ const getMongoDBURL = (config)=>{ 'mongodb://localhost/homebrewery'; }; -const disconnect = ()=>{ - return Mongoose.close(); +const handleConnectionError = (error)=>{ + if(error) { + console.error('Could not connect to a Mongo database: \n'); + console.error(error); + console.error('\nIf you are running locally, make sure mongodb.exe is running and DB URL is configured properly'); + process.exit(1); // non-zero exit code to indicate an error + } }; -const connect = (config)=>{ - const resolver = (resolve, reject)=>{ - Mongoose.connect(getMongoDBURL(config), - { retryWrites: false }, - (error)=>{ - if(error) { - console.error('Could not connect to a Mongo Database.'); - console.log(error); - console.error('If you are running locally, make sure mongodb.exe is running.'); - // FIXME: do we need to pass 'error' to 'reject'? - return reject(); - } +const disconnect = async ()=>{ + return await Mongoose.disconnect(); +}; - return resolve(); - }); - - }; - - return new Promise(resolver); +const connect = async (config)=>{ + return await Mongoose.connect(getMongoDBURL(config), + { retryWrites: false }, handleConnectionError); }; module.exports = {