Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Creating Entries in a Table

How can I connect multiple databases to a sequelize app?

I want to build an app that has three connections.. One for mysql, postgreSQL and SQLite??

So far I have a connection for a postgreSQL I am finding it difficult to add a MySQL connection as well.

Any help would be great... Here is what I have so far..

config.json:

{ "development": { "url": "postgres://dabladmin:dabldem@localhost:5432/dablpatient", "dialect": "postgres" } }

database.js:

var Sequelize = require('sequelize');

var connection = 'postgres://dabladmin:dabldem@localhost:5432/dablpatient'; var connection2 = 'mysql://root:weak@localhost:3306/dablpatient2';

var postgresDB = new Sequelize(connection); var mysqlDB = new Sequelize(connection2);

module.exports = postgresDB; module.exports = mysqlDB;

app.js:

var express = require('express'); var path = require('path'); var consolidate = require('consolidate'); var bodyParser = require('body-parser'); var database = require('./database/database'); var Patient = require('./models/models').Patient; var morgan = require('morgan'); var routes = require('./routes'); var app = express();

app.engine('html', consolidate.nunjucks);

app.set('view engine', 'html'); app.set('views', './views');

app.use(morgan('dev')); app.use(routes);

app.use(bodyParser.urlencoded({ extended: false }));

app.listen(3055);

module.exports = app;

1 Answer

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

I don't have time to post the complete solution I will however, link to a Stack Overflow that somewhat answers it. Generally what you should do is create a new instance of Sequalize for each database.

Here is a link to a Stack Overflow answer that tackles somewhat the same problem as you. I am unsure however, if this will work for multiple databases if they are each different (SQLite, Postgres, MySQL), as a question though. Why are you using SQLite, Postgres, and MySQL in the same project (not accusing, just really curious), I am unsure as to why you would want to use them all at the same time...

Thanks I'll have a look at this!