jason-hollander/fastify-rules icon
public
Published on 4/24/2025
jason-hollander/fastify-rules

Rules

@luva-ca/api Fastify Backend Development Guidelines

Architecture & Core Principles

  • Build a scalable and maintainable Fastify v5 backend API
  • Implement proper plugin-based architecture using fastify-plugin
  • Structure code with clear separation of concerns (routes, controllers, services, models)
  • Use MongoDB for data persistence via @fastify/mongodb
  • Use Redis for caching and session management via @fastify/redis and ioredis
  • Implement proper error handling and validation

Authentication & Security

  • Use @fastify/jwt for token-based authentication
  • Implement secure password hashing with argon2 (preferred) or bcryptjs
  • Apply security best practices using @fastify/helmet
  • Set up proper CORS policies with @fastify/cors
  • Create middleware for role-based access control
  • Implement proper token refresh mechanisms

API Design & Documentation

  • Design RESTful endpoints following API best practices
  • Document API using @fastify/swagger and @fastify/swagger-ui
  • Implement proper HTTP status codes and responses using @fastify/sensible
  • Use JSON Schema for request/response validation
  • Follow consistent naming conventions for endpoints

Data Validation & Schema

  • Use Zod for robust schema validation
  • Create reusable validation schemas
  • Implement proper request validation pipelines
  • Ensure proper error messages for validation failures

Database Operations

  • Create efficient MongoDB queries and indexes
  • Implement proper connection pooling
  • Use MongoDB transactions where appropriate
  • Create data models with proper validation
  • Implement efficient pagination strategies

Caching & Performance

  • Implement Redis caching for frequently accessed data
  • Create proper cache invalidation strategies
  • Optimize response times with appropriate caching headers
  • Use Redis for rate limiting and request throttling

Event Processing

  • Implement event-driven architecture using Inngest
  • Create proper event handlers and processors
  • Design reliable retry mechanisms
  • Implement proper logging for event processing

Testing & Quality Assurance

  • Write unit and integration tests for API endpoints
  • Mock external dependencies in tests
  • Test edge cases and error scenarios
  • Implement proper test coverage for critical paths

Logging & Monitoring

  • Implement structured logging
  • Set up proper error tracking
  • Create health check endpoints
  • Implement performance monitoring

Code Quality & Standards

  • Follow consistent code style and formatting
  • Implement proper TypeScript typing
  • Use async/await patterns consistently
  • Create reusable utility functions
  • Document code with JSDoc comments

Expectations

  • Provide complete, working code snippets
  • Include proper error handling in all examples
  • Focus on scalable, production-ready implementations
  • Ensure security best practices throughout
  • ASSUME ADVANCED KNOWLEDGE - skip basic explanations
  • DO NOT EXPLAIN HOW TO SET UP THE PROJECT - focus on implementation details

When requesting specific features or endpoints, I'll provide context and requirements for you to generate appropriate code. Ask questions for clarity.