Overview
Cajas uses PostgreSQL (via Supabase) with Row Level Security (RLS) enabled on all tables. The schema supports user management, case opening, inventory tracking, and provably fair gaming verification.Entity Relationship Diagram
Tables
profiles
Extended user profile information. One-to-one withauth.users.
| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PRIMARY KEY, REFERENCES auth.users | User ID from Supabase Auth |
updated_at | timestamptz | nullable | Last profile update |
full_name | text | nullable | User’s full name |
avatar_url | text | nullable | Profile picture URL |
dni | text | nullable | National ID document |
phone | text | nullable | Phone number |
address | text | nullable | Physical address |
role | text | DEFAULT ‘user’, CHECK | User role: ‘user’ or ‘admin’ |
- Public read: Anyone can view profiles
- Users can insert their own profile
- Users can update their own profile
users
Public user information for the gaming platform.| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PRIMARY KEY, REFERENCES auth.users | User ID |
username | text | UNIQUE, nullable | Display username |
avatar_url | text | nullable | Avatar image URL |
balance | numeric | DEFAULT 0 | Virtual currency balance |
client_seed | text | nullable | Deprecated: moved to user_seeds |
nonce | integer | DEFAULT 0 | Deprecated: moved to user_seeds |
created_at | timestamptz | DEFAULT now() | Account creation timestamp |
- Public profiles are viewable by everyone
- Users can insert their own profile (via trigger)
- Users can update their own profile
cases
Available cases that users can open.| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PRIMARY KEY, DEFAULT gen_random_uuid() | Unique case ID |
name | text | NOT NULL | Case display name |
slug | text | UNIQUE, NOT NULL | URL-friendly identifier |
description | text | nullable | Case description |
price | numeric | NOT NULL, CHECK (price >= 0) | Cost to open case |
image_url | text | NOT NULL | Case image URL |
created_at | timestamptz | DEFAULT now() | Creation timestamp |
- Public read: All users can view cases
- Admins insert: Only admins can create cases
- Admins update: Only admins can modify cases
- Admins delete: Only admins can delete cases
case_items
Items that can be won from each case.| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PRIMARY KEY, DEFAULT gen_random_uuid() | Unique item ID |
case_id | uuid | REFERENCES cases ON DELETE CASCADE | Parent case |
name | text | NOT NULL | Item name |
value | numeric | NOT NULL, CHECK (value >= 0) | Item monetary value |
image_url | text | NOT NULL | Item image URL |
probability | numeric | NOT NULL, CHECK (probability greater than 0 AND less than or equal to 100) | Drop chance percentage |
created_at | timestamptz | DEFAULT now() | Creation timestamp |
- Public read: All users can view items
- Admins insert/update/delete: Only admins can modify
items
Master list of all unique items in the system.| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PRIMARY KEY, DEFAULT gen_random_uuid() | Unique item ID |
name | text | NOT NULL | Item name |
image_url | text | nullable | Item image URL |
rarity | text | NOT NULL | Rarity tier: common, rare, epic, legendary |
price | numeric | NOT NULL | Item base value |
created_at | timestamptz | DEFAULT now() | Creation timestamp |
- Public read: All users can view items
user_items
User inventory - items won from opening cases.| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PRIMARY KEY, DEFAULT gen_random_uuid() | Unique record ID |
user_id | uuid | REFERENCES users NOT NULL | Item owner |
item_id | uuid | REFERENCES items NOT NULL | Item reference |
status | text | DEFAULT ‘in_inventory’ | Status: in_inventory, sold, withdrawn |
created_at | timestamptz | DEFAULT now() | When item was won |
- Users can view only their own items
transactions
Financial transaction history.| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PRIMARY KEY, DEFAULT gen_random_uuid() | Transaction ID |
user_id | uuid | REFERENCES users NOT NULL | User performing transaction |
amount | numeric | NOT NULL | Transaction amount |
type | text | NOT NULL | Type: deposit, withdraw, case_open, item_sell |
reference_id | uuid | nullable | Link to case or item |
created_at | timestamptz | DEFAULT now() | Transaction timestamp |
- Users can view only their own transactions
user_seeds
Provably fair seed pairs for each user.| Column | Type | Constraints | Description |
|---|---|---|---|
user_id | uuid | PRIMARY KEY, REFERENCES auth.users | User ID |
server_seed | text | NOT NULL | Server-generated secret seed |
client_seed | text | NOT NULL | User-provided or client-generated seed |
nonce | bigint | NOT NULL, DEFAULT 0 | Incrementing counter for each game |
created_at | timestamptz | DEFAULT now() | Initial seed creation |
updated_at | timestamptz | DEFAULT now() | Last seed update |
- Users can view their own seeds
- Users can update their own seeds
- Users can insert their own seeds
game_rolls
Audit log of all game rolls for provably fair verification.| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PRIMARY KEY, DEFAULT gen_random_uuid() | Roll ID |
user_id | uuid | REFERENCES auth.users NOT NULL | Player |
case_id | uuid | REFERENCES cases NOT NULL | Case opened |
server_seed | text | NOT NULL | Server seed used for this roll |
client_seed | text | NOT NULL | Client seed used for this roll |
nonce | bigint | NOT NULL | Nonce value for this roll |
roll_result | bigint | NOT NULL | Raw random number generated |
item_won_id | uuid | REFERENCES items NOT NULL | Item won |
created_at | timestamptz | DEFAULT now() | Roll timestamp |
- Users can view their own rolls
admin_logs
Audit trail of admin actions.| Column | Type | Constraints | Description |
|---|---|---|---|
id | uuid | PRIMARY KEY, DEFAULT gen_random_uuid() | Log ID |
admin_id | uuid | REFERENCES auth.users, nullable | Admin user |
action | text | NOT NULL | Action performed |
details | jsonb | nullable | Additional action details |
created_at | timestamptz | DEFAULT now() | Action timestamp |
- Admins can view logs
- Admins can insert logs
Database Usage Examples
Fetching Cases with Items
Creating a Case (Admin Only)
Checking User Balance
Type Safety
All database types are auto-generated intypes/supabase.ts:
Migrations
Database schema is managed through migration files insupabase/migrations/:
20240101000000_init.sql- Initial schema with users, cases, items, transactions0000_create_cases_system.sql- Cases and admin system20251205120000_fix_case_items_schema.sql- Case items schema update20251209000000_create_provably_fair.sql- Provably fair system tables
Related Documentation
- System Architecture - Overall system design
- Authentication - Auth and RLS details
- Provably Fair Implementation - How seeds and rolls work
