mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-10 20:03:03 +00:00
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.
This commit is contained in:
@@ -79,7 +79,6 @@ const config = require('nconf')
|
|||||||
|
|
||||||
// DB
|
// DB
|
||||||
const DB = require('./server/db.js');
|
const DB = require('./server/db.js');
|
||||||
// FIXME: consider implementing error hanlding for a Promise returned by connect
|
|
||||||
DB.connect(config);
|
DB.connect(config);
|
||||||
|
|
||||||
//Account Middleware
|
//Account Middleware
|
||||||
|
|||||||
40
server/db.js
40
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 Mongoose = require('mongoose');
|
||||||
|
|
||||||
const getMongoDBURL = (config)=>{
|
const getMongoDBURL = (config)=>{
|
||||||
@@ -6,29 +13,22 @@ const getMongoDBURL = (config)=>{
|
|||||||
'mongodb://localhost/homebrewery';
|
'mongodb://localhost/homebrewery';
|
||||||
};
|
};
|
||||||
|
|
||||||
const disconnect = ()=>{
|
const handleConnectionError = (error)=>{
|
||||||
return Mongoose.close();
|
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 disconnect = async ()=>{
|
||||||
const resolver = (resolve, reject)=>{
|
return await Mongoose.disconnect();
|
||||||
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();
|
const connect = async (config)=>{
|
||||||
});
|
return await Mongoose.connect(getMongoDBURL(config),
|
||||||
|
{ retryWrites: false }, handleConnectionError);
|
||||||
};
|
|
||||||
|
|
||||||
return new Promise(resolver);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
Reference in New Issue
Block a user