Skip to content

nestarc/example-saas-api

Repository files navigation

example-saas-api

Minimal NestJS API demonstrating all 6 @nestarc packages working together.

What's Inside

Package What it does in this example
@nestarc/tenancy Extracts tenant from X-Tenant-Id header, enforces RLS
@nestarc/safe-response Wraps all responses in { success, data, error }
@nestarc/audit-log Auto-tracks User create/update/delete with before/after diffs
@nestarc/feature-flag Gates GET /api/users/analytics behind PREMIUM_ANALYTICS flag
@nestarc/soft-delete DELETE /api/users/:id sets deletedAt instead of hard delete
@nestarc/pagination GET /api/users supports offset/cursor pagination + filters

Quick Start

# 1. Start PostgreSQL
docker compose up -d

# 2. Install dependencies
npm install

# 3. Apply database schema
npx prisma db push

# 4. Start the server
npm run start:dev

Server runs on http://localhost:3000/api.

API Endpoints

# Create a user (tenant-scoped, audit-logged)
curl -X POST http://localhost:3000/api/users \
  -H "Content-Type: application/json" \
  -H "X-Tenant-Id: tenant-1" \
  -H "X-User-Id: admin-1" \
  -d '{"name": "Alice", "email": "alice@example.com"}'

# List users (paginated, filtered, soft-delete aware)
curl "http://localhost:3000/api/users?page=1&limit=10&sortBy=name:ASC" \
  -H "X-Tenant-Id: tenant-1"

# Update a user (audit-log tracks before/after diff)
curl -X PATCH http://localhost:3000/api/users/<id> \
  -H "Content-Type: application/json" \
  -H "X-Tenant-Id: tenant-1" \
  -H "X-User-Id: admin-1" \
  -d '{"role": "admin"}'

# Soft-delete a user (sets deletedAt, doesn't hard delete)
curl -X DELETE http://localhost:3000/api/users/<id> \
  -H "X-Tenant-Id: tenant-1" \
  -H "X-User-Id: admin-1"

# Feature-flagged endpoint (returns 403 unless PREMIUM_ANALYTICS flag is enabled)
curl http://localhost:3000/api/users/analytics \
  -H "X-Tenant-Id: tenant-1"

Prisma Extension Order

// prisma.service.ts — order matters!
this.$extends(createPrismaTenancyExtension(...))    // 1. must be first
  .$extends(createPrismaSoftDeleteExtension(...))    // 2. before audit
  .$extends(createAuditExtension(...))               // 3. last — sees final state

Learn More

License

MIT

About

Minimal NestJS API demonstrating all 6 @nestarc packages working together

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors