This text file contains more than 500k rows which I want to read and insert into PostgreSQL table using nodejs. These blanks spaces should also be read and saved like this empty space only.
I wrote the script like this and it is working also the data is getting inserted into table but it is taking very much time like 10 mins for 20 thousands rows only.
const readTextFile = async () => {
const File = await fs.readFileSync('data.txt', 'utf-8');
let arr = File.split("|");
let modified = [];
let temp = [];
for (let i = 0; i < arr.length; i++) {
if (i % 10 === 0) {
modified.push([...temp]);
temp = [];
temp.push(arr[i].replace('\x00\r\n', ''));
} else {
temp.push(arr[i]);
}
}
console.log("modified", modified.length);
for (let i = 0; i < modified.length; i++) {
await insertValuesToDB(modified[i]);
}
}
const insertValuesToDB = async (values) => {
try {
const text = `INSERT INTO requirement(container, module, mod_devdate, part_no, qty, tapdate, tap_qty, taptime, sup_cd, namc_id) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`;
const result = await client.query(text, values);
console.log("result", result);
} catch (e) {
console.log("ERROR", e);
}
}