Skip to content
This repository was archived by the owner on Aug 3, 2025. It is now read-only.

nows-edu/backend

Repository files navigation

MVP Backend - Monolithic NestJS Application

A simplified, monolithic NestJS backend application with Prisma ORM for rapid MVP development.

Features

  • Monolithic Architecture: Single NestJS application instead of microservices
  • Prisma ORM: Type-safe database access with PostgreSQL
  • User Management: CRUD operations for users
  • Product Management: CRUD operations for products with user relationships
  • Validation: Built-in request validation with class-validator
  • Docker Support: PostgreSQL database via Docker Compose

Quick Start

Prerequisites

  • Node.js 18+
  • Docker and Docker Compose
  • npm or yarn

Installation

  1. Clone and install dependencies:
npm install
  1. Set up environment variables:
cp .env.example .env
# Edit .env with your database credentials
  1. Start the database:
npm run db:up
  1. Run database migrations:
npm run prisma:migrate
  1. Start the development server:
npm run start:dev

The application will be available at http://localhost:3000

Safe Development Workflow

Daily Development (Recommended)

npm run dev

This command safely:

  • Checks if the database is already running (starts only if needed)
  • Runs database migrations
  • Starts the development server

Fresh Start (When needed)

npm run dev:fresh

Use this when you need a completely clean environment:

  • Restarts the PostgreSQL container
  • Runs database migrations
  • Starts the development server

Troubleshooting Commands

# Check database status
npm run db:status

# Stop database
npm run db:down

# Restart database only
npm run db:restart

# Check what's using port 3000
lsof -ti:3000

# Kill process on port 3000
kill $(lsof -ti:3000)

Common Issues & Solutions

"Port 3000 already in use":

kill $(lsof -ti:3000)
npm run dev

"Container name already in use":

npm run dev:fresh

Database connection issues:

npm run db:restart
npm run prisma:migrate
npm run start:dev

Available Scripts

Development

  • npm run start:dev - Start development server with hot reload
  • npm run dev - Start database, migrate, and run dev server
  • npm run build - Build the application
  • npm run start:prod - Start production server

Database

  • npm run db:up - Start PostgreSQL database
  • npm run db:down - Stop database
  • npm run prisma:migrate - Run database migrations
  • npm run prisma:generate - Generate Prisma client
  • npm run prisma:studio - Open Prisma Studio
  • npm run prisma:reset - Reset database

Code Generation

  • npm run res:create <name> - Generate module, controller, service, and DTO folder (Prisma-friendly)

Code Quality

  • npm run format - Format code with Prettier
  • npm run lint - Lint and fix code with ESLint
  • npm run test - Run unit tests
  • npm run test:watch - Run tests in watch mode
  • npm run test:cov - Run tests with coverage
  • npm run test:e2e - Run end-to-end tests

Code Generation

Creating New Resources

For Prisma-based projects, use the custom resource generator:

npm run res:create video-streaming

This creates:

  • Module: src/video-streaming/video-streaming.module.ts
  • Controller: src/video-streaming/video-streaming.controller.ts
  • Service: src/video-streaming/video-streaming.service.ts
  • DTO folder: src/video-streaming/dto/

No entities are generated since we use Prisma schema instead.

API Endpoints

Users

  • GET /users - Get all users
  • GET /users/:id - Get user by ID
  • POST /users - Create new user
  • PUT /users/:id - Update user
  • DELETE /users/:id - Delete user

Products

  • GET /products - Get all products
  • GET /products?ownerId=:id - Get products by owner
  • GET /products/:id - Get product by ID
  • POST /products - Create new product
  • PUT /products/:id - Update product
  • DELETE /products/:id - Delete product

Project Structure

src/
├── app.module.ts          # Main application module
├── main.ts               # Application entry point
├── prisma/
│   └── prisma.service.ts # Prisma service
├── user/                 # User module
│   ├── user.controller.ts
│   ├── user.service.ts
│   ├── user.module.ts
│   └── dto/
└── product/              # Product module
    ├── product.controller.ts
    ├── product.service.ts
    ├── product.module.ts
    └── dto/
  1. Generate Prisma client:

    pnpm prisma:generate
  2. Build the project:

    pnpm build
  3. Start all services:

    pnpm dev

Alternatively, run all steps at once:

pnpm start:all

API Endpoints

User Service (port 3001)

  • POST /api/users - Create a new user

    {
      "email": "user@example.com",
      "name": "John Doe"
    }
  • GET /api/users - Get all users

Product Service (port 3002)

  • POST /api/products - Create a new product

    {
      "name": "Product Name",
      "price": 29.99,
      "ownerId": "user_id"
    }
  • GET /api/products - Get all products

  • GET /api/products/owner/:ownerId - Get products by owner ID

Testing

Run the end-to-end test script:

./e2e-test.sh

Database Management

View your database with Prisma Studio:

pnpm prisma:studio

Stopping the Services

Stop all services:

Ctrl+C in the terminal where the services are running

Stop and remove the PostgreSQL container:

pnpm db:down

Database Schema

The application uses a simple schema with Users and Products:

  • User: id, email, name, products[]
  • Product: id, name, price, ownerId, owner

Development

Adding New Features

  1. Create a new module: nest g module feature-name
  2. Add service: nest g service feature-name
  3. Add controller: nest g controller feature-name
  4. Update database schema in prisma/schema.prisma
  5. Run migration: npm run prisma:migrate

Testing

npm run test          # Unit tests
npm run test:e2e      # End-to-end tests
npm run test:cov      # Test coverage
./test-api.sh         # API endpoint tests

Deployment

  1. Build the application: npm run build
  2. Set production environment variables
  3. Run migrations: npm run prisma:migrate
  4. Start: npm run start:prod

Environment Variables

DATABASE_URL="postgresql://mvpuser:mvppassword@localhost:5433/mvpdb?schema=public"
PORT=3000
NODE_ENV=development

Migration from Microservices

This project was simplified from a complex microservices architecture to a monolithic structure perfect for MVP development. See MIGRATION_SUMMARY.md for detailed information about the transformation process and benefits achieved.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published