Sikau

Prisma · Day 1 · about 20 min

What is Prisma?

A type-safe ORM: schema, client and migrations.

SQL power, TypeScript comfort

Writing SQL strings in app code is error-prone: typos in column names only show up at runtime, and results come back untyped. Prisma is an ORM: you describe your data in schema.prisma, and it generates a fully typed client — prisma.dish.findMany() — with autocomplete for every field.

Schema + client

import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

const cheap = await prisma.dish.findMany({
  where: { price: { lt: 200 } },
  orderBy: { price: "asc" },
  select: { name: true, price: true },
});
console.log(cheap);

console.log("dishes:", await prisma.dish.count());
  • Real setup: npm i -D prisma && npm i @prisma/client, npx prisma init, then npx prisma migrate dev creates tables from the schema.
  • npx prisma studio opens a visual data browser.
  • Prisma supports PostgreSQL, MySQL, SQLite, SQL Server, CockroachDB and MongoDB.

The rest of this lesson

  • Three parts of Prismaexplain
  • What the types buy youexplain
  • What does it return?predict
  • Which part?match
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 15 lessons of Prisma