0

I want to develop a task in node js which fetch data from table in sql server and insert into table in mysql. This task need to run continuously after certain time period (say after each 5 seconds). Please guide me to achieve this.

JS code

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);
  
  });
}
Community
  • 1
  • 1
Azad Shaikh
  • 61
  • 5
  • 13
  • 2
    What did you tried so far? Put the info in your question! – jogo Nov 25 '15 at 07:01
  • Please show us what you have done so far. If you are looking for a clue, then you could use bookshelf.js module to connect your mysql db from node. And you can use any cron job module (from npmjs.org) to run the task at a given frequency of time. – Jasnan Nov 25 '15 at 07:44
  • guys sorry i didn't tried anything . as I mention am new to node js (just started in last week) so I am bit confused about how to achieve this. can you please provide me any link, library , material so I can learn from there. – Azad Shaikh Nov 25 '15 at 08:49
  • guyz I have fetch around 100000 records from sql server but insert into mysql is not done in one shot i.e. first 100+ record is added if I refresh mysql database record count is updated. I want to insert all 1 lacs record using single query – Azad Shaikh Dec 01 '15 at 07:32

0 Answers0