所以,我需要更改Mysql数据库,这将被一些异步调用用于从数据库中读取。
有一个数据库,有一个包含多个数据库名称的表,我可以在其中读取数据库名称。并相应地更改我的数据库。
但是如果旧的读取操作正在进行中,则数据库连接会被修改。
如何创建新的数据库连接,保留旧的数据库连接?
即使两者都有两个不同的变量名。
下面是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;
})
});
});
以下是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
}
在area.js
var companyDB = require('mysql2-promise')();
var getAreaData = function( config, companyId, lastUpdated ){
companyDB.configure( config );
/*
Manipulate Area Table
*/
}
答案 0 :(得分:0)
我用mysql poolcluster解决了promise问题。 如果其他人需要任何帮助,则发布
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 );
});
});
});