TypeScript ORM
with Zero-Cost Type Safety for Your Database

Query data from MySQL, PostgreSQL & SQL Server databases with Prisma – a type-safe TypeScript ORM for Node.js

What is Prisma?

Prisma is an open-source ORM that drastically simplifies data modeling, migrations, and data access for SQL databases in Node.js and TypeScript.

Query
// Creating a new record
await prisma.user.create({
firstName: “Alice”,
email: “alice@prisma.io”
})
Table
id firstName email
1 Bobby bobby@tables.io
2 Nilufar nilu@email.com
3 Jürgen jums@dums.edu
4 Alice alice@prisma.io

How Prisma and TypeScript fit together

TypeScript is a statically typed language which builds on JavaScript. It provides you with all the functionality of JavaScript with the additional ability to type and verify your code which saves you time by catching errors and providing fixes before you run your code. All valid JavaScript code is also TypeScript code which makes TypeScript easy for you to adopt.

Prisma is an ORM for Node.js and TypeScript that gives you the benefits of type safety at zero cost by auto-generating types from your database schema. It's ideal for building reliable data intensive application. Prisma makes you more confident and productive when storing data in a relational database. You can use it with any Node.js server framework to interact with your database.

Diagram showing Prisma fitting in the Server part of the Client-server-database stack
clientTypeScriptdatabase

The Prisma schema

The Prisma schema uses Prisma's modeling language to define your database schema and to generate the corresponding TypeScript types. It makes data modeling easy and intuitive, especially when it comes to modeling relations.

The types generated from the Prisma schema ensure that all your database queries are type safe. Prisma Client gives you a fantastic autocomplete experience so you can move quickly and be sure you don't write an invalid query.

// Define the `User` table in the database
model User {
id String @id @default(cuid())
email String @unique
password String
name String?
posts Post[]
}
// Define the `Post` table in the database
model Post {
id String @id @default(cuid())
createdAt DateTime @default(now())
title String
content String?
authorId String
author User @relation(fields: [authorId], references: [id])
}
type User = {
id: string
email: string
password: string
name: string | null
}
export type Post = {
id: string
createdAt: Date
title: string
content: string | null
authorId: string
}

Prisma and TypeScript Code Examples

Define your schema once and Prisma will generate the TypeScript types for you. No need for manual syncing between the types in your database schema and application code.

The code below demonstrates how database queries with Prisma are fully type safe – for all queries, including partial queries, and relations.

Type-safe database queries

Zero-cost type safety

Queries with Prisma Client always have their return type inferred making it easy to reason about the returned data – even when you fetch relations.

1// Inferred type:
2// User & {
3// posts: Post[];
4// }
5const user = await prisma.user.create({
6 data: {
7 email: 'alice@prisma.io',
8 password: '0ee4808f893b8e05bdd251048d5c4c8af8bb89403676dda95619841a481f8e87',
9 name: 'Alice',
10 posts: {
11 create: {
12 title: 'Learn how to use Prisma with TypeScript',
13 content: 'https://www.prisma.io/docs/',
14 },
15 },
16 },
17 include: {
18 posts: true,
19 },
20})
Generated types

Zero-cost type safety

Queries with Prisma Client always have their return type inferred making it easy to reason about the returned data – even when you fetch relations.

1// Inferred type:
2// User & {
3// posts: Post[];
4// }
5const user = await prisma.user.create({
6 data: {
7 email: 'alice@prisma.io',
8 password: '0ee4808f893b8e05bdd251048d5c4c8af8bb89403676dda95619841a481f8e87',
9 name: 'Alice',
10 posts: {
11 create: {
12 title: 'Learn how to use Prisma with TypeScript',
13 content: 'https://www.prisma.io/docs/',
14 },
15 },
16 },
17 include: {
18 posts: true,
19 },
20})

Why Prisma and TypeScript?

Type-safe database client

Prisma Client ensures fully type-safe database queries so that you never write an invalid query

Optimized database queries

Prisma's built-in dataloader ensures optimized and performant database queries, even for N+1 queries.

Autocompletion

Prisma helps you write your queries with rich autocompletion as you write queries

Type inference

Queries with Prisma Client always have their return type inferred making it easy to reason about your data.

Easy database migrations

Map your Prisma schema to the database so you don't need to write SQL to manage your database schema.

Filters, pagination & ordering

Prisma Client reduces boilerplates by providing convenient APIs for common database features.

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

We ❤️ TypeScript

At Prisma, we love TypeScript and believe in its bright future. Since the inception of Prisma, we've pushed the TypeScript compiler to its limits in order to provide unprecedented type-safe database access, and rich autocompletion for a delightful developer experience.

Prisma is built with type safety at its heart so that you make less mistakes. By tapping into TypeScript's structural type system Prisma maps database queries into structural types so you can know the precise shape of the data returned for every Prisma Client query you write.

Our TypeScript Resources

TypeScript Berlin Meetup

The TypeScript Berlin Meetup started in 2019 and is one of the most popular TypeScript Meetups in the world

Prisma TypeScript examples

Ready-to-run example projects using Prisma, TypeScript and a variety of different frameworks and API technologies

Building a modern backend with TypeScript, PostgreSQL & Prisma

A tutorial series for building a modern backend with hapi and Prisma

Get Involved

Join the TypeScript + Prisma Community

Join us on Slack

Get help and engage with thousands of TypeScript and Prisma developers on Slack

Join Prisma Slack

Showcase on Twitter

We’d love to see what you’re building with TypeScript and Prisma

Tweet about Prisma and TypeScript

Get Stickers

Show your TypeScript + Prisma love with custom free stickers

Order stickers