This is a production-ready NestJS backend template within an Nx Monorepo, featuring DDD architecture, Authentication, and Prisma.
- Node.js v20+
- Docker & Docker Compose
-
Install dependencies
npm install
-
Environment Setup Copy
.env.exampleto.envand adjust values if needed.cp .env.example .env
Required Variables:
DATABASE_URL: Connection string for PostgreSQL (e.g.,postgresql://user:pass@localhost:5432/db?schema=public).BETTER_AUTH_URL: The base URL for auth endpoints (e.g.,http://localhost:3000/api/auth).BETTER_AUTH_SECRET: A secure random string for signing session tokens.
-
Database Setup Start the PostgreSQL container:
npm run db:up
Run migrations:
npm run db:migrate
-
Start Application
npm start
The API will be available at
http://localhost:3000/api. Swagger Docs:http://localhost:3000/docs.
You can run the entire stack (App + Database) using Docker Compose:
docker-compose up -d- Authentication: Better-Auth integrated with Prisma (Email/Password supported).
- Database: Prisma ORM with PostgreSQL.
- Logging: Structured JSON logging with
pinoand context tracking. - Architecture: Domain-Driven Design (DDD) with clear separation of concerns (Domain, Application, Infrastructure, Presentation).
- Security: Helmet, CORS, and Zod environment validation.
- Health Checks: Ready for k8s/Docker health monitoring (
/health,/health/ready).
apps/backend-template/src:common: Shared filters, guards (SessionGuard), interceptors.config: Typed configuration with validation.database: Prisma service and module.health: Health checks.modules: Feature modules (e.g.,user,auth).domain: Entities, Services, Repository Interfaces.infrastructure: Repository Implementations, Mappers.dto: Data Transfer Objects.
Run unit tests (Mocked persistence):
npm testRun e2e tests:
npm run e2eNote: E2E tests require a running database instance as configured in your .env.