I have a problem with my NestJS function,
I need to send 300 thousand records to my database, it is not a POST, it is a PATCH,
I have the function to know what data to update, but when uploading the data I found 2 problems:
1.- Sending all the data returned an error, saying that there is too much data to send.
2.- Make a for loop and put my query there, I tried it and it literally took days.
Is there a way to make it more efficient?
By a lot or something like that?
Thank you.
for (let i = 0; i < readFile.length; i++) {
try {
const aseguradoFile = readFile[i];
const findAsegurado = usuarios.find(asegurado => {
const removeGuion = usuarios.fc_nombre_usuario?.replace(/-/g, ' ');
const removeDobleGuion = removeGuion?.replace(/--/g, ' ');
const removeDobleEspacio = removeDobleGuion?.replace(/ /g, ' ');
return removeDobleEspacio.toUpperCase().trim() == String(usuarioFile.Nombre_Completo).toUpperCase().trim();
});
usuariosFromFile.push(findUsuario);
/* const jsonres = {
...aseguradoFile,
...findAsegurado,
rfc: calcularRFC(usuarioFile.Nombre, usuarioFile.Paterno, usuarioFile.Materno, findUsuario.fecha_nacimiento)
} */
const toUpdate = {
id_usuario: findUsiario.id_usuario,
rfc: calcularRFC(usuarioFile.Nombre, usuarioFile.Paterno, usuarioFile.Materno, findUsuario.fecha_nacimiento),
recalculado: rfc !== findUsuario.rfc ? 1 : 0,
error: 0
};
nombres.push(toUpdate);
//console.log("toUpdate", toUpdate);
console.log("asegurado::", i);
} catch (error) {
console.log("Error al actualizar el registro", error);
const toUpdate = {
id_usuario: findUsiario.id_usuario,
rfc: null,
recalculado: 0,
error: 1
};
errors.push(toUpdate);
};
};
const finalArray = [...nombres, ...errors];
const toUpdate = {
//id_usuario: () => `CASE id_usuario ${finalArray.map(usuario => `WHEN ${usuario.id_usuario} THEN ${asegurado.id_usuario}`).join(' ')} END`,
};
await this.checkNombreExcelRepository
.createQueryBuilder()
.update(CheckNombreExcel)
.set(toUpdate)
.where('id_usuario IN (:...ids)', { ids: usuarioFromFile.map(usuario => usuario.id_usuario) })
.execute();
return {
total: usuariosFromFile.length,
message: 'Datos actualizados correctamente',
code : HttpStatus.OK
};
} catch (error) {
console.log('Error al actualizar los datos', error);
throw new HttpException('Error al actualizar los datos', HttpStatus.BAD_REQUEST);
};
Thanks for your help
Bulk upload data to the database, my method takes many days
Invited is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.