1. const mysql = require('mysql');
    2. const os = require("os");
    3. const homedir = os.homedir();
    4. const fs = require('fs');
    5. const path = require('path');
    6. const mysqlConfigStr = fs.readFileSync(path.resolve(homedir, './custom_mysql_config.json'));
    7. const mysqlConfig = JSON.parse(mysqlConfigStr.toString())
    8. console.log(mysqlConfig)
    9. var connectionA = mysql.createConnection(mysqlConfig.connA);
    10. var connectionB = mysql.createConnection(mysqlConfig.connB);
    11. connectionA.connect();
    12. // const table = 'yida_pages';
    13. // const table = 'application_list'
    14. new Promise((resolve) => {
    15. connectionA.query(`select * from ${table} where is_delete = ?`, [0], function (error, results) {
    16. if (error) throw error;
    17. resolve(results.map(item => ({...item})))
    18. })
    19. }).then((data) => {
    20. console.log(data)
    21. return Promise.all(data.map(item => {
    22. const keys = Object.keys(item).filter(item => item != 'id');
    23. return new Promise((resolve) => {
    24. connectionB.query(`insert into ${table} (${keys.join(',')}) values (${keys.map(item => '?').join(',')})`, keys.map(key => item[key]), function (error, results) {
    25. if (error) throw error;
    26. resolve(results)
    27. })
    28. })
    29. }))
    30. }).finally(() => {
    31. console.log("成功")
    32. connectionA.end();
    33. connectionB.end();
    34. })