Prisma Config 参考
概述
¥Overview
此功能目前在 抢先体验 中,仍有可能更改。
¥This feature is currently in Early Access and still subject to change.
prisma.config.ts
文件使用 TypeScript 配置 Prisma CLI,包括 migrate
和 studio
等子命令。你可以通过两种方式定义配置:
¥The prisma.config.ts
file configures the Prisma CLI, including subcommands like migrate
and studio
, using TypeScript. You can define your config in two ways:
使用 defineConfig
助手:
¥Using the defineConfig
helper:
import path from 'node:path';
import { defineConfig } from 'prisma/config';
export default defineConfig({
earlyAccess: true,
schema: path.join('prisma', 'schema.prisma'),
});
或者使用 TypeScript 的 satisfies
运算符和 PrismaConfig
类型:
¥Or using TypeScript's satisfies
operator with the PrismaConfig
type:
import path from 'node:path';
import type { PrismaConfig } from 'prisma'
export default {
earlyAccess: true,
schema: path.join('prisma', 'schema.prisma'),
} satisfies PrismaConfig
配置界面
¥Configuration interface
export declare type PrismaConfig<Env extends EnvVars = never> = {
/**
* Whether features with an unstable API are enabled.
*/
earlyAccess: true;
/**
* The path to the schema file or path to a folder that shall be recursively searched for .prisma files.
*/
schema?: string;
/**
* The configuration for Prisma Studio.
*/
studio?: PrismaStudioConfigShape<Env>;
/**
* The configuration for Prisma Migrate + Introspect
*/
migrate?: PrismaMigrateConfigShape<Env>;
};
选项参考
¥Options reference
earlyAccess
-
类型:
boolean
¥Type:
boolean
-
必需:是(在抢先体验期间)
¥Required: Yes (during Early Access)
-
默认:none
¥Default: none
控制是否启用配置文件。在早期访问期间必须设置为 true
。
¥Controls whether the config file is enabled. Must be set to true
during Early Access.
migrate
-
类型:
object
¥Type:
object
-
必需:不
¥Required: No
-
默认:
{}
¥Default:
{}
配置 Prisma Migrate 与底层数据库的通信方式。有关详细信息,请参阅下面的子选项。
¥Configures how Prisma Migrate communicates with your underlying database. See sub-options below for details.
migrate.adapter
-
类型:
(env: Env) => Promise<SqlMigrationAwareDriverAdapterFactory>
¥Type:
(env: Env) => Promise<SqlMigrationAwareDriverAdapterFactory>
-
必需:不
¥Required: No
-
默认:none
¥Default: none
返回 Prisma 驱动程序适配器实例的函数,Prisma CLI 会使用该实例运行迁移。该函数接收一个包含环境变量的 env
参数,并返回一个解析为有效 Prisma 驱动程序适配器的 Promise
参数。
¥A function that returns a Prisma driver adapter instance which is used by the Prisma CLI to run migrations. The function receives an env
parameter containing environment variables and should return a Promise
that resolves to a valid Prisma driver adapter.
使用 Prisma ORM D1 驱动程序适配器的示例:
¥Example using the Prisma ORM D1 driver adapter:
import path from 'node:path'
import type { PrismaConfig } from 'prisma'
import { PrismaD1HTTP } from '@prisma/adapter-d1'
// import your .env file
import 'dotenv/config'
type Env = {
CLOUDFLARE_D1_TOKEN: string
CLOUDFLARE_ACCOUNT_ID: string
CLOUDFLARE_DATABASE_ID: string
}
export default {
earlyAccess: true,
schema: path.join('prisma', 'schema.prisma'),
migrate: {
async adapter(env) {
return new PrismaD1HTTP({
CLOUDFLARE_D1_TOKEN: env.CLOUDFLARE_D1_TOKEN,
CLOUDFLARE_ACCOUNT_ID: env.CLOUDFLARE_ACCOUNT_ID,
CLOUDFLARE_DATABASE_ID: env.CLOUDFLARE_DATABASE_ID,
})
},
},
} satisfies PrismaConfig<Env>
schema
-
类型:
string
¥Type:
string
-
必需:不
¥Required: No
-
默认:
./prisma/schema.prisma
和./schema.prisma
¥Default:
./prisma/schema.prisma
and./schema.prisma
配置 Prisma ORM 定位和加载架构文件的方式。可以是文件或文件夹路径。相对路径是相对于 prisma.config.ts
文件位置解析的。有关模式位置选项的更多信息,请参阅 此处。
¥Configures how Prisma ORM locates and loads your schema file(s). Can be a file or folder path. Relative paths are resolved relative to the prisma.config.ts
file location. See here for more info about schema location options.
studio
-
类型:
object
¥Type:
object
-
必需:不
¥Required: No
-
默认:none
¥Default: none
配置 Prisma Studio 如何连接到你的数据库。
¥Configures how Prisma Studio connects to your database.
studio.adapter
-
类型:
(env: Env) => Promise<SqlMigrationAwareDriverAdapterFactory>
¥Type:
(env: Env) => Promise<SqlMigrationAwareDriverAdapterFactory>
-
必需:不
¥Required: No
-
默认:none
¥Default: none
返回 Prisma 驱动程序适配器实例的函数。该函数接收一个包含环境变量的 env
参数,并返回一个解析为有效 Prisma 驱动程序适配器的 Promise
参数。
¥A function that returns a Prisma driver adapter instance. The function receives an env
parameter containing environment variables and should return a Promise
that resolves to a valid Prisma driver adapter.
使用 Prisma ORM LibSQL 驱动程序适配器的示例:
¥Example using the Prisma ORM LibSQL driver adapter:
import type { PrismaConfig } from 'prisma'
export default {
earlyAccess: true,
studio: {
adapter: async (env: Env) => {
const { PrismaLibSQL } = await import('@prisma/adapter-libsql')
const { createClient } = await import('@libsql/client')
const libsql = createClient({
url: env.DOTENV_PRISMA_STUDIO_LIBSQL_DATABASE_URL,
})
return new PrismaLibSQL(libsql)
},
},
} satisfies PrismaConfig
常见模式
¥Common patterns
设置你的项目
¥Setting up your project
要开始使用 Prisma Config,请在项目根目录中创建一个 prisma.config.ts
文件。你可以使用以下任一方法:
¥To get started with Prisma Config, create a prisma.config.ts
file in your project root. You can use either of these approaches:
使用 defineConfig
:
¥Using defineConfig
:
import { defineConfig } from 'prisma/config'
export default defineConfig({
earlyAccess: true
})
使用 TypeScript 类型:
¥Using TypeScript types:
import type { PrismaConfig } from 'prisma'
export default {
earlyAccess: true
} satisfies PrismaConfig
使用环境变量
¥Using environment variables
使用 prisma.config.ts
时,不会自动加载来自 .env
文件的环境变量。你需要:
¥When using prisma.config.ts
, environment variables from .env
files are not automatically loaded. You'll need to:
-
安装
dotenv
包:¥Install the
dotenv
package:
npm install dotenv
-
在你的配置文件中导入
dotenv/config
:¥Import
dotenv/config
in your config file:
import 'dotenv/config'
import type { PrismaConfig } from 'prisma'
export default {
earlyAccess: true,
// now you can use process.env variables
} satisfies PrismaConfig
使用多文件模式
¥Using multi-file schemas
如果你要将 Prisma 模式拆分为多个文件,则需要通过 schema
属性指定 Prisma 模式文件夹的路径:
¥If you want to split your Prisma schema into multiple files, you need to specify the path to your Prisma schema folder via the schema
property:
import path from 'node:path';
import type { PrismaConfig } from 'prisma'
export default {
earlyAccess: true,
schema: path.join('prisma', 'schema'),
} satisfies PrismaConfig
在这种情况下,你的 migrations
目录必须位于定义 datasource
块的 .prisma
文件旁边。
¥In that case, your migrations
directory must be located next to the .prisma
file that defines the datasource
block.
例如,假设 schema.prisma
定义了 datasource
,则迁移文件夹的放置方式如下:
¥For example, assuming schema.prisma
defines the datasource
, here's how how need to place the migrations folder:
# `migrations` and `schema.prisma` are on the same level
.
├── migrations
├── models
│ ├── posts.prisma
│ └── users.prisma
└── schema.prisma
自定义配置位置
¥Custom config location
你可以在运行 Prisma CLI 命令时为配置文件指定自定义位置:
¥You can specify a custom location for your config file when running Prisma CLI commands:
prisma validate --config ./path/to/myconfig.ts