Refactor the DB connection to be able to reuse

master
Mark Moffat 2019-06-08 17:19:40 +09:30
parent 211da71fd1
commit bc4a5ee71b
2 changed files with 52 additions and 24 deletions

27
app.js
View File

@ -7,13 +7,12 @@ const session = require('express-session');
const moment = require('moment'); const moment = require('moment');
const _ = require('lodash'); const _ = require('lodash');
const MongoStore = require('connect-mongodb-session')(session); const MongoStore = require('connect-mongodb-session')(session);
const MongoClient = require('mongodb').MongoClient;
const numeral = require('numeral'); const numeral = require('numeral');
const helmet = require('helmet'); const helmet = require('helmet');
const colors = require('colors'); const colors = require('colors');
const cron = require('node-cron'); const cron = require('node-cron');
const common = require('./lib/common'); const common = require('./lib/common');
const mongodbUri = require('mongodb-uri'); const { initDb } = require('./lib/db');
let handlebars = require('express-handlebars'); let handlebars = require('express-handlebars');
// Validate our settings schema // Validate our settings schema
@ -324,35 +323,15 @@ app.on('uncaughtException', (err) => {
process.exit(2); process.exit(2);
}); });
MongoClient.connect(config.databaseConnectionString, {}, (err, client) => { // MongoClient.connect(config.databaseConnectionString, {}, (err, client) => {
initDb(config.databaseConnectionString, (err, db) => {
// On connection error we display then exit // On connection error we display then exit
if(err){ if(err){
console.log(colors.red('Error connecting to MongoDB: ' + err)); console.log(colors.red('Error connecting to MongoDB: ' + err));
process.exit(2); process.exit(2);
} }
// select DB
const dbUriObj = mongodbUri.parse(config.databaseConnectionString);
let db;
// if in testing, set the testing DB
if(process.env.NODE_ENV === 'test'){
db = client.db('testingdb');
}else{
db = client.db(dbUriObj.database);
}
// setup the collections
db.users = db.collection('users');
db.products = db.collection('products');
db.orders = db.collection('orders');
db.pages = db.collection('pages');
db.menu = db.collection('menu');
db.customers = db.collection('customers');
db.cart = db.collection('cart');
db.sessions = db.collection('sessions');
// add db to app for routes // add db to app for routes
app.dbClient = client;
app.db = db; app.db = db;
app.config = config; app.config = config;
app.port = app.get('port'); app.port = app.get('port');

49
lib/db.js Normal file
View File

@ -0,0 +1,49 @@
const MongoClient = require('mongodb').MongoClient;
const mongodbUri = require('mongodb-uri');
let _db;
function initDb(dbUrl, callback){ // eslint-disable-line
if(_db){
console.warn('Trying to init DB again!');
return callback(null, _db);
}
MongoClient.connect(dbUrl, { useNewUrlParser: true }, connected);
function connected(err, client){
if(err){
return callback(err);
}
// select DB
const dbUriObj = mongodbUri.parse(dbUrl);
let db;
// if in testing, set the testing DB
if(process.env.NODE_ENV === 'test'){
db = client.db('testingdb');
}else{
db = client.db(dbUriObj.database);
}
// setup the collections
db.users = db.collection('users');
db.products = db.collection('products');
db.orders = db.collection('orders');
db.pages = db.collection('pages');
db.menu = db.collection('menu');
db.customers = db.collection('customers');
db.cart = db.collection('cart');
db.sessions = db.collection('sessions');
_db = db;
return callback(null, _db);
}
};
function getDb(){
return _db;
}
module.exports = {
getDb,
initDb
};