Skip to content

florianzemma/nestjs-template

Repository files navigation

NestJS Template

Production-ready NestJS API starter with Prisma, Pino logging, Swagger, Throttler, and global exception handling.

Stack

  • NestJS 11 — framework
  • Prisma 6 — ORM + migrations
  • nestjs-pino — structured JSON logging (pino-pretty in dev)
  • @nestjs/swagger — OpenAPI docs at /api/docs
  • @nestjs/throttler — rate limiting via global guard
  • class-validator + class-transformer — DTO validation
  • Vitest — unit testing
  • Husky — Git hooks
  • lint-staged — run Prettier only on staged files
  • commitlint — enforce conventional commit format
  • Prettier — code formatting

Getting started

# 1. Install dependencies
pnpm install

# 2. Initialize Husky hooks (run once after cloning)
pnpm prepare

# 3. Configure environment
cp .env.example .env
# Edit DATABASE_URL and other vars

# 4. Generate Prisma client
pnpm prisma generate

# 5. Run migrations
pnpm prisma migrate dev --name init

# 6. Start dev server
pnpm dev

Project structure

src/
├── main.ts                          # Bootstrap (CORS, Swagger, ValidationPipe)
├── app.module.ts                    # Root module (LoggerModule, PrismaModule, ThrottlerModule)
├── prisma/
│   ├── prisma.module.ts             # Global Prisma module
│   └── prisma.service.ts            # PrismaClient wrapper
└── common/
    └── filters/
        └── http-exception.filter.ts # Global exception filter

Git hooks

Hook What it does
pre-commit Runs lint-staged (Prettier on staged files)
commit-msg Validates commit message format (commitlint)

Commit message format

Follows Conventional Commits:

<type>(<scope>): <description>

Allowed types: feat, fix, docs, refactor, test, chore, perf, style, ci, build, revert

Prisma commands

pnpm prisma migrate dev --name <migration-name>   # Create & apply migration
pnpm prisma migrate deploy                         # Apply migrations in production
pnpm prisma studio                                 # Open Prisma Studio GUI
pnpm prisma generate                               # Regenerate client after schema changes

Adding a new module

nest g module <name>
nest g service <name>
nest g controller <name>

Register the module in app.module.ts imports.

Environment variables

Variable Description Default
NODE_ENV development or production development
PORT HTTP port 3001
DATABASE_URL PostgreSQL connection string
ALLOWED_ORIGINS Comma-separated CORS origins http://localhost:3000

Scripts

Command Description
pnpm dev Dev server with hot-reload
pnpm build Compile to dist/
pnpm start Run compiled build
pnpm lint ESLint check
pnpm test Run unit tests
pnpm test:coverage Run tests with coverage
pnpm prepare Install Husky hooks

About

Production-ready NestJS starter: Prisma, Pino, Swagger, Throttler, class-validator

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors