CodingNic

Introduction to ORMs and Prisma Setup

Exercises

Introduction to ORMs and Prisma Setup 25 min read

Exercises

Objectives

By the end of this lesson, you should be able to:

  • Set up a complete Prisma project from scratch, connected to a real PostgreSQL database
  • Write and read a basic Prisma schema with more than one model
  • Generate and use Prisma Client for a simple read query

⚠️ A note on verification: as throughout this module, the Prisma CLI can’t run inside this course’s own sandboxed tooling, so the commands and expected output below reflect Prisma’s stable, current, documented behavior rather than this course’s own live execution. This is a genuinely hands-on exercise, run every step yourself, on your own machine, against your own database.

Exercise: A Library Schema

a) Set up a project. Create a new project, library-prisma, install prisma and @prisma/client, and run prisma init --datasource-provider postgresql.

b) Connect it. Point DATABASE_URL in .env at a real PostgreSQL database (reuse library from Module 1’s exercises, or create a fresh one).

c) Define two models. In schema.prisma, add:

text
model Book {
  id       Int     @id @default(autoincrement())
  title    String
  author   String
  available Boolean @default(true)
}

model Member {
  id    Int    @id @default(autoincrement())
  name  String
  email String @unique
}

d) Generate the client. Run npx prisma generate, and confirm it completes without error.

e) Write a script. Create index.js, import and instantiate PrismaClient, and call prisma.book.findMany() and prisma.member.findMany(), logging both results (they’ll be empty arrays for now, since no migration has created the tables yet, that’s next module).

f) Explain the gap. In your own words, explain why schema.prisma having a Book model doesn’t mean a books table exists in the database yet, what step is still missing, and which module covers it.

g) Compare to Module 1. Write out, side by side, the CREATE TABLE statement Module 1 would use for Member, and the Prisma model Member block above. Identify which parts of each map to which.

Recap

This module covered installing Prisma, the schema file’s three parts (generator, datasource, model), and Prisma Client, the generated library every query in this course runs through from here on.

Next module: turning the models defined here into real database tables, with migrations.