Prisma for TypeScript: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Prisma: A Modern ORM for TypeScript



Prisma is a modern ORM (Object-Relational Mapping) for TypeScript and Node.js that fundamentally changes how you work with databases. Unlike traditional ORMs such as Sequelize or TypeORM, Prisma offers a declarative data model, automatic type generation, and an intuitive API. The library supports PostgreSQL, MySQL, SQLite, SQL Server, MongoDB, and CockroachDB.



The main goal of Prisma is to make database work safe, performant, and convenient. Thanks to TypeScript integration, you get autocompletion and type checking at compile time, which significantly reduces the number of runtime errors. Prisma consists of three key components:



  • Prisma Client — an auto-generated client for working with the database
  • Prisma Migrate — a migration management system
  • Prisma Studio — a GUI for viewing and editing data


Installing Prisma



Installing Prisma in a TypeScript project is done via npm. First, create a new project:



mkdir my-prisma-projectcd my-prisma-projectnpm init -y


Install Prisma CLI and Prisma Client:



npm install prisma @prisma/client --save


Initialize Prisma in the project:



npx prisma init


This command will create a prisma directory with a schema.prisma file and a .env file for environment variables.



Quick Start: Code Example



Let's create a simple application for managing users. First, define the model in prisma/schema.prisma:



generator client {  provider = "prisma-client-js"}

datasource db { provider = "sqlite" url = env("DATABASE_URL")}

model User { id Int @id @default(autoincrement()) email String @unique name String? posts Post[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt}

model Post { id Int @id @default(autoincrement()) title String content String? published Boolean @default(false) author User @relation(fields: [authorId], references: [id]) authorId Int createdAt DateTime @default(now()) updatedAt DateTime @updatedAt}


Run the migration to create tables in SQLite:



npx prisma migrate dev --name init


Now create an index.ts file with a usage example:



import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function main() { // Create a user const user = await prisma.user.create({ data: { email: 'alice@example.com', name: 'Alice', posts: { create: { title: 'Hello World' } } } }) console.log('Created user:', user)

// Get all users with their posts const users = await prisma.user.findMany({ include: { posts: true } }) console.log('All users:', JSON.stringify(users, null, 2))}

main() .catch(e => console.error(e)) .finally(async () => await prisma.$disconnect())


Run the code:



npx ts-node index.ts


Key Prisma Client Methods



1. findUnique

What it does: Finds a single record by a unique identifier (id, email, etc.).

Parameters:

  • where — an object with search conditions (required)
  • include — (optional) to include related models
  • select — (optional) to select specific fields

Example:

const user = await prisma.user.findUni                    

Recommendations