内容简介:Try the new Prisma Client in 5 minutes by following theThe new version of Prisma Client is a modern database access libary for Node.js and TypeScript. It can be used as an alternative to traditional ORMs and SQL query builders to read and write data in you
Contents
- Modern database access with Prisma Client 2.0
- What's in this release?
- Renaming the
prisma2
repository toprisma
- Renaming the
prisma2
CLI - I currently use Prisma 1, what should I do?
- Try out Prisma 2.0 and share your feedback
TLDR
- The Prisma 2.0 Beta is ready. With the new website and documentation, it's now the default for new developers getting started with Prisma.
- Prisma 2.0 mainly consists of Prisma Client, an auto-generated and type-safe query builder for Node.js and TypeScript. Prisma Migrate is considered experimental .
- The
prisma/prisma2
repo was renamed toprisma/prisma
(and the former Prisma 1 repoprisma/prisma
repo is now calledprisma/prisma1
).
Try the new Prisma Client in 5 minutes by following the Quickstart in the new docs.
Modern database access with Prisma Client 2.0
The new version of Prisma Client is a modern database access libary for Node.js and TypeScript. It can be used as an alternative to traditional ORMs and SQL query builders to read and write data in your database.
To setup, you need aPrisma schema file and must add Prisma Client as a dependency to your project:
npm install @prisma/client
Prisma Client can be used in any Node.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.
Be more productive with your database
The main goal of Prisma Client is to increase the productivity of application developers when working with databases. It achieves this by providing a clean data access API that returns plain JavaScript objects.
This approach enables simpler reasoning about database queries and increases the confidence with predictibable (and type-safe) query results. Here are a couple of the major benefits Prisma Client provides:
- Auto-completion in code editors instead of needing to look up documentation
- Thinking in objects instead of mapping relational data
- Type-safe database queries that can be validated at compile time
- Single source of truth for database and application models
- Healthy constraints that prevent common pitfalls and antipatterns
- An abstraction that make the right thing easy ("pit of success")
- Queries not classes to avoid complex model objects
- Less boilerplate so developers can focus on the important parts of their app
Learn more about how Prisma makes developers productive in theIntroduction or get a taste of the Prisma Client API by checking out the code examples on thewebsite.
A "smart" node module
The @prisma/client
module is different from "conventional" node modules. With conventional node modules (e.g. lodash
), the entire package is downloaded into your node_modules
directory and only gets updated when you re-install the package.
The @prisma/client
node module is different. It is a"facade package" (basically a stub ) that doesn't contain any functional code.
While you do need to install it once with npm install @prisma/client
, it is likely that the code inside the node_modules/@prisma/client
directory changes more often as you're evolving your application. That's because whenever you make changes to the Prisma schema, you need to re-generate Prisma Client which updates the code in the @prisma/client
node module.
Because the node_modules/@prisma/client
directory contains some code that is tailored to your project, it is sometimes called a "smart node module":
Auto-completion and type-safety benefits even in plain JavaScript
Auto-completion is an extremely powerful tool for developers. It allows them to explore an API directly in their editor instead of referring to documentation. Prisma Client brings auto-completion to your database queries!
Thanks to Prisma Client's generated types which are included in the index.d.ts
of the @prisma/client
module, this feature is available not only to TypeScript developers, but also when developing an application in plain JavaScript.
Type-safety for partial database queries
A major benefit of Prisma Client compared to other ORMs and database tools is that it provides full type safety - even for "partial" database queris (i.e. when you query only the subset of a model's field or include arelation).
As an example, consider this Prisma Client query (you can swith the tab to view the correspondingPrisma models):
const usersWithPartialPosts = await prisma.user.findMany({ include: { posts: { select: { title: true, published: true } } } })
Note that the resulting usersWithPartialPosts
will be statically typed to:
export type User = { id: number email: string name: string | null } const usersWithPartialPosts: (User & { posts: { title: string; published: boolean; }[]; })[]
This means that TypeScript will catch any errors when you make a typo or accidentally access a property that was not requested from the database!
Getting started
The best way to get started with Prisma Client is by following the Quickstart in the docs:
Quickstart (5 min)Alternatively you can:
What's in this release?
The Prisma 2.0 Beta comes with the following tools:
- Prisma Client : Auto-generated, type-safe query builder for Node.js & TypeScript
- Prisma Migrate ( experimental ): Declarative schema migration tool
- Prisma Studio ( experimental ): GUI to view and edit data in your database
Try the new Prisma Client in 5 minutes by following the Quickstart in the new docs.
Note: Learn more about the Beta release in the release notes .
Renaming the prisma2
repository to prisma
Since its initial release, the main repository for Prisma 2.0 has been called prisma2
.
Because Prisma 2.0 is now the default for developers getting started with Prisma, the Prisma repositories have been renamed as follows:
- The
prisma/prisma2
repository has been renamed toprisma/prisma
- The
prisma/prisma
repository has been renamed toprisma/prisma1
Renaming the prisma2
CLI
During the Preview period, the CLI for Prisma 2.0 was invoked using the prisma2
command. With Prisma 2.0 being the default for new developers getting started with Prisma, the command is changed to just prisma
. The exising prisma
command of Prisma 1 is renamed to prisma1
.
Also note that the installation of the npm packages changes:
Prisma version | Old CLI command | New CLI command | Old npm package name | New npm package name |
---|---|---|---|---|
2.0 | prisma2 |
prisma |
prisma2 |
@prisma/cli |
1.X | prisma |
prisma1 |
prisma |
prisma1 |
A note for current Prisma 1 users
If you're currently using Prisma 1 with the prisma
command, you can keep using it as before. If you want to upgrade to Prisma 2.0 , it is recommended to uninstall your current prisma
installation and install the new CLI versions locally in the projects where they are needed:
# Remove global installation npm uninstall -g prisma # Navigate into Prisma 1 project directory & install locally cd path/to/prisma-1-project npm install prisma1 --save-dev # Invoke `prisma1` by prefixing it with `npx` npx prisma1
The prisma2
npm package is deprecated
The prisma2
npm package is now deprecated. To prevent confusions during installation, it now outputs the following when you try to install it:
┌─────────────────────────────────────────────────────────────┐ │ │ │ The package prisma2 has been renamed to @prisma/cli. │ │ │ │ Please uninstall prisma2 from your project or globally. │ │ Then install @prisma/cli to continue using Prisma 2.0: │ │ │ │ # Uninstall old CLI │ │ npm uninstall prisma2 │ │ │ │ # Install new CLI │ │ npm install @prisma/cli --save-dev │ │ │ │ # Invoke via npx │ │ npx prisma2 --help │ │ │ │ Learn more here: https://pris.ly/preview025 │ │ │ └─────────────────────────────────────────────────────────────┘
I currently use Prisma 1, what should I do?
First of all, we want to hugely thank all existing Prisma 1 users! :pray: We are deeply grateful for a supportive and active community that has formed on GitHub and onSlack!
How does Prisma 2.0 compare to Prisma 1?
Prisma 2.0 comes with a number of changes compared to Prisma 1. Here's a high-level overview of the main differences :
- Prisma 2.0 doesn't require hosting a database proxy server (i.e. thePrisma server).
- Prisma 2.0 doesn't expose "a GraphQL API for your database" any more, but only allows for programmatic access via the Prisma Client API.
- Prisma 2.0 makes the features of Prisma 1 more modular and splits them into dedicated tools:
prisma deploy
- More powerful introspection allows to connect Prisma 2.0 to any existing database.
- Prisma 1 datamodel and
prisma.yml
have been merged into thePrisma schema. - Prisma 2.0 uses its own modeling language instead of being based on GraphQL SDL.
- You can build GraphQL servers with Prisma using Nexus or any otherGraphQL library of your choice.
How can I access the Prisma 1 docs?
You can keep accessing specific versions of the Prisma 1 docs by appending the version number to https://www.prisma.io/docs
. For example, to view the docs for Prisma version 1.34, you can go to https://www.prisma.io/docs/1.34
.
The Prisma 1 examples have been moved to the prisma1-examples
repository.
Should I upgrade?
Whether or not you should upgrade depends on the context of your project. In general, one major consideration is the fact that Prisma Migrate is still experimental, this means you probably need to make any future adjustments to your database schema using SQL or another migration tool.
Also note that we will put together an upgrade guide as well as dedicated tooling for the upgrade process over the next weeks. So, if you do want to upgrade despite Prisma Migrate not being ready, it might be worth waiting until these resources are in place.
I use Prisma 1 with Prisma client and nexus-prisma
, should I upgrade?
It is certainly possible to upgrade a project that's running on Prisma 1 and nexus-prisma
to Prisma 2.0.
If you decide to upgrade, be aware that changing your database schema after having upgraded to Prisma 2.0, will require running migrations with SQL or a third-party migration tool. Also note that the nexus-prisma
API changes with Prisma 2.0.
Here is a high-level overview of the steps needed to upgrade:
npm install @prisma/cli --save-dev datasource npx prisma introspect npm install @prisma/client npx prisma generate nexus-prisma
Note: You can find your database credentials in the Docker Compose file that you used to deploy the Prisma server. These credentials are needed to compose theconnection URL for Prisma 2.0.
I use Prisma 1 with Prisma client (without nexus-prisma
), should I upgrade?
It is certainly possible to upgrade a project that's running on Prisma 1.
If you decide to upgrade, be aware that changing your database schema after having upgraded to Prisma 2.0, will require running migrations with SQL or a third-party migration tool. Here is a high-level overview of the steps needed to upgrade:
npm install @prisma/cli --save-dev datasource npx prisma introspect npm install @prisma/client npx prisma generate
Note: You can find your database credentials in the Docker Compose file that you used to deploy the Prisma server. These credentials are needed to compose theconnection URL for Prisma 2.0.
I use Prisma 1 with prisma-binding
, should I upgrade?
It is certainly possible to upgrade a project that's running on Prisma 1 and prisma-binding
to Prisma 2.0.
If you decide to upgrade, be aware that changing your database schema after having upgraded to Prisma 2.0, will require running migrations with SQL or a third-party migration tool.
Also note that the way how your GraphQL resolvers are implemented changes with Prisma 2.0. As Prisma 2.0 doesn't expose a GraphQL API for your database, you can't use the prisma-binding
npm package any more. This is mostly relevant for implementing relations , resolvers for these now need to be implemented on a type level . Here's quickguide for implementing relations in GraphQL with Prisma client. To learn more about why this is necessary, be sure to read this article about the basics ofGraphQL schemas.
Here is a high-level overview of the steps needed to upgrade:
npm install @prisma/cli --save-dev datasource npx prisma introspect npm install @prisma/client npx prisma generate prisma-binding
If you want to switch to acode-first approach, check out GraphQL Nexus .
Note: You can find your database credentials in the Docker Compose file that you used to deploy the Prisma server. These credentials are needed to compose theconnection URL for Prisma 2.0.
Try out Prisma 2.0 and share your feedback
We are really excited to finally share the Beta version of Prisma 2.0 and can't wait to see what you all will build with it.
Get started with Prisma 2.0 If you want to leave feedback, share ideas, create feature requests or submit bug reports please do so in the (renamed) prisma
repository on GitHub and join the (renamed) #prisma2-beta
channel on thePrisma Slack!
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。