I created a script called 'test.js' just to demonstrate a simple insertion of log records into a MySQL database, using NodeJS, as below. I will generate something around 5 logs per second, which makes me assume that it will require a certain processing of the machine to be running this script my every log generated. My question is: is there a way to leave an open connection so logs are being inserted, without having to open and close the connection to each log? I do not know ... if there is a totally different way, I'd like to know about any alternative.
const mysql = require('mysql');
const con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '123456',
database: 'meu_db_teste'
});
con.connect((err) => {
if(err){
console.log('Erro de conexão do DB');
return;
}
console.log('Conexão estabilizada com threadId: ', con.threadId);
});
// Estes dados do "reg_log" virão de um outro script
const reg_log = { info: 'abcde...xyz', dttime: 'xx:xx:xx' };
con.query('INSERT INTO logs SET ?', reg_log, (err, res, fields) => {
if(err) throw err;
console.log('Last insert ID:', res.insertId);
});
con.end((err) => {
console.log('Connection closed');
});