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.
- 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)
- CRUD operations for books:
GET /books– get all booksGET /books/:id– get a book by UUIDPOST /books– create a new book (requirestitleandauthorId)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
- Install dependencies
npm install
- Create .env file in the project root:
DATABASE_URL=postgresql://<user>:<password>@<host>:<port>/<database>
- Start PostgreSQL with Docker
docker compose up -d
- Push schema to the database
npx drizzle-kit push
- Run the server
npm start
- Launch Drizzle Studio (GUI for your database):
npx drizzle-kit studio- 📚 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" } |
{
"title": "Atomic Habits",
"authorId": "a1b2c3d4-e5f6-7890-g1h2-i3j4k5l6m7n8"
}.
├── 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