Skip to content

Database ​

SafeCall uses SQLite for all data storage. Database initialization and migrations are handled by lib/src/db.ts.

Databases ​

The server runs two separate SQLite databases:

Main Database (safecall.db) ​

Stores all application data. Path is configured via server_db_path in config/main.config.json (legacy fallback: root main.config.json, default path value: db/safecall.db).

Sensor Database (db/sensor_data.db) ​

Stores time-series sensor data (temperature, humidity, battery) from beacon adv1/adv8 messages. Kept separate to avoid bloating the main database.

Migrations ​

Migrations live in server/migrations/ as TypeScript files, numbered sequentially (001-029). In production builds, migrations are embedded via server/src/migration_embeds.ts.

The migration system:

  1. Creates a migrations table to track applied migrations
  2. Runs pending migrations in order
  3. Each migration receives the Database instance

Current Schema ​

TableMigrationPurpose
users001User accounts (email, password hash, admin flag)
locations002Physical locations/buildings
beacons003, 015, 021, 027BLE beacons (MAC, name, type, sensor config)
beacon_events004, 026Panic/SOS events with zone ID
gateways005, 007, 008, 011, 017BLE gateways (MAC, IP, location, map)
logs006System logs
maps009Floor map images and metadata
zones010, 012Zones with rectangle coordinates
zone_connections013, 018Zone adjacency graph
tracking_logs014Beacon tracking intervals
nodes020Navigation graph nodes (x, y, floor)
edges020Navigation graph edges (weight)
floors019Floor metadata for navigation
device_gateways022Gateway device sync table
device_beacons023Beacon device sync table
beacon_gateway_sightings024, 028Beacon-gateway RSSI sightings
panic_event_gateways025Gateways involved in panic events
navigation_tokens029Read-only tokens for mobile navigation

Sensor Data Schema ​

TableColumnsPurpose
sensor_dataid, dmac, type, value, timestampTime-series sensor readings

Column details:

  • id — auto-increment primary key
  • dmac — beacon MAC address (TEXT)
  • type — sensor type: 1 = Humidity, 2 = Temperature, 3 = Battery (INTEGER)
  • value — sensor reading (REAL)
  • timestamp — milliseconds since epoch (INTEGER)

Indexes: (dmac, timestamp DESC) and (type).

WAL Mode ​

Both databases run in WAL (Write-Ahead Logging) mode for better concurrent read/write performance.

Query Result Types ​

All query results are validated at runtime using Valibot schemas defined in server/src/schemas.ts. The corresponding TypeScript types are in lib/src/types/query.d.ts.