2

I want to connect to two databases as one database is used for login and other db is used to fetch data for logged in user.

I have created two DB connections as following, but it is overwriting the previous connection

var express = require('express');
var path = require('path');
var AWS = require('aws-sdk');
var connection  = require('express-myconnection'); 

//second connection
var myConnection = require('express-myconnection');

var mysql = require('mysql');
var app = express();
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;

app.use(
connection(mysql,{
    host: 'localhost',
    user: 'user1',
    password : 'pass1',
    port : 3306, //port mysql
    database:'database1'
},'pool') //or single
);

app.use(
myConnection(mysql,{
    host: 'localhost',
    user: 'user2',
    password : 'pass2',
    port : 3306, //port mysql
    database:'database2'
},'pool') //or single
);
3
  • The reason the first connection is being over written is because you are setting the same middleware on the same router (app). If you show a sample of your routes as well we should be able to provide a solution using different routers for each. Commented Jul 27, 2015 at 8:46
  • As you can see in the source code, no matter what you name your function, the connection is always bound to req.getConnection - with no option to change it. github.com/pwalczyszyn/express-myconnection/blob/master/lib/… Commented Jul 27, 2015 at 8:56
  • I know it is not in the scope of this question, but I was wondering why you are keeping this info separated into two different db's Commented Dec 7, 2017 at 14:48

1 Answer 1

2

Ok! For connecting second database I have used mysql connector file containing the pool and assigned it to a variable "newConnection" :

var express = require('express');
var path = require('path');
var AWS = require('aws-sdk');
var connection  = require('express-myconnection'); 

var mysql = require('mysql');
var app = express();
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;

app.use(
connection(mysql,{
    host: 'localhost',
    user: 'user1',
    password : 'pass1',
    port : 3306, //port mysql
    database:'database1'
},'pool') //or single
);
    var newConnection = mysql.createPool({
    host: 'localhost',
    user: 'user2',
    password : 'pass2',
    port : 3306, 
    database:'database2'});
    app.set('connection_user',newConnection);//setting DB configuration in a variable

Later you can simply include the connector in another file lets call it webservice.js:

var connection_user = request.app.get('connection_user');
connection_user.getConnection(function(){});

now using "connection_user" variable we are connected to 2nd database. hope you got it!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.