0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 16:22:44 +00:00

[NFC] Outline database connection into a separate file

This commit is contained in:
Alexey Sachkov
2021-12-29 19:01:03 +03:00
parent 7b19bbb1a7
commit 6e04535eff
2 changed files with 41 additions and 10 deletions

37
server/db.js Normal file
View File

@@ -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
};