Skip to content

Repository files navigation

Library Management System v5

A production-grade, modular Library Management Platform: FastAPI + SQLAlchemy + PostgreSQL backend, React + TypeScript frontend, JWT auth with role-based access control, a real circulation domain (issue/return/ renew/reserve/fines with enforced state machines), an explainable recommendation engine, analytics, audit logging, and an AI librarian built on a swappable provider abstraction.

This is a from-scratch architectural rebuild of an earlier CSV-backed student project of the same name β€” see CHANGELOG.md for exactly what changed and why.

Features

  • Book catalog & copies β€” books and their individual physical copies (accession number, location, condition) are modeled separately.
  • Circulation β€” issue, return, renew (with a renewal cap), reserve, cancel reservation, automatic overdue detection, fine calculation on late return.
  • RBAC β€” ADMIN / LIBRARIAN / MEMBER roles with an explicit, centralized permission map (app/core/dependencies.py).
  • JWT authentication β€” bcrypt password hashing, access + refresh tokens, account-status enforcement on every request.
  • Analytics dashboard β€” totals, utilization rate, most-borrowed books, category distribution, monthly circulation trend β€” rendered as live charts in the frontend.
  • Explainable recommendations β€” deterministic signals (category/ author history + popularity), each with a plain-language reason. No ML is used or claimed.
  • AI librarian β€” provider-agnostic (LLMProvider protocol); ships with a dependency-free MockProvider by default and an optional Anthropic-backed provider.
  • Audit logging β€” every meaningful mutation is recorded to an append-only audit_logs table.
  • REST API β€” versioned under /api/v1, documented at /swagger and /redoc.

Tech stack

Layer Technology
Backend Python 3.12, FastAPI, SQLAlchemy 2.0, Alembic, Pydantic v2
Auth passlib/bcrypt, python-jose (JWT)
Database PostgreSQL (production), SQLite (dev/tests)
Frontend React 19, TypeScript, Vite, React Router, Recharts
Testing pytest, pytest-cov, httpx (via FastAPI TestClient)
Quality ruff, mypy, oxlint, tsc
Deployment Docker, docker-compose, GitHub Actions

Project structure

library-management-system-v5/
β”œβ”€β”€ app/                # FastAPI backend
β”‚   β”œβ”€β”€ core/            # config, security, logging, exceptions, RBAC deps
β”‚   β”œβ”€β”€ api/v1/           # HTTP routes β€” thin, no business logic
β”‚   β”œβ”€β”€ models/           # SQLAlchemy ORM models + state machines
β”‚   β”œβ”€β”€ schemas/          # Pydantic request/response schemas
β”‚   β”œβ”€β”€ repositories/     # Data access (SQLAlchemy)
β”‚   β”œβ”€β”€ services/         # Business logic β€” the real "domain layer"
β”‚   β”œβ”€β”€ ai/                # LLMProvider protocol + Mock/Anthropic providers
β”‚   └── database/          # Session, Base, Alembic migrations
β”œβ”€β”€ frontend/            # React + TypeScript + Vite SPA
β”œβ”€β”€ tests/               # unit / api / integration pytest suites
β”œβ”€β”€ scripts/             # seed_database.py, migrate_data.py, create_admin.py
β”œβ”€β”€ docs/                # architecture, api, database, security, deployment, development
β”œβ”€β”€ data/seed/            # copied legacy CSVs, used by migrate_data.py
β”œβ”€β”€ Dockerfile, docker-compose.yml
└── .github/workflows/    # ci.yml, security.yml

Quick start (Docker)

cp .env.example .env
docker compose up --build

Quick start (local, no Docker)

# Backend
python -m venv .venv && source .venv/bin/activate
pip install -r requirements-dev.txt
cp .env.example .env
alembic upgrade head
python scripts/seed_database.py     # demo accounts + books
uvicorn app.main:app --reload

# Frontend (separate terminal)
cd frontend
npm install
npm run dev

Demo accounts after seeding (see script output): admin@example.com / AdminPass123! (also librarian@ and two member@ accounts).

Database setup

Dev default is SQLite (zero setup). For PostgreSQL, set DATABASE_URL in .env and run alembic upgrade head. See docs/database.md.

Environment variables

See .env.example for the full list with descriptions.

API documentation

Full endpoint reference: docs/api.md. Interactive/auto-generated docs at /swagger and /redoc once the server is running.

Authentication

JWT access + refresh tokens; see docs/security.md for the full auth/authorization design.

Testing

pytest -q --cov=app --cov-report=term-missing

Verified in this environment: 25 passed, ~80% statement coverage. Covers unit tests for circulation state machines, the recommendation engine, analytics, user management, and the AI mock provider; API tests for auth and books; and one full end-to-end integration test that exercises the exact workflow β€” create user β†’ create book β†’ create copy β†’ issue β†’ verify unavailable β†’ return β†’ verify available.

CI/CD

.github/workflows/ci.yml runs lint (ruff/oxlint), type checks (mypy/tsc), the pytest suite, an Alembic migration smoke test, and a frontend production build on every push/PR. .github/workflows/security.yml runs pip-audit and npm audit on a weekly schedule.

Deployment

See docs/deployment.md for Docker Compose and bare-metal instructions.

Data migration

scripts/migrate_data.py converts the legacy books.csv into the new schema. Verified against this repository's actual data: 211/211 book rows migrated, 0 rejected (see docs/database.md for what is and isn't migrated, and why).

AI architecture

See docs/architecture.md. Default AI_PROVIDER=mock requires no API key. Set AI_PROVIDER=anthropic and AI_API_KEY to use a real Claude model instead.

Security

See docs/security.md and SECURITY.md.

Contributing

See CONTRIBUTING.md.

Roadmap / known limitations

Honestly documented rather than glossed over β€” see the end of CHANGELOG.md:

  • Token revocation/denylist not implemented (stateless JWT logout only).
  • Rate limiting is a reverse-proxy responsibility, not in-process middleware.
  • Legacy circulation history (issued_books.csv, issue_log.txt) is reported by the migration script but not fabricated into Loan rows.
  • The frontend covers the core flows end-to-end but not every UI component listed in the original brief (e.g. a dedicated copy-management screen, per-role dashboard layouts) β€” these are natural next additions on top of a now-complete API.

License

MIT β€” see LICENSE.

About

πŸ“š Library Management System A Python-based library app for storing books, managing users, and issuing/returning books β€” structured for clarity and extendability.

Topics

Resources

Contributing

Security policy

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages