Skip to content

pdldipak/nodejs-typescript-rest-api-postgres

Repository files navigation

Node TypeScript PostgreSQL REST API

Production-ready RESTful API built with Node.js, TypeScript, Express, Prisma, PostgreSQL, Zod, ESLint, Prettier, Jest, and Docker.

Folder Structure

.
├── prisma
│   ├── migrations
│   │   ├── 20260427220500_init
│   │   │   └── migration.sql
│   │   └── 20260427222212_user_table
│   │       └── migration.sql
│   └── schema.prisma
├── src
│   ├── config
│   │   ├── env.ts
│   │   └── prisma.ts
│   ├── controllers
│   │   └── user.controller.ts
│   ├── middleware
│   │   ├── asyncHandler.ts
│   │   ├── errorHandler.ts
│   │   ├── notFound.ts
│   │   └── validate.ts
│   ├── repositories
│   │   └── user.repository.ts
│   ├── routes
│   │   ├── index.ts
│   │   └── user.routes.ts
│   ├── schemas
│   │   └── user.schema.ts
│   ├── services
│   │   └── user.service.ts
│   ├── types
│   │   └── pagination.ts
│   ├── utils
│   │   └── AppError.ts
│   ├── app.ts
│   └── server.ts
├── tests
│   └── user.schema.test.ts
├── docker-compose.yml
├── Dockerfile
├── eslint.config.js
├── jest.config.ts
├── package.json
└── tsconfig.json

API Endpoints

Base URL: http://localhost:3000/api

  • GET /
  • GET /health
  • GET /users?page=1&limit=10&search=jane
  • GET /users/:id
  • POST /users
  • PUT /users/:id
  • DELETE /users/:id

Example POST /users body:

{
  "name": "Dipak Poudel",
  "email": "dipak@example.com"
}

Setup

  1. Install dependencies:
npm install
  1. Copy environment variables:
cp .env.example .env
  1. Start PostgreSQL with Docker:
docker compose up -d postgres
  1. Generate the Prisma client and run migrations:
npm run prisma:generate
npm run prisma:migrate
  1. Start development mode:
npm run dev

Production

Build and run locally:

npm run build
npm start

Run the full stack with Docker:

docker compose up --build

Quality Checks

npm run lint
npm run format:check
npm test

Notes

  • Input validation and normalization are handled with Zod.
  • Prisma parameterized queries protect database access from SQL injection.
  • Helmet, CORS, request logging, global error handling, graceful shutdown, pagination, and search are included.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors