apps/blog/content/blog/prisma-the-complete-orm-inw24qjeawmb/index.mdx
Prisma is a next-generation ORM for Node.js and TypeScript. After more than two years of development, we are excited to share that all Prisma tools are ready for production!
Prisma is a next-generation and open-source ORM for Node.js and TypeScript. It consists of the following tools:
These tools can be adopted together or individually in any Node.js or TypeScript project. Prisma currently supports PostgreSQL, MySQL, SQLite, SQL Server (Preview). A connector for MongoDB is in the works, sign up for the Early Access program here.
Working with databases is one of the most challenging areas of application development. Data modeling, schema migrations and writing database queries are common tasks application developers deal with every day.
At Prisma, we found that the Node.js ecosytem – while becoming increasingly popular to build database-backed applications – does not provide modern tools for application developers to deal with these tasks.
Application developers should care about data – not SQL
As tools become more specialized, application developers should be able to focus on implementing value-adding features for their organizations instead of spending time plumbing together the layers of their application by writing glue code.
Although Prisma solves similar problems as traditional ORMs, its approach to these problems is fundamentally different.
<Youtube videoId="EEDGwLB55bI" />When using Prisma, you define your data model in the Prisma schema. Here's a sample of what your models look like with Prisma:
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[]
}
Each of these models maps to a table in the underlying database and serves as foundation for the generated data access API provided by Prisma Client. Prisma's VS Code extension provides syntax highlighting, autocompletion, quick fixes and lots of other features to make data modeling a magical and delightful experience ✨
Prisma Migrate translates the Prisma schema into the required SQL to create and alter the tables in your database. It can be used via the prisma migrate commands provided the Prisma CLI.
PRIMARY KEY ("id")
);
CREATE TABLE "User" ( "id" SERIAL NOT NULL, "email" TEXT NOT NULL, "name" TEXT,
PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "User.email_unique" ON "User"("email");
ALTER TABLE "Post" ADD FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
```sql
CREATE TABLE `Post` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`title` VARCHAR(191) NOT NULL,
`content` VARCHAR(191),
`published` BOOLEAN NOT NULL DEFAULT false,
`authorId` INTEGER,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE TABLE `User` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`email` VARCHAR(191) NOT NULL,
`name` VARCHAR(191),
UNIQUE INDEX `User.email_unique`(`email`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE `Post` ADD FOREIGN KEY (`authorId`) REFERENCES `User`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
CREATE TABLE "Post" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" TEXT NOT NULL,
"content" TEXT,
"published" BOOLEAN NOT NULL DEFAULT false,
"authorId" INTEGER,
FOREIGN KEY ("authorId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE "User" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"email" TEXT NOT NULL,
"name" TEXT
);
CREATE UNIQUE INDEX "User.email_unique" ON "User"("email");
CREATE TABLE [dbo].[Post] (
[id] INT NOT NULL IDENTITY(1,1),
[title] NVARCHAR(1000) NOT NULL,
[content] NVARCHAR(1000),
[published] BIT NOT NULL CONSTRAINT [DF__Post__published] DEFAULT 0,
[authorId] INT,
CONSTRAINT [PK__Post__id] PRIMARY KEY ([id])
);
CREATE TABLE [dbo].[User] (
[id] INT NOT NULL IDENTITY(1,1),
[email] NVARCHAR(1000) NOT NULL,
[name] NVARCHAR(1000),
CONSTRAINT [PK__User__id] PRIMARY KEY ([id]),
CONSTRAINT [User_email_unique] UNIQUE ([email])
);
ALTER TABLE [dbo].[Post] ADD CONSTRAINT [FK__Post__authorId] FOREIGN KEY ([authorId]) REFERENCES [dbo].[User]([id]) ON DELETE SET NULL ON UPDATE CASCADE;
While the SQL is generated automatically based on the Prisma schema, you can easily customize it to your specific needs. With this approach, Prisma Migrate strikes a great balance between productivity and control.
A major benefit of working with Prisma Client is that it lets developers think in objects and therefore offers a familiar and natural way to reason about their data.
Prisma Client doesn't have the concept of model instances. Instead, it helps to formulate database queries that always return plain JavaScript objects. Thanks to the generated types, you get autocompletion for these queries as well.
Also, as a bonus for TypeScript developers: All results of Prisma Client queries are fully typed. In fact, Prisma provides the strongest type-safety guarantees of any TypeScript ORM (you can read a type-safety comparison with TypeORM here).
Click through the tabs in this code block to explore some Prisma Client queries (or explore the full API reference):
// Find all posts
const posts = await prisma.post.findMany()
// Find all posts and include their authors in the result
const postsWithAuthors = await prisma.post.findMany({
include: { author: true },
})
// Create a new user with a new post
const userWithPosts: User = await prisma.user.create({
data: {
email: '[email protected]',
name: 'Ada Lovelace',
posts: {
create: [{ title: 'Hello World' }],
},
},
})
// Find all users with `@prisma` emails
const users = await prisma.user.findMany({
where: {
email: { contains: '@prisma' },
},
})
const postsByUser = await prisma.user.findUnique({ where: { email: '[email protected]' } }).posts()
const posts = await prisma.post.findMany({
take: 5,
cursor: { id: 2 },
})
Prisma also comes with a modern admin interface for your database – think phpMyAdmin but in 2021 😉
Prisma is agnostic to the application that you build and will complement your stack nicely, no matter what your favorite technologies are. You can find out more about how Prisma works with your favorite framework or library here:
If you want to explore Prisma with any these techonlogies or others, you can checkout our ready-to-run examples:
Explore Prisma example projects
Prisma has evolved a lot in the past three years and we are incredibly excited to share the result with the developer community.
As a company we've gone through a number of major product iterations and pivots over the last years since we've started building developer tools:
Prisma is the result of the learnings we've gathered from being an early innovator in the GraphQL ecosystem and the insights we gained into the data layers of companies of all sizes, from small startups to major enterprises.
Used by thousands of companies since the initial release three years ago, Prisma has been battle-tested and is ready to be used in mission-critical applications.
Prisma is developed in the open. Our Product and Engineering teams are monitoring GitHub issues and typically respond within 24 hours after an issue is opened.
New releases happen every two weeks with new features, bug fixes and lots of improvements. After each release, we do a livestream on Youtube to present the new features and get feedback from our community.
We also try to help developers wherever they raise questions about Prisma, be it on Slack, GitHub Discussions or Stackoverflow via a dedicated Community Support team.
Here is our community in numbers:
| What? | How many? |
|---|---|
| Closed GitHub issues since initial release | > 2,5k |
| Resolved support requests (GitHub, Slack, Stackoverflow, ...) | > 3k |
| Members on Prisma Slack | > 40k |
| GitHub contributors across repos | > 300 |
| GitHub stars (help us get to 10k 🌟) | > 9,9k |
| Shipped sticker packs in 2021 (order your stickers here) | > 100 |
| Hosted developer events since 2017 (Meetups, conferences, ...) | > 50 |
If you want to learn about all the great stuff that has happened in 2021 already, check out this blog post: What's new in Prisma? (Q1/21)
We have been excited to see how Prisma has helped companies of all sizes become more productive and ship products faster.
Throughout our journey, companies such as Adidas, HyreCar, Agora Systems, Labelbox, and many more, have provided us with valuable input on how to evolve our product. We've had the pleasure of working with some of the most innovative and ingenuous tech leaders, such as:
{/* <UsingCompanyListCarousel initialActiveIndex={0} itemsToShow={2} outerSpacing={100}>
<UsingCompany title="Rapha" text="“Prisma helps Rapha stay flexible, allowing us to evaluate data storage options while maintaining a consistent developer experience.”" imagePath="/blog/posts/companies/rapha.jpeg" repDetails="Tom Hutchinson – Head of Mobile" techStack={['graphql', 'apollo_graphql_compact', 'aws', 'aws_rds', 'postgresql']} /> <UsingCompany title="Grover" text="“I can see only benefits in using Prisma both from a development and deployment perspective. I introduced it in one project and now other teams are adopting it organically.”" imagePath="/blog/posts/companies/grover.png" repDetails=" Ricardo Almeida – Software Engineer" techStack={['nestjs', 'jest', 'aws', 'aws_rds', 'postgresql']} /> <UsingCompany title="iopool" text="“Because we found Prisma, we decided to start the refactor for the full project. We knew that Prisma would help us to go faster and be more confident, especially as we had a limited time to do the refactor.”" imagePath="/blog/posts/companies/iopool.png" repDetails="Luc Matagne - Engineering Lead" techStack={['graphql', 'apollo_graphql_compact', 'aws', 'aws_rds', 'postgresql']} /> </UsingCompanyListCarousel> */}
If you want to learn how Prisma helped these companies be more productive, check out these resources:
The best developer tools are the ones that go out of your way and easily adapt with the increasing complexity of a project. That's exactly how we designed Prisma.
Prisma has built-in workflows for all stages of the development lifecycle, from prototyping, to development, to deployment, to CI/CD, to testing and more. Check out our documentation and articles to learn about these workflows and how to accomplish all of them with Prisma.
<Accordions type="single"> <Accordion title="Expand to learn about the application lifecycle with Prisma"> | Development Stage | Link | Resource | | :---------------- | :---------------------------------------------------------------------------------------------------------------------------------- | ------------- | | Plan | [Data Modeling](https://www.prisma.io/dataguide/datamodeling) | Data Guide | | Plan | [Prisma schema](https://www.prisma.io/docs/concepts/components/prisma-schema) | Documentation | | Code | [Prisma Client API](https://www.prisma.io/docs/concepts/components/prisma-client) | Documentation | | Test | [Testing best practices with Prisma](https://www.prisma.io/docs/guides/testing) | Documentation | | Deploy | [Expand and contract pattern for database migrations](https://www.prisma.io/dataguide/types/relational/expand-and-contract-pattern) | Data Guide | | Deploy | [Deployment guides for Prisma-based applications](https://www.prisma.io/docs/guides/deployment/deployment-guides) | Documentation | | Monitor | [Best practices for monitoring apps in production](https://www.prisma.io/blog/monitoring-best-practices-monitor5g08d0b) | Blog | | Operate | [Database troubleshooting](https://www.prisma.io/dataguide/managing-databases/database-troubleshooting) | Data Guide | </Accordion> </Accordions>We are especially humbled that many framework and library authors choose Prisma as the default ORMs for their tools. Here's a selection of higher-level frameworks that are using Prisma:
We are a VC-funded company with a team that's passionate about improving the lives of application developers. While we are starting our journey by building open-source tools, our longterm vision for Prisma is much bigger than building "just" an ORM.
During our recent Enterprise Event and a Prisma Meetup, we started sharing this vision which we call an Application Data Platform.
Prisma's vision is to democratize the custom data access layer used by companies like Facebook, Twitter and Airbnb and make it available to development teams and organizations of all sizes.
This idea is largely inspired by companies like Facebook, Twitter and Airbnb that built custom data access layers on top of their databases and other data sources to make it easier for the application developers to access the data they need in a safe and efficient manner.
Prisma's goal is to democratize the idea of this custom data access layer and make it available to development teams and organizations of any size.
We'd love to help you build your next project with Prisma! To learn more about our Enterprise offering and how Prisma fits into your stack and vision, contact us.
There are various ways to get started with Prisma:
Community has been incredibly important for us since we've started. From hosting Meetups and conferences to helping out users on Slack and GitHub Discussions we are always trying to be closely in touch with the developer community. Come join us!
<TweetEmbedComp tweets={['1349886964363882499', '1369304411177123843', '1352663140203442176', '1382806675875332103']}/>