A RESTful API built with TypeScript, Express, and MongoDB for managing a library system with books, authors, and loan operations.
- Docker & Docker Compose
- Node.js 18+ (for local development)
# Build and start services
docker-compose up -d
# Stop services
docker-compose downThe API will be available at http://localhost:3000
# Install dependencies
npm install
# Create .env file
cp .env.example .env
# Start development server
npm run dev
# Build for production
npm run build
# Run production build
npm start| Method | Endpoint | Description |
|---|---|---|
| POST | /api/authors |
Create a new author |
| GET | /api/authors |
List all authors |
| GET | /api/authors/:id |
Get author by ID |
| PUT | /api/authors/:id |
Update author |
| DELETE | /api/authors/:id |
Delete author |
Example Request:
POST /api/authors
{
"name": "Gabriel",
"lastName": "García Márquez",
"nationality": "Colombian",
"birthdate": "1927-03-06T00:00:00.000Z"
}| Method | Endpoint | Description |
|---|---|---|
| POST | /api/books |
Create a new book |
| GET | /api/books |
List all books (paginated) |
| GET | /api/books/:id |
Get book by ID |
| GET | /api/books/search |
Search books by title/author |
| PUT | /api/books/:id |
Update book |
| DELETE | /api/books/:id |
Delete book |
Example Request:
POST /api/books
{
"title": "One Hundred Years of Solitude",
"isbn": "978-0-06-088328-7",
"authorId": "507f1f77bcf86cd799439011",
"year": 1967,
"genre": "Magical Realism",
"quantity": 5
}Search Examples:
# Search by title
GET /api/books/search?title=Solitude
# Search by author
GET /api/books/search?authorId=507f1f77bcf86cd799439011
# Search by both
GET /api/books/search?title=Solitude&authorId=507f1f77bcf86cd799439011| Method | Endpoint | Description |
|---|---|---|
| POST | /api/loans |
Register a book loan |
| GET | /api/loans/active |
List active loans |
| PUT | /api/loans/:id/return |
Return a borrowed book |
Example Request:
POST /api/loans
{
"bookId": "507f1f77bcf86cd799439011",
"username": "John Doe",
"loanDate": "2025-01-15T00:00:00.000Z"
}- Runtime: Node.js
- Language: TypeScript
- Framework: Express.js
- Database: MongoDB
- ODM: Mongoose
- Containerization: Docker
library-api/
├── src/
│ ├── controllers/ # Request handlers (future)
│ ├── middlewares/ # Express middlewares
│ ├── models/ # Mongoose schemas
│ ├── routes/ # API routes
│ └── server.ts # Entry point
├── dist/ # Compiled JavaScript
├── docker-compose.yaml # Docker services
├── Dockerfile
├── tsconfig.json # TypeScript config
└── package.json
Create a .env file in the root directory:
NODE_ENV=dev
PORT=3000
MONGO_URL_DEV=mongodb://admin:password@localhost:27017/library-db?authSource=admin
MONGO_URL_PROD=mongodb://admin:password@mongodb:27017/library-db?authSource=admin-
MongoDB: Port 27017
- Username:
admin - Password:
123456 - Database:
library-db
- Username:
-
Backend: Port 3000
Success Response:
{
"id": "507f1f77bcf86cd799439011",
"title": "Example Book",
...
}Error Response:
{
"message": "Error description",
"status": 400
}- ISBN must be unique for each book
- Books can only be loaned if
quantity > 0 - Quantity decreases by 1 when a book is loaned
- Quantity increases by 1 when a book is returned
- Active loans have
returned: false