src/db/schema.ts.
Schema Structure
The schema is organized into logical sections:- AUTH: User authentication and session management tables
- RATE LIMIT: API rate limiting storage
- COMMON: Shared utilities like timestamps
Naming Conventions
The project follows strict naming conventions:- Tables:
snake_casenaming enforced via Drizzle config - Columns: Automatically converted to
snake_case(e.g.,createdAt→created_at) - Schema definition: Use camelCase in TypeScript, which gets converted to snake_case in SQL
created_at, updated_at, and deleted_at in the database.
Authentication Tables
User Table
Theuser table stores core user information:
- Text-based primary key for flexibility
- Unique email constraint
- Email verification tracking
- Optional profile image
- Automatic timestamp management
Session Table
Thesession table manages user authentication sessions:
- Unique session tokens
- Expiration tracking
- IP address and user agent logging
- Cascade delete on user removal
Account Table
Theaccount table handles OAuth and authentication provider accounts:
- Multi-provider support (OAuth, credentials)
- Token management (access, refresh, ID tokens)
- Token expiration tracking
- Cascade delete on user removal
Verification Table
Theverification table stores email verification codes and other verification tokens:
- Generic verification storage
- Expiration support
- Flexible identifier system
Rate Limiting Table
Therate_limit table stores API rate limiting counters:
- UUID primary key with auto-generation
- Unique rate limit keys
- Request counting
- Last request timestamp tracking (bigint for precision)
Relationships and Foreign Keys
The schema uses foreign key constraints to maintain referential integrity:User Relationships
session and account tables use cascade delete, meaning:
- When a user is deleted, all associated sessions are automatically removed
- When a user is deleted, all associated accounts are automatically removed
Type Safety with Zod
Each table exports a Zod schema for runtime validation:- Type-safe database queries
- Runtime validation
- Automatic TypeScript types
- Schema validation for API responses
Common Patterns
Timestamp Fields
All major tables include standard timestamp fields:Soft Deletes
The schema supports soft deletes via thedeletedAt field:
null= active recordtimestamp= soft-deleted record
Primary Key Strategies
- Text IDs: Used for auth tables (user, session, account) for flexibility with external auth providers
- UUID: Used for rate limiting table with auto-generation
