我想在节点 js 中开发一个任务,它从 sql server 中的表中获取数据并插入到 mysql 中的表中。此任务需要在一定时间段后(例如每 5 秒后)连续运行。请指导我实现这一目标。
JS代码
var sql = require('mssql');
var mysql = require("mysql");
var config = {
user: 'user',
password: '*****',
server: 'url',
database: 'DB',
stream: true //work with large amount of rows
}
var connection1 = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'test'
});
connection1.connect();
var connection = new sql.Connection(config, function(err) {
if(err)
console.log(err);
var request = new sql.Request(connection);
//request.stream = true; // You can set streaming differently for each request
request.query('select TOP 100000 * FROM ShipmentAuditLog with (nolock)'); // or request.execute(procedure);
console.time('Time-Taken');
request.on('recordset', function(recordset) {
// Emitted once for each recordset in a query
//console.log(recordset);
});
request.on('row', function(row) {
// Emitted for each row in a recordset
//console.log(row);
syncing(row);
});
request.on('error', function(err) {
console.log(err);
});
request.on('done', function(returnValue) {
// Always emitted as the last one
console.log('Completed');
console.timeEnd('Time-Taken');
connection.close();
});
});
var syncing = function(row){
connection1.query('INSERT INTO shipmentauditlog SET ?',row,function(err,res){
if(err)
console.log(err);
});
}