feat: initialize sensors database table and index on server startup
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user