Wednesday, 15 March 2017

express js app multi-company connection to mySql

I am currently developing an application with NodeJs, ExpressJs and mySql, the application is a kind of ERP, where each company will have its own database, for the user to log in, the company id, name and password are required. The first query, is to a common database, where are the companies, with their id, and the name of their database. How can I do that from now on, the queries are made to the database of the company?

I currently have a global variable that stores the connection:

var conn = mysql.createConnection({ mySql connection info });
conexion.connect(function(err){
    if(err) throw err;
    console.log("Connection success");
});

global.conn = conn;

and in my models:

var conn = global.conn;
conn.query("select * from table", function(err,rows){
    if(err){
         callback(err);
    }else{
         callback(null, rows);
    }
});

Can I overwrite that connection, with the data of the new database? Whereas if a user from another company connects, it will overwrite it.

Any ideas or suggestions? Thanks :)



via RalleSaid

No comments:

Post a Comment