Skip to content

PrathameshRSH26/bookstore-backend

Repository files navigation

Books–Author Management API

A RESTful backend API for managing books, built with Node.js, Express, PostgreSQL, and Drizzle ORM. Uses Docker for database setup and provides full CRUD operations for books with UUID-based records and foreign key references to authors.

🛠️ Tech Stack

  • Node.js (CommonJS)
  • Express.js (^5.2.1)
  • PostgreSQL (via Docker)
  • Drizzle ORM (^0.45.1) and Drizzle Kit (^0.31.8)
  • pg (^8.16.3)
  • dotenv (^17.2.3)
  • uuid (^13.0.0)
  • nodemon (^3.1.11)

✅ Core Capabilities

  • CRUD operations for books:
    • GET /books – get all books
    • GET /books/:id – get a book by UUID
    • POST /books – create a new book (requires title and authorId)
    • DELETE /books/:id – delete a book by UUID
  • Relational data: Books reference authors using authorId (UUID foreign key)
  • UUID primary keys: Generated using Drizzle’s .defaultRandom()
  • Database schema defined with Drizzle ORM (booksTable, authorsTable)
  • PostgreSQL database managed via Docker
  • Schema pushed to DB using npx drizzle-kit push

🚀 Installation & Setup

  1. Install dependencies
    npm install
  2. Create .env file in the project root:
     DATABASE_URL=postgresql://<user>:<password>@<host>:<port>/<database> 
  3. Start PostgreSQL with Docker
     docker compose up -d
  4. Push schema to the database
    npx drizzle-kit push
  5. Run the server
     npm start

🧪 Development Tools

  • Launch Drizzle Studio (GUI for your database):
   npx drizzle-kit studio

📌 REST API Endpoints

  • 📚 Books API:
| Method | Endpoint   | Request Body                              | Description         | Response                      |
| ------ | ---------- | ----------------------------------------- | ------------------- | ----------------------------- |
| GET    | /books     | None                                      | Fetch all books     | Array of book objects         |
| GET    | /books/:id | None                                      | Fetch a book by ID  | Single book object            |
| POST   | /books     | { "title": "string", "authorId": "uuid" } | Add a new book      | Created book object           |
| DELETE | /books/:id | None                                      | Delete a book by ID | { "message": "book deleted" } |

📥 Create Book – Request Body Example

   {
     "title": "Atomic Habits",
     "authorId": "a1b2c3d4-e5f6-7890-g1h2-i3j4k5l6m7n8"
    }

🗂️ Project Structure

.
├── controllers/
│   └── books.controller.js   # Book API routes (Express Router)
├── db/
│   └── index.js              # Drizzle database connection
├── drizzle/                  # Migration files (generated by drizzle-kit)
├── models/
│   ├── author.model.js       # Authors table schema
│   ├── book.model.js         # Books table schema
│   └── index.js              # Exports all database tables
├── node_modules/             # Installed dependencies
├── .env                      # Environment variables
├── docker-compose.yml        # PostgreSQL Docker configuration
├── drizzle.config.js         # Drizzle ORM configuration
├── index.js                  # Main Express app entry point
├── package-lock.json         # Dependency lock file
└── package.json              # Project metadata & dependencies

✅ Output


Screenshot 1
Screenshot 2
Screenshot 3
Screenshot 4

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors