This repo is intentionally broken in specific ways. Your job is to apply the patterns from this LU to stabilize it.
You do not need to install PostgreSQL on your computer for this assignment. We will use a free cloud database instead so you can start coding immediately.
- Go to Neon.tech and sign up for a free account (use your GitHub or Google account).
- Click "Create a Project".
- Name it
prisma-liveand select Postgres 15 (or whatever the latest default is). Click Create. - On your project dashboard, find the box labeled "Connection Details".
- Copy your Postgres connection string. It will look something like this:
postgresql://neondb_owner:xyz123@ep-cool-butterfly-a5.us-east-2.aws.neon.tech/neondb?sslmode=require
- Create a
.envfile in the root folder of this project. - Paste your Neon database URL into it:
DATABASE_URL="YOUR_NEON_CONNECTION_STRING_HERE"
PORT=3000Open your terminal and run these commands in order:
# 1. Install dependencies
npm install
# 2. Add the stock field to schema.prisma, THEN run the migration:
npx prisma migrate dev --name add-stock
# 3. Add test data to your database (Run ONLY AFTER the migration is successful)
node prisma/seed.js
# 4. Start the development server
npm run dev| File | Bug |
|---|---|
schema.prisma |
stock field missing from Product |
product.controller.js |
getProducts uses raw pg pool + SQL string |
product.controller.js |
new PrismaClient() inline — leaks connections |
order.controller.js |
new PrismaClient() inline — leaks connections |
product.controller.js + order.controller.js |
product.price read without null check → crash |
order.controller.js |
purchaseItem has two unprotected awaits — no transaction |
⚠️ src/lib/db.jsdoes not exist yet. Task is to create it.
-
getProductsreturns data fromprisma.product.findMany()— nopool.query -
stock Int @default(0)added toProductinschema.prisma - Migration committed:
prisma/migrations/folder present in PR -
src/lib/db.jscreated with singletonPrismaClientexport - Both controllers import from
../lib/db— nonew PrismaClient()anywhere -
getProductByIdreturns404when product is not found — no crash -
purchaseItemwraps Order create + stock decrement inprisma.$transaction([]) - GitHub PR submitted with all fixes listed
| Method | Route | Notes |
|---|---|---|
| GET | /products |
Must use Prisma, not raw SQL |
| GET | /products/:id |
Must return 404 for unknown IDs |
| POST | /orders/purchase |
Body: { userId, productId } — must use transaction |
| GET | /orders/:userId |
Returns all orders for a user |