Estou fazendo uma API que cadastra um CSV em uma database, porém preciso remover o cabeçalho do CSV antes, transformei em JSON para facilitar, mas não sei como remove, acredito que de para manipular.
Linha 1 - matricula,mes,valor,status
Linha 2 - 001,2022-01,1000.0,pago
Linha 3 - 001,2022-02,1000.0,pago
Linha 4 - 001,2022-03,1000.0,aberto
Quero remover a Linha 1, cabeçalho.
JSON
[
Linha 1 - { matricula: 1, mes: '2022-01', valor: 1000, status: 'pago' },
Linha 2 - { matricula: 1, mes: '2022-02', valor: 1000, status: 'pago' },
Linha 3 - { matricula: 1, mes: '2022-03', valor: 1000, status: 'aberto' },
Linha 4 - { matricula: 1, mes: '2022-04', valor: 1000, status: 'aberto' },
]
interface Pagamentos {
matricula: number;
mes: string;
valor: number;
status: string
}
router.post(
"/pagamentos",
multerConfig.single("file"), async (request: Request, response: Response) => {
//console.log(_req.file?.buffer.toString("utf-8"));
const { file } = request;
const buffer = file?.buffer;
const readableFile = new Readable();
readableFile.push(buffer);
readableFile.push(null);
const pagamentosLine = readline.createInterface({
input: readableFile
});
const pagamentos: Pagamentos[] = [];
for await (let line of pagamentosLine) {
const pagamentosLineSplit = line.split(",");
pagamentos.push({
matricula: Number(pagamentosLineSplit[0]),
mes: pagamentosLineSplit[1],
valor: Number(pagamentosLineSplit[2]),
status: pagamentosLineSplit[3],
});
console.log(pagamentos);
}
for await ( let {matricula,valor,mes,status} of pagamentos ) {
await client.pagamentos.create({
data: {
matricula,
mes,
valor,
status,
},
})
}
return response.json(pagamentos);
}
);