NestJS Blog API

A scalable and type-safe RESTful API for a blogging platform built using NestJS, PostgreSQL, and Prisma. This backend includes modular features like authentication, user management, profile handling, post CRUD, image upload, validation, and error handling.


๐Ÿš€ Tech Stack

  • Backend Framework: NestJS (TypeScript)
  • Database: PostgreSQL with Prisma ORM
  • Validation: Zod + Global Pipes
  • Authentication: JWT (Access Token)
  • File Uploads: Multer (local disk storage)
  • API Docs: Swagger (auto-generated)
  • Error Handling: Global Filters (HTTP, Zod, Prisma)

๐Ÿ“‚ Features

โœ… Authentication

  • Register (with optional image)
  • Login (returns access token)
  • Forgot / Reset Password (email-based link with token)
  • Change Password (authenticated)

๐Ÿ‘ค User Profile

  • View own profile
  • Update name/email
  • Upload/change profile picture

๐Ÿง‘โ€๐Ÿ’ผ Admin

  • View all users
  • View individual user
  • Create, update, or delete users
  • Assign roles (user/admin)

โœ๏ธ Blog Posts

  • Create a post (with optional image)
  • Get all or single post
  • Update or delete own posts

๐Ÿ›ก๏ธ Protected Routes

  • All user and post actions require a valid JWT.
  • Admin routes are restricted to admin role only.
  • Global JwtAuthGuard and RolesGuard used.

๐Ÿงช Validation

  • Zod-based validation schemas for every request (body, params, query)
  • Global Zod Validation Pipe handles schema errors

๐Ÿ“ Project Structure

nestjs-psql-api-project/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ auth/          # Auth module (login/register/reset)
โ”‚   โ”œโ”€โ”€ user/          # Admin-only user mgmt
โ”‚   โ”œโ”€โ”€ profile/       # Profile update & view
โ”‚   โ”œโ”€โ”€ post/          # Post CRUD
โ”‚   โ”œโ”€โ”€ common/        # DTOs, filters, pipes, interceptors
โ”‚   โ”œโ”€โ”€ prisma/        # Prisma client + service
โ”‚   โ””โ”€โ”€ main.ts        # App entry point

๐Ÿ” Environment Variables

DATABASE_URL=postgresql://USER:PASSWORD@localhost:5432/DATABASE_NAME
JWT_SECRET=your_jwt_secret
JWT_EXPIRES_IN=1d

๐Ÿ“ฆ Setup & Run

git clone https://github.com/Vishvjeet-Rana/nestjs-psql-api-project.git
cd nestjs-psql-api-project
npm install

# setup db
npx prisma migrate dev --name init
npx prisma generate

# start dev server
npm run start:dev

๐Ÿ“‘ API Documentation

Swagger is available at /docs after running the project.


๐Ÿง‘ Author

Built with care by Vishvjeet Rana