refactor: extract database connection logic

This commit is contained in:
Paul Bienkowski 2020-11-28 19:21:58 +01:00
parent ba4634ab12
commit 734ff5a288
2 changed files with 14 additions and 11 deletions

13
src/db.js Normal file
View file

@ -0,0 +1,13 @@
const mongoose = require('mongoose');
const isProduction = process.env.NODE_ENV === 'production';
const mongodbUrl =
process.env.MONGODB_URL || (isProduction ? 'mongodb://localhost/obs' : 'mongodb://localhost/obsTest');
mongoose.connect(mongodbUrl);
mongoose.set('debug', !isProduction);
require('./models/TrackData');
require('./models/User');
require('./models/Track');
require('./models/Comment');
require('./config/passport');

View file

@ -4,7 +4,6 @@ const bodyParser = require('body-parser');
const session = require('express-session'); const session = require('express-session');
const cors = require('cors'); const cors = require('cors');
const errorhandler = require('errorhandler'); const errorhandler = require('errorhandler');
const mongoose = require('mongoose');
const auth = require('./routes/auth'); const auth = require('./routes/auth');
const isProduction = process.env.NODE_ENV === 'production'; const isProduction = process.env.NODE_ENV === 'production';
@ -30,16 +29,7 @@ if (!isProduction) {
app.use(errorhandler()); app.use(errorhandler());
} }
const mongodbUrl = require('./db');
process.env.MONGODB_URL || (isProduction ? 'mongodb://localhost/obs' : 'mongodb://localhost/obsTest');
mongoose.connect(mongodbUrl);
mongoose.set('debug', !isProduction);
require('./models/TrackData');
require('./models/User');
require('./models/Track');
require('./models/Comment');
require('./config/passport');
app.use(require('./routes')); app.use(require('./routes'));