Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
Prisma ORM is a next-generation ORM that consists of these tools:
Prisma Client can be used in any Node.js or TypeScript backend application (including serverless applications and microservices). This can be a REST API, a GraphQL API, a gRPC API, or anything else that needs a database.
If you need a database to use with Prisma ORM, check out Prisma Postgres.
Prisma ORM can further be extended with these Prisma products:
The fastest way to get started with Prisma is by following the quickstart guides. You can choose either of two databases:
If you already have your own database, you can follows these guides:
This section provides a high-level overview of how Prisma ORM works and its most important technical components. For a more thorough introduction, visit the Prisma documentation.
Every project that uses a tool from the Prisma toolkit starts with a Prisma schema file. The Prisma schema allows developers to define their application models in an intuitive data modeling language. It also contains the connection to a database and defines a generator:
// Data source
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
// Generator
generator client {
provider = "prisma-client-js"
}
// Data model
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
In this schema, you configure three things:
On this page, the focus is on the data model. You can learn more about Data sources and Generators on the respective docs pages.
The data model is a collection of models. A model has two major functions:
There are two major workflows for “getting” a data model into your Prisma schema:
Once the data model is defined, you can generate Prisma Client which will expose CRUD and more queries for the defined models. If you’re using TypeScript, you’ll get full type-safety for all queries (even when only retrieving the subsets of a model’s fields).
The first step when using Prisma Client is installing its npm package:
npm install @prisma/client
Note that the installation of this package invokes the prisma generate
command which reads your Prisma schema and generates the Prisma Client code. The code will be located in node_modules/.prisma/client
, which is exported by node_modules/@prisma/client/index.d.ts
.
After you change your data model, you’ll need to manually re-generate Prisma Client to ensure the code inside node_modules/.prisma/client
gets updated:
npx prisma generate
Refer to the documentation for more information about “generating the Prisma client”.
Once the Prisma Client is generated, you can import it in your code and send queries to your database. This is what the setup code looks like.
You can import and instantiate Prisma Client as follows:
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
or
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
Now you can start sending queries via the generated Prisma Client API, here are a few sample queries. Note that all Prisma Client queries return plain old JavaScript objects.
Learn more about the available operations in the Prisma Client docs or watch this demo video (2 min).
User
records from the database// Run inside `async` function
const allUsers = await prisma.user.findMany()
posts
relation on each returned User
object// Run inside `async` function
const allUsers = await prisma.user.findMany({
include: { posts: true },
})
Post
records that contain "prisma"
// Run inside `async` function
const filteredPosts = await prisma.post.findMany({
where: {
OR: [{ title: { contains: 'prisma' } }, { content: { contains: 'prisma' } }],
},
})
User
and a new Post
record in the same query// Run inside `async` function
const user = await prisma.user.create({
data: {
name: 'Alice',
email: '[email protected]',
posts: {
create: { title: 'Join us for Prisma Day 2021' },
},
},
})
Post
record// Run inside `async` function
const post = await prisma.post.update({
where: { id: 42 },
data: { published: true },
})
Note that when using TypeScript, the result of this query will be statically typed so that you can’t accidentally access a property that doesn’t exist (and any typos are caught at compile-time). Learn more about leveraging Prisma Client’s generated types on the Advanced usage of generated types page in the docs.
Prisma has a large and supportive community of enthusiastic application developers. You can join us on Discord and here on GitHub.
Built something awesome with Prisma? 🌟 Show it off with these badges, perfect for your readme or website.
[![Made with Prisma](http://made-with.prisma.io/dark.svg)](https://prisma.io)
[![Made with Prisma](http://made-with.prisma.io/indigo.svg)](https://prisma.io)
If you have a security issue to report, please contact us at [email protected].
You can ask questions and initiate discussions about Prisma-related topics in the prisma
repository on GitHub.
If you see an error message or run into an issue, please make sure to create a bug report! You can find best practices for creating bug reports (like including additional debugging output) in the docs.
If Prisma currently doesn’t have a certain feature, be sure to check out the roadmap to see if this is already planned for the future.
If the feature on the roadmap is linked to a GitHub issue, please make sure to leave a 👍 reaction on the issue and ideally a comment with your thoughts about the feature!
Refer to our contribution guidelines and Code of Conduct for contributors.