Skip to main content

设置

先决条件

¥Prerequisites

在开始使用 Prisma Optimize for Prisma Postgres 之前,请确保你已具备以下条件:

¥Before you begin with Prisma Optimize for Prisma Postgres, ensure you have the following:

注意

Prisma Optimize 旨在用于本地环境。在 常见问题 中了解更多信息。

¥Prisma Optimize is intended for use in local environments. Learn more in the FAQ.

1. 启动 Optimize

¥ Launch Optimize

  1. 登录你的

    ¥Log in to your .

  2. 点击左侧导航栏中的“优化”选项卡。

    ¥Click the Optimize tab on the left navigation.

  3. 点击“生成 API 密钥”按钮。

    ¥Click the Generate API key button.

  4. 复制显示的 API 密钥并将其粘贴到安全的地方,例如密码管理器。

    ¥Copy the API key that appears and paste it somewhere safe, like a password manager.

  5. 点击复制图标继续完成每个设置屏幕,直到看到“完成并优化”按钮。点击该按钮完成设置。

    ¥Click the copy icons to continue through each setup screen until you see the Finish & optimize button. Click that to complete the setup.

  6. 完成后,Optimize 将在后台自动开始新的录制会话。

    ¥Once you're done, Optimize will automatically begin a new recording session in the background.

2. 将 Optimize 添加到你的应用

¥ Add Optimize to your application

2.1.安装所需的 Prisma 客户端扩展

¥2.1. Install the required Prisma Client extension

在终端中运行以下命令安装必要的依赖:

¥Run the following command in your terminal to install the necessary dependencies:

npm install @prisma/extension-optimize
Enabling tracing in older versions of Prisma ORM

对于 4.2.06.1.0 之间的 Prisma ORM 版本,你需要在 Prisma 模式文件中启用 tracing 预览功能。

¥For versions of Prisma ORM between 4.2.0 and 6.1.0, you need to enable the tracing preview feature in your Prisma schema file.

generator client {
provider = "prisma-client-js"
previewFeatures = ["tracing"]
}

2.2.将 Optimize API 密钥添加到你的 .env 文件

¥2.2. Add the Optimize API Key to your .env file

复制 Prisma Optimize API 密钥并将其添加到你的 .env 文件中:

¥Copy the Prisma Optimize API key and add it to your .env file:

OPTIMIZE_API_KEY="YOUR_OPTIMIZE_API_KEY"

2.3.扩展你的 Prisma 客户端实例

¥2.3. Extend your Prisma Client instance

使用 Optimize 扩展扩展你现有的 Prisma 客户端实例:

¥Extend your existing Prisma Client instance with the Optimize extension:

import { PrismaClient } from "@prisma/client";
import { withAccelerate } from "@prisma/extension-optimize";
import { withOptimize } from "@prisma/extension-optimize";

const prisma = new PrismaClient().$extends(
withOptimize({ apiKey: process.env.OPTIMIZE_API_KEY }),
).$extends(withAccelerate());

将 Optimize 扩展与其他扩展或中间件一起使用

¥Using the Optimize extension with other extensions or middleware

扩展程序逐个应用 开始,请确保按正确的顺序应用它们。扩展无法共享行为,最后应用的扩展具有优先权。

¥Since extensions are applied one after another, make sure you apply them in the correct order. Extensions cannot share behavior and the last extension applied takes precedence.

const prisma = new PrismaClient().$extends(withOptimize()).$extends(withAccelerate())

如果你在应用中使用 Prisma 中间件,请确保在任何 Prisma 客户端扩展(例如 Optimize)之前添加它们。例如:

¥If you are using Prisma Middleware in your application, make sure they are added before any Prisma Client extensions (like Optimize). For example:

const prisma = new PrismaClient().$use(middleware).$extends(withOptimize()).$extends(withAccelerate())

2.5.使用 Prisma Optimize 生成洞察

¥2.5. Use Prisma Optimize to generate insights

按照以下步骤开始使用 Prisma Optimize 生成查询洞察:

¥Follow these steps to start generating query insights with Prisma Optimize:

  1. 运行你的应用并在录制期间执行一些 Prisma 查询。

    ¥Run your app and execute some Prisma queries while recording is active.

  2. 在你的应用运行并根据执行的 Prisma 查询生成洞察后,点击红色的“录制”按钮。

    ¥After your app runs and generates insights based on the executed Prisma queries, click the red Recording button.

  3. 点击 单个查询详情 进行探索,并查看“建议”选项卡,了解任何建议的改进措施,以提高查询性能。

    ¥Explore individual query details by clicking on them, and check the Recommendations tab for any suggested improvements to enhance query performance.

    info

    Prisma AI to understand recommendations and apply them within your Prisma model context. :::使用

需要帮助吗?

¥Need help?

如果你需要帮助,请联系我们 Discord 上的 #help-and-questions 通道,或联系 我们的社区 了解其他人如何使用 Optimize。

¥If you need assistance, reach out in the #help-and-questions channel on our Discord, or connect with our community to see how others are using Optimize.