const mysql = require('mysql');
const os = require("os");
const homedir = os.homedir();
const fs = require('fs');
const path = require('path');
const mysqlConfigStr = fs.readFileSync(path.resolve(homedir, './custom_mysql_config.json'));
const mysqlConfig = JSON.parse(mysqlConfigStr.toString())
console.log(mysqlConfig)
var connectionA = mysql.createConnection(mysqlConfig.connA);
var connectionB = mysql.createConnection(mysqlConfig.connB);
connectionA.connect();
// const table = 'yida_pages';
// const table = 'application_list'
new Promise((resolve) => {
connectionA.query(`select * from ${table} where is_delete = ?`, [0], function (error, results) {
if (error) throw error;
resolve(results.map(item => ({...item})))
})
}).then((data) => {
console.log(data)
return Promise.all(data.map(item => {
const keys = Object.keys(item).filter(item => item != 'id');
return new Promise((resolve) => {
connectionB.query(`insert into ${table} (${keys.join(',')}) values (${keys.map(item => '?').join(',')})`, keys.map(key => item[key]), function (error, results) {
if (error) throw error;
resolve(results)
})
})
}))
}).finally(() => {
console.log("成功")
connectionA.end();
connectionB.end();
})