Skip to main content

Prisma 快速入门 Postgres

在本快速入门指南中,你将学习如何在普通的 TypeScript 项目中从头开始使用 Prisma ORM 和 Prisma Postgres 数据库。它涵盖以下工作流程:

¥In this Quickstart guide, you'll learn how to get started from scratch with Prisma ORM and a Prisma Postgres database in a plain TypeScript project. It covers the following workflows:

注意

如果你想将 Prisma Postgres 与其他 ORM 或数据库库(例如 Drizzle ORM、TypeORM 或 Kysely)一起使用,你可以按照 此处 的说明进行操作。

¥If you want to use Prisma Postgres with another ORM or database library (like Drizzle ORM, TypeORM or Kysely), you can follow the instructions here.

先决条件

¥Prerequisites

要成功完成本教程,你需要:

¥To successfully complete this tutorial, you need:

1. 在平台控制台中设置 Prisma Postgres 数据库

¥ Set up a Prisma Postgres database in the Platform Console

按照以下步骤创建你的 Prisma Postgres 数据库:

¥Follow these steps to create your Prisma Postgres database:

  1. 登录 并打开控制台。

    ¥Log in to and open the Console.

  2. 在你选择的 workspace 中,单击新建项目按钮。

    ¥In a workspace of your choice, click the New project button.

  3. 在名称字段中为你的项目输入一个名称,例如 hello-ppg。

    ¥Type a name for your project in the Name field, e.g. hello-ppg.

  4. 在 Prisma Postgres 部分中,单击“开始”按钮。

    ¥In the Prisma Postgres section, click the Get started button.

  5. 在“区域”下拉菜单中,选择最接近你当前位置的区域,例如美国东部(弗吉尼亚北部)。

    ¥In the Region dropdown, select the region that's closest to your current location, e.g. US East (N. Virginia).

  6. 单击创建项目按钮。

    ¥Click the Create project button.

此时,你将被重定向到数据库页面,你需要等待几秒钟,同时数据库的状态将从 PROVISIONING 更改为 CONNECTED

¥At this point, you'll be redirected to the Database page where you will need to wait for a few seconds while the status of your database changes from PROVISIONING to CONNECTED.

出现绿色 CONNECTED 标签后,你的数据库即可使用!

¥Once the green CONNECTED label appears, your database is ready to use!

2. 下载示例并安装依赖

¥ Download example and install dependencies

复制控制台中显示的 try-prisma 命令,将其粘贴到你的终端并执行它。

¥Copy the try-prisma command that's shown in the Console, paste it into your terminal and execute it.

作为参考,命令如下所示:

¥For reference, this is what the command looks like:

npx try-prisma@latest \
--template databases/prisma-postgres \
--name hello-prisma \
--install npm

try-prisma 命令终止后,导航到项目目录:

¥Once the try-prisma command has terminated, navigate into the project directory:

cd hello-prisma

3. 设置数据库连接 URL

¥ Set database connection URL

与数据库的连接是通过 .env 文件中的环境变量配置的。

¥The connection to your database is configured via an environment variable in a .env file.

首先,将现有的 .env.example 文件重命名为 .env

¥First, rename the existing .env.example file to just .env:

mv .env.example .env

然后,在平台控制台的项目环境中,在“设置数据库访问”部分中找到你的数据库凭据,复制 DATABASE_URL 环境变量并将其粘贴到 .env 文件中。

¥Then, in your project environment in the Platform console, find your database credentials in the Set up database access section, copy the DATABASE_URL environment variable and paste them into the .env file.

作为参考,该文件现在应类似于以下内容:

¥For reference, the file should now look similar to this:

DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=ey...."

4. 创建数据库表(使用架构迁移)

¥ Create database tables (with a schema migration)

接下来,你需要在数据库中创建表。你可以通过使用 Prisma CLI 的以下命令创建并执行架构迁移来执行此操作:

¥Next, you need to create the tables in your database. You can do this by creating and executing a schema migration with the following command of the Prisma CLI:

npx prisma migrate dev --name init

这会将 Prisma 架构 中定义的 UserPost 模型映射到你的数据库。你还可以查看执行的 SQL 迁移并在新建的 prisma/migrations 目录中创建表。

¥This will map the User and Post models that are defined in your Prisma schema to your database. You can also review the SQL migration that was executed and created the tables in the newly created prisma/migrations directory.

5. 使用 Prisma ORM 执行查询

¥ Execute queries with Prisma ORM

src/queries.ts 脚本包含许多 CRUD 查询,这些查询将在你的数据库中写入和读取数据。你可以通过在终端中运行以下命令来执行它:

¥The src/queries.ts script contains a number of CRUD queries that will write and read data in your database. You can execute it by running the following command in your terminal:

npm run queries

脚本完成后,你可以检查终端中的日志或使用 Prisma Studio 探索数据库中已创建的记录:

¥Once the script has completed, you can inspect the logs in your terminal or use Prisma Studio to explore what records have been created in the database:

npx prisma studio

6. 使用 Prisma Accelerate 探索缓存

¥ Explore caching with Prisma Accelerate

src/caching.ts 脚本包含一个示例查询,该查询使用 Stale-While-Revalidate (SWR) 和 生存时间 (TTL) 使用 Prisma Accelerate 缓存数据库查询。你可以按如下方式执行它:

¥The src/caching.ts script contains a sample query that uses Stale-While-Revalidate (SWR) and Time-To-Live (TTL) to cache a database query using Prisma Accelerate. You can execute it as follows:

npm run caching

记下执行查询所花费的时间,例如:

¥Take note of the time that it took to execute the query, e.g.:

The query took 2009.2467149999998ms.

现在,再次运行脚本:

¥Now, run the script again:

npm run caching

你会注意到,这次查询所花的时间会短很多,例如:

¥You'll notice that the time the query took will be a lot shorter this time, e.g.:

The query took 300.5655280000001ms.

7. 下一步

¥ Next steps

在本快速入门指南中,你学习了如何在普通 TypeScript 项目中开始使用 Prisma ORM。你可以自行更多地探索 Prisma Client API,例如 通过在 findMany 查询中包含过滤、排序和分页选项或探索更多操作(如 updatedelete 查询)。

¥In this Quickstart guide, you have learned how to get started with Prisma ORM in a plain TypeScript project. Feel free to explore the Prisma Client API a bit more on your own, e.g. by including filtering, sorting, and pagination options in the findMany query or exploring more operations like update and delete queries.

探索 Prisma Studio 中的数据

¥Explore the data in Prisma Studio

Prisma ORM 附带一个内置 GUI,用于查看和编辑数据库中的数据。你可以使用以下命令打开它:

¥Prisma ORM comes with a built-in GUI to view and edit the data in your database. You can open it using the following command:

npx prisma studio

使用 Prisma Postgres,你还可以通过选择项目中的 Studio 选项卡直接在 中使用 Prisma Studio。

¥With Prisma Postgres, you can also directly use Prisma Studio inside the by selecting the Studio tab in your project.

使用 Next.js 构建全栈应用

¥Build a fullstack app with Next.js

了解如何在全栈应用中使用 Prisma Postgres:

¥Learn how to use Prisma Postgres in a fullstack app:

探索可立即运行的 Prisma ORM 示例

¥Explore ready-to-run Prisma ORM examples

查看 GitHub 上的 prisma-examples 存储库,了解如何将 Prisma ORM 与你喜欢的库一起使用。该存储库包含 Express、NestJS、GraphQL 的示例以及 Next.js 和 Vue.js 的全栈示例等等。

¥Check out the prisma-examples repository on GitHub to see how Prisma ORM can be used with your favorite library. The repo contains examples with Express, NestJS, GraphQL as well as fullstack examples with Next.js and Vue.js, and a lot more.

这些示例默认使用 SQLite,但你可以按照项目 README 中的说明,通过几个简单的步骤切换到 Prisma Postgres。

¥These examples use SQLite by default but you can follow the instructions in the project README to switch to Prisma Postgres in a few simple steps.


Stay connected with Prisma

Continue your Prisma journey by connecting with our active community. Stay informed, get involved, and collaborate with other developers:

We genuinely value your involvement and look forward to having you as part of our community!