A backend system for managing a digital library, built with Express.js, TypeScript, and MongoDB. It supports book cataloging, borrowing workflows, and strict data validation using Mongoose and Zod.
Looking for the frontend? 👉 https://github.com/coder7475/libraray_management_frontend
- ✅ RESTful API for managing books and borrowing
- 📦 Modular project structure using TypeScript
- 🌿 Environment configuration using
.env - 🔐 Robust validation using Zod + Mongoose
- 📊 MongoDB Aggregation for borrow summary
- 🧠 Static & Instance Methods for business logic
- ⛔ Mongoose Middleware (
pre,post) support - 🔍 Filter & Sort query parameters
.
├── README.md
├── biome.json # BiomeJS config for linting & formatting
├── package.json # Project metadata
├── pnpm-lock.yaml # Lockfile
├── tsconfig.json # TS config for dev
├── tsconfig.build.json # TS config for prod build
└── src
├── app.ts # Express app config
├── server.ts # Entry point
├── configs/ # Env & DB setup
├── controller/ # Business logic
├── interfaces/ # TypeScript interfaces
├── middlewares/ # Error handler, Zod validation
├── models/ # Mongoose models (Book, Borrow)
├── routes/ # Express route definitions
└── schemas/ # Zod validation schemas
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/books |
Add a new book |
| GET | /api/books |
List all books (with filter & sort) |
| GET | /api/books/:id |
Get a single book by ID |
| PUT | /api/books/:id |
Update book fields (e.g., copies) |
| DELETE | /api/books/:id |
Delete a book |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/borrow |
Borrow a book (checks quantity, updates copies, handles availability flag) |
| GET | /api/borrow |
Aggregated borrow summary by book (title, isbn, totalQuantity) |
git clone https://github.com/coder7475/library_management_apis.git
cd library_management_apis
pnpm installCreate a .env file in the root directory:
PORT=5000
HOST=localhost
NODE_ENV=development
MONGODB_URI="mongodb://127.0.0.1:27017/your_db_name"
pnpm run devpnpm format && pnpm lintpnpm run buildFor /api/books:
| Query Param | Description |
|---|---|
filter |
Filter books by genre |
sortBy |
Field to sort by (e.g., title) |
sort |
Sort order: asc or desc |
limit |
Limit number of books returned |
page |
Current page for pagination |
- ✅ Prevent borrow if available copies < requested quantity
- ➖ Deduct
quantityfrom book’scopies - ❌ If
copiesbecomes 0, setavailabletofalse - 📈 Borrow summary via MongoDB aggregation
- 🧠 Logic implemented using static & instance methods
Licensed under the MIT License. Free to use and modify.
