Database Schema
Overview of the database schema and types used in the application.
This document provides an overview of the database schemas and types used throughout the application. All database types are automatically inferred using Drizzle's $inferSelect utility, ensuring type safety across the application.
Core Schemas
Users & Authentication
users
auth
Workspaces
workspaces
workspaceMembers
invitations
Permissions & Access Control
permissions
roles
Feature Schemas
Notifications
notifications
userNotificationSettings
Billing & Subscriptions
subscriptions
Integration
items
Type Definitions
The following TypeScript types are automatically inferred from the schema, ensuring type safety throughout the application.
Inferred Types
Schema Organization
All schema definitions are organized in separate files under
server/drizzle/schema/
and exported through a central
index.ts
file.
File Structure
This modular approach allows for:
- Better code organization and maintainability
- Easier schema updates and migrations
- Clear separation of concerns
- Type-safe database operations
Key Features
Type Safety
🔄 Full TypeScript integration with Drizzle ORM
Schema Migrations
📊 Automated migration generation and application
Query Building
🔍 Type-safe query building with autocompletion
Validation
Database Operations
Query Examples
Relational Queries
Schema Migrations
When adding new tables or modifying existing ones, make sure to: 1. Update the schema files 2. Generate and run migrations 3. Update type definitions if needed
Migration Commands
Migration Process
- Update your schema files with new tables or columns
- Run
npm run db:generate
to create a new migration file - Review the generated SQL in the migration file
- Apply the migration with
npm run db:migrate
- Update any affected type definitions or queries
Best Practices
Schema Design
- Use UUIDs for primary keys
- Include created_at and updated_at timestamps
- Define explicit foreign key relationships
- Use appropriate PostgreSQL column types
Type Safety
- Use inferred types from schema definitions
- Create explicit type definitions for complex objects
- Validate user input with Zod before database operations
- Avoid manual type assertions when possible
Query Construction
- Use the query builder for type-safe queries
- Implement pagination for large data sets
- Use transactions for related operations
- Handle database errors appropriately
Additional Resources
Drizzle ORM Documentation
Complete documentation for Drizzle ORM
PostgreSQL Documentation
PostgreSQL database documentation
TypeScript Types
See the tRPC documentation for information on using these types with the API