Docs
Database Flow
Database Flow
Data persistence and management with PostgreSQL using Kysely and raw SQL
Overview
Manages data persistence using PostgreSQL with Kysely and raw SQL. Handles user authentication, session management, and subscription data with proper relationships and constraints.
Main libraries/services:
kysely- Type-safe SQL query builder- Custom raw SQL repositories - Fine-grained control over complex queries
- PostgreSQL - Primary database (Neon hosting)
- SQL migrations (
/migrations) for schema management
File Map
š migrations/ - SQL migration files
š lib/db.ts - Kysely client configuration
š lib/db.types.ts - Database type definitions for Kysely
š repositories/ - Data access layer (Kysely + raw SQL)
š services/ - Business logic built on repositories
š actions/ - Server actions orchestrating database operations
Database Schema
Schema changes are managed through SQL migrations under /migrations. Key tables:
- users ā Stores profile data, OAuth linkage, and Stripe identifiers
- accounts ā OAuth provider accounts linked to users
- sessions ā NextAuth session tokens with expiry metadata
- projects / project_members / project_invitations ā Core project collaboration tables
- subscription_plans / subscriptions ā Billing and plan data
Refer to the latest migration files for the exact SQL definitions.
Step-by-Step Flow
User Creation (OAuth)
- User authenticates via OAuth provider
- NextAuth KyselyAdapter handles user creation through raw SQL repositories
- User record created with OAuth account linked
- Stripe customer ID generated (if needed)
- User role set to default "USER"
User Lookup Operations
getUserById(id)- Fetches user by IDgetUserByEmail(email)- Fetches user by emailgetCurrentUser()- Gets current session user- All operations include proper error handling
Subscription Data Management
getUserSubscriptionPlan(userId)- Fetches subscription data- Stripe webhooks update subscription fields
- Plan validation against current period end
- Pricing data matching against Stripe price IDs
Database Connection Management
- Kysely client configured with connection pooling
- Development: Cached global instance
- Production: New instance per request
- Automatic connection management
Data Flow Diagram
[NextAuth] ā [KyselyAdapter] ā [User Creation/Update] ā [Database]
ā
[Webhooks] ā [Subscription Updates] ā [Database]
ā
[Queries] ā [Kysely Client + raw SQL] ā [Database] ā [Application Logic]
Database Operations
User CRUD
- Create: Via NextAuth OAuth flow
- Read:
getUserById,getUserByEmail,getCurrentUser - Update: Via webhooks and user actions
- Delete: Cascade delete with accounts/sessions
Subscription CRUD
- Create: Via Stripe webhooks
- Read:
getUserSubscriptionPlan - Update: Via Stripe webhooks (renewals, changes)
- Delete: Via Stripe cancellation webhooks
Environment Configuration
- DATABASE_URL: PostgreSQL connection string
- Neon: Hosted PostgreSQL service
- Migrations: Managed via SQL files executed with preferred migration runner
Notes and TODOs
- ā User authentication with OAuth integration
- ā Session management with JWT strategy
- ā Stripe subscription data persistence
- ā Proper foreign key relationships
- ā Cascade delete for data integrity
- ā ļø No soft delete implementation
- š Add user profile management
- š Implement audit logging
- š Add database backup strategies
- š Consider adding user preferences table
- š Add indexes for performance optimization