A modern REST API for managing courses, built with Node.js, TypeScript, Fastify, and Drizzle ORM.
- 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
- β Course CRUD operations
- β Data validation with Zod
- β Automatic API documentation
- β Database migrations
- β TypeScript with strict typing
- β Structured logging with Pino
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
- Node.js (version 18 or higher)
- npm or yarn
- PostgreSQL (or Docker)
- Clone the repository:
git clone <repository-url>
cd school-api- Install dependencies:
npm install- Set up environment variables:
cp .env.example .env- Edit the
.envfile with your configurations:
NODE_ENV=development
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/school_db# Start PostgreSQL with Docker
docker-compose up -d
# Run migrations
npm run db:migrate- Create the
school_dbdatabase - Configure the
DATABASE_URLin the.envfile - Run migrations:
npm run db:migrate# Development mode (with hot reload)
npm run devThe server will be available at http://localhost:3333
In development mode, access the documentation at:
- Swagger UI:
http://localhost:3333/docs
# 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)| Method | Endpoint | Description |
|---|---|---|
GET |
/courses |
List all courses |
GET |
/courses/:id |
Get course by ID |
POST |
/courses |
Create new course |
curl -X POST http://localhost:3333/courses \
-H "Content-Type: application/json" \
-d '{"title": "JavaScript Fundamentals"}'curl http://localhost:3333/coursescurl http://localhost:3333/courses/{course-id}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
- Fork the project
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the ISC License. See the LICENSE file for details.