Skip to content

Repository files navigation

School API

A modern REST API for managing courses, built with Node.js, TypeScript, Fastify, and Drizzle ORM.

πŸš€ Technologies

  • Node.js - JavaScript runtime
  • TypeScript - Typed superset of JavaScript
  • Fastify - Fast and efficient web framework
  • Drizzle ORM - TypeScript-first ORM
  • PostgreSQL - Relational database
  • Zod - TypeScript-first schema validation
  • Swagger/OpenAPI - API documentation

πŸ“‹ Features

  • βœ… Course CRUD operations
  • βœ… Data validation with Zod
  • βœ… Automatic API documentation
  • βœ… Database migrations
  • βœ… TypeScript with strict typing
  • βœ… Structured logging with Pino

πŸ“Š Application Architecture Flow

graph TB
    %% Client Layer
    subgraph "Client Layer"
        CLI[Client Application]
        CURL[cURL/Postman]
        BROWSER[Browser]
    end
    
    %% API Layer
    subgraph "API Layer - Fastify Server :3333"
        FASTIFY[Fastify Instance]
        SWAGGER["Swagger/OpenAPI Docs (/docs)"]
        LOGGER[Pino Logger]
        MIDDLEWARE["Type Provider (fastify-zod)"]
    end
    
    %% Route Layer
    subgraph "Route Layer"
        CREATE["POST /courses (Create Course)"]
        GET_ALL["GET /courses (List Courses)"]
        GET_BY_ID["GET /courses/:id (Get Course by ID)"]
    end
    
    %% Validation Layer
    subgraph "Validation Layer"
        ZOD_CREATE["Zod Schema (title: string.min(3))"]
        ZOD_PARAMS["Zod Schema (id: uuid())"]
        ZOD_RESPONSE["Response Validation (201/200/404)"]
    end
    
    %% Data Layer
    subgraph "Data Access Layer"
        DRIZZLE["Drizzle ORM (Query Builder)"]
        SCHEMA["Database Schema (courses table)"]
    end
    
    %% Database Layer
    subgraph "Database Layer"
        POSTGRES[("PostgreSQL (school_db)")]
        MIGRATIONS["Migration Files (./drizzle/*.sql)"]
    end
    
    %% Flow connections
    CLI --> FASTIFY
    CURL --> FASTIFY
    BROWSER --> FASTIFY
    
    FASTIFY --> MIDDLEWARE
    MIDDLEWARE --> CREATE
    MIDDLEWARE --> GET_ALL
    MIDDLEWARE --> GET_BY_ID
    
    CREATE --> ZOD_CREATE
    GET_BY_ID --> ZOD_PARAMS
    GET_ALL --> ZOD_RESPONSE
    
    ZOD_CREATE --> DRIZZLE
    ZOD_PARAMS --> DRIZZLE
    ZOD_RESPONSE --> DRIZZLE
    
    DRIZZLE --> SCHEMA
    SCHEMA --> POSTGRES
    
    MIGRATIONS --> POSTGRES
    
    %% Documentation and Logging
    SWAGGER -.->|"API Documentation"| FASTIFY
    LOGGER -.->|"Request/Response Logs"| FASTIFY
    
    %% Error handling
    ZOD_CREATE -.->|"Validation Error"| FASTIFY
    ZOD_PARAMS -.->|"Invalid UUID"| FASTIFY
    DRIZZLE -.->|"Database Error"| FASTIFY
    
    %% Styling
    classDef clientClass fill:#e1f5fe,stroke:#0277bd,stroke-width:2px
    classDef apiClass fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px
    classDef routeClass fill:#e8f5e8,stroke:#2e7d32,stroke-width:2px
    classDef validationClass fill:#fff3e0,stroke:#ef6c00,stroke-width:2px
    classDef dataClass fill:#fce4ec,stroke:#c2185b,stroke-width:2px
    classDef dbClass fill:#f1f8e9,stroke:#558b2f,stroke-width:2px
    
    class CLI,CURL,BROWSER clientClass
    class FASTIFY,SWAGGER,LOGGER,MIDDLEWARE apiClass
    class CREATE,GET_ALL,GET_BY_ID routeClass
    class ZOD_CREATE,ZOD_PARAMS,ZOD_RESPONSE validationClass
    class DRIZZLE,SCHEMA dataClass
    class POSTGRES,MIGRATIONS dbClass
Loading

πŸ› οΈ Installation

Prerequisites

  • Node.js (version 18 or higher)
  • npm or yarn
  • PostgreSQL (or Docker)

Environment Setup

  1. Clone the repository:
git clone <repository-url>
cd school-api
  1. Install dependencies:
npm install
  1. Set up environment variables:
cp .env.example .env
  1. Edit the .env file with your configurations:
NODE_ENV=development
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/school_db

Database Setup

Option 1: Docker (Recommended)

# Start PostgreSQL with Docker
docker-compose up -d

# Run migrations
npm run db:migrate

Option 2: Local PostgreSQL

  1. Create the school_db database
  2. Configure the DATABASE_URL in the .env file
  3. Run migrations:
npm run db:migrate

🎯 Usage

Starting the server

# Development mode (with hot reload)
npm run dev

The server will be available at http://localhost:3333

API Documentation

In development mode, access the documentation at:

  • Swagger UI: http://localhost:3333/docs

Available commands

# Development
npm run dev                 # Start server with hot reload

# Database
npm run db:generate         # Generate migrations from schema
npm run db:migrate          # Apply pending migrations
npm run db:studio           # Open Drizzle Studio (database GUI)

πŸ“š Endpoints

Courses

Method Endpoint Description
GET /courses List all courses
GET /courses/:id Get course by ID
POST /courses Create new course

Usage examples

Create course

curl -X POST http://localhost:3333/courses \
  -H "Content-Type: application/json" \
  -d '{"title": "JavaScript Fundamentals"}'

List courses

curl http://localhost:3333/courses

Get course by ID

curl http://localhost:3333/courses/{course-id}

πŸ—οΈ Project Structure

src/
β”œβ”€β”€ database/
β”‚   β”œβ”€β”€ client.ts          # Drizzle client configuration
β”‚   └── schema.ts          # Database table schemas
β”œβ”€β”€ routes/
β”‚   β”œβ”€β”€ create-course.ts   # Create course route
β”‚   β”œβ”€β”€ get-courses.ts     # List courses route
β”‚   └── get-course-by-id.ts # Get course by ID route
└── server.ts              # Fastify server configuration

🀝 Contributing

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

πŸ“ License

This project is licensed under the ISC License. See the LICENSE file for details.

About

NodeJS API with Fastify

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages