Monday, 1 April 2019

[Help] Auto connect to database?

Hi all. I was wondering how you manage your database connections especially when you're dealing with multiple files and entry points. For example I'm building a scraper that is scraping multiple websites however my code looks a little something like this right now.//connection.js mongoose.connect('mongodb://localhost:27017/myapp', {useNewUrlParser: true}); ​//ScraperOne.js require('./database/connnection'); const Site = require('./database/Site'); const SiteData = require('./database/SiteData'); // Do some scraping stuff and store it to the database ​//ScraperTwo.js require('./database/connnection'); const Site = require('./database/Site'); const SiteData = require('./database/SiteData'); // Do some scraping stuff and store it to the database As you can see this forces my to require the connection in every file that needs to do something do this with the database. Is there a better way to this? Could I require the connection in my model files? Would this create an issue when importing multiple models? I'm curious how you deal with this issue

Submitted April 01, 2019 at 03:23PM by runo9

No comments:

Post a Comment