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

tech

What is Prisma?

Prisma makes working with data easy! It offers a type-safe Node.js & TypeScript ORM, global database caching, connection pooling, and real-time database events.

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.

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.

1// Define the `User` table in the database
2model User {
3 id String @id @default(cuid())
4 email String @unique
5 password String
6 name String?
7 posts Post[]
8}
9
10// Define the `Post` table in the database
11model Post {
12 id String @id @default(cuid())
13 createdAt DateTime @default(now())
14 title String
15 content String?
16 authorId String
17 author User @relation(fields: [authorId], references: [id])
18}

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.

1type User = {
2 id: string
3 email: string
4 password: string
5 name: string | null
6}
7
8export type Post = {
9 id: string
10 createdAt: Date
11 title: string
12 content: string | null
13 authorId: string
14}

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 & Prism

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