How felipefl200/saas-turborepo-next works
Project Type and Purpose
• This is a monorepo SaaS application managed by Turborepo.
• Frontend: Next.js (React) web application (inferred at apps/web).
• Backend: Fastify-based REST API at apps/api.
• Shared Packages: RBAC/auth logic (packages/auth), environment utilities (packages/env).
• Database: Prisma ORM backed by a relational database (e.g., Postgres).
• Auth: Custom RBAC multi-tenant system, GitHub OAuth, password flows.
File Structure and Key Layers
• Monorepo Orchestration (turbo.json, root package.json, docker-compose.yml): central build/run.
• Apps/
• apps/api/: Fastify server
– HTTP layer: src/http/server.ts, routers in routes/*, middleware in middlewares/*, error handlers.
– Business/core: utils in src/utils/*, Prisma client in src/lib/prisma.ts.
– Data model & migrations: prisma/schema.prisma, seed scripts.
• apps/web/ (not shown but documented): Next.js UI, pages/components, UI-level RBAC.
• Packages/ (shared libraries)
• packages/auth/: RBAC rules, permission subjects, models for user/org/project, middleware hooks.
• packages/env/: Environment variable loader, type definitions.
• Config/: ESLint, Prettier, TypeScript base and overrides.
Main Components
A. User & Client
– Browser (React/Next.js SPA)
– HTTP (HTTPS)
B. Frontend (apps/web)
– Next.js Server-Side Rendering & API Routes (if any)
– UI Components & RBAC Guards (from packages/auth)
C. API Gateway / Backend (apps/api)
– Fastify HTTP Server
– Authentication & Authorization Middleware
– Route Handlers → call business logic/utilities
D. Shared Business Logic
– packages/auth RBAC engine
– packages/env configuration
E. Data Layer
– Prisma Client (auto-generated code in apps/api/src/generated/prisma)
– Relational Database (Postgres/MySQL)
F. External Services
– GitHub OAuth endpoint
– Email/SMS provider for password recovery
G. Infrastructure & DevOps
– Turborepo build pipeline
– Docker Compose for local dev
– CI/CD (e.g., GitHub Actions – inferred)
Relationships & Data Flows
• Client → Frontend: User requests page or API route → Next.js
• Frontend → Backend: REST calls over HTTPS to Fastify
• Backend:
• Auth middleware checks JWT/session → resolves user identity & roles via packages/auth.
• Route handlers invoke Prisma client to query/update database.
• Database: Prisma translates queries → SQL → Database
• External OAuth: Backend ↔ GitHub for social login
• Dev Tools: Turborepo orchestrates builds/test across packages/apps; Docker Compose spins up API + DB
Architectural Patterns & Principles
• Monorepo with Code Sharing: Turborepo sharable packages vs. siloed apps
• Layered Architecture: Presentation (Next.js), API (Fastify), Business (packages/auth), Data (Prisma)
• RBAC Pattern: Centralized permission engine in packages/auth plus middleware enforcement
• Modularization: HTTP routes separated by domain (auth, orgs, projects)
• Infrastructure as Code: Docker Compose, environment config in code
Final Checklist
• All major modules: Web, API, Auth Package, Env Package, DB, External Services.
• Labels for directories, libraries, protocols.