I recommend that you make use of modules to make your life easier by working with nodejs, and look for a package for postgree that works with promises
.
Suppose you have a file called DB.js
done as follows:
module.exports = {
limite: () => new Promise((resolve, reject) => {
db.query(`SELECT COUNT (*) FROM segunda`, function(err, result){
if(err)
return reject(err);
return resolve(result.rowCount);
})
})
};
We can proceed in several ways, but the easiest way would be to control such a limit on the route you need, so if you have something like:
const DB = require('DB.js');
app.post('insert/registro', async (req, res) => {
try{
const limite = await DB.limite();
if(limite > MEU_LIMITE_AQUI)
throw Error('Limite maximo atingido');
// Daqui em diante só inserir o teu código de insert...
} catch(e){
res.status(413); // Forbidden status header
res.send({ error:'O HORARIO ESTÁ CHEIO' });
}
});
If you need to implement this control on more routes, you can add the Danizavtz response and use this control as a middleware
, would look something like:
const controller = require('MODULO CRIADO NA RESPOSTA DO Danizavtz');
app.post('insert/registro', controller, (req, res) => {
// aqui dentro você pode fazer o insert sem precisar fazer nenhum controle
});
Which lib you are using to access the database?
– Danizavtz
I’m using postgresql with Pool
– Lucas Emanuel