Created
October 9, 2020 12:13
-
-
Save nk-gears/6c8fcbada5b3c46569e39f6b8a7aa134 to your computer and use it in GitHub Desktop.
Export Data from Mysql to Multiple CSV Files - page by page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mysql = require("mysql"); | |
const fastcsv = require("fast-csv"); | |
const Json2csvParser = require("json2csv").Parser; | |
const fs = require("fs"); | |
var pool = mysql.createPool({ | |
connectionLimit : 10, | |
host: "host", | |
user: "root", | |
password: "", | |
database: "master", | |
multipleStatements: true | |
}); | |
// | |
const getAllLeads=async (page)=>{ | |
const limit = 5000; | |
const offset = (page - 1) * limit | |
const leadQry = "select * from leads L limit "+limit+" OFFSET "+offset | |
pool.getConnection(function(err, connection) { | |
connection.query(leadQry, function (error, results, fields) { | |
// When done with the connection, release it. | |
connection.release(); | |
let jsonFinal=[]; | |
if(error) throw(error); | |
results.forEach(lead=>{ | |
try{ | |
if(lead.lead_fields!=''){ | |
let lead_fields=JSON.parse(lead.lead_fields); | |
let jsonDataFinal={supplier_id:lead.supplier_id,email_id:lead.email_id,...lead_fields,timestamp:lead.created,id:lead.id}; | |
jsonFinal.push(jsonDataFinal); | |
} | |
}catch(ex){ | |
console.log(lead); | |
process.exit(); | |
} | |
}); | |
const json2csvParser = new Json2csvParser({ header: true}); | |
const csv = json2csvParser.parse(jsonFinal); | |
fs.writeFile(`./${page}_results.csv`, csv, function(error) { | |
if (error) throw error; | |
console.log(`Exporting page : ${page}`); | |
}); | |
}) | |
}) | |
} | |
(async ()=>{ | |
//no of pages to export | |
for(k=1;k<=168;k++){ | |
await getAllLeads(k); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment