Before you start
Check these off so you’re ready to save a real row.
Not today
Do not add Cloudinary, Vercel, or login. Never commit .env or paste the Neon connection string in chat.
Neon + Prisma 6 + Listing table
- 1
Create a Neon project named
marketplaceand copy thepostgresql://connection string. - 2
In your Next.js app run
npm install prisma@6 @prisma/client@6thennpx prisma init. Use version 6 — latest Prisma needs extra files we skip in class. - 3
Put the string in
.envasDATABASE_URL="...". - 4
Add the Listing model, then run
npx prisma migrate dev --name initandnpx prisma studio.
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Listing {
id Int @id @default(autoincrement())
title String
description String
price Float
imageUrl String @default("https://placehold.co/300x200")
createdAt DateTime @default(now())
}Prisma Studio should open an empty Listing table. Empty is success. If you see prisma.config.ts or adapter errors, reinstall Prisma 6.
GET and POST at /api/listings
- 1
Create
lib/db.tswith onePrismaClient. Import this file everywhere you need the database. - 2
Create
app/api/listings/route.ts(not route.js). - 3
Export
GETthatfindManynewest first, andPOSTthatcreates from the JSON body. - 4
Open
http://localhost:3000/api/listings— you should see JSON, not the homepage.
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export default prisma;
// route.ts
import prisma from '@/lib/db';
export async function GET() {
const listings = await prisma.listing.findMany({
orderBy: { createdAt: 'desc' },
});
return Response.json(listings);
}
export async function POST(request: Request) {
const body = await request.json();
const listing = await prisma.listing.create({
data: {
title: body.title,
description: body.description,
price: parseFloat(body.price),
},
});
return Response.json(listing);
}fetch('/api/listings', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
title: 'Test Item',
description: 'A test.',
price: 100,
}),
})An empty array [] means the route works and the table is still empty.
Home reads the table
- 1
Remove the hard-coded listings array from
app/page.tsx. - 2
Make Home an
asyncfunction and load listings withprisma.listing.findMany. - 3
Keep Module 6
ListingCardandLink. - 4
Add a link to
/post.
import ListingCard from '../components/ListingCard';
import Link from 'next/link';
import prisma from '@/lib/db';
export default async function Home() {
const listings = await prisma.listing.findMany({
orderBy: { createdAt: 'desc' },
});
return (
<main style={{ padding: '32px' }}>
<div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '24px' }}>
<h2>Available Listings</h2>
<Link href="/post">Post an Item</Link>
</div>
<div style={{ display: 'flex', gap: '24px', flexWrap: 'wrap' }}>
{listings.map((item) => (
<Link key={item.id} href={'/listings/' + item.id}>
<ListingCard title={item.title} price={item.price} description={item.description} />
</Link>
))}
</div>
</main>
);
}The home page reads the table. The form writes through the API. Same database.
Post form + detail page
- 1
Create
app/post/page.tsxwith'use client'at the top. - 2
Use
useStatefor title, price, and description — this page runs in the browser so inputs can remember text. - 3
On submit,
POST /api/listingsthenrouter.push('/'). - 4
Update
app/listings/[id]/page.tsxtofindUniqueafterawait params— database ids are not the old 1, 2, 3.
'use client';
import { useState } from 'react';
import { useRouter } from 'next/navigation';
export default function PostItem() {
const [title, setTitle] = useState('');
const [price, setPrice] = useState('');
const [description, setDescription] = useState('');
const router = useRouter();
async function handleSubmit(e) {
e.preventDefault();
await fetch('/api/listings', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title, price, description }),
});
router.push('/');
}
return (
<main style={{ padding: '32px', maxWidth: '500px' }}>
<h2>Post an Item</h2>
<form onSubmit={handleSubmit}>
<input value={title} onChange={(e) => setTitle(e.target.value)} placeholder="Title" required />
<input type="number" value={price} onChange={(e) => setPrice(e.target.value)} placeholder="Price" required />
<textarea value={description} onChange={(e) => setDescription(e.target.value)} placeholder="Description" required />
<button type="submit">Post Item</button>
</form>
</main>
);
}import prisma from '@/lib/db';
export default async function ListingDetail({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params;
const listing = await prisma.listing.findUnique({
where: { id: parseInt(id) },
});
if (!listing) {
return <p style={{ padding: '32px' }}>Listing not found.</p>;
}
return (
<main style={{ padding: '32px' }}>
<h1>{listing.title}</h1>
<p>₱{listing.price}</p>
<p>{listing.description}</p>
</main>
);
}Post through the form. Refresh home. The listing stays. That is Wall 1, gone.
Seed sample listings
- 1
Download seed.ts and save it as
prisma/seed.ts. - 2
In the project folder run
npx tsx prisma/seed.ts. - 3
Refresh home — six campus sample listings should appear.
- 4
Optional: open Prisma Studio and confirm the same six rows.
npx tsx prisma/seed.ts
# Expected:
# Seeding sample marketplace listings...
# Done. 6 listings ready — refresh your home page.Seed clears existing Listing rows first (deleteMany), then inserts six samples. Post your own after seeding.
Done checklist
Tick everything before you submit.
Submit
Turn in the repo (no .env) and two screenshots that prove the row survived a refresh.
| Deliverable | What to turn in |
|---|---|
| Repo | GitHub URL — without .env |
| Home | Screenshot after refresh, showing a listing you posted |
| Studio | Screenshot of that row in Prisma Studio |
Still on a fake array? Finish Module 6 first then come back. Photos and a live URL are Pre-final Module 8 .