Where Prisma Fits

Prisma Fundamentals

Chapter 1 ยท What an ORM Is & Where Prisma Fits

Almost every application stores data in a database, and almost every application is written in a language that isn't SQL. Something has to sit between the two. This course is about one popular choice for Node.js and TypeScript, Prisma. This first chapter explains the problem Prisma solves, the other ways of solving it, and how Prisma is put together, before any code is written in Chapter 2.

The Problem: Two Different Worlds

A relational database stores data in tables of rows and columns, linked by foreign keys. Your application works with objects: a user with a list of posts, each with its own author. Converting between the two by hand is repetitive and error-prone:

// Using a plain database driver: SQL as a string, results as untyped rows const result = await pool.query( 'SELECT id, email, name FROM users WHERE id = $1', [userId] ); const user = result.rows[0]; // what shape is this? the compiler doesn't know console.log(user.emial); // typo: no error until it prints "undefined"

The SQL is just text, so typos in column names only show up when the code runs, and the results carry no type information. This gap between the table model and the object model is sometimes called the object-relational impedance mismatch.

Three Ways to Bridge the Gap

ApproachExamples (Node.js)How you write queriesTrade-off
Database driverpg, mysql2, better-sqlite3Raw SQL stringsFull control, but no type safety and a lot of repetitive code
Query builderKnex, KyselyJavaScript methods that build SQLClose to SQL, with safer composition; you still map results yourself
ORMSequelize, TypeORM, Prisma, DrizzleMethods on models or a generated clientMuch less code and better types, at the cost of some control and an extra layer to learn

ORM stands for object-relational mapper: a library that maps database tables to objects in your code, so you can read and write data without writing most of the SQL yourself.

An ORM doesn't replace knowing SQL
Every query an ORM runs is still SQL underneath. When something is slow or behaves oddly, you need to understand the SQL it produced — Chapter 9's N+1 problem is the classic example. Prisma also lets you write raw SQL when you need it (Prisma Intermediate/Advanced, Chapter 3).

How Prisma Is Different

Many ORMs have you describe your tables with classes in your application code. Prisma instead starts from a single schema file, written in its own small language, and generates a type-safe client from it:

// schema.prisma: the single source of truth (Chapter 3) model User { id Int @id @default(autoincrement()) email String @unique name String? } // Your code: using the generated client (Chapter 5) const user = await prisma.user.findUnique({ where: { id: userId } }); console.log(user?.emial); // compile error: 'emial' does not exist on type User

The typo that slipped past the plain driver is now caught by TypeScript before the code runs, and your editor can autocomplete every field and query option.

ORMWhere the data model livesStyle
SequelizeJavaScript model definitionsA long-established ORM; types added on top
TypeORMTypeScript classes with decoratorsClose to ORMs in Java and C#
DrizzleTypeScript table definitionsDeliberately SQL-like queries, very thin layer
PrismaA separate schema.prisma fileGenerated client with an object-style query API

None of these is simply "best." Prisma's strengths are its readable schema, strong typing, and polished migration tooling. Its costs are an extra generation step, its own schema language to learn, and a query API that's further from SQL than Drizzle or a query builder.

The Parts of Prisma

PartWhat it doesCovered in
Prisma schemaDescribes your database connection, data models and relationsChapters 3, 7, 8
Prisma ClientThe generated, type-safe library your code uses to query the databaseChapters 5, 6, 9
Prisma MigrateTurns changes to the schema into SQL migration files and applies themChapter 4
Prisma StudioA browser-based viewer and editor for your dataChapter 5
Prisma CLIThe prisma command that runs generation, migrations and StudioThroughout

The company behind Prisma also sells hosted products, such as a managed Postgres database and a connection pooling and caching service. They're optional; everything in this course works with a database you run yourself.

Which Databases, and Which Version?

This course uses Prisma ORM 7, the current stable release line. It supports PostgreSQL, MySQL, MariaDB, SQLite, Microsoft SQL Server, CockroachDB and MongoDB. Examples use SQLite for quick experiments and PostgreSQL for anything closer to production.

What changed in Prisma 7
Earlier versions ran every query through a separate engine written in Rust. Prisma 7's client is written in TypeScript and talks to your database through an ordinary JavaScript database driver, plugged in with a driver adapter. It also added a prisma.config.ts settings file and moved to ES modules. Many older tutorials show the previous setup, so if a guide doesn't match what you see, check which version it was written for. Chapter 2 sets up a project the Prisma 7 way.
Prisma 8 is on the way
At the time of writing, Prisma ORM 8 is available as a release candidate, but only for some databases. Prisma ORM 7 remains the stable, fully supported version and will keep receiving fixes for 18 months after version 8's full release, which is why this course uses it.

Hands-On Exercises

Exercise 1

In your own words, explain the "object-relational impedance mismatch," and give two concrete problems with the plain-driver example at the start of this chapter.

๐Ÿ“„ View solution
Exercise 2

For each project, suggest whether a plain driver, a query builder, or an ORM such as Prisma would suit best, and why: (a) a one-off script that copies data between two tables; (b) a medium-sized web app written in TypeScript by a small team; (c) a reporting tool made almost entirely of complex hand-tuned SQL.

๐Ÿ“„ View solution
Exercise 3

Find a Prisma tutorial online and work out which Prisma version it was written for. What clues in its code or setup instructions tell you, based on this chapter?

๐Ÿ“„ View solution

Chapter 1 Quick Reference

  • An ORM (object-relational mapper) maps database tables to objects in code
  • Options range from raw drivers (pg) through query builders (Knex, Kysely) to ORMs (Sequelize, TypeORM, Drizzle, Prisma)
  • Prisma is schema-first: schema.prisma describes the data, and a type-safe client is generated from it
  • Its parts: the schema, Prisma Client, Prisma Migrate, Prisma Studio and the prisma CLI
  • This course uses Prisma ORM 7: a TypeScript client with driver adapters, prisma.config.ts and ES modules
  • Prisma 8 is a release candidate for some databases; version 7 stays the stable choice
  • You still need SQL to understand what an ORM is doing