The Library Management API is a backend system built with Express, TypeScript, and MongoDB (Mongoose). It manages books with schema validation, supports CRUD operations, enforces valid genres and unique ISBNs, and handles borrowing through Mongoose methods. Aggregation is used to summarize borrow data, providing an efficient, schema-driven solution for library systems.
- Automatic copy deduction & availability status update
- Borrowed book summary via aggregation
- Filtering, sorting for books
- Node.js + Express
- TypeScript
- MongoDB + Mongoose
- Zod (for schema validation)
- dotenv
- ESLint + Prettier (for code quality)
# 1. Clone the repository
git clone https://github.com/md-afsar-dev/Library-management-server
cd Library-management-server
# 2. Install dependencies
bun install
# 3. Set up environment variables
touch .envPORT = 4000
DB_NAME = ******
DB_PASSWORD = *****
LIBRARY-MANAGEMENT-SERVER/
βββ .vercel/ # Vercel deployment configuration
βββ dist/ # Compiled TypeScript files
βββ node_modules/ # Node.js dependencies
βββ src/ # Source code
β βββ config/ # Environment and DB config
β βββ controllers/ # Request handlers
β βββ middlewares/ # Custom Express middlewares (e.g., error handling)
β βββ models/ # Mongoose schemas and methods
β βββ routes/ # API route definitions
β βββ services/ # Business logic layer
β βββ tests/ # Unit and integration tests
β βββ types/ # TypeScript custom types and interfaces
β βββ validations/ # Zod schemas for request validation
β βββ app.ts # Express app setup
β βββ server.ts # Server entry point
βββ .env # Environment variables
βββ .gitignore # Git ignore rules
βββ .prettierignore # Prettier ignore rules
βββ .prettierrc.json # Prettier config
βββ erdiagram.png # ER diagram image
βββ eslint.config.mjs # ESLint configuration
βββ bun.lock # bun lockfile
βββ package.json # Project metadata and scripts
βββ README.md # Project documentation
βββ tsconfig.json # TypeScript compiler config
βββ vercel.json # Vercel project settingsBase URL
http://localhost:5000/apiPOST /api/create-book
{
"title": "The Theory of Everything",
"image": "https://res.cloudinary.com/dz1fy2tof/image/upload/v1751522624/download_n8dmde.jpg",
"author": "Stephen Hawking",
"genre": "SCIENCE",
"isbn": "9780553380163",
"description": "An overview of cosmology and black holes.",
"copies": 5,
"available": true
}GET /api/books?filter=SCIENCE&sortBy=createdAt&sort=desc&limit=5
Supports:
filter(genre)sortBy(e.g., createdAt)sort(asc|desc)limit(number of items)
GET /api/books/:id
PUT /api/edit-book/:id
{
"copies": 50
}DELETE /api/delete-book/:id
POST /api/borrow/:bookId
{
"bookId": "64ab3f9e2a4b5c6d7e8f9012",
"quantity": 2,
"dueDate": "2025-07-18T00:00:00.000Z"
}π Business Logic:
- Quantity must be less than or equal to available copies
- Deduct copies automatically
- If copies become 0, set
available = false
GET /api/borrow-summary
π Uses MongoDB Aggregation to return:
[
{
"book": {
"title": "The Theory of Everything",
"isbn": "9780553380163"
},
"totalQuantity": 5
}
]| Field | Type | Required | Validation |
|---|---|---|---|
image |
string |
Yes |
Must be a valid URL string |
title |
string |
Yes |
β |
author |
string |
Yes |
β |
genre |
enum |
Yes |
One of: FICTION, NON_FICTION, SCIENCE, HISTORY, BIOGRAPHY, FANTASY |
isbn |
string |
Yes |
Must be a valid string (unique enforced in DB) |
description |
string |
No |
Optional field |
copies |
number |
Yes |
Must be an integer β₯ 0 |
available |
boolean |
No |
Optional field, defaults to true if not provided |
| Field | Type | Required | Validation |
|---|---|---|---|
bookId |
ObjectId |
Yes |
Must be a valid book |
quantity |
number |
Yes |
Must be positive |
dueDate |
Date |
Yes |
Future date required |
Md Afsar Mahmud
Full Stack Developer
HackerRank β’
LinkedIn
