This project is a simple Library Management System API built with Express.js, TypeScript, and Prisma. The system provides CRUD operations for managing Users, Books, Authors, and Categories. It also includes JWT-based user authentication.
- User Authentication: JWT-based authentication for protected routes.
- CRUD Operations: Full CRUD capabilities for managing users, books, authors, and categories.
- Relational Database: Prisma ORM is used to manage relational data across models.
- Node.js and Express.js
- TypeScript
- Prisma with PostgreSQL (or other relational databases)
- JWT for authentication
- Node.js (>= 14.x)
- PostgreSQL (or any other relational database supported by Prisma)
- npm or yarn
- Prisma CLI (optional for manual Prisma commands)
-
Clone the repository:
git clone url cd project
-
Install dependencies:
npm install
-
Set up environment variables:
Create a
.env
file in the root directory and configure your environment variables as follows:DATABASE_URL="postgresql://user:password@localhost:5432/library_db" JWT_SECRET="your_secret_key" # A secret key for JWT signing
Replace
user
,password
, andlibrary_db
with your actual database credentials and database name. -
Set up the database:
Run the following Prisma commands to set up the database:
npx prisma migrate dev
-
Generate Prisma client:
npx prisma generate
-
Run the server:
npm run dev
The API will be accessible at
http://localhost:5000
.
Below are some example routes for each resource:
-
Auth:
POST /api/auth/register
- Register a new userPOST /api/auth/login
- Log in a user to receive a JWT
-
Books:
GET /api/books
- Get all booksGET /api/books/:id
- Get a book by IDPOST /api/books
- Create a new bookPUT /api/books/:id
- Update a book by IDDELETE /api/books/:id
- Delete a book by ID
-
Authors:
GET /api/authors
- Get all authorsGET /api/authors/:id
- Get an author by IDPOST /api/authors
- Create a new authorPUT /api/authors/:id
- Update an author by IDDELETE /api/authors/:id
- Delete an author by ID
-
Categories:
GET /api/categories
- Get all categoriesGET /api/categories/:id
- Get a category by IDPOST /api/categories
- Create a new categoryPUT /api/categories/:id
- Update a category by IDDELETE /api/categories/:id
- Delete a category by ID
All protected routes (e.g., /api/books
, /api/authors
, /api/categories
) require a valid JWT token. Use the Authorization
header with Bearer <token>
to access these routes.
- Start server:
npm run dev
- Prisma migration:
npx prisma migrate dev
- Prisma Studio:
npx prisma studio
(to view and interact with the database) - Lint:
npm run lint