How andriishupta/nestjs-supabase-setup works
Project Type & Purpose
• This is a backend API project built with NestJS.
• Its primary purpose is to demonstrate how to integrate Supabase (v1) with NestJS, focusing on authentication and the creation of a request-scoped Supabase client.
• The README also points to setting up guards (using NestJS’s APP_GUARD mechanism) to secure routes via Supabase authentication.
Main Components to Highlight
• Client (or API Consumer): Represents the external system or user making HTTP requests with an access_token in the Authorization header.
• Controller Layer: This is where incoming HTTP requests are handled. In the file tree, the AppController in src/app.controller.ts is the entry point that routes requests.
• Service Layer: For business logic implementation; the AppService located in src/app.service.ts exemplifies this.
• NestJS Module: The AppModule (src/app.module.ts) orchestrates dependencies and organizes the overall application structure.
• Supabase Integration/Module: All Supabase-related functionality (authentication, client creation) is encapsulated in the supabase folder under src/common/supabase. This includes:
• The SupabaseModule (src/common/supabase/supabase.module.ts) for module-level configuration.
• The SupabaseGuard (src/common/supabase/supabase.guard.ts) which ensures that requests are authenticated.
• Supporting files like the index, strategy, and supabase.ts for client interaction and instantiation scoped to each request.
• NestJS Bootstrapping: The main entry point (src/main.ts) that initializes the NestJS application.
• Configuration & Environment: Files like .env.example indicate environment-specific configurations crucial for connecting to Supabase (credentials, tokens, etc.).
Relationships and Interactions
• The Client sends HTTP requests (with an access_token) to the API.
• The request first reaches the Controller, which then delegates processing to the Service layer.
• Before processing, the SUPABASE_GUARD (applied globally or per route) validates and injects the correct Supabase client instance using a request-scoped provider.
• The SupabaseModule encapsulates the logic of creating and managing the Supabase client, ensuring that each request receives its own context.
• The Service then uses the per-request Supabase client (via supabase.getClient()) to interact with Supabase (e.g., for data queries and authentication checks).
Architectural Patterns & Principles
• Dependency Injection: A core principle in NestJS is the injection of dependencies (like Supabase clients) to manage component lifecycles.
• Request Scoping: Particularly important here is the request-scoped provider for the Supabase client.
• Separation of Concerns: Clearly separates controller, service, and module logic. The Supabase integration is modularized within its own common directory.
• Global and Route-Specific Guards: Use of APP_GUARD to apply authentication globally or via decorators on specific routes.
Relevant Technologies & Frameworks
• NestJS for the core framework and architectural pattern.
• Supabase as the backend-as-a-service handling database and auth.
• TypeScript, as indicated by the tsconfig files.
• Testing frameworks (Jest based on the test folder) for e2e tests.
• Configuration tools such as ESLint, Prettier, and environment files for code quality and environment management.
• "Client (HTTP Request)"
• "NestJS Controller" (AppController)
• "NestJS Service" (AppService)
• "NestJS Module" (AppModule)
• "Supabase Module" encompassing:
• SupabaseGuard
• SupabaseStrategy
• Supabase Client Provider (per-request instance)
• "Configuration/Environment" for environment variables and .env file content.
• Optionally, show a return flow (response sent back to the client).