[英]Create multiple new Mysql database connections using mysql2-promise and node
So, I need to change the Mysql Database, which will be used by some asynchronous calls to read from a database. 因此,我需要更改Mysql数据库,某些异步调用将使用该数据库来读取数据库。
There is one databases, having a table containing multiple database names, where I could read database names. 有一个数据库,有一个包含多个数据库名称的表,我可以在其中读取数据库名称。 And change my database accordingly. 并相应地更改我的数据库。
But if the old read operation is in process database connection gets modified. 但是,如果旧的读取操作正在进行中,则数据库连接将被修改。
How can I create a new Database connection, retaining the old database connection? 如何创建新的数据库连接,同时保留旧的数据库连接?
Even when both have 2 different variable names. 即使两个都有两个不同的变量名。
Below is index.js routing file. 以下是index.js路由文件。
var mysqldb = require('mysql2-promise')();
var config = require('../config.js');
function scheduledSync( req, res, next ){
var companyIds = [];
var getCompanies = function (){
var deferred = Q.defer();
schema.getModel('Company').find([] , function( err, data ){
if(err){
deferred.reject();
} else{
deferred.resolve( data );
}
});
return deferred.promise;
}
getCompanies().then(function(companyData){
promise.each( companyData, function( company ){
mysqldb.configure( config.sql );
var companyQuery = "SELECT * FROM companies WHERE id = "+company.company_id+"";
return mysqldb.query( companyQuery ).spread(function( rows ){
var value = rows[0];
var lastUpdated = company.lastUpdated.toISOString().replace(/T/, ' ').replace(/\..+/, '');
if( value != null ){
return callAPIs( value, value.id, lastUpdated ).then(function(){
console.log( 'Company '+company.company_id + ' Migrated' );
}, function(error) {
console.log('rejection');
});
}
});
}). then(function(){
response = 'success';
return response;
});
});
}
var callAPIs = promise.method( function( companyData, companyId, lastUpdated ){
config.companysql.database = companyData.dbname;
url = companyData.domainname;
var promises = [
areas.getAreaData( config.companysql, companyId, lastUpdated ),
];
Q.all(promises).then( function( values ){
var lastPromise = [
company.getCompanyData( config.companysql, url, companyId, lastUpdated )
];
Q.all(lastPromise).then(function(){
complete();
complete();
return true;
})
});
});
Below is the config.js file. 以下是config.js文件。
var _ENVIRONMENT = 'Development';
//process.env.NODE_ENV = env;
/**
We will configure the database credentials as well as API Layer End point based on the environment selected.
*/
switch (_ENVIRONMENT) {
case 'Development':
var _DBHOST = 'dummyhost'
, _DBUSER = 'xxxxxxx'
, _DBPASS = 'xxxxxxx'
, _DBNAME = 'xxxxxxxx'
, _MONGOURL = 'mongodb://dummyhost:27017/xxxxx'
, _DEBUG = false;
break;
case 'Staging':
var _DBHOST = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
, _DBUSER = 'xxxxxxxxxx'
, _DBPASS = 'xxxxxxxxxxxx'
, _DBNAME = 'xxxxxxxxxxxxxxxx'
, _MONGOURL = 'mongodb://xxxxxxxxxxx:xxxx/xxxxx'
, _DEBUG = false;
break;
// case 'Production':
// break;
default:
}
module.exports = {
sql: {
host : _DBHOST
, user : _DBUSER
, password : _DBPASS
, database : _DBNAME
, debug : _DEBUG
},
companysql: {
host : _DBHOST
, user : _DBUSER
, password : _DBPASS
, database : ''
, debug : _DEBUG
},
mongourl : _MONGOURL
}
in area.js 在area.js中
var companyDB = require('mysql2-promise')();
var getAreaData = function( config, companyId, lastUpdated ){
companyDB.configure( config );
/*
Manipulate Area Table
*/
}
I solved the promise issue with mysql poolcluster. 我用mysql poolcluster解决了promise问题。 Posting it if anyone else would need any help 如果其他人需要任何帮助,将其发布
var poolCluster = mysql.createPoolCluster();
poolCluster.add( 'MASTER', config.sql );
schema = promise.promisifyAll(schema);
var getsignupdata = function( compIdUpdate, companyIds, mysqldb ){
var deferred = Q.defer();
var companyQuery = "SELECT * FROM companies WHERE id IN ("+companyIds+") ";
poolCluster.getConnection( 'MASTER', function ( err2, mysqlcon ) {
mysqlcon.query( companyQuery , function( err, rows ){
if( !err ){
for( i in rows ){
var companyId = rows[i].id;
config.companysql.database = rows[i].dbname;
poolCluster.add( 'SLAVE'+companyId, config.companysql );
if( compIdUpdate != null )
rows[i]['lastUpdated'] = compIdUpdate[""+companyId];
}
deferred.resolve( rows );
}
});
});
return deferred.promise;
};
function scheduledSync( req, res, next ){
getCompanies().then(function( data ){
getsignupdata( data.compIdUpdate, data.companyIds, mysqldb ).then(function( companyData ){
console.log("scheduledSync " + companyData);
promise.map( companyData, function( company ){
var lastUpdated;
if(company.lastUpdated != null)
lastUpdated = company.lastUpdated.toISOString().replace(/T/, ' ').replace(/\..+/, '');
else
lastUpdated = null;
if( company != null ){
return callAPIs( company, company.id, lastUpdated ).then(function(){
console.log( 'Company '+ company.id + ' Migrated' );
});
}
}). then(function( err ){
response = 'success';
return response;
});
});
});
}
var callAPIs = promise.method( function( companyData, companyId, lastUpdated ){
poolCluster.getConnection( 'SLAVE'+companyId, function ( err, companyConfig ) {
entity.getEntityData( companyConfig, companyId, lastUpdated ).then(function(){
areas.getAreaData( companyConfig, companyId, lastUpdated );
discount.getDiscountData( companyConfig, companyId, lastUpdated );
holderscheme.getHolderSchemeData( companyConfig, companyId, lastUpdated );
scheme.getSchemeData( companyConfig, companyId, lastUpdated );
skunits.getSkunitData( companyConfig, companyId, lastUpdated );
stockAtWarehouse.getStockAtWareMap( companyConfig, companyId, lastUpdated );
company.getCompanyData( config.companysql, companyData, companyId, lastUpdated );
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.