Commit e1ef3ad6 by bovornsiriampairat

Added Truncate Function

parent b1d40966
...@@ -60,43 +60,48 @@ xml2js.parseString(xmlString, async (err, result) => { ...@@ -60,43 +60,48 @@ xml2js.parseString(xmlString, async (err, result) => {
const startTime = new Date(); const startTime = new Date();
for (let i = 1; i < data.length; i++) {
const row = data[i];
const mappedEmployee = {};
// Map the employeeid value from the Excel column
const employeeid = row[employeeidColumnIndex];
mappedEmployee.employeeid = replacePlaceholders(employeeidTag, employeeid);
mappedEmployee.formulaid = formulaidTags.reduce((result, tag) => {
const { column, value } = tag.$;
const columnIndex = data[0].indexOf(column);
if (columnIndex !== -1 && row[columnIndex] !== undefined) {
const tagType = column.startsWith('INC') ? 'INC' : 'DCT';
result.push({
tagType,
formulaid: replacePlaceholders(column, row[columnIndex]),
value: replacePlaceholders(value, row[columnIndex]),
});
}
return result;
}, []);
jsonData.employee.push(mappedEmployee);
// Calculate progress
const progress = (i / (data.length - 1)) * 100;
console.log(`Progress: ${progress.toFixed(2)}%`);
}
try { try {
// Connect to the PostgreSQL database // Connect to the PostgreSQL database
const client = await pool.connect(); const client = await pool.connect();
// Insert the data into the PostgreSQL database
const tableName = 'tpytran1'; // Specify the table name const tableName = 'tpytran1'; // Specify the table name
// Truncate the table and restart identity
const truncateQuery = `TRUNCATE TABLE ${tableName} RESTART IDENTITY`;
await client.query(truncateQuery);
// Insert the data into the PostgreSQL database
for (let i = 1; i < data.length; i++) {
const row = data[i];
const mappedEmployee = {};
// Map the employeeid value from the Excel column
const employeeid = row[employeeidColumnIndex];
mappedEmployee.employeeid = replacePlaceholders(employeeidTag, employeeid);
mappedEmployee.formulaid = formulaidTags.reduce((result, tag) => {
const { column, value } = tag.$;
const columnIndex = data[0].indexOf(column);
if (columnIndex !== -1 && row[columnIndex] !== undefined) {
const tagType = column.startsWith('INC') ? 'INC' : 'DCT';
result.push({
tagType,
formulaid: replacePlaceholders(column, row[columnIndex]),
value: replacePlaceholders(value, row[columnIndex]),
});
}
return result;
}, []);
jsonData.employee.push(mappedEmployee);
// Calculate progress
const progress = (i / (data.length - 1)) * 100;
console.log(`Progress: ${progress.toFixed(2)}%`);
}
for (const employee of jsonData.employee) { for (const employee of jsonData.employee) {
const { employeeid, formulaid } = employee; const { employeeid, formulaid } = employee;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment