Skip to main content

Prisma Accelerate 的本地开发

Prisma Accelerate 通过集成连接池和全局数据库缓存高效地扩展生产流量。

¥Prisma Accelerate efficiently scales production traffic with integrated connection pooling and a global database cache.

在开发环境中,你可能希望使用本地数据库来最大限度地降低成本。此外,你可以考虑使用 Accelerate 客户端扩展程序一次性扩展 Prisma 客户端,以便你可以在开发中使用本地数据库,并在启用 Accelerate 连接池和缓存的情况下使用托管数据库。这消除了在开发和生产之间切换客户端的条件逻辑。

¥In development environments, you may want to use a local database to minimize expenses. Furthermore, you may consider extending Prisma Client with the Accelerate client extension once so that you can use a local database in development and a hosted database with Accelerate’s connection pooling and caching enabled. This eliminates the need for conditional logic to switch clients between development and production.

本指南将讲解如何在具有本地数据库的开发环境中使用 Prisma Accelerate 客户端扩展。

¥This guide will explain how to use Prisma Accelerate client extension in a development environment with a local database.

在开发和生产环境中使用 Prisma Accelerate 客户端扩展

¥Using Prisma Accelerate client extension in development and production


Using Prisma Accelerate client extension in development

Accelerate 不支持本地数据库。但是,在开发环境中,你仍然可以将 Prisma 客户端与 Accelerate 客户端扩展一起使用。此设置不提供 Accelerate 的连接池和缓存功能。

¥Accelerate does not work with a local database. However, in a development environment, you can still use Prisma Client with the Accelerate client extension. This setup will not provide Accelerate's connection pooling and caching features.

以下步骤概述了如何在本地 PostgreSQL 数据库中使用 Prisma ORM 和 Prisma Accelerate。

¥The following steps outline how to use Prisma ORM and Prisma Accelerate with a local PostgreSQL database.

  1. 使用本地数据库的连接字符串更新 DATABASE_URL 环境变量:

    ¥Update the DATABASE_URL environment variable with your local database's connection string:

    DATABASE_URL="postgres://username:password@127.0.0.1:5432/localdb"
  2. 生成 Prisma 客户端:

    ¥Generate a Prisma Client:

    npx prisma generate

    注意:--no-engine 标志仅应在预览和生产环境中使用。该命令生成不包含 查询引擎 文件的 Prisma 客户端构件,这需要 Accelerate 连接字符串。

    ¥Note: The --no-engine flag should only be used in preview and production environments. The command generates Prisma Client artifacts without a Query Engine file, which requires an Accelerate connection string.

  3. 使用 Accelerate 客户端扩展设置 Prisma 客户端:

    ¥Set up Prisma Client with the Accelerate client extension:

    import { PrismaClient } from '@prisma/client'
    import { withAccelerate } from '@prisma/extension-accelerate'

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

    Prisma Client 的扩展实例将使用本地数据库。因此,Prisma Accelerate 将不会在你的开发环境中用于响应你的 Prisma 客户端查询。

    ¥The extended instance of Prisma Client will use the local database. Hence, Prisma Accelerate will not be used in your development environment to respond to your Prisma Client queries.

Using Prisma Accelerate client extension in production

如果使用 Accelerate 连接字符串作为 DATABASE_URL 环境变量,Prisma 客户端将通过 Accelerate 路由你的查询。

¥If an Accelerate connection string is used as the DATABASE_URL environment variable, Prisma Client will route your queries through Accelerate.

在边缘函数中本地使用 Prisma Accelerate

¥Using Prisma Accelerate locally in an edge function

在开发环境中使用边缘函数(例如 Vercel 的边缘运行时)时,请按如下方式更新 Prisma 客户端导入:

¥When using an edge function, e.g., Vercel's edge runtime, for your development environment, update your Prisma Client import as follows:

import { PrismaClient } from '@prisma/client/edge'

通常,边缘函数环境缺乏对现有基于 TCP 的数据库连接 API 的原生支持。Prisma Accelerate 提供的连接字符串允许通过 HTTP 查询数据库,HTTP 是所有边缘运行时都支持的协议。

¥Generally, edge function environments lack native support for existing APIs enabling TCP-based database connections. Prisma Accelerate provides a connection string that allows querying your database over HTTP, a protocol supported in all edge runtimes.