refactor: standardize database environment variables to POSTGRES prefix and update gitignore with common development patterns

This commit is contained in:
Giuseppe Raffa
2026-03-28 16:05:11 +01:00
parent bcfce32adb
commit 33b9be13de
4 changed files with 34 additions and 17 deletions

View File

@@ -2,11 +2,11 @@ const { Pool } = require('pg');
const { hash, generateShortId } = require('./cryptoUtils');
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
user: process.env.POSTGRES_USER,
host: process.env.POSTGRES_HOST,
database: process.env.POSTGRES_NAME,
password: process.env.POSTGRES_PASSWORD,
port: process.env.POSTGRES_PORT,
})
async function checkDB() {
@@ -30,7 +30,7 @@ async function getSensor(token) {
async function createSensor(name, code) {
const hashedCode = hash(code);
// Verifica se l'hash esiste già
const result = await pool.query('SELECT id FROM sensors WHERE code_hash = $1', [hashedCode]);
if (result.rows.length > 0) {
@@ -40,7 +40,7 @@ async function createSensor(name, code) {
// Genera un ID casuale di 8 caratteri (ottimizzato per spazio, non solo alfanumerico)
const sensorId = generateShortId(8);
await pool.query('INSERT INTO sensors (id, name, code_hash, is_active, last_seen, created_at) VALUES ($1, $2, $3, $4, $5, $6)',
await pool.query('INSERT INTO sensors (id, name, code_hash, is_active, last_seen, created_at) VALUES ($1, $2, $3, $4, $5, $6)',
[sensorId, name, hashedCode, true, new Date(), new Date()]);
}
@@ -61,7 +61,7 @@ async function updateLastSeen(id) {
*/
async function setSensorActivity(id, is_active) {
await pool.query('UPDATE sensors SET is_active = $1 WHERE id = $2', [is_active, id]);
}
}
async function sensorsExists(id) {
const result = await pool.query('SELECT id FROM sensors WHERE id = $1', [id]);