Practice & Assessment
Test your understanding of Database Integration with PostgreSQL and Prisma
Multiple Choice Questions
5Why should PrismaClient be instantiated as a singleton rather than creating a new instance in each module?
What does `select` do in a Prisma query and why is it important for API security?
An API fetches 50 blog posts and their authors. Without eager loading, how many database queries does this likely produce?
Why should you use `prisma migrate deploy` instead of `prisma migrate dev` in production deployments?
Inside a Prisma interactive transaction, what happens if you throw an error?
Coding Challenges
1Build a posts API with Prisma and PostgreSQL
Using a local PostgreSQL database: 1) Create a Prisma schema with User (id, email, name, createdAt) and Post (id, title, content, published boolean default false, authorId FK, createdAt). Run migrations. 2) Seed 3 users and 10 posts (mix of published/draft). 3) Implement: GET /posts?published=true&authorId=1 (with eager-loaded author name), POST /posts (create with authorId in body), PATCH /posts/:id (update title, content, or published), DELETE /posts/:id. 4) All endpoints must use the PrismaClient singleton. Test with curl. Time estimate: 30 minutes.
Mini Project
E-Commerce API with PostgreSQL
Build an e-commerce REST API backed by PostgreSQL via Prisma. Schema: User, Product (id, name, price, stock, categoryId), Category, Order (id, userId, status, totalAmount, createdAt), OrderItem (orderId, productId, quantity, unitPrice). Endpoints: full CRUD for /products and /categories, GET /products?category=&minPrice=&maxPrice=&inStock=true with eager-loaded category. POST /orders — creates order and all OrderItems in a single Prisma transaction; decrement stock for each product, throw if any product is out of stock. GET /orders/:id with items and product names. PATCH /orders/:id/status (admin only). All responses use select to exclude internal fields. Include at least two database migrations (initial schema, then add a description field to Product).
