# 5.3.4 Database Schema and Persistence Model

## Core Entity Relationship Diagram

```mermaid
erDiagram
    USERS ||--o{ DEVICES : owns
    USERS ||--o{ DEVICE_LINKS : owns
    DEVICES ||--o{ DEVICE_LINKS : cameraDeviceId
    DEVICES ||--o{ DEVICE_LINKS : clientDeviceId
    USERS ||--o{ STREAM_SESSIONS : owns
    DEVICES ||--o{ STREAM_SESSIONS : cameraDeviceId
    DEVICES ||--o{ STREAM_SESSIONS : requesterDeviceId

    USERS {
      uuid id PK
      varchar email
      varchar name
    }
    DEVICES {
      uuid id PK
      uuid user_id FK
      varchar role
      varchar status
      timestamp last_seen_at
    }
    DEVICE_LINKS {
      uuid id PK
      uuid owner_user_id FK
      uuid camera_device_id FK
      uuid client_device_id FK
      varchar status
    }
    STREAM_SESSIONS {
      uuid id PK
      uuid owner_user_id FK
      uuid camera_device_id FK
      uuid requester_device_id FK
      varchar status
      varchar reason
    }
```

## Media and Event Persistence

```mermaid
erDiagram
    USERS ||--o{ EVENTS : owns
    DEVICES ||--o{ EVENTS : triggeredBy
    STREAM_SESSIONS ||--o{ RECORDINGS : produces
    EVENTS ||--o{ RECORDINGS : mayCreate
    USERS ||--o{ NOTIFICATIONS : receives
    EVENTS ||--o{ NOTIFICATIONS : generates
    USERS ||--o{ NOTIFICATION_DELIVERIES : owns
    DEVICES ||--o{ NOTIFICATION_DELIVERIES : targets
    USERS ||--o{ AUDIT_LOGS : owns
    DEVICES ||--o{ AUDIT_LOGS : actor

    EVENTS {
      uuid id PK
      uuid user_id FK
      uuid device_id FK
      varchar status
      timestamp started_at
      timestamp ended_at
    }
    RECORDINGS {
      uuid id PK
      uuid stream_session_id FK
      uuid event_id FK
      varchar object_key
      varchar bucket
      varchar status
    }
    NOTIFICATIONS {
      uuid id PK
      uuid event_id FK
      uuid user_id FK
      varchar channel
      varchar status
    }
    NOTIFICATION_DELIVERIES {
      uuid id PK
      uuid recipient_device_id FK
      varchar type
      varchar status
      int attempts
    }
    AUDIT_LOGS {
      uuid id PK
      uuid owner_user_id FK
      uuid actor_device_id FK
      varchar action
      varchar target_type
      varchar target_id
    }
```
