Skip to content
/ prisma Public

Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB

License

Notifications You must be signed in to change notification settings

prisma/prisma

Prisma

Prisma

Discord

Quickstart Website Docs Examples Blog Discord Twitter

What is Prisma?

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:

Getting started

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:

How Prisma works

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.

The Prisma schema

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

The Prisma data model

On this page, the focus is on the data model. You can learn more aboutData sourcesandGeneratorson the respective docs pages.

Functions of Prisma models

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

Getting a data model

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).


Accessing your database with Prisma Client

Generating Prisma Client

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 generatecommand 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/clientgets updated:

npx prisma generate

Refer to the documentation for more information about"generating the Prisma client".

Using Prisma Client to send queries to your database

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.

Import and instantiate Prisma Client

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).

Retrieve allUserrecords from the database
// Run inside `async` function
constallUsers=awaitprisma.user.findMany()
Include thepostsrelation on each returnedUserobject
// Run inside `async` function
constallUsers=awaitprisma.user.findMany({
include:{posts:true},
})
Filter allPostrecords that contain"prisma"
// Run inside `async` function
constfilteredPosts=awaitprisma.post.findMany({
where:{
OR:[{title:{contains:'prisma'}},{content:{contains:'prisma'}}],
},
})
Create a newUserand a newPostrecord in the same query
// Run inside `async` function
constuser=awaitprisma.user.create({
data:{
name:'Alice',
email:'[email protected]',
posts:{
create:{title:'Join us for Prisma Day 2021'},
},
},
})
Update an existingPostrecord
// Run inside `async` function
constpost=awaitprisma.post.update({
where:{id:42},
data:{published:true},
})

Usage with TypeScript

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.

Community

Prisma has a large and supportivecommunityof enthusiastic application developers. You can join us onDiscordand here onGitHub.

Security

If you have a security issue to report, please contact us at[email protected].

Support

Ask a question about Prisma

You can ask questions and initiatediscussionsabout Prisma-related topics in theprismarepository on GitHub.

👉Ask a question

Create a bug report for Prisma

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.

👉Create bug report

Submit a feature request

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!

👉Submit feature request

Contributing

Refer to ourcontribution guidelinesandCode of Conduct for contributors.

Tests Status

  • Prisma Tests Status:
    Prisma Tests Status
  • Ecosystem Tests Status:
    Ecosystem Tests Status