diff --git a/content/200-orm/050-overview/500-databases/900-turso.mdx b/content/200-orm/050-overview/500-databases/900-turso.mdx index d4bdb03925..df54ae82b0 100644 --- a/content/200-orm/050-overview/500-databases/900-turso.mdx +++ b/content/200-orm/050-overview/500-databases/900-turso.mdx @@ -132,7 +132,7 @@ LIBSQL_DATABASE_TOKEN="..." ### 3. Set up Prisma Config file -Make sure that you have a [`prisma.config.ts`](/orm/reference/prisma-config-reference) file for your project. Then, set up the [migration driver adapter](/orm/reference/prisma-config-reference#adapter) to use `PrismaLibSQL`: +Make sure that you have a [`prisma.config.ts`](/orm/reference/prisma-config-reference) file for your project. Then, set up the [migration driver adapter](/orm/reference/prisma-config-reference#adapter-removed) to use `PrismaLibSQL`: ```ts file=prisma.config.ts import path from 'node:path' diff --git a/content/200-orm/050-overview/500-databases/950-cloudflare-d1.mdx b/content/200-orm/050-overview/500-databases/950-cloudflare-d1.mdx index 3f6da24c51..4ce7183a19 100644 --- a/content/200-orm/050-overview/500-databases/950-cloudflare-d1.mdx +++ b/content/200-orm/050-overview/500-databases/950-cloudflare-d1.mdx @@ -84,7 +84,7 @@ CLOUDFLARE_D1_TOKEN="F8Cg..." #### 3. Set up Prisma Config file -Make sure that you have a [`prisma.config.ts`](/orm/reference/prisma-config-reference) file for your project. Then, set up the [migration driver adapter](/orm/reference/prisma-config-reference#adapter) to reference D1: +Make sure that you have a [`prisma.config.ts`](/orm/reference/prisma-config-reference) file for your project. Then, set up the [migration driver adapter](/orm/reference/prisma-config-reference#adapter-removed) to reference D1: ```ts file=prisma.config.ts import type { PrismaConfig } from 'prisma'; diff --git a/content/200-orm/100-prisma-schema/20-data-model/65-externally-managed-tables.mdx b/content/200-orm/100-prisma-schema/20-data-model/65-externally-managed-tables.mdx index 05851a8b33..bf14f9c00f 100644 --- a/content/200-orm/100-prisma-schema/20-data-model/65-externally-managed-tables.mdx +++ b/content/200-orm/100-prisma-schema/20-data-model/65-externally-managed-tables.mdx @@ -49,7 +49,17 @@ If you want to use external tables, here's the main workflow: You can specify externally managed tables in your [Prisma Config](/orm/reference/prisma-config-reference) file via the `tables.external` property: ```ts file=prisma.config.ts +import 'dotenv/config' +import { defineConfig, env } from 'prisma/config' + export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + datasource: { + url: env('DATABASE_URL'), + }, // required when using unstable features experimental: { // add-next-line @@ -88,7 +98,14 @@ If the external table is not referenced by any managed table—that is no manage ```ts file=prisma.config.ts +import 'dotenv/config' +import { defineConfig, env } from 'prisma/config' + export default defineConfig({ + schema: 'prisma/schema.prisma', + datasource: { + url: env('DATABASE_URL'), + }, // required when using unstable features experimental: { // add-next-line @@ -102,6 +119,7 @@ export default defineConfig({ ] }, migrations: { + path: 'prisma/migrations', // setup the users table for the shadow database initShadowDb: ` CREATE TABLE public.users (id SERIAL PRIMARY KEY); @@ -168,7 +186,17 @@ CREATE TABLE posts ( Enable use of externally managed tables via the `tables.external` property: ```ts file=prisma.config.ts +import 'dotenv/config' +import { defineConfig, env } from 'prisma/config' + export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + datasource: { + url: env('DATABASE_URL'), + }, experimental: { // add-next-line externalTables: true @@ -280,12 +308,26 @@ enum role { Then add a `migrations.initShadowDb` script so Prisma knows about the `users` table during migrations. -```ts -// prisma.config.ts +```ts file=prisma.config.ts +import 'dotenv/config' +import { defineConfig, env } from 'prisma/config' + export default defineConfig({ - // ... + schema: 'prisma/schema.prisma', + datasource: { + url: env('DATABASE_URL'), + }, + experimental: { + externalTables: true + }, + tables: { + external: [ + "public.users", + ] + }, // add-start migrations: { + path: 'prisma/migrations', // setup the users table for the shadow database initShadowDb: ` CREATE TABLE public.users (id SERIAL PRIMARY KEY); diff --git a/content/200-orm/500-reference/100-prisma-schema-reference.mdx b/content/200-orm/500-reference/100-prisma-schema-reference.mdx index ec555ae819..0a113a1579 100644 --- a/content/200-orm/500-reference/100-prisma-schema-reference.mdx +++ b/content/200-orm/500-reference/100-prisma-schema-reference.mdx @@ -20,7 +20,7 @@ A `datasource` block accepts the following fields: | `provider` | **Yes** | String (`postgresql`, `mysql`, `sqlite`, `sqlserver`, `mongodb`, `cockroachdb`) | Describes which data source connectors to use. | | `url` | **Yes** | String (URL) | **Deprecated in Prisma ORM v7.** Configure the connection URL in Prisma Config instead: see [`datasource.url`](/orm/reference/prisma-config-reference#datasourceurl). Existing schemas continue to work, but you should migrate to Prisma Config. | | `shadowDatabaseUrl` | No | String (URL) | **Deprecated in Prisma ORM v7.** Configure the shadow database URL in Prisma Config instead: see [`datasource.shadowDatabaseUrl`](/orm/reference/prisma-config-reference#datasourceshadowdatabaseurl). | -| `directUrl` | No | String (URL) | **Deprecated in Prisma ORM v7.** Configure the direct connection URL in Prisma Config instead: see [`datasource.directUrl`](/orm/reference/prisma-config-reference#datasourcedirecturl). | +| `directUrl` | No | String (URL) | **Deprecated in Prisma ORM v7.** Configure the direct connection URL in Prisma Config instead: see [`datasource.directUrl`](/orm/reference/prisma-config-reference#datasourcedirecturl-removed). | | `relationMode` | No | String (`foreignKeys`, `prisma`) | Sets whether [referential integrity](/orm/prisma-schema/data-model/relations/relation-mode) is enforced by foreign keys in the database or emulated in the Prisma Client.

In preview in versions 3.1.1 and later. The field is named `relationMode` in versions 4.5.0 and later, and was previously named `referentialIntegrity`. | | `extensions` | No | List of strings (PostgreSQL extension names) | Allows you to [represent PostgreSQL extensions in your schema](/orm/prisma-schema/postgresql-extensions). Available in preview for PostgreSQL only in Prisma ORM versions 4.5.0 and later. | diff --git a/content/200-orm/500-reference/325-prisma-config-reference.mdx b/content/200-orm/500-reference/325-prisma-config-reference.mdx index c81cd8e241..713ec76d34 100644 --- a/content/200-orm/500-reference/325-prisma-config-reference.mdx +++ b/content/200-orm/500-reference/325-prisma-config-reference.mdx @@ -9,25 +9,25 @@ sidebar_label: "Prisma Config" The Prisma Config file configures the Prisma CLI, including subcommands like `migrate` and `studio`, using TypeScript. +:::info[Prisma ORM v7 changes] + +Starting with Prisma ORM v7, when you run `prisma init`, a `prisma.config.ts` file is automatically created. The database connection URL is now configured in this file instead of in the `schema.prisma` file. See [Using environment variables](#using-environment-variables) for setup details. + +::: + You can define your config in either of two ways: - Using the `defineConfig` helper: ```ts - import path from "node:path"; + import 'dotenv/config' import { defineConfig, env } from "prisma/config"; export default defineConfig({ - schema: path.join("prisma", "schema.prisma"), + schema: 'prisma/schema.prisma', migrations: { - path: path.join("db", "migrations"), - }, - views: { - path: path.join("db", "views"), - }, - typedSql: { - path: path.join("db", "queries"), + path: 'prisma/migrations', + seed: 'tsx prisma/seed.ts', }, - engine: "classic", datasource: { url: env("DATABASE_URL") } @@ -36,21 +36,16 @@ You can define your config in either of two ways: - Using TypeScript's `satisfies` operator with the `PrismaConfig` type: ```ts - import path from "node:path"; + import 'dotenv/config' import type { PrismaConfig } from "prisma"; + import { env } from "prisma/config"; export default { - schema: path.join("db", "schema.prisma"), + schema: "prisma/schema.prisma", migrations: { - path: path.join("db", "migrations"), + path: "prisma/migrations", + seed: 'tsx prisma/seed.ts', }, - views: { - path: path.join("db", "views"), - }, - typedSql: { - path: path.join("db", "queries"), - }, - engine: "classic", datasource: { url: env("DATABASE_URL") } @@ -66,22 +61,12 @@ export declare type PrismaConfig = { // Whether features with an unstable API are enabled. experimental: { - adapter: boolean; externalTables: boolean; - studio: boolean; }, // The path to the schema file, or path to a folder that shall be recursively searched for *.prisma files. schema?: string; - // The Driver Adapter used for Prisma CLI. - adapter?: () => Promise; - - // Configuration for Prisma Studio. - studio?: { - adapter: () => Promise; - }; - // Configuration for Prisma migrations. migrations?: { path: string; @@ -98,19 +83,29 @@ export declare type PrismaConfig = { typedSql?: { path: string; }; - // Depending on the choice, you must provide either a `datasource` object or driver adapter - engine: 'classic' | 'js' - // If using the classic engine, datasource sets the database url, shadowDatabaseUrl, or directURL + // Database connection configuration datasource?: { url: string; - directUrl?: string; shadowDatabaseUrl?: string; } }; ``` +:::note[Prisma ORM v6.19 and earlier] + +In Prisma ORM v6.19 and earlier, the configuration interface also included: +- `experimental.adapter` and `experimental.studio` flags +- `adapter` property for configuring driver adapters +- `studio` property for Prisma Studio configuration +- `datasource.directUrl` property for direct database connections +- `engine` property for choosing between `classic` and `js` engines + +These have been removed in Prisma ORM v7. See the individual property sections below for migration guidance. + +::: + ## Supported file extensions Prisma Config files can be named as `prisma.config.*` or `.config/prisma.*` with the extensions `js`, `ts`, `mjs`, `cjs`, `mts`, or `cts`. Other extensions are supported to ensure compatibility with different TypeScript compiler settings. @@ -133,80 +128,6 @@ Configures how Prisma ORM locates and loads your schema file(s). Can be a file o | -------- | -------- | -------- | ---------------------------------------------- | | `schema` | `string` | No | `./prisma/schema.prisma` and `./schema.prisma` | -### `adapter` - -A function that returns a Prisma driver adapter instance which is used by the Prisma CLI to run migrations. The function should return a `Promise` that resolves to a valid Prisma driver adapter. - -| Property | Type | Required | Default | -| ----------------- | -------------------------------------------------------------- | -------- | ------- | -| `adapter` | `() => Promise` | No | none | - -Example using the Prisma ORM D1 driver adapter: - -```ts -import path from "node:path"; -import type { PrismaConfig } from "prisma"; -import { PrismaD1 } from "@prisma/adapter-d1"; - -export default { - experimental: { - adapter: true - }, - schema: path.join("prisma", "schema.prisma"), - async adapter() { - return new PrismaD1({ - CLOUDFLARE_D1_TOKEN: process.env.CLOUDFLARE_D1_TOKEN, - CLOUDFLARE_ACCOUNT_ID: process.env.CLOUDFLARE_ACCOUNT_ID, - CLOUDFLARE_DATABASE_ID: process.env.CLOUDFLARE_DATABASE_ID, - }); - }, -} satisfies PrismaConfig; -``` - -:::note - -As of [Prisma ORM v6.11.0](https://github.com/prisma/prisma/releases/tag/6.11.0), the D1 adapter has been renamed from `PrismaD1HTTP` to `PrismaD1`. - -::: - -### `studio` - -Configures how Prisma Studio connects to your database. See sub-options below for details. - -| Property | Type | Required | Default | -| -------- | -------- | -------- | ------- | -| `studio` | `object` | No | none | - -#### `studio.adapter` - -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. - -| Property | Type | Required | Default | -| ----------------- | -------------------------------------------------------------- | -------- | ------- | -| `studio.adapter ` | `(env: Env) => Promise` | No | none | - -Example using the Prisma ORM LibSQL driver adapter: - -```ts -import type { PrismaConfig } from "prisma"; - -export default { - experimental: { - studio: 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; -``` ### `tables.external` and `enums.external` @@ -220,9 +141,17 @@ These options declare tables and enums in your database that are **managed exter **Example:** ```ts -import { defineConfig } from "prisma/config"; +import 'dotenv/config' +import { defineConfig, env } from "prisma/config"; export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + datasource: { + url: env('DATABASE_URL'), + }, experimental: { externalTables: true, }, @@ -256,11 +185,17 @@ This option allows you to define a script that Prisma runs to seed your database **Example:** ```ts -import { defineConfig } from "prisma/config"; +import 'dotenv/config' +import { defineConfig, env } from "prisma/config"; export default defineConfig({ + schema: 'prisma/schema.prisma', migrations: { - seed: `tsx db/seed.ts`, + path: 'prisma/migrations', + seed: 'tsx db/seed.ts', + }, + datasource: { + url: env('DATABASE_URL'), }, }); ``` @@ -276,20 +211,26 @@ This option allows you to define SQL statements that Prisma runs on the **shadow **Example:** ```ts -import { defineConfig } from "prisma/config"; +import 'dotenv/config' +import { defineConfig, env } from "prisma/config"; export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + initShadowDb: ` + CREATE TABLE public.users (id SERIAL PRIMARY KEY); + `, + }, + datasource: { + url: env('DATABASE_URL'), + }, experimental: { externalTables: true, }, tables: { external: ["public.users"], }, - migrations: { - initShadowDb: ` - CREATE TABLE public.users (id SERIAL PRIMARY KEY); - `, - }, }); ``` @@ -317,37 +258,178 @@ Enables specific experimental features in the Prisma CLI. | Property | Type | Required | Default | | ---------------- | --------- | -------- | ------- | -| `adapter` | `boolean` | No | `false` | | `externalTables` | `boolean` | No | `false` | -| `studio` | `boolean` | No | `false` | Example: ```ts -import { defineConfig } from "prisma/config"; +import 'dotenv/config' +import { defineConfig, env } from "prisma/config"; export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + datasource: { + url: env('DATABASE_URL'), + }, experimental: { - adapter: true, externalTables: true, - studio: true, }, - schema: "prisma/schema.prisma", }); ``` :::note -If you use features like `adapter`, `studio` or `externalTables` without enabling the corresponding experimental flag, Prisma will throw an error: +If you use the `externalTables` feature without enabling the experimental flag, Prisma will throw an error: ```terminal -Failed to load config file "~" as a TypeScript/JavaScript module. Error: Error: The `studio` configuration requires `experimental.studio` to be set to `true`. +Failed to load config file "~" as a TypeScript/JavaScript module. Error: Error: The `externalTables` configuration requires `experimental.externalTables` to be set to `true`. +``` + +::: + +:::info[Prisma ORM v6.19 and earlier] + +In Prisma ORM v6.19 and earlier, the `experimental` object also included `adapter` and `studio` flags. These have been removed in Prisma ORM v7. See the [`adapter`](#adapter-removed) and [`studio`](#studio-removed) sections for details. + +::: + + +### `datasource.url` + +Connection URL including authentication info. Most connectors use [the syntax provided by the database](/orm/reference/connection-urls#format). + +:::info[Prisma ORM v7 changes] + +In Prisma ORM v7, the `url` field is configured in `prisma.config.ts` instead of in [the `datasource` block of your `schema.prisma`](/orm/prisma-schema/overview/data-sources) file. When you run `prisma init`, the generated `schema.prisma` file will not include a `url` property in the `datasource` block. + +For Prisma ORM v6.19 and earlier, the `url` field remains in the `schema.prisma` file's `datasource` block. + +::: + +| Property | Type | Required | Default | +| -------- | ------------------ | -------- | ----------------- | +| `datasource.url` | `string` | Yes | `''` | + +**Example:** + +```ts +import 'dotenv/config' +import { defineConfig, env } from "prisma/config"; + +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + datasource: { + url: env('DATABASE_URL'), + }, +}); +``` + + +### `datasource.shadowDatabaseUrl` + +Connection URL to the shadow database used by Prisma Migrate. Allows you to use a cloud-hosted database as the shadow database. + +:::info[Prisma ORM v7 changes] + +In Prisma ORM v7, the `shadowDatabaseUrl` field is configured in `prisma.config.ts` instead of in the `datasource` block of your `schema.prisma` file. + +For Prisma ORM v6.19 and earlier, the `shadowDatabaseUrl` field remains in the `schema.prisma` file's `datasource` block. + +::: + +| Property | Type | Required | Default | +| -------- | ------------------ | -------- | ----------------- | +| `datasource.shadowDatabaseUrl` | `string` | No | `''` | + + +### `datasource.directUrl` (Removed) + +:::warning[Removed in Prisma ORM v7] + +The `datasource.directUrl` property has been removed in Prisma ORM v7 in favor of the [`url` property](#datasourceurl). + +::: + +
+For Prisma ORM v6.19 and earlier + +Connection URL for direct connection to the database. + +If you use a connection pooler URL in the `url` argument (for example, pgBouncer), Prisma CLI commands that require a direct connection to the database use the URL in the `directUrl` argument. + +The `directUrl` property is supported by Prisma Studio from version 5.1.0 upwards. The `directUrl` property is not needed when using [Prisma Postgres](/postgres) database. + +| Property | Type | Required | Default | +| -------- | ------------------ | -------- | ----------------- | +| `datasource.directUrl` | `string` | No | `''` | + +
+ + +### `adapter` (Removed) + +:::warning[Removed in Prisma ORM v7] + +The `adapter` property has been removed in Prisma ORM v7. Migrations for driver adapters work automatically without additional configuration in `prisma.config.ts` as of Prisma ORM v7. + +::: + +
+For Prisma ORM v6.19 and earlier + +A function that returns a Prisma driver adapter instance which is used by the Prisma CLI to run migrations. The function should return a `Promise` that resolves to a valid Prisma driver adapter. + +| Property | Type | Required | Default | +| ----------------- | -------------------------------------------------------------- | -------- | ------- | +| `adapter` | `() => Promise` | No | none | + +Example using the Prisma ORM D1 driver adapter: + +```ts +import path from "node:path"; +import type { PrismaConfig } from "prisma"; +import { PrismaD1 } from "@prisma/adapter-d1"; + +export default { + experimental: { + adapter: true + }, + engine: "js", + schema: path.join("prisma", "schema.prisma"), + async adapter() { + return new PrismaD1({ + CLOUDFLARE_D1_TOKEN: process.env.CLOUDFLARE_D1_TOKEN, + CLOUDFLARE_ACCOUNT_ID: process.env.CLOUDFLARE_ACCOUNT_ID, + CLOUDFLARE_DATABASE_ID: process.env.CLOUDFLARE_DATABASE_ID, + }); + }, +} satisfies PrismaConfig; ``` +:::note + +As of [Prisma ORM v6.11.0](https://github.com/prisma/prisma/releases/tag/6.11.0), the D1 adapter has been renamed from `PrismaD1HTTP` to `PrismaD1`. + ::: +
+ +### `engine` (Removed) + +:::warning[Removed in Prisma ORM v7] + +The `engine` property has been removed in Prisma ORM v7. -### `engine` +::: + +
+For Prisma ORM v6.19 and earlier Configure the schema engine your project should use. @@ -355,8 +437,7 @@ Configure the schema engine your project should use. | -------- | ------------------ | -------- | ----------------- | | `engine` | `classic` or `js` | No | `classic` | - By default it is set to use the classic engine, which requires that `datasource` be set - in your `prisma.config.ts`. +By default it is set to use the classic engine, which requires that `datasource` be set in your `prisma.config.ts`. ```ts import path from "node:path"; @@ -370,48 +451,65 @@ export default defineConfig({ }); ``` -### `datasource.url` +
-Connection URL including authentication info. Most connectors use [the syntax provided by the database](/orm/reference/connection-urls#format). -:::note -Replaces the deprecated `url` field in the Prisma schema `datasource` block (Prisma ORM v7 and later). -::: +### `studio` (Removed) -| Property | Type | Required | Default | -| -------- | ------------------ | -------- | ----------------- | -| `datasource.url` | `string` | Yes | `''` | +:::warning[Removed in Prisma ORM v7] +The `studio` property has been removed in Prisma ORM v7. To run Prisma Studio, use: -### `datasource.shadowDatabaseUrl` +```terminal +npx prisma studio --config ./prisma.config.ts +``` -Connection URL to the shadow database used by Prisma Migrate. Allows you to use a cloud-hosted database as the shadow database +Prisma Studio now uses the connection configuration from the `datasource` property automatically. See the [Prisma Studio documentation](/orm/reference/prisma-cli-reference#studio) for more details. -:::note -Replaces the deprecated `shadowDatabaseUrl` field in the Prisma schema `datasource` block (Prisma ORM v7 and later). ::: -| Property | Type | Required | Default | -| -------- | ------------------ | -------- | ----------------- | -| `datasource.shadowDatabaseUrl` | `string` | No | `''` | +
+For Prisma ORM v6.19 and earlier +Configures how Prisma Studio connects to your database. See sub-options below for details. -### `datasource.directUrl` +| Property | Type | Required | Default | +| -------- | -------- | -------- | ------- | +| `studio` | `object` | No | none | -Connection URL for direct connection to the database. +#### `studio.adapter` (Removed) -:::note -Replaces the deprecated `directUrl` field in the Prisma schema `datasource` block (Prisma ORM v7 and later). -::: +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. -If you use a connection pooler URL in the `url` argument (for example, if you use [Prisma Accelerate](/accelerate) or pgBouncer), Prisma CLI commands that require a direct connection to the database use the URL in the `directUrl` argument.

The `directUrl` property is supported by Prisma Studio from version 5.1.0 upwards.

The `directUrl` property is not needed when using [Prisma Postgres](/postgres) database. +| Property | Type | Required | Default | +| ----------------- | -------------------------------------------------------------- | -------- | ------- | +| `studio.adapter ` | `(env: Env) => Promise` | No | none | -| Property | Type | Required | Default | -| -------- | ------------------ | -------- | ----------------- | -| `datasource.directUrl` | `string` | No | `''` | +Example using the Prisma ORM LibSQL driver adapter: +```ts +import type { PrismaConfig } from "prisma"; + +export default { + experimental: { + studio: true + }, + engine: "js", + 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 @@ -422,75 +520,108 @@ To get started with Prisma Config, create a `prisma.config.ts` file in your proj Using `defineConfig`: ```ts -import { defineConfig } from "prisma/config"; +import 'dotenv/config' +import { defineConfig, env } from "prisma/config"; -export default defineConfig({}); +export default defineConfig({ + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + datasource: { + url: env('DATABASE_URL'), + }, +}); ``` Using TypeScript types: ```ts +import 'dotenv/config' import type { PrismaConfig } from "prisma"; +import { env } from "prisma/config"; -export default {} satisfies PrismaConfig; +export default { + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, + datasource: { + url: env('DATABASE_URL'), + }, +} satisfies PrismaConfig; ``` ### Using environment variables -When using `prisma.config.ts`, environment variables from `.env` files are not automatically loaded. Using `tsx`, you can pass a `--env-file` flag and that will automatically add those values to `process.env` +:::info[Prisma ORM v7 changes] + +In Prisma ORM v7, when you run `prisma init`, the generated `prisma.config.ts` file includes `import 'dotenv/config'` by default. You must install the `dotenv` package to use environment variables. + +::: + +When using `prisma.config.ts`, environment variables from `.env` files need to be loaded explicitly. There are several approaches depending on your runtime and Node version: -If using Node or Deno: +#### Using dotenv (Recommended for Prisma ORM v7) + +1. Install the `dotenv` package: ```terminal -tsx --env-file=.env src/index.ts -tsx watch --env-file=.env --env-file=.local.env src/index.ts -tsx --env-file=.env ./prisma/seed.ts +npm install dotenv ``` -For Bun, `.env` files are automatically loaded. +2. Import `dotenv/config` at the top of your `prisma.config.ts` file: -For accessing environment variables within `prisma.config.ts`, use the `env()` helper function to -provide a type-safe way of accessing that variable: - -```tsx -import path from "node:path"; +```ts +import 'dotenv/config' import { defineConfig, env } from "prisma/config"; -type Env = { - DATABASE_URL: string -} export default defineConfig({ - engine: "classic", + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + seed: 'tsx prisma/seed.ts', + }, datasource: { - url: env('DATABASE_URL'), + url: env('DATABASE_URL'), }, - schema: path.join("prisma", "schema.prisma"), }); ``` -For releases of Node before v20, you'll need to: +#### Using Node.js v20+ or tsx with --env-file flag -1. Install the `dotenv` package: +If using Node.js v20+ or `tsx`, you can pass a `--env-file` flag to automatically load environment variables: ```terminal -npm install dotenv +tsx --env-file=.env src/index.ts +tsx watch --env-file=.env --env-file=.local.env src/index.ts +tsx --env-file=.env ./prisma/seed.ts ``` -2. Import `dotenv/config` in your config file: +#### Using Bun + +For Bun, `.env` files are automatically loaded without additional configuration. + +#### Type-safe environment variables + +Use the `env()` helper function to provide type-safe access to environment variables: ```ts -import "dotenv/config"; +import 'dotenv/config' import { defineConfig, env } from "prisma/config"; type Env = { DATABASE_URL: string } + export default defineConfig({ - engine: "classic", + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + }, datasource: { - url: env('DATABASE_URL'), + url: env('DATABASE_URL'), }, - schema: path.join("prisma", "schema.prisma"), }); ``` @@ -611,12 +742,23 @@ prisma validate --config ./path/to/myconfig.ts ## Loading environment variables +:::info[Prisma ORM v7 changes] + + +In Prisma ORM v7, `prisma init` generates a `prisma.config.ts` file automatically. To load environment variables with `dotenv`, do the following: + +1. Install the `dotenv` package. +2. Add `import 'dotenv/config'` at the top of your `prisma.config.ts` file. + +This is required for Prisma to read values from your `.env` file. +::: + To load environment variables in your Prisma application, you can use the `prisma.config.ts` file along with the `env` helper from `prisma/config`. This approach provides better type safety and configuration management. -1. First, install the required dependency: +1. Install the `dotenv` package: ```bash - npm install dotenv --save-dev + npm install dotenv ``` 2. Create a `.env` file in your project root (if it doesn't exist) and add your database connection string: @@ -625,15 +767,23 @@ To load environment variables in your Prisma application, you can use the `prism DATABASE_URL="your_database_connection_string_here" ``` -3. Update your `prisma.config.ts` file in your project root: +3. Ensure your `prisma.config.ts` file imports `dotenv/config` at the top: - ```ts - import "dotenv/config"; + ```ts file=prisma.config.ts + // add-start + import 'dotenv/config' + // add-end import { defineConfig, env } from "prisma/config"; export default defineConfig({ - datasource: { - url: env("DATABASE_URL"), - }, + schema: 'prisma/schema.prisma', + migrations: { + path: 'prisma/migrations', + seed: 'tsx prisma/seed.ts', + }, + datasource: { + url: env("DATABASE_URL"), + }, }); - ``` \ No newline at end of file + ``` + diff --git a/content/800-guides/070-cloudflare-d1.mdx b/content/800-guides/070-cloudflare-d1.mdx index 7264a75355..5d8083a679 100644 --- a/content/800-guides/070-cloudflare-d1.mdx +++ b/content/800-guides/070-cloudflare-d1.mdx @@ -177,7 +177,7 @@ CLOUDFLARE_D1_TOKEN="F8Cg..." ### 5.2 Configure Prisma Config -Ensure that you have a `prisma.config.ts` file set up in the root of your project with a [driver adapter](/orm/reference/prisma-config-reference#adapter) defined. +Ensure that you have a `prisma.config.ts` file set up in the root of your project with a [driver adapter](/orm/reference/prisma-config-reference#adapter-removed) defined. ```ts import type { PrismaConfig } from 'prisma';