feat: initialize sensors database table and index on server startup

This commit is contained in:
Giuseppe Raffa
2026-04-06 11:47:54 +02:00
parent e65f2ba3a0
commit f785fbedca
2 changed files with 23 additions and 3 deletions

View File

@@ -19,6 +19,25 @@ async function checkDB() {
}
}
async function initDB() {
try {
await pool.query(`
CREATE TABLE IF NOT EXISTS sensors (
id VARCHAR(10) PRIMARY KEY,
name VARCHAR(100) NOT NULL,
code_hash TEXT NOT NULL UNIQUE,
is_active BOOLEAN DEFAULT TRUE,
last_seen TIMESTAMP DEFAULT NOW(),
created_at TIMESTAMP DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_sensors_code_hash ON sensors(code_hash);
`);
console.log('[DB] Database schema initialized (sensors table ensured)');
} catch (error) {
console.error('[DB] Schema initialization failed:', error);
}
}
/**
* Restituisce i dati del sensore in base al token ricevuto.
* Il token viene hashato prima della comparazione con il database.