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 .env
PORT = 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 settings
Base URL
http://localhost:5000/api
POST /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