aethex.live/docs/DATABASE_SCHEMA.md
MrPiglr 58d231e72f
Major: Add complete platform architecture with full data models
- Add Prisma schema with all database models
- Creator channels, streams, VOD, clips, monetization
- Real-time chat, polls, interactions
- Subscriptions, donations, payouts
- Social graph (followers), clips, leaderboards
- Complete Platform Architecture documentation
- API structure and routes documentation
- Implementation roadmap (8 phases)
- Updated README with platform vision
- Install core dependencies: Prisma, Clerk, Stripe, Socket.io

Ready for Phase 1 development with database setup.
2026-02-07 04:52:01 +00:00

10 KiB

Database Schema for AeThex LIVE

Users & Authentication

-- Users table
CREATE TABLE users (
  id UUID PRIMARY KEY,
  username VARCHAR(255) UNIQUE NOT NULL,
  email VARCHAR(255) UNIQUE NOT NULL,
  display_name VARCHAR(255),
  bio TEXT,
  avatar_url VARCHAR(255),
  banner_url VARCHAR(255),
  is_creator BOOLEAN DEFAULT false,
  verified BOOLEAN DEFAULT false,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- User settings
CREATE TABLE user_settings (
  user_id UUID PRIMARY KEY REFERENCES users(id),
  email_notifications BOOLEAN DEFAULT true,
  two_factor_enabled BOOLEAN DEFAULT false,
  privacy_level VARCHAR(50) DEFAULT 'public', -- public, private, friends
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Channels & Streaming

-- Channels table
CREATE TABLE channels (
  id UUID PRIMARY KEY,
  user_id UUID NOT NULL REFERENCES users(id),
  name VARCHAR(255) NOT NULL,
  slug VARCHAR(255) UNIQUE NOT NULL,
  description TEXT,
  category VARCHAR(100), -- gaming, music, education, etc.
  language VARCHAR(50),
  is_live BOOLEAN DEFAULT false,
  total_views BIGINT DEFAULT 0,
  follower_count INT DEFAULT 0,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Streams (live sessions)
CREATE TABLE streams (
  id UUID PRIMARY KEY,
  channel_id UUID NOT NULL REFERENCES channels(id),
  title VARCHAR(255) NOT NULL,
  description TEXT,
  status VARCHAR(50), -- live, ended, scheduled
  stream_key VARCHAR(255) UNIQUE NOT NULL,
  hls_url VARCHAR(255),
  thumbnail_url VARCHAR(255),
  viewer_count INT DEFAULT 0,
  started_at TIMESTAMP,
  ended_at TIMESTAMP,
  duration_seconds INT,
  is_archived BOOLEAN DEFAULT true,
  archive_url VARCHAR(255),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Stream tags
CREATE TABLE stream_tags (
  stream_id UUID REFERENCES streams(id),
  tag VARCHAR(100),
  PRIMARY KEY (stream_id, tag)
);

Social Features

-- Followers/Subscriptions
CREATE TABLE followers (
  follower_id UUID NOT NULL REFERENCES users(id),
  channel_id UUID NOT NULL REFERENCES channels(id),
  follow_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  notification_enabled BOOLEAN DEFAULT true,
  PRIMARY KEY (follower_id, channel_id)
);

-- User follows
CREATE TABLE user_follows_user (
  follower_id UUID NOT NULL REFERENCES users(id),
  following_id UUID NOT NULL REFERENCES users(id),
  follow_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (follower_id, following_id)
);

Chat & Moderation

-- Chat messages
CREATE TABLE chat_messages (
  id UUID PRIMARY KEY,
  stream_id UUID NOT NULL REFERENCES streams(id),
  user_id UUID NOT NULL REFERENCES users(id),
  message TEXT NOT NULL,
  is_deleted BOOLEAN DEFAULT false,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Chat bans/moderation
CREATE TABLE chat_bans (
  id UUID PRIMARY KEY,
  channel_id UUID NOT NULL REFERENCES channels(id),
  banned_user_id UUID NOT NULL REFERENCES users(id),
  reason TEXT,
  banned_until TIMESTAMP,
  is_permanent BOOLEAN DEFAULT false,
  created_by UUID NOT NULL REFERENCES users(id),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Moderation queue
CREATE TABLE moderation_queue (
  id UUID PRIMARY KEY,
  stream_id UUID REFERENCES streams(id),
  message_id UUID REFERENCES chat_messages(id),
  reported_by UUID NOT NULL REFERENCES users(id),
  reason VARCHAR(255),
  status VARCHAR(50) DEFAULT 'pending', -- pending, approved, rejected
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Monetization

-- Subscriptions
CREATE TABLE subscriptions (
  id UUID PRIMARY KEY,
  subscriber_id UUID NOT NULL REFERENCES users(id),
  channel_id UUID NOT NULL REFERENCES channels(id),
  tier VARCHAR(50), -- basic, premium, vip
  price_cents INT NOT NULL,
  stripe_subscription_id VARCHAR(255),
  status VARCHAR(50), -- active, cancelled, expired
  started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  ended_at TIMESTAMP,
  auto_renew BOOLEAN DEFAULT true,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Donations/Tips
CREATE TABLE donations (
  id UUID PRIMARY KEY,
  donor_id UUID NOT NULL REFERENCES users(id),
  channel_id UUID NOT NULL REFERENCES channels(id),
  stream_id UUID REFERENCES streams(id),
  amount_cents INT NOT NULL,
  currency VARCHAR(3) DEFAULT 'USD',
  message TEXT,
  stripe_charge_id VARCHAR(255),
  status VARCHAR(50) DEFAULT 'completed', -- pending, completed, failed
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Creator payouts
CREATE TABLE payouts (
  id UUID PRIMARY KEY,
  user_id UUID NOT NULL REFERENCES users(id),
  amount_cents INT NOT NULL,
  currency VARCHAR(3) DEFAULT 'USD',
  stripe_payout_id VARCHAR(255),
  status VARCHAR(50) DEFAULT 'pending', -- pending, paid, failed
  period_start DATE,
  period_end DATE,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Content

-- VODs (Video on Demand)
CREATE TABLE vods (
  id UUID PRIMARY KEY,
  stream_id UUID UNIQUE NOT NULL REFERENCES streams(id),
  title VARCHAR(255),
  description TEXT,
  thumbnail_url VARCHAR(255),
  duration_seconds INT,
  views INT DEFAULT 0,
  is_public BOOLEAN DEFAULT true,
  processing_status VARCHAR(50) DEFAULT 'processing', -- processing, ready, failed
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Clips
CREATE TABLE clips (
  id UUID PRIMARY KEY,
  vod_id UUID REFERENCES vods(id),
  stream_id UUID REFERENCES streams(id),
  creator_id UUID NOT NULL REFERENCES users(id),
  channel_id UUID NOT NULL REFERENCES channels(id),
  title VARCHAR(255) NOT NULL,
  slug VARCHAR(255) UNIQUE NOT NULL,
  description TEXT,
  thumbnail_url VARCHAR(255),
  video_url VARCHAR(255),
  start_seconds INT,
  duration_seconds INT,
  views INT DEFAULT 0,
  likes INT DEFAULT 0,
  is_public BOOLEAN DEFAULT true,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Clip reactions
CREATE TABLE clip_likes (
  user_id UUID NOT NULL REFERENCES users(id),
  clip_id UUID NOT NULL REFERENCES clips(id),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (user_id, clip_id)
);

Interactions & Engagement

-- Live polls
CREATE TABLE polls (
  id UUID PRIMARY KEY,
  stream_id UUID NOT NULL REFERENCES streams(id),
  question VARCHAR(255) NOT NULL,
  duration_seconds INT,
  status VARCHAR(50) DEFAULT 'active', -- active, ended
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  ended_at TIMESTAMP
);

-- Poll options
CREATE TABLE poll_options (
  id UUID PRIMARY KEY,
  poll_id UUID NOT NULL REFERENCES polls(id),
  option VARCHAR(255) NOT NULL,
  vote_count INT DEFAULT 0
);

-- Poll votes
CREATE TABLE poll_votes (
  user_id UUID NOT NULL REFERENCES users(id),
  poll_option_id UUID NOT NULL REFERENCES poll_options(id),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (user_id, poll_option_id)
);

-- Reactions/Emotes
CREATE TABLE chat_reactions (
  id UUID PRIMARY KEY,
  stream_id UUID NOT NULL REFERENCES streams(id),
  user_id UUID NOT NULL REFERENCES users(id),
  emoji VARCHAR(50),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Gifting
CREATE TABLE gifts (
  id UUID PRIMARY KEY,
  gifter_id UUID NOT NULL REFERENCES users(id),
  recipient_id UUID NOT NULL REFERENCES users(id),
  stream_id UUID REFERENCES streams(id),
  gift_type VARCHAR(50), -- custom types
  amount_cents INT NOT NULL,
  message TEXT,
  stripe_charge_id VARCHAR(255),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Gaming/Competitions

-- Tournaments
CREATE TABLE tournaments (
  id UUID PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  description TEXT,
  owner_id UUID NOT NULL REFERENCES users(id),
  game VARCHAR(255),
  status VARCHAR(50) DEFAULT 'draft', -- draft, active, ended
  max_participants INT,
  prize_pool_cents INT,
  started_at TIMESTAMP,
  ended_at TIMESTAMP,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Tournament participants
CREATE TABLE tournament_participants (
  id UUID PRIMARY KEY,
  tournament_id UUID NOT NULL REFERENCES tournaments(id),
  user_id UUID NOT NULL REFERENCES users(id),
  channel_id UUID NOT NULL REFERENCES channels(id),
  seed INT,
  final_rank INT,
  joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Leaderboards
CREATE TABLE leaderboards (
  id UUID PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  game VARCHAR(255),
  period VARCHAR(50), -- weekly, monthly, all-time
  reset_date DATE
);

-- Leaderboard entries
CREATE TABLE leaderboard_entries (
  leaderboard_id UUID NOT NULL REFERENCES leaderboards(id),
  user_id UUID NOT NULL REFERENCES users(id),
  rank INT,
  score BIGINT,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (leaderboard_id, user_id)
);

Analytics

-- Stream analytics
CREATE TABLE stream_analytics (
  id UUID PRIMARY KEY,
  stream_id UUID NOT NULL REFERENCES streams(id),
  hour_timestamp TIMESTAMP,
  viewer_count INT,
  peak_viewers INT,
  avg_watch_time_seconds INT,
  new_followers INT,
  subscriptions INT,
  donations_cents INT,
  chat_messages INT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Channel analytics (daily)
CREATE TABLE channel_analytics (
  id UUID PRIMARY KEY,
  channel_id UUID NOT NULL REFERENCES channels(id),
  date DATE,
  total_views INT,
  new_followers INT,
  subscriptions INT,
  donations_cents INT,
  revenue_cents INT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Indexes

-- Performance indexes
CREATE INDEX idx_users_username ON users(username);
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_channels_user_id ON channels(user_id);
CREATE INDEX idx_channels_slug ON channels(slug);
CREATE INDEX idx_streams_channel_id ON streams(channel_id);
CREATE INDEX idx_streams_status ON streams(status);
CREATE INDEX idx_streams_is_live ON streams(is_live);
CREATE INDEX idx_chat_messages_stream_id ON chat_messages(stream_id, created_at DESC);
CREATE INDEX idx_followers_channel_id ON followers(channel_id);
CREATE INDEX idx_followers_follower_id ON followers(follower_id);
CREATE INDEX idx_subscriptions_channel_id ON subscriptions(channel_id);
CREATE INDEX idx_subscriptions_status ON subscriptions(status);
CREATE INDEX idx_donations_channel_id ON donations(channel_id);
CREATE INDEX idx_vods_stream_id ON vods(stream_id);
CREATE INDEX idx_clips_channel_id ON clips(channel_id);