streamflix-api / README.md
Akshar2325
feat(readme): update README metadata for project details
9026edb
|
Raw
History Blame
7.74 kB
metadata
title: Streamflix Node API
emoji: 🎬
colorFrom: red
colorTo: purple
sdk: docker
app_file: Dockerfile
pinned: false

πŸš€ NestJS Starter Template

A production-ready NestJS starter template with authentication, Prisma ORM, and best practices built-in. Clone this repo to kickstart any new project without repetitive setup.

✨ Features

  • Authentication System - Complete JWT-based auth for Users and Super Admins
  • Prisma ORM - Type-safe database access with PostgreSQL
  • Modular Architecture - Clean separation of concerns with core services
  • Swagger Documentation - Auto-generated API docs with authentication
  • Security - Password hashing with bcrypt, JWT tokens, session management
  • TypeScript - Full type safety throughout the codebase
  • ESLint & Prettier - Code quality and formatting
  • Husky - Git hooks for pre-commit checks

πŸ“ Project Structure

src/
β”œβ”€β”€ core/                    # Core business logic services
β”‚   β”œβ”€β”€ base-query-core/     # Base query builder with pagination
β”‚   β”œβ”€β”€ user-core/           # User CRUD operations
β”‚   β”œβ”€β”€ user-credential-core/# User password management
β”‚   β”œβ”€β”€ user-session-core/   # User session management
β”‚   β”œβ”€β”€ super-admin-core/    # Super admin CRUD operations
β”‚   β”œβ”€β”€ super-admin-credential-core/
β”‚   └── super-admin-session-core/
β”œβ”€β”€ modules/                 # Feature modules
β”‚   β”œβ”€β”€ user/               # User module with auth
β”‚   β”‚   └── auth/           # User authentication
β”‚   └── super-admin/        # Super admin module with auth
β”‚       └── auth/           # Super admin authentication
β”œβ”€β”€ shared/                  # Shared utilities
β”‚   β”œβ”€β”€ decorators/         # Custom decorators
β”‚   β”œβ”€β”€ keys/               # Constants and messages
β”‚   β”œβ”€β”€ libs/               # Helper libraries
β”‚   β”œβ”€β”€ modules/            # Shared modules (Prisma, Common)
β”‚   └── types/              # TypeScript types
β”œβ”€β”€ app.module.ts           # Root module
β”œβ”€β”€ main.ts                 # Application entry point
└── swagger-setup.ts        # Swagger configuration

πŸ› οΈ Quick Start

1. Clone the Repository

git clone <repository-url> my-new-project
cd my-new-project

2. Install Dependencies

npm install

3. Configure Environment

cp .env.example .env

Edit .env with your configuration:

# Database
DATABASE_URL="postgresql://user:password@localhost:5432/database_name?schema=public"

# JWT Secrets
JWT_ACCESS_SECRET="your-super-secret-access-key"
JWT_REFRESH_SECRET="your-super-secret-refresh-key"

# JWT Expiration
JWT_ACCESS_EXPIRES_IN="24h"
JWT_REFRESH_EXPIRES_IN="7d"

# Swagger
SWAGGER_PASSWORD="admin123"

# Application
PORT=3000
NODE_ENV="development"

4. Setup Database

# Generate Prisma Client
npm run prisma:generate

# Push schema to database (development)
npm run prisma:push

# Or create migration (production)
npm run prisma:migrate dev --name init

5. Start Development Server

npm run start:dev

πŸ“š API Documentation

Swagger UI available at: http://localhost:3000/api

  • Username: admin
  • Password: (from SWAGGER_PASSWORD in .env)

πŸ” Authentication Endpoints

User Authentication

Method Endpoint Description
POST /user/auth/register Register new user
POST /user/auth/login Login user
POST /user/auth/logout Logout user
POST /user/auth/refresh-token Refresh access token
POST /user/auth/profile Get user profile

Super Admin Authentication

Method Endpoint Description
POST /super-admin/auth/register Register super admin
POST /super-admin/auth/login Login super admin
POST /super-admin/auth/logout Logout super admin
POST /super-admin/auth/refresh-token Refresh access token
POST /super-admin/auth/profile Get super admin profile

πŸ“œ Available Scripts

# Development
npm run start:dev      # Start with hot-reload

# Production
npm run build          # Build the project
npm run start:prod     # Start production server

# Database
npm run prisma:generate  # Generate Prisma Client
npm run prisma:push      # Push schema to DB
npm run prisma:migrate   # Run migrations
npm run prisma:studio    # Open Prisma Studio

# Code Quality
npm run lint           # Run ESLint
npm run format         # Format with Prettier
npm run type-check     # TypeScript type checking

# Testing
npm run test           # Run unit tests
npm run test:e2e       # Run e2e tests
npm run test:cov       # Test coverage

πŸ—„οΈ Database Schema

The starter includes these models:

  • User - Basic user with email, phone, status
  • UserCredential - Encrypted password storage
  • UserSession - Session tracking with IP, user agent, geo
  • SuperAdmin - Admin user management
  • SuperAdminCredential - Admin password storage
  • SuperAdminSession - Admin session tracking

πŸ”§ Customization

Adding a New Module

  1. Create core service in src/core/
  2. Create module in src/modules/
  3. Add to Prisma schema
  4. Run npm run prisma:generate

Adding New Auth Type

  1. Copy src/modules/user/auth/ structure
  2. Create new JWT strategy
  3. Create new guard
  4. Update TOKEN_USER_TYPE enum

πŸ›‘οΈ Security Features

  • βœ… Password hashing with bcrypt (12 rounds)
  • βœ… JWT access & refresh tokens
  • βœ… Separate credential tables
  • βœ… Session tracking with geo-location
  • βœ… Rate limiting with Throttler
  • βœ… Swagger protected with basic auth

πŸ“¦ Tech Stack

  • Framework: NestJS 11
  • Language: TypeScript 5.7
  • ORM: Prisma 7 (with PostgreSQL adapter)
  • Database: PostgreSQL
  • Auth: Passport + JWT
  • Docs: Swagger/OpenAPI
  • Validation: class-validator
  • Concurrency: p-limit (latest ESM version)

⚑ Prisma 7 Setup

This starter uses Prisma 7 with the new adapter pattern for better performance and compatibility:

// src/shared/modules/prisma/prisma.service.ts
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
  constructor() {
    super({
      datasources: {
        db: {
          url: process.env.DATABASE_URL,
        },
      },
    });
  }

  async onModuleInit() {
    await this.$connect();
  }
}

Prisma 7 Key Features Used:

  • βœ… Extends PrismaClient directly (best practice)
  • βœ… OnModuleInit for automatic connection
  • βœ… Type-safe queries with full TypeScript support
  • βœ… Improved connection pooling

πŸ”„ ESM Package Handling (p-limit)

The starter handles ESM-only packages like p-limit (v7+) using dynamic imports:

// src/shared/modules/prisma/safe-prisma-call.ts
let prismaConcurrencyLimit: <T>(fn: () => Promise<T>) => Promise<T>;

const initPLimit = (async () => {
  const pLimit = (await import('p-limit')).default;
  prismaConcurrencyLimit = pLimit(concurrencyLimit);
})();

export async function safePrismaCall<T>(fn: () => Promise<T>): Promise<T> {
  await initPLimit;
  return prismaConcurrencyLimit(fn);
}

This pattern allows using the latest ESM-only packages in a CommonJS NestJS environment.

🀝 Contributing

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/amazing)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing)
  5. Open a Pull Request

πŸ“„ License

This project is MIT licensed.