Prisma Client

Explore the
Prisma Client API

From simple reads to complex nested writes, the Prisma Client supports a wide range of operations to help you make the most of your data.

  TypeScript
Get Started
Reading Data
1// This is your Prisma schema file. Learn more in the
2// documentation: https://pris.ly/d/prisma-schema
3
4datasource db {
5 provider = "postgres"
6 url = env("DATABASE_URL")
7}
8
9generator client {
10 // change to `go run github.com/prisma/prisma-client-go` for Go
11 provider = "prisma-client-js"
12}
13
14model User {
15 id Int @id @default(autoincrement())
16 createdAt DateTime @default(now())
17 email String @unique
18 name String
19 age Int
20 role Role @default(USER)
21 country String
22 posts Post[]
23 profile Profile?
24}
25
26model Profile {
27 id Int @id @default(autoincrement())
28 bio String
29 user User @relation(fields: [userId], references: [id])
30 userId Int @unique
31}
32
33model Post {
34 id Int @id @default(autoincrement())
35 createdAt DateTime @default(now())
36 title String
37 published Boolean @default(false)
38 categories Category[] @relation(references: [id])
39 author User @relation(fields: [authorId], references: [id])
40 authorId Int
41}
42
43model Category {
44 id Int @id @default(autoincrement())
45 name String
46 posts Post[] @relation(references: [id])
47}
48
49enum Role {
50 USER
51 ADMIN
52}
53
1// Find all posts
2const allPosts: Post[] = await prisma.post.findMany()
1// Find a user by ID
2const userById: User | null = await prisma.user.findUnique({
3 where: {
4 id: 2,
5 },
6})
1// Find a user by email
2const userByEmail = await prisma.user.findUnique({
3 where: {
4 email: 'ada@prisma.io',
5 },
6})
1// Find the first user that contains Ada
2const userByName = await prisma.user.findFirst({
3 where: {
4 name: {
5 contains: 'Ada',
6 },
7 },
8})
1// Select specific fields
2const userName = await prisma.user.findUnique({
3 where: {
4 email: 'ada@prisma.io',
5 },
6 select: {
7 name: true,
8 email: true,
9 },
10})
1// This is your Prisma schema file. Learn more in the
2// documentation: https://pris.ly/d/prisma-schema
3
4datasource db {
5 provider = "postgres"
6 url = env("DATABASE_URL")
7}
8
9generator client {
10 // change to `go run github.com/prisma/prisma-client-go` for Go
11 provider = "prisma-client-js"
12}
13
14model User {
15 id Int @id @default(autoincrement())
16 createdAt DateTime @default(now())
17 email String @unique
18 name String
19 age Int
20 role Role @default(USER)
21 country String
22 posts Post[]
23 profile Profile?
24}
25
26model Profile {
27 id Int @id @default(autoincrement())
28 bio String
29 user User @relation(fields: [userId], references: [id])
30 userId Int @unique
31}
32
33model Post {
34 id Int @id @default(autoincrement())
35 createdAt DateTime @default(now())
36 title String
37 published Boolean @default(false)
38 categories Category[] @relation(references: [id])
39 author User @relation(fields: [authorId], references: [id])
40 authorId Int
41}
42
43model Category {
44 id Int @id @default(autoincrement())
45 name String
46 posts Post[] @relation(references: [id])
47}
48
49enum Role {
50 USER
51 ADMIN
52}
53
1// Find all posts
2const allPosts: Post[] = await prisma.post.findMany()
1// Find a user by ID
2const userById: User | null = await prisma.user.findUnique({
3 where: {
4 id: 2,
5 },
6})
1// Find a user by email
2const userByEmail = await prisma.user.findUnique({
3 where: {
4 email: 'ada@prisma.io',
5 },
6})
1// Find the first user that contains Ada
2const userByName = await prisma.user.findFirst({
3 where: {
4 name: {
5 contains: 'Ada',
6 },
7 },
8})
1// Select specific fields
2const userName = await prisma.user.findUnique({
3 where: {
4 email: 'ada@prisma.io',
5 },
6 select: {
7 name: true,
8 email: true,
9 },
10})

Supported Databases

index.tsx
1await prisma.user.findMany({
2 where: {
3 email: {
4 endsWith: '@prisma.io',
5 }
6 },
7 include: {
8 p
9 }
10})
 
postsPost[]
profileProfile

Editor Integration

Autocomplete your
way to Success

The best code is the code that writes itself. Prisma Client gives you a fantastic autocomplete experience so you can move quickly and be sure you don't write an invalid query. Our obsession with type safety means you can rest assured that your code works as expected, every time.

Interface sketch and icon of Prisma studio

Prisma Studio

Visual database browser

The easiest way to explore and manipulate Data in your Prisma projects. Understand your data by browsing across tables and their relations and edit them with safety. Available for macOS, Windows and Linux.

diagram showing model schema evolution

Prisma Migrate

Hassle-free migrations

Based on the Prisma Schema, Prisma Migrate automatically generates SQL database migrations, that are fully customizable. For quick data model prototyping in development, prisma db push lets you make changes to the database without generating migration files.

Get Involved

Join the Prisma Community

Join us on Slack

Get help and engage with thousands of Node.js, Typescript and Prisma developers on Slack

Join Prisma Slack

Get TypeScript Stickers

Show your TS + Prisma love with custom free stickers

Order Stickers

Get Node.js Stickers

Show your Node.js + Prisma love with custom free stickers

Order stickers
prisma_logo

© 2022 Prisma Data, Inc.

Newsletter

Stay up to date with the latest features and changes to Prisma