Initial commit: tischlerctrl home automation project
This commit is contained in:
86
server/src/db/schema.js
Normal file
86
server/src/db/schema.js
Normal file
@@ -0,0 +1,86 @@
|
||||
import Database from 'better-sqlite3';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { dirname, join } from 'path';
|
||||
import { existsSync, mkdirSync } from 'fs';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
/**
|
||||
* Initialize the SQLite database with all required tables
|
||||
* @param {string} dbPath - Path to the SQLite database file
|
||||
* @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 });
|
||||
}
|
||||
|
||||
const db = new Database(dbPath);
|
||||
|
||||
// Enable WAL mode for better concurrent performance
|
||||
db.pragma('journal_mode = WAL');
|
||||
|
||||
// Create tables
|
||||
db.exec(`
|
||||
-- API keys for agent authentication
|
||||
CREATE TABLE IF NOT EXISTS api_keys (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
key TEXT UNIQUE NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
device_prefix TEXT NOT NULL,
|
||||
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 (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
timestamp DATETIME NOT NULL,
|
||||
device TEXT NOT NULL,
|
||||
channel TEXT NOT NULL,
|
||||
value REAL NOT NULL
|
||||
);
|
||||
|
||||
-- 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);
|
||||
`);
|
||||
|
||||
console.log('[DB] Database initialized successfully');
|
||||
return db;
|
||||
}
|
||||
|
||||
export default { initDatabase };
|
||||
Reference in New Issue
Block a user