Prisma is anext-generation ORMthat consists of these tools:
- Prisma Client:Auto-generated and type-safe query builder for Node.js & TypeScript
- Prisma Migrate:Declarative data modeling & migration system
- Prisma Studio:GUI to view and edit data in your database
Prisma Client can be used inanyNode.js or TypeScript backend application (including serverless applications and microservices). This can be aREST API,aGraphQL API,a gRPC API, or anything else that needs a database.
The Prisma ORM can also further be extended with these Prisma products:
- Prisma Accelerate:Global database cache with scalable connection pooling
- Prisma Pulse:Real-time database events with type-safe subscriptions
- Prisma Optimize:AI-powered query optimization and performance insights
- Prisma Studio:A visual editor for the data in your database
The fastest way to get started with Prisma is by following theQuickstart (5 min).
The Quickstart is based on a preconfigured SQLite database. You can also get started with your own database (PostgreSQL and MySQL) by following one of these guides:
This section provides a high-level overview of how Prisma works and its most important technical components. For a more thorough introduction, visit thePrisma documentation.
Every project that uses a tool from the Prisma toolkit starts with aPrisma schema file.The Prisma schema allows developers to define theirapplication modelsin an intuitive data modeling language. It also contains the connection to a database and defines agenerator:
// Data source
datasourcedb{
provider="postgresql"
url=env("DATABASE_URL")
}
// Generator
generatorclient{
provider="prisma-client-js"
}
// Data model
modelPost{
idInt@id@default(autoincrement())
titleString
contentString?
publishedBoolean@default(false)
authorUser?@relation(fields:[authorId],references:[id])
authorIdInt?
}
modelUser{
idInt@id@default(autoincrement())
emailString@unique
nameString?
postsPost[]
}
In this schema, you configure three things:
- Data source:Specifies your database connection (via an environment variable)
- Generator:Indicates that you want to generate Prisma Client
- Data model:Defines your application models
On this page, the focus is on the data model. You can learn more aboutData sourcesandGeneratorson the respective docs pages.
The data model is a collection ofmodels.A model has two major functions:
- Represent a table in the underlying database
- Provide the foundation for the queries in the Prisma Client API
There are two major workflows for "getting" a data model into your Prisma schema:
- Generate the data model fromintrospectinga database
- Manually writing the data model and mapping it to the database withPrisma Migrate
Once the data model is defined, you cangenerate Prisma Clientwhich 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 theprisma generate
command which reads your Prisma schema andgeneratesthe Prisma Client code. The code will be located innode_modules/.prisma/client
,which is exported bynode_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 insidenode_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'
constprisma=newPrismaClient()
or
const{PrismaClient}=require('@prisma/client')
constprisma=newPrismaClient()
Now you can start sending queries via the generated Prisma Client API, here are a few sample queries. Note that all Prisma Client queries returnplain old JavaScript objects.
Learn more about the available operations in thePrisma Client docsor watch thisdemo video(2 min).
// Run inside `async` function
constallUsers=awaitprisma.user.findMany()
// Run inside `async` function
constallUsers=awaitprisma.user.findMany({
include:{posts:true},
})
// Run inside `async` function
constfilteredPosts=awaitprisma.post.findMany({
where:{
OR:[{title:{contains:'prisma'}},{content:{contains:'prisma'}}],
},
})
// Run inside `async` function
constuser=awaitprisma.user.create({
data:{
name:'Alice',
email:'[email protected]',
posts:{
create:{title:'Join us for Prisma Day 2021'},
},
},
})
// Run inside `async` function
constpost=awaitprisma.post.update({
where:{id:42},
data:{published:true},
})
Note that when using TypeScript, the result of this query will bestatically typedso 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 theAdvanced usage of generated typespage in the docs.
Prisma has a large and supportivecommunityof enthusiastic application developers. You can join us onDiscordand here onGitHub.
If you have a security issue to report, please contact us at[email protected].
You can ask questions and initiatediscussionsabout Prisma-related topics in theprisma
repository on GitHub.
If you see an error message or run into an issue, please make sure to create a bug report! You can findbest 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 theroadmapto 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 ourcontribution guidelinesandCode of Conduct for contributors.