From 734ff5a288dd79ae3bdcb122d506a62086439497 Mon Sep 17 00:00:00 2001 From: Paul Bienkowski Date: Sat, 28 Nov 2020 19:21:58 +0100 Subject: [PATCH] refactor: extract database connection logic --- src/db.js | 13 +++++++++++++ src/index.js | 12 +----------- 2 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 src/db.js diff --git a/src/db.js b/src/db.js new file mode 100644 index 0000000..aa6a93c --- /dev/null +++ b/src/db.js @@ -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'); diff --git a/src/index.js b/src/index.js index 36f6417..2bb124d 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,6 @@ const bodyParser = require('body-parser'); const session = require('express-session'); const cors = require('cors'); const errorhandler = require('errorhandler'); -const mongoose = require('mongoose'); const auth = require('./routes/auth'); const isProduction = process.env.NODE_ENV === 'production'; @@ -30,16 +29,7 @@ if (!isProduction) { app.use(errorhandler()); } -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'); +require('./db'); app.use(require('./routes'));