# Supabase Backend Infrastructure

This Nx app contains the Supabase backend infrastructure for BuzzTrail, including database migrations and Edge Functions.

## Overview

This app was created as part of **Issue #179** to separate permanent Supabase infrastructure from temporary Motia code that was previously mixed in `apps/slide-system/`.

### Architecture Decision

**Why an Nx app (not root `/supabase/`)?**

After researching Supabase + Nx monorepo best practices (2024-2025), we chose to create an Nx app for several key reasons:

✅ **Nx Integration**: Can use `nx affected` to detect Supabase changes
✅ **CI/CD**: Path-based triggers on `apps/supabase-backend/**`
✅ **Workspace Integration**: Declared as pnpm workspace for dependency management
✅ **Semantic Clarity**: Infrastructure in `apps/`, client code in `libs/`
✅ **Consistent Tooling**: All Supabase operations through Nx targets

## Structure

```
apps/supabase-backend/
├── supabase/
│   ├── config.toml              # Supabase project configuration
│   ├── migrations/              # Database migrations (7 files)
│   ├── functions/               # Supabase Edge Functions
│   ├── MIGRATION_GUIDE.md       # Migration documentation
│   ├── MIGRATION_CHEATSHEET.md  # Quick reference
│   └── README.md                # Supabase-specific docs
├── package.json                 # Workspace declaration
├── project.json                 # Nx targets
├── README.md                    # This file
└── LOCAL-DEVELOPMENT.md         # Local dev guide
```

## Nx Targets

All Supabase operations are available as Nx targets:

### Database Operations

```bash
# Pull schema from remote
pnpm nx run supabase-backend:db:pull

# Push migrations to remote
pnpm nx run supabase-backend:db:push

# Create new migration
pnpm nx run supabase-backend:migration:new my-migration
```

### Edge Functions

```bash
# Create new Edge Function
pnpm nx run supabase-backend:functions:new my-function

# Serve functions locally
pnpm nx run supabase-backend:functions:serve

# Deploy functions to production
pnpm nx run supabase-backend:functions:deploy
```

## Direct Supabase CLI

You can also use Supabase CLI directly with the `--workdir` flag:

```bash
# From project root
supabase status --workdir apps/supabase-backend/supabase
supabase migration list --workdir apps/supabase-backend/supabase

# Or cd into the directory
cd apps/supabase-backend
supabase status
```

## CI/CD Integration

GitHub Actions can trigger on changes to this app:

```yaml
on:
  push:
    paths:
      - 'apps/supabase-backend/supabase/migrations/**'
      - 'apps/supabase-backend/supabase/functions/**'
```

Or use Nx affected:

```yaml
- name: Push migrations (if affected)
  run: pnpm nx affected:db:push
```

## Related Libraries

This app manages **infrastructure only**. Client libraries are separate:

- **`libs/slidev/supabase-client.ts`** - Shared Supabase client wrapper
- **`libs/gating/src/lib/supabase/`** - Domain-specific Supabase code (lead submission)

## Migration History

Moved from `apps/slide-system/supabase/` as part of Issue #179 to:
1. Separate permanent infrastructure from temporary Motia code
2. Enable clean removal of Motia in the future
3. Provide semantic clarity for backend infrastructure location

## Resources

- [Supabase Documentation](https://supabase.com/docs)
- [Supabase CLI Reference](https://supabase.com/docs/reference/cli/introduction)
- [Supabase in Monorepos](https://www.rapidevelopers.com/supabase-tutorial/how-to-use-supabase-in-a-monorepo)
- [Nx Documentation](https://nx.dev)
