Skip to main content

无 Rust 引擎

v6.16.0 起,在 PostgreSQL、CockroachDB、Neon、MySQL、PlanetScale、SQLite、D1 和 MS SQL Server 数据库上使用不带 Rust 引擎 二进制文件的 Prisma ORM 已达到 一般可用 级别。

¥As of v6.16.0, usage of Prisma ORM without Rust engine binaries on PostgreSQL, CockroachDB, Neon, MySQL, PlanetScale, SQLite, D1 & MS SQL Server databases has been Generally Available.

本页概述了如何使用此版本的 Prisma ORM。

¥This page gives an overview of how to use this version of Prisma ORM.

不带 Rust 引擎的 Prisma ORM

¥Prisma ORM without Rust engines

如果你使用不带 Rust 引擎的 Prisma ORM,主要技术差异如下:

¥The main technical differences if you're using Prisma ORM without a Rust engine are:

  • generator 块中没有 binaryTargets 字段

    ¥no binaryTargets field on the generator block

  • 没有使用生成的 Prisma 客户端下载到目录中的查询引擎二进制文件

    ¥no query engine binary that's downloaded into the directory with your generated Prisma Client

  • 需要在 generator 块中将 engineType 设置为 "client"

    ¥engineType needs to be set to "client" on the generator block

  • 数据库连接管理需要使用 驱动适配器

    ¥required usage of driver adapters for database connection management

警告

Prisma ORM 的无 Rust 版本已使用 prisma-client 生成器(见下文)进行了全面测试,而非使用 prisma-client-js。请自行决定是否使用旧的生成器。

¥The Rust-free version of Prisma ORM has been thoroughly tested with the prisma-client generator (see below), not with prisma-client-js. Use the old generator at your discretion.

用法

¥Usage

先决条件

¥Prerequisites

  • Prisma ORM v6.15.0(或更高版本)

    ¥Prisma ORM v6.15.0 (or later)

1. 在 generator 块上设置 engineType

¥ Set engineType on the generator block

schema.prisma
generator client {
provider = "prisma-client-js" // or `prisma-client`
output = "../generated/prisma"
engineType = "client" // enable Prisma ORM without Rust
}

2. 重新生成 Prisma 客户端

¥ Re-generate Prisma Client

要使配置生效,你需要重新生成 Prisma 客户端:

¥To make the configuration take effect, you need re-generate Prisma Client:

npx prisma generate

3. 安装驱动程序适配器

¥ Install the driver adapter

根据你使用的数据库,你需要安装不同的驱动程序适配器库:

¥Depending on the database you use, you need to install a different driver adapter library:

npm install @prisma/adapter-pg

4. 实例化 Prisma 客户端

¥ Instantiate Prisma Client

最后,实例化 Prisma 客户端,你可以使用驱动程序适配器和你正在使用的数据库实例的连接 URL 来执行此操作:

¥Finally, instantiate Prisma Client which you can do using the driver adapter and the connection URL for the database instance you're using:

import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from './generated/prisma'

const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
const prisma = new PrismaClient({ adapter })

5. 查询数据库

¥ Query your database

如果你已完成前面的步骤,则可以像使用 Prisma Client 一样查询数据库。无需进行其他更改。

¥If you went through the previous steps, you can query your database as you're used to with Prisma Client. No other changes are needed.

与旧版本的使用(预览版)

¥Usage with older versions (Preview)

Prisma ORM 的无 Rust 版本已从 v6.7.0 到 v6.14.0 版本处于预览阶段。如果你正在使用这些版本中的任何一个并且无法升级到最新版本,请在下方展开。

¥The Rust-free version of Prisma ORM has been in Preview from versions v6.7.0 to v.6.14.0. Expand below if you're using any of these versions and are unable to upgrade to the latest one.

Expand to see instructions for Prisma ORM v6.7.0 to v6.14.0

先决条件

¥Prerequisites

  • Prisma ORM 6.7.0 到 6.14.0 之间的任何版本

    ¥Any Prisma ORM version between 6.7.0 and 6.14.0

1. 设置功能标志

¥ Set feature flags

使用新架构需要设置 driverAdaptersqueryCompiler 功能标志:

¥Usage of the new architecture requires the driverAdapters and queryCompiler feature flags to be set:

schema.prisma
generator client {
provider = "prisma-client-js" // or `prisma-client`
previewFeatures = ["queryCompiler", "driverAdapters"]
output = "../generated/prisma"
}

2. 重新生成 Prisma 客户端

¥ Re-generate Prisma Client

要使功能标志生效,你需要重新生成 Prisma 客户端:

¥To make the feature flags take effect, you need re-generate Prisma Client:

npx prisma generate

3. 安装驱动程序适配器

¥ Install the driver adapter

根据你使用的数据库,你需要安装不同的驱动程序适配器库:

¥Depending on the database you use, you need to install a different driver adapter library:

npm install @prisma/adapter-pg

4. 实例化 Prisma 客户端

¥ Instantiate Prisma Client

最后,你需要实例化 Prisma 客户端,你可以使用驱动程序适配器和你正在使用的数据库实例的连接 URL 来完成此操作。

¥Finally, you need to instantiate Prisma Client which you can do using the driver adapter and the connection URL for the database instance you're using.

import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from './generated/prisma'

const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
const prisma = new PrismaClient({ adapter })

5. 查询数据库

¥ Query your database

如果你已完成前面的步骤,则可以像使用 Prisma Client 一样查询数据库。无需进行其他更改。

¥If you went through the previous steps, you can query your database as you're used to with Prisma Client. No other changes are needed.