Skip to content

A robust RESTful API for a digital library, built with Node.js, Express, Prisma, and PostgreSQL. Fully containerized with Docker and protected by a comprehensive integration test suite.

License

Notifications You must be signed in to change notification settings

alissonpef/Digital-Library-Backend

Repository files navigation

BookHub API 📚

A robust, professional-grade RESTful API built with Node.js, Express, and JavaScript, designed to serve as an exemplary backend for a library management application. The project is fully containerized with Docker, uses the Prisma ORM to interact with a PostgreSQL database, and is protected by automated integration tests.

✨ Core Architectural Features & Best Practices

This project is built on a foundation of modern software engineering practices:

  • ✅ Consistent and Isolated Environment with Docker: Uses Docker Compose to orchestrate the PostgreSQL database, ensuring the development environment is 100% identical for all contributors and fully isolated from the host machine.
  • ✅ Next-Generation ORM with Prisma: Manages the entire database lifecycle, from schema definition and migration generation to type-safe data access, preventing common errors and vulnerabilities.
  • ✅ Layered Architecture: The code is strictly organized into Controllers, Services, Routes, and Middlewares, promoting a clear separation of concerns, high cohesion, and low coupling, which makes the project scalable and easy to maintain.
  • ✅ Reliable Integration Tests: A comprehensive test suite with Jest and Supertest that validates the API flows from end to end. The tests run against a separate, containerized test database that is created and destroyed for each run, ensuring atomic and reliable tests.
  • ✅ Atomic Transactions: Critical business logic (such as creating a loan) uses database transactions to ensure data integrity. If any part of the operation fails, all changes are rolled back.
  • ✅ Workflow Automation: Powerful and well-organized NPM scripts automate complex tasks like starting the development environment, running full test pipelines, and maintaining code quality.
  • ✅ Centralized Error Handling: A global error middleware and custom error classes (HttpError) ensure that all API error responses are consistent, predictable, and secure, without leaking internal implementation details.

🛠️ Tech Stack

Category Technology
Main Stack JavaScript Node.js Express.js PostgreSQL Prisma
Testing & Quality Jest ESLint Prettier
Environment & DevOps Docker Git JWT

🚀 Getting Started

Prerequisites:

  • Node.js (v18+) and npm
  • Docker and Docker Compose

1. Clone and Install

git clone https://github.com/alissonpef/Digital-Library-Backend.git
cd Digital-Library-Backend
npm install

2. Configure Environment Variables

Create two files in the project root: .env (for development) and .env.test (for testing). Copy the content below into each, ensuring JWT_SECRET is a long and random string.

.env file:

# Development Environment
POSTGRES_USER=app_user
POSTGRES_PASSWORD=app_password
POSTGRES_DB=book_api_dev_db
POSTGRES_PORT=5432
DATABASE_URL="postgresql://app_user:app_password@localhost:5432/book_api_dev_db"
JWT_SECRET=your-super-strong-secret-for-development

.env.test file:

# Test Environment
POSTGRES_USER=app_user
POSTGRES_PASSWORD=app_password
POSTGRES_DB=book_api_test_db
POSTGRES_PORT=5433
DATABASE_URL="postgresql://app_user:app_password@localhost:5433/book_api_test_db"
JWT_SECRET=any-secret-for-testing

⚙️ Workflows & Scripts

The project is configured with NPM scripts to simplify workflows.

Command Description
npm run dev Starts the complete development environment. Spins up the database container and starts the server with nodemon for hot-reloading.
npm run test Executes the integration test pipeline. Spins up a clean test database, applies migrations, runs all tests, and shuts down the environment at the end.
npm run test:watch Ideal for TDD. Requires the test environment to be started manually (npm run services:up:test), then runs Jest in watch mode, re-running tests on each code change.
npm run lint Runs ESLint and Prettier to check the code quality and formatting across the entire project.
npm run services:down Shuts down the development database container.

📖 API Endpoints

Endpoint Method Description Authentication Required?
/auth/register POST Registers a new user. No
/auth/login POST Authenticates a user and returns a JWT. No
/api/books GET Lists all books. Yes
/api/books/:id GET Gets the details of a specific book. Yes
/api/books POST Creates a new book. Yes
/api/books/:id PUT Updates a book's data. Yes
/api/books/:id DELETE Removes a book from the system. Yes
/api/loans GET Lists all loans. Yes
/api/loans/:id GET Gets the details of a specific loan. Yes
/api/loans POST Creates a new loan (borrows a book). Yes
/api/loans/:id/return POST Registers a book's return. Yes

📁 Project Structure

The structure follows a layered architecture pattern, clearly separating responsibilities.

/
├── .env
├── .env.test
├── infra/
│   ├── compose.yaml
│   └── scripts/
│       └── wait-for-postgres.js
├── prisma/
│   ├── migrations/
│   └── schema.prisma
├── src/
│   ├── controllers/
│   ├── errors/
│   ├── middlewares/
│   ├── routes/
│   └── services/
└── tests/
    ├── helpers/
    │   └── db.js
    └── integration/

📫 Let's Connect!

I'd love to exchange ideas about backend development, Node.js, Express, or other technologies. Feel free to get in touch or add me on social media.

LinkedIn Gmail


Made with ❤️ by Alisson Pereira.

About

A robust RESTful API for a digital library, built with Node.js, Express, Prisma, and PostgreSQL. Fully containerized with Docker and protected by a comprehensive integration test suite.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published