Skip to main content

Prisma 客户端简介

Prisma Client 是一个自动生成且类型安全的查询构建器,专为你的数据量身定制。开始使用 Prisma 客户端的最简单方法是遵循 快速开始

¥Prisma Client is an auto-generated and type-safe query builder that's tailored to your data. The easiest way to get started with Prisma Client is by following the Quickstart.

快速入门(5 分钟)

¥Quickstart (5 min)

设置说明 below 提供了设置 Prisma 客户端所需步骤的高级概述。如果你想开始将 Prisma Client 与你自己的数据库一起使用,请遵循以下指南之一:

¥The setup instructions below provide a high-level overview of the steps needed to set up Prisma Client. If you want to get started using Prisma Client with your own database, follow one of these guides:

从头开始建立一个新项目

¥Set up a new project from scratch



将 Prisma 添加到现有项目

¥Add Prisma to an existing project

设置

¥Set up

1. 先决条件

¥ Prerequisites

为了设置 Prisma 客户端,你需要一个带有数据库连接的 Prisma 架构文件、Prisma 客户端生成器和至少一个模型:

¥In order to set up Prisma Client, you need a Prisma schema file with your database connection, the Prisma Client generator, and at least one model:

schema.prisma
datasource db {
url = env("DATABASE_URL")
provider = "postgresql"
}

generator client {
provider = "prisma-client-js"
output = "../src/generated/prisma"
}

model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
}

还要确保 安装 Prisma CLI

¥Also make sure to install the Prisma CLI:

npm install prisma --save-dev
npx prisma

2. 安装

¥ Installation

使用以下命令在你的项目中安装 Prisma 客户端:

¥Install Prisma Client in your project with the following command:

npm install @prisma/client

3. 导入 Prisma 客户端

¥ Importing Prisma Client

根据你的用例,有多种方法可以将 Prisma 客户端导入到你的项目中:

¥There are multiple ways to import Prisma Client in your project depending on your use case:

import { PrismaClient } from './generated/prisma'

const prisma = new PrismaClient()
// use `prisma` in your application to read and write data in your DB

对于边缘环境,你可以按如下方式导入 Prisma 客户端:

¥For edge environments, you can import Prisma Client as follows:

import { PrismaClient } from './generated/prisma/edge'

const prisma = new PrismaClient()
// use `prisma` in your application to read and write data in your DB

注意:如果你使用 驱动适配器,则可以直接从生成器的 output 路径中指定的位置导入,例如 ./src/generated/prisma。无需从 ./src/generated/prisma/edge 导入。

¥Note: If you're using driver adapters, you can import from the location spefified in your generator's output path directly, e.g. ./src/generated/prisma. No need to import from ./src/generated/prisma/edge.

4. 使用 Prisma 客户端将查询发送到你的数据库

¥ Use Prisma Client to send queries to your database

实例化 PrismaClient 后,你可以开始在代码中发送查询:

¥Once you have instantiated PrismaClient, you can start sending queries in your code:

// run inside `async` function
const newUser = await prisma.user.create({
data: {
name: 'Alice',
email: 'alice@prisma.io',
},
})

const users = await prisma.user.findMany()
info

所有 Prisma 客户端方法都会返回 PrismaPromise 的实例,该实例仅在你调用 await.then().catch() 时执行。

¥All Prisma Client methods return an instance of PrismaPromise which only executes when you call await or .then() or .catch().

5. 改进你的应用

¥ Evolving your application

每当你对 Prisma 架构中反映的数据库进行更改时,你需要手动重新生成 Prisma 客户端以更新 node_modules/.prisma/client 目录中生成的代码:

¥Whenever you make changes to your database that are reflected in the Prisma schema, you need to manually re-generate Prisma Client to update the generated code in the node_modules/.prisma/client directory:

prisma generate