This commit is contained in:
sebseb7
2025-12-24 23:11:24 +01:00
parent 23a6f900ec
commit 0d08a4d924
5 changed files with 202 additions and 186 deletions

View File

@@ -12,20 +12,20 @@ const __dirname = dirname(__filename);
* @returns {Database} - The initialized database instance
*/
export function initDatabase(dbPath) {
// Ensure data directory exists
const dataDir = dirname(dbPath);
if (!existsSync(dataDir)) {
mkdirSync(dataDir, { recursive: true });
}
// Ensure data directory exists
const dataDir = dirname(dbPath);
if (!existsSync(dataDir)) {
mkdirSync(dataDir, { recursive: true });
}
const db = new Database(dbPath);
const db = new Database(dbPath);
// Enable WAL mode for better concurrent performance
db.pragma('journal_mode = WAL');
// Enable WAL mode for better concurrent performance
db.pragma('journal_mode = WAL');
// Create tables
db.exec(`
-- API keys for agent authentication
// Create tables
// API keys for agent authentication
db.exec(`
CREATE TABLE IF NOT EXISTS api_keys (
id INTEGER PRIMARY KEY AUTOINCREMENT,
key TEXT UNIQUE NOT NULL,
@@ -34,53 +34,35 @@ export function initDatabase(dbPath) {
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
last_used_at DATETIME
);
`);
-- Raw sensor data (1-minute resolution, kept for 1 week)
CREATE TABLE IF NOT EXISTS sensor_data (
// --- MIGRATION: Drop old tables if they exist ---
// User requested deleting old sensor data but keeping keys.
db.exec(`
DROP TABLE IF EXISTS sensor_data;
DROP TABLE IF EXISTS sensor_data_10m;
DROP TABLE IF EXISTS sensor_data_1h;
`);
// --- NEW SCHEMA: Sensor Events with RLE support ---
db.exec(`
CREATE TABLE IF NOT EXISTS sensor_events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME NOT NULL,
until DATETIME, -- NULL if point, Time if duplicated range end
device TEXT NOT NULL,
channel TEXT NOT NULL,
value REAL NOT NULL
value REAL, -- Nullable
data TEXT, -- Nullable (JSON)
data_type TEXT NOT NULL -- 'number' or 'json'
);
-- Index for time-based queries and cleanup
CREATE INDEX IF NOT EXISTS idx_sensor_data_time
ON sensor_data(timestamp);
-- Index for device/channel queries
CREATE INDEX IF NOT EXISTS idx_sensor_data_device
ON sensor_data(device, channel, timestamp);
-- 10-minute aggregated data (kept for 1 month)
CREATE TABLE IF NOT EXISTS sensor_data_10m (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME NOT NULL,
device TEXT NOT NULL,
channel TEXT NOT NULL,
value REAL NOT NULL,
sample_count INTEGER NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_sensor_data_10m_unique
ON sensor_data_10m(timestamp, device, channel);
-- 1-hour aggregated data (kept forever)
CREATE TABLE IF NOT EXISTS sensor_data_1h (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME NOT NULL,
device TEXT NOT NULL,
channel TEXT NOT NULL,
value REAL NOT NULL,
sample_count INTEGER NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_sensor_data_1h_unique
ON sensor_data_1h(timestamp, device, channel);
CREATE INDEX IF NOT EXISTS idx_sensor_events_search
ON sensor_events(device, channel, timestamp);
`);
console.log('[DB] Database initialized successfully');
return db;
console.log('[DB] Database initialized successfully');
return db;
}
export default { initDatabase };