November 28, 2024
Prisma 6: Better Performance, More Flexibility & Type-Safe SQL
Today, we are releasing Prisma v6! Since the last major version, we have been hard at work incorporating user feedback, making Prisma ORM faster and more flexible, and adding amazing features like type-safe raw SQL queries.
Prisma v6: What you need to know
We are excited to share that we've released another major version increment of Prisma ORM. We want to take this opportunity to recap everything that has happened since Prisma v5.
If you are using Prisma ORM and want to upgrade to v6, check out the upgrade guide.
Upgrade to v6
Performance: JOINs and more efficient queries
With our development of Prisma ORM, we have followed the "Make it work, make it right, make it fast" approach. Since the initial release in 2021, we have continuously invested in better query performance and are proud to share that we've been able to significantly improve query speed since last major release.
Pick the best JOIN strategy
In principle, there are two different approaches when you need to query data from multiple tables that are related via foreign keys:
- Database-level: Send a single query to the database using the SQL
JOIN
keyword and let the data be joined by the database directly. - Application-level: Send multiple queries to individual tables to the database and join the data yourself in your application.
Depending on your use case, your database schema, and several other factors, one strategy may be more appropriate than the other. Up until Prisma ORM v5.7.0, Prisma ORM would always use the application-level JOIN strategy.
However, with the v5.7.0 release, we now allow you to pick the best JOIN strategy for your use case, ensuring you can always get the best performance for your queries. To choose a join strategy, you can use the relationLoadStrategy
option on relation queries, e.g.:
If you want to learn more about how these two approaches work under the hood and when to prefer which JOIN strategy, check out this blog post: Prisma ORM Now Lets You Choose the Best Join Strategy (Preview).
Performance improvements in nested create-operations
With Prisma ORM, you can create multiple new records in nested queries, for example:
In versions before v5.11.0, Prisma ORM would translate this into multiple SQL INSERT
queries, each requiring its own round-trip to the database. As of this release, these nested create
queries are optimized and the INSERT
queries are sent to the database in bulk in a single roundtrip. These optimizations apply to one-to-many as well as many-to-many relations.
Faster queries in almost every release since v5
If you go through Prisma ORM releases on GitHub, you'll find that almost every release came with some kind of performance improvements.
We're committed to continue our investment in query performance to ensure your queries are as fast as possible 💚 By the way, if you're curious to learn how Prisma ORM compares to other TypeScript ORMs in terms of performance, check out our TypeScript ORM benchmarks.
Flexibility: Node.js drivers, Edge support, multiple schema files & more
Over the past year, we not only improved performance, but also made Prisma ORM a lot more flexible. Since Prisma v5, we have made it possible to use Prisma ORM in new environments (like Cloudflare Workers or even React Native apps via Expo), with new databases, and have implemented highly popular features like splitting your schema.prisma
into multiple files, being able to return created records from a bulk create query, or exclude specific fields from the result payload of a query.
Support for serverless drivers from PlanetScale and Neon
Both PlanetScale and Neon have released serverless drivers that enable accessing their database instances via HTTP (instead of TCP, which is commonly used for database connections). These serverless drivers are particularly useful in serverless and edge environments, where initiating a TCP connection may be too expensive or entirely impossible.
In v5.4.0, we released support for custom database drivers in Prisma ORM, enabling the use of both the PlanetScale and Neon serverless drivers. Here's an example for connecting to a PlanetScale instance using their serverless driver:
Prisma ORM: An edge-ready way to talk to your database
Edge functions, such as Cloudflare Workers or Vercel Edge Functions, have been rising in popularity. Thanks to their high geographic distribution and closeness to users, they enable lightning-fast response times.
Since v5.11.0, Prisma ORM can be used in these edge environments. This has been a major achievement and we're excited to see what developers are building with Prisma ORM at the edge!
You can learn more about this feature in this blog post: Prisma ORM Support for Edge Functions is Now in Preview.
New databases: D1 and Turso
Since v5, we not only enabled usage of Prisma ORM in new runtimes. We also added support for new databases, like Cloudflare D1 and Turso.
Both databases are based on SQLite and well-suited for building applications at the edge.
Split your Prisma schema into multiple files
In v5.15.0, we tackled a popular feature request and made it possible to have multiple files that make up your Prisma schema. For example, you can now have these two files that each contain a single model:
user.prisma
post.prisma
When you run a migration or another Prisma CLI command, the CLI will merge the individual files into a single file. Learn more about this feature in our announcement blog post: Organize Your Prisma Schema into Multiple Files in v5.15
Making the Prisma Client API more flexible
Besides all these improvements, we've also incorporated new capabilities in the Prisma Client API, making it even easier and more convenient to query data from the database.
As an example, the new createManyAndReturn()
query enables you to create multiple records in a single query while also returning all the new records:
In previous versions, the only way to create multiple records at once was by using createMany
which only returned the count of the created records.
Another example for more flexibility in the Prisma Client API is the new omit
option. It is the counterpart to select
and lets you exclude fields from the result payload of a query:
The query above will return all fields of the User
model, except for the password
field.
Typed SQL: Type-safe, raw SQL queries
Finally, one of the most exciting features we've delivered this year was a way to write type-safe raw SQL queries. With that addition, Prisma ORM now gives you the best of both worlds: A convenient high-level abstraction for the majority of queries and a flexible, type-safe escape hatch for raw SQL.
Consider this example of a raw SQL query you may need to write in your application:
After a generation step, you'll be able to use the conversionByVariant
query via the new $queryRawTyped
method in Prisma Client:
Learn more about this on our blog: Announcing TypedSQL: Make your raw SQL queries type-safe with Prisma ORM
One more thing: Prisma Postgres — The best database to use with Prisma ORM
While we've made great progress with Prisma ORM, our highlight has been the recent launch of Prisma Postgres — the best database to use with Prisma ORM!
Prisma Postgres is a managed PostgreSQL service that gives developers an always-on database with pay-as-you-go pricing for storage and queries (no fixed cost, no cost for compute). It's like a serverless database — but without cold starts and a generous free tier!
To build a service with these capabilities, we've designed a unique architecture using bare metal machines, a revolutionary millisecond cloud stack, and unikernels (think: "hyper-specialized operating systems") running as ultra-lightweight microVMs.
Thanks to the first-class integration of Prisma products, Prisma Postgres comes with connection pooling, caching, real-time subscriptions, and query optimization recommendations out-of-the-box.
Try Prisma Postgres
Thank you for your support 💚
It is only thanks to you—our amazing community—that we have been able to create the most popular ORM in the TypeScript ecosystem.
Thank you all so much for the ongoing support, your feedback and input that helps us make Prisma better every day.
If you have anything to share, be it a help request, an excited compliment, or constructive feedback, you can always reach us on our Discord and on X.
✨ We're always trying to improve! If you've recently used Prisma ORM, we'd appreciate hearing your thoughts about your experience via this 2min survey.
Don’t miss the next post!
Sign up for the Prisma Newsletter