Skip to content

akhunters/code-stream-backend

Repository files navigation

🚀 Code Stream Backend

A scalable, production-ready backend built with NestJS, TypeScript, PostgreSQL, Docker, and TypeORM. This backend powers the Code Stream Blog application, handling authentication, user management, and blog post operations.

Checkout the API Documentation deployed at - https://api.code-stream.avinash-kumar.com/api/docs


🛠️ Tech Stack

  • Framework: NestJS (TypeScript)
  • Database: PostgreSQL (TypeORM for ORM)
  • Authentication: Passport.js (JWT Strategy)
  • Containerization: Docker
  • Validation: Zod (Runtime environment variable validation)
  • Documentation: Swagger (Accessible at /api/docs)

📂 Folder Structure

src/
 ├── main.ts                # Entry point of the application
 ├── app.module.ts          # Root module
 ├── orm.config.ts          # TypeORM database configuration
 ├── config/                # Configuration files
 ├── entities/              # TypeORM entity files
 ├── filters/               # Exception and other filters
 ├── guards/                # HTTP guards (e.g., Auth Guard)
 ├── middlewares/           # Middlewares (e.g., Logger)
 ├── migrations/            # TypeORM migration files
 ├── modules/               # Feature modules
 │   ├── user/              # User module
 │   ├── auth/              # Authentication module
 │   ├── post/              # Post module
 │   ├── healthcheck/       # Healthcheck module
 ├── strategies/            # Authentication strategies (e.g., JWT Strategy)
 ├── types/                 # TypeScript types

🔥 Features

  • User Authentication: Google/Facebook login with JWT token generation
  • User & Post Management: Full CRUD operations on users and posts
  • Swagger API Documentation: Available at /api/docs
  • Database Migration Support: Using TypeORM migrations
  • Security & Validation: JWT authentication and Zod runtime validation
  • Developer-Friendly Makefile: Simplifies running commands
  • Future Roadmap: AWS SDK integration for direct S3 uploads

🛠️ Database Schema

Users Table (users)

Column Type Constraints
id Autogenerated Number (PK) Primary Key
name VARCHAR(255) Not Null
email VARCHAR(255) Unique, Not Null
thumbnail TEXT Nullable (S3 URL)
created_at TIMESTAMP (without timezone) Default: now()
updated_at TIMESTAMP (without timezone) Default: now()

Posts Table (posts)

Column Type Constraints
id Autogenerated Number (PK) Primary Key
user_id Number (FK to users.id) Foreign Key, Not Null
title VARCHAR(255) Not Null
description VARCHAR(255) Not Null
body TEXT Not Null
thumbnail TEXT Nullable (S3 URL)
created_at TIMESTAMP (without timezone) Default: now()
updated_at TIMESTAMP (without timezone) Default: now()

Relations:

  • One-to-Many: usersposts (Foreign Key: user_id)

🔑 Authentication Flow

  1. User sends a login request with a provider (Google/Facebook) and an OAuth access token.
  2. Backend validates the token with the respective provider.
  3. If successful, fetches user data and creates or retrieves the user in the database.
  4. Returns a signed JWT token for subsequent authenticated requests.

🚀 Running the Application

1️⃣ Clone the Repository

git clone https://github.com/your-repo/code-stream-backend.git
cd code-stream-backend

2️⃣ Set Up Environment Variables

Create a .env file in the root directory:

NODE_ENV=development
SERVER_PORT=3000
JWT_SECRET=codestream_secret_32_chars_long_string_for_making_jwt_token
DATABASE_URL=postgres://codestream_user:codestream_password@db:5432/codestream_db

3️⃣ Run the Application

Using Docker (Recommended)

make start  # Starts the app using Docker Compose

Without Docker

nvm use
npm install
npm run start:dev

🛠️ Makefile Commands

The project includes a Makefile for easier command execution:

make help        # List all available commands

📌 API Documentation

Swagger documentation is available at: http://localhost:3000/api/docs


🗺️ Future Roadmap

  • AWS SDK Integration: Generate pre-signed URLs for direct S3 uploads.
  • Rate Limiting & Caching: Improve API performance and security.

📖 Useful Resources


About

CodeStream blog site backend

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors