A modern, production-ready Fastify-based REST API starter with TypeScript, PostgreSQL, Drizzle ORM, and comprehensive tooling. Features a complete task management system as an example implementation.
- Fastify 5.x - High-performance web framework
- TypeScript 5.x - Full type safety and modern JavaScript features
- ES Modules - Native ES module support
- Node.js 22+ - Latest LTS version support
- PostgreSQL - Robust relational database
- Drizzle ORM - Type-safe SQL ORM with excellent TypeScript support
- Database Migrations - Automated schema management
- Drizzle Studio - Visual database browser
- In-Memory Testing - PGlite for fast, isolated tests
- JWT Authentication - Stateless token-based auth
- Request Validation - Zod schema validation
- OpenAPI 3.0 - Standard API specification
- Swagger UI - Interactive API documentation
- Scalar API Reference - Modern API documentation UI
- Auto-generated Schemas - Zod to OpenAPI transformation
- Live Documentation - Available at
/docs
endpoint
- Vitest - Fast unit and integration testing
- In-Memory Database - Isolated test environment
- Comprehensive Test Coverage - Route and integration tests
- ESLint - Code quality and consistency
- TypeScript Strict Mode - Compile-time type checking
- Pre-commit Hooks - Automated code quality checks
- Hot Reloading - Development server with file watching
- Pretty Logging - Colored, structured logs with Pino
- Path Aliases - Clean imports with
@/
prefix - Auto-route Loading - Automatic route discovery
- Environment Management - Type-safe configuration
- GitHub Actions - Automated testing and linting
- PostgreSQL Service - CI database integration
- Caching - Optimized dependency installation
- Multi-branch Support - Main and develop workflows
- pnpm - Fast, efficient package manager
- Workspace Support - Monorepo capabilities
- Lockfile - Reproducible builds
- Node.js (v22.14.1 or later)
- pnpm (v10.8.1 or later)
- PostgreSQL database running
git clone <repository-url>
cd fastify-starter
pnpm install
cp env.example .env
# Edit .env with your configuration:
# DATABASE_URL=postgresql://user:password@localhost:5432/database
# JWT_SECRET=your-secret-key-at-least-32-characters
# Generate and run migrations
pnpm run db:generate
pnpm run db:migrate
# Seed with sample data
pnpm run db:seed
pnpm run dev
The API will be available at http://localhost:3000
- Interactive Docs:
http://localhost:3000/docs
- OpenAPI JSON:
http://localhost:3000/json
- Health Check:
http://localhost:3000/health
# Run all tests
pnpm test
# Watch mode
pnpm test:watch
# Test-specific database seeding
pnpm run db:seed:test
- In-memory PostgreSQL - Fast, isolated test environment
- Automatic cleanup - Fresh database state per test
- JWT mocking - Simplified authentication testing
- Route testing - Comprehensive endpoint coverage
# Generate migrations from schema changes
pnpm run db:generate
# Apply pending migrations
pnpm run db:migrate
# Open Drizzle Studio (visual database browser)
pnpm run db:studio
# Seed database with sample data
pnpm run db:seed
# Drop all tables (β οΈ destructive)
pnpm run db:drop
- Type-safe schemas - Drizzle ORM with TypeScript
- Migration management - Version-controlled schema changes
- Visual browser - Drizzle Studio for data inspection
- Sample data - Realistic test data generation
pnpm run dev # Start development server with hot reload
pnpm run build # Build for production
pnpm run start # Start production server
pnpm run test # Run test suite
pnpm run test:watch # Run tests in watch mode
pnpm run lint # Run ESLint
pnpm run lint:fix # Fix linting issues automatically
pnpm run type-check # TypeScript type checking
pnpm run openapi:generate # Generate OpenAPI specification
src/
βββ modules/ # Feature modules
β βββ auth/ # Authentication routes
β βββ task/ # Task management (example)
βββ plugins/ # Fastify plugins
β βββ db.ts # Database connection
β βββ jwt.ts # JWT authentication
β βββ openapi.ts # API documentation
β βββ sensible.ts # Error handling
βββ scripts/ # Utility scripts
β βββ db/ # Database management
β βββ test/ # Test configuration
βββ index.ts # Application entry point
βββ server.ts # Server configuration
For development and testing, generate a test JWT token:
curl http://localhost:3000/auth/token
Use the authenticate
decorator to protect routes:
server.get('/protected', {
preHandler: server.authenticate()
}, async (request, reply) => {
// Route logic here
});
GET /auth/token
- Generate test JWT token
POST /task
- Create a new taskGET /task
- List all tasksPUT /task/:id
- Update a taskDELETE /task/:id
- Delete a task
GET /health
- Health check endpointGET /docs
- API documentationGET /json
- OpenAPI specification
- Runs on push to
main
/develop
and pull requests - Sets up PostgreSQL service container
- Runs complete test suite
- Generates test coverage reports
- Runs on push to
main
/develop
and pull requests - ESLint code quality checks
- TypeScript type checking
- Ensures code consistency
Test the workflows locally:
pnpm lint # Run linting
pnpm type-check # Run type checking
pnpm test # Run tests
pnpm build # Build project
NODE_ENV=development # Environment (development, production, test)
PORT=3000 # Server port
HOST=0.0.0.0 # Server host
DATABASE_URL=postgresql://... # PostgreSQL connection string
JWT_SECRET=your-secret-key # JWT signing secret (min 32 chars)
- Strict mode enabled
- Path aliases configured (
@/
prefix) - ES modules support
- Modern target (ES2022)
# Build the application
pnpm run build
# Start production server
pnpm run start
- Set
NODE_ENV=production
- Configure production
DATABASE_URL
- Set secure
JWT_SECRET
- Configure reverse proxy (nginx, etc.)
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all CI checks pass
- Submit a pull request
- Follow TypeScript strict mode
- Write tests for new routes
- Use ESLint for code quality
- Follow conventional commit messages
- Update documentation as needed
[Specify your license here]
For issues and questions:
- Check the documentation
- Review existing issues
- Create a new issue with detailed information
Built with β€οΈ using Fastify, TypeScript, and Drizzle ORM