From 83ad7ef0c2e0e6f429353f8338fab53b6d58c61b Mon Sep 17 00:00:00 2001 From: Jon Harrell <4829245+jharrell@users.noreply.github.com> Date: Thu, 27 Mar 2025 12:28:48 -0500 Subject: [PATCH 01/10] d1 docs --- .../500-databases/950-cloudflare-d1.mdx | 42 +++++++++++--- .../325-prisma-config-reference.mdx | 33 +++++++++-- content/800-guides/070-cloudflare-d1.mdx | 57 +++++++------------ 3 files changed, 80 insertions(+), 52 deletions(-) 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 dcb2e41ce6..1526ae3562 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 @@ -6,19 +6,15 @@ tocDepth: 3 preview: true --- - - -This guide discusses the concepts behind using Prisma ORM and Cloudflare D1, explains the commonalities and differences between Cloudflare D1 and other database providers, and leads you through the process for configuring your application to integrate with Cloudflare D1. +This page discusses the concepts behind using Prisma ORM and Cloudflare D1, explains the commonalities and differences between Cloudflare D1 and other database providers, and leads you through the process for configuring your application to integrate with Cloudflare D1. Prisma ORM support for Cloudflare D1 is currently in [Preview](/orm/more/releases#preview). We would appreciate your feedback [on GitHub](https://github.com/prisma/prisma/discussions/23646). If you want to deploy a Cloudflare Worker with D1 and Prisma ORM, follow this [tutorial](/guides/cloudflare-d1). - - ## What is Cloudflare D1? -D1 is Cloudflare's native serverless database and was initially [launched in 2022](https://blog.cloudflare.com/introducing-d1/). It's based on SQLite and can be used when deploying applications with Cloudflare. +D1 is Cloudflare's native serverless database and was initially [launched in 2022](https://blog.cloudflare.com/introducing-d1/). It's based on SQLite and can be used when deploying applications with Cloudflare Workers. Following Cloudflare's principles of geographic distribution and bringing compute and data closer to application users, D1 supports automatic read-replication. It dynamically manages the number of database instances and locations of read-only replicas based on how many queries a database is getting, and from where. @@ -39,7 +35,7 @@ Many aspects of using Prisma ORM with D1 are just like using Prisma ORM with any There are a number of differences between D1 and SQLite to consider. You should be aware of the following when deciding to use D1 and Prisma ORM: - **Local and remote D1 (SQLite) databases**. Cloudflare provides local and remote versions of D1. The [local](https://developers.cloudflare.com/d1/build-with-d1/local-development/) version is managed using the `--local` option of the `wrangler d1` CLI and is located in `.wrangler/state`. The [remote](https://developers.cloudflare.com/d1/build-with-d1/remote-development/) version is managed by Cloudflare and is accessed via HTTP. -- **Making schema changes**. Since D1 uses HTTP to connect to the remote database, this makes it incompatible with some commands of Prisma Migrate, like `prisma migrate dev`. However, you can use D1's [migration system](https://developers.cloudflare.com/d1/reference/migrations/) and the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for your migration workflows. See the [Migration workflows](#migration-workflows) below for more information. +- **Making schema changes**. With a `prisma.config.ts` file and a defined schema engine adapter, you can use Prisma Migrate's normal commands, like `prisma migrate dev`. However, if you would prefer a Cloudflare first approach, you can use D1's [migration system](https://developers.cloudflare.com/d1/reference/migrations/) and the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for your migration workflows. See the [Migration workflow](#migration-workflow) and [Alternative migration workflow](#alternative-migration-workflow) sections below for more information. ## How to connect to D1 in Cloudflare Workers or Cloudflare Pages @@ -47,9 +43,37 @@ When using Prisma ORM with D1, you need to use the `sqlite` database provider an If you want to deploy a Cloudflare Worker with D1 and Prisma ORM, follow these [step-by-step instructions](/guides/cloudflare-d1). -## Migration workflows +## Migration workflow + +Our recommended migration workflow is to use a `prisma.config.ts` file to define a schema engine adapter and to use standard `prisma migrate` commands. + +### Setting up Prisma config + +First, make sure that you have [a `prisma.config.ts` file](/orm/reference/prisma-config-reference) for your project. + +Then, set up a [schema driver adapter](/orm/reference/prisma-config-reference#schemaadapter) to reference D1. + +```ts +import type { PrismaConfig } from 'prisma' + +export default { + earlyAccess: true, + schema: { + adapter: async (env: Env) => { + await import { PrismaD1 } from '@prisma/adapter-d1' + return new PrismaD1(env.DB) + }, + }, +} satisfies PrismaConfig +``` + +### Migrate your database + +You should now be able to take advantage of `prisma migrate`. Use `prisma migrate dev --name init` in order to create your first migration and apply it to your D1 database. + +## Alternative migration workflow -Cloudflare D1 comes with its own [migration system](https://developers.cloudflare.com/d1/reference/migrations/). We recommend that you use this migration system via the `wrangler d1 migrations` command to create and manage migration files on your file system. +Cloudflare D1 comes with its own [migration system](https://developers.cloudflare.com/d1/reference/migrations/). While we recommend that you use [the prisma migrate workflow](#migration-workflow), this migration system via the `wrangler d1 migrations` command is available. This command doesn't help you in figuring out the SQL statements for creating your database schema that need to be put _inside_ of these migration files though. If you want to query your database using Prisma Client, it's important that your database schema maps to your Prisma schema, this is why it's recommended to generate the SQL statements from your Prisma schema. 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 5701ff6ad7..77b7476711 100644 --- a/content/200-orm/500-reference/325-prisma-config-reference.mdx +++ b/content/200-orm/500-reference/325-prisma-config-reference.mdx @@ -79,12 +79,28 @@ Controls whether the config file is enabled. Must be set to `true` during Early Configures how Prisma locates and loads your schema file(s). See sub-options below for details. -#### `schema.kind` -- Type: `'single' | 'multi'` -- Required: Only if `schema` is specified -- Default: `'single'` +#### `schema.adapter` +- Type: `(env: Env) => Promise` +- Required: No +- Default: none -Determines whether Prisma uses a single schema file or multiple files. Use `'single'` for a traditional single `.prisma` file setup, or `'multi'` when using the `prismaSchemaFolder` preview feature to split your schema across multiple files. +A function that returns a Prisma driver adapter instance which is used by the Prisma ORM schema engine to run migrations. The function receives an `env` parameter containing environment variables and should return a Promise that resolves to a valid Prisma adapter. + +Example using the Prisma ORM D1 driver adapter: + +```ts +import type { PrismaConfig } from 'prisma' + +export default { + earlyAccess: true, + schema: { + adapter: async (env: Env) => { + await import { PrismaD1 } from '@prisma/adapter-d1' + return new PrismaD1(env.DB) + }, + }, +} satisfies PrismaConfig +``` #### `schema.filePath` - Type: `string` @@ -100,6 +116,13 @@ Specifies the path to your Prisma schema file when using a single-file setup. Specifies the path to the folder containing your Prisma schema files when using a multi-file setup. +#### `schema.kind` +- Type: `'single' | 'multi'` +- Required: Only if `schema` is specified +- Default: `'single'` + +Determines whether Prisma uses a single schema file or multiple files. Use `'single'` for a traditional single `.prisma` file setup, or `'multi'` when using the `prismaSchemaFolder` preview feature to split your schema across multiple files. + ### `studio` - Type: `object` - Required: No diff --git a/content/800-guides/070-cloudflare-d1.mdx b/content/800-guides/070-cloudflare-d1.mdx index 3a9eb201c2..9f2b974747 100644 --- a/content/800-guides/070-cloudflare-d1.mdx +++ b/content/800-guides/070-cloudflare-d1.mdx @@ -17,7 +17,7 @@ This guide shows you how to use Prisma ORM with Cloudflare D1, a serverless SQL Before starting this guide, make sure you have: - A Cloudflare account -- Node.js installed (version 16 or higher) +- Node.js installed (version 18 or higher) - Wrangler CLI installed (version 3.39.0 or higher) - Basic familiarity with Cloudflare Workers and D1 @@ -75,54 +75,35 @@ If you weren't able to grab this ID from the terminal output, you can also find ## 4. Set up database migrations -Create and apply migrations using D1's [migration system](https://developers.cloudflare.com/d1/reference/migrations/): +:::note -```terminal -# Create migration directory and file -npx wrangler d1 migrations create __YOUR_D1_DATABASE_NAME__ create_user_table -``` +We recommend using `prisma migrate` in order to keep your data in D1 migrated. However, if you would prefer to use Cloudflare's migration system, [that workflow is also available](/orm/overview/database/cloudflare-d1#alternative-migration-workflow) -Replace `__YOUR_D1_DATABASE_NAME__` with the name of your database again and, when prompted, confirm that you want to create the `migrations` directory. After having run this command, there should be a new folder called `migrations` with a file called `0001_create_user_table.sql` inside of it. +::: -You can now generate the required SQL statement for creating a `User` table that can be mapped to the `User` model in your the Prisma schema as follows: +Make sure that you have a `prisma.config.ts` file set up in the root of your project with a [schema driver adapter](/orm/reference/prisma-config-reference#schemaadapter) defined. -```terminal -# Generate SQL using Prisma Migrate -npx prisma migrate diff --from-empty --to-schema-datamodel ./prisma/schema.prisma --script --output migrations/0001_create_user_table.sql -``` +```ts +import type { PrismaConfig } from 'prisma' -Note that the resulting SQL statement is stored in a file in the `migrations` directory called `0001_create_user_table.sql` which looks as follows: - -```sql file=migrations/0001_create_user_table.sql no-copy --- CreateTable -CREATE TABLE "User" ( - "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - "email" TEXT NOT NULL, - "name" TEXT -); - --- CreateIndex -CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); +export default { + earlyAccess: true, + schema: { + adapter: async (env: Env) => { + await import { PrismaD1 } from '@prisma/adapter-d1' + return new PrismaD1(env.DB) + }, + }, +} satisfies PrismaConfig ``` -You now need to use the [`wrangler d1 migrations apply`](https://developers.cloudflare.com/workers/wrangler/commands/#migrations-apply) command to send this SQL statement to D1. Note that this command accepts two options: - -- `--local`: Executes the statement against a _local_ version of D1. This local version of D1 is a SQLite database file that'll be located in your project. This approach is useful, when you want to develop and test your Worker on your local machine. Learn more in the [Cloudflare docs](https://developers.cloudflare.com/d1/build-with-d1/local-development/). -- `--remote`: Executes the statement against your _remote_ version of D1. This version is used by your _deployed_ Cloudflare Workers. Learn more in the [Cloudflare docs](https://developers.cloudflare.com/d1/build-with-d1/remote-development/). - -In this tutorial, you'll do both: test the Worker locally _and_ deploy it afterwards. So, you need to run both commands. Open your terminal and paste the following commands: +This will allow `prisma migrate` to interact with your D1 database. You can now run `prisma migrate dev` to migrate your database to match your local schema: ```terminal -# For the local database -npx wrangler d1 migrations apply __YOUR_D1_DATABASE_NAME__ --local - -# For the remote database -npx wrangler d1 migrations apply __YOUR_D1_DATABASE_NAME__ --remote +npx prisma migrate dev --name init ``` -As before, you need to replace `__YOUR_D1_DATABASE_NAME__` with the name of your D1 database. - -Let's also create some dummy data that we can query once the Worker is running. This time, you'll run the SQL statement without storing it in a file: +Let's also create some dummy data that we can query once the Worker is running. This time, we'll use wrangler to run a SQL statement without storing it in a file: ```terminal # For the local database From f8ac572fc5a473bb286851900d6aea84815cc156 Mon Sep 17 00:00:00 2001 From: Jon Harrell <4829245+jharrell@users.noreply.github.com> Date: Thu, 27 Mar 2025 12:47:13 -0500 Subject: [PATCH 02/10] Update content/800-guides/070-cloudflare-d1.mdx --- content/800-guides/070-cloudflare-d1.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/800-guides/070-cloudflare-d1.mdx b/content/800-guides/070-cloudflare-d1.mdx index 9f2b974747..7b253a64f8 100644 --- a/content/800-guides/070-cloudflare-d1.mdx +++ b/content/800-guides/070-cloudflare-d1.mdx @@ -77,7 +77,7 @@ If you weren't able to grab this ID from the terminal output, you can also find :::note -We recommend using `prisma migrate` in order to keep your data in D1 migrated. However, if you would prefer to use Cloudflare's migration system, [that workflow is also available](/orm/overview/database/cloudflare-d1#alternative-migration-workflow) +We recommend using `prisma migrate` in order to keep your data in D1 migrated. However, if you would prefer to use Cloudflare's migration system, [that workflow is also available](/orm/overview/databases/cloudflare-d1#alternative-migration-workflow) ::: From 0b9be3cae9c0d14c91d84dd540a22426adf4e2b9 Mon Sep 17 00:00:00 2001 From: Jon Harrell <4829245+jharrell@users.noreply.github.com> Date: Thu, 27 Mar 2025 12:51:43 -0500 Subject: [PATCH 03/10] fix broken anchor --- content/200-orm/500-reference/200-prisma-cli-reference.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/200-orm/500-reference/200-prisma-cli-reference.mdx b/content/200-orm/500-reference/200-prisma-cli-reference.mdx index ee4f1d65fc..e9fda4df2e 100644 --- a/content/200-orm/500-reference/200-prisma-cli-reference.mdx +++ b/content/200-orm/500-reference/200-prisma-cli-reference.mdx @@ -1306,7 +1306,7 @@ One of the following `--from-...` options is required: | `--from-schema-datamodel` | Path to a Prisma schema file, uses the data model for the diff | | | `--from-schema-datasource` | Path to a Prisma schema file, uses the URL in the `datasource` block for the diff | | | `--from-empty` | Assume that you the data model you are migrating from is empty | | -| `--from-local-d1` | Path to a local D1 instance ([learn more](/orm/overview/databases/cloudflare-d1#migration-workflows)) | Available since [5.12.0](https://github.com/prisma/prisma/releases/tag/5.12.0) | +| `--from-local-d1` | Path to a local D1 instance ([learn more](/orm/overview/databases/cloudflare-d1#alternative-migration-workflow)) | Available since [5.12.0](https://github.com/prisma/prisma/releases/tag/5.12.0) | One of the following `--to-...` options is required: @@ -1317,7 +1317,7 @@ One of the following `--to-...` options is required: | `--to-schema-datamodel` | Path to a Prisma schema file, uses the data model for the diff | | | `--to-schema-datasource` | Path to a Prisma schema file, uses the URL in the `datasource` block for the diff | | | `--to-empty` | Assume that you the data model you are migrating to is empty | | -| `--to-local-d1` | Path to a local D1 instance ([learn more](/orm/overview/databases/cloudflare-d1#migration-workflows)) | Available since [5.12.0](https://github.com/prisma/prisma/releases/tag/5.12.0) | +| `--to-local-d1` | Path to a local D1 instance ([learn more](/orm/overview/databases/cloudflare-d1#alternative-migration-workflow)) | Available since [5.12.0](https://github.com/prisma/prisma/releases/tag/5.12.0) | Other options: From 2d538a7588ef1ffabc6cc3f372b32178c863caf0 Mon Sep 17 00:00:00 2001 From: Jon Harrell <4829245+jharrell@users.noreply.github.com> Date: Fri, 28 Mar 2025 08:48:49 -0500 Subject: [PATCH 04/10] update d1 docs with latest info --- .../500-databases/950-cloudflare-d1.mdx | 56 +++++++++++++--- content/800-guides/070-cloudflare-d1.mdx | 64 ++++++++++++++++--- 2 files changed, 101 insertions(+), 19 deletions(-) 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 1526ae3562..b57bd7649c 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 @@ -47,24 +47,62 @@ If you want to deploy a Cloudflare Worker with D1 and Prisma ORM, follow these [ Our recommended migration workflow is to use a `prisma.config.ts` file to define a schema engine adapter and to use standard `prisma migrate` commands. +### Add needed environment variables + +In order to set up the D1 adapter, you'll need to add a few secrets to a `.env` file: +- `DATABASE_URL`: A path to a SQLite database, used in local development +- `CLOUDFLARE_ACCOUNT_ID`: Your Cloudflare account ID, fetched via `npx wrangler whoami` +- `CLOUDFLARE_DATABASE_ID`: Retrieved during D1 database creation. If you have an existing D1 database, you can use `npx wrangler d1 list` and `npx wrangler d1 info ` to get the ID. +- `CLOUDFLARE_D1_TOKEN`: This API token is used by Prisma ORM to communicate with your D1 instance directly. To create this, follow these steps: + 1. Visit https://dash.cloudflare.com/profile/api-tokens + 2. Click "Create Token" + 3. Click "Custom token" template + 4. Fill out the template: Make sure you use a recognizable name and add the `Account / D1 / Edit` permission. + 5. Click "Continue to summary" and then "Create Token". + +You can then add these to your `.env` file or use them directly if they are stored in a different secret store. + +```bash file=.env +DATABASE_URL="file:./prisma/db.sqlite" + +CLOUDFLARE_ACCOUNT_ID="0773..." +CLOUDFLARE_DATABASE_ID="01f30366-..." +CLOUDFLARE_D1_TOKEN="F8Cg..." +``` + ### Setting up Prisma config First, make sure that you have [a `prisma.config.ts` file](/orm/reference/prisma-config-reference) for your project. -Then, set up a [schema driver adapter](/orm/reference/prisma-config-reference#schemaadapter) to reference D1. +Then, set up a [migration driver adapter](/orm/reference/prisma-config-reference#migrationadapter) to reference D1. ```ts -import type { PrismaConfig } from 'prisma' +import path from 'node:path' +import { defineConfig } from 'prisma/config' +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 { +export default defineConfig({ earlyAccess: true, - schema: { - adapter: async (env: Env) => { - await import { PrismaD1 } from '@prisma/adapter-d1' - return new PrismaD1(env.DB) + schema: path.join('prisma', 'schema.prisma'), + migrate: { + async adapter(env: 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 + } +}) ``` ### Migrate your database diff --git a/content/800-guides/070-cloudflare-d1.mdx b/content/800-guides/070-cloudflare-d1.mdx index 7b253a64f8..7731a1af96 100644 --- a/content/800-guides/070-cloudflare-d1.mdx +++ b/content/800-guides/070-cloudflare-d1.mdx @@ -81,23 +81,67 @@ We recommend using `prisma migrate` in order to keep your data in D1 migrated. H ::: -Make sure that you have a `prisma.config.ts` file set up in the root of your project with a [schema driver adapter](/orm/reference/prisma-config-reference#schemaadapter) defined. +### 4.1 Add needed environment variables + +In order to use the Prisma D1 adapter, you'll need to add a few secrets to a `.env` file: +- `DATABASE_URL`: A path to your local D1 instance. Usually `"file:./prisma/db.sqlite"`. +- `CLOUDFLARE_ACCOUNT_ID`: Your Cloudflare account ID, fetched via `npx wrangler whoami` +- `CLOUDFLARE_DATABASE_ID`: The ID of your database, retrieved [during D1 database creation](#3-set-up-d1-database-connection). +- `CLOUDFLARE_D1_TOKEN`: This API token is used by Prisma ORM to communicate with your D1 instance directly. To create this, follow these steps: + 1. Visit https://dash.cloudflare.com/profile/api-tokens + 2. Click "Create Token" + 3. Click "Custom token" template + 4. Fill out the template: Make sure you use a recognizable name and add the `Account / D1 / Edit` permission. + 5. Click "Continue to summary" and then "Create Token". + +You can now store these secrets to be used by Prisma ORM. We recommend a `.env` file for local development, but any secret store will work. + +```bash file=.env +DATABASE_URL="file:./prisma/db.sqlite" + +CLOUDFLARE_ACCOUNT_ID="0773..." +CLOUDFLARE_DATABASE_ID="01f30366-..." +CLOUDFLARE_D1_TOKEN="F8Cg..." +``` + +### 4.2 Configure Prisma Config + +Ensure that you have a `prisma.config.ts` file set up in the root of your project with a [migration driver adapter](/orm/reference/prisma-config-reference#migrationadapter) defined. ```ts -import type { PrismaConfig } from 'prisma' +import path from 'node:path' +import { defineConfig } from 'prisma/config' +import { PrismaD1HTTP } from '@prisma/adapter-d1' -export default { +// import your .env file +import 'dotenv/config' + +type Env = { + CLOUDFLARE_D1_TOKEN: string + CLOUDFLARE_ACCOUNT_ID: string + CLOUDFLARE_DATABASE_ID: string +} + +export default defineConfig({ earlyAccess: true, - schema: { - adapter: async (env: Env) => { - await import { PrismaD1 } from '@prisma/adapter-d1' - return new PrismaD1(env.DB) + schema: path.join('prisma', 'schema.prisma'), + migrate: { + async adapter(env: 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 + } +}) ``` -This will allow `prisma migrate` to interact with your D1 database. You can now run `prisma migrate dev` to migrate your database to match your local schema: +This will allow `prisma migrate` to interact with your D1 database. + +### 4.3 Run your first migration + +You can now run `prisma migrate dev` to migrate your database to match your local schema: ```terminal npx prisma migrate dev --name init From 56a1801822a4ece94ab87b280723629b2db79d6f Mon Sep 17 00:00:00 2001 From: Jon Harrell <4829245+jharrell@users.noreply.github.com> Date: Fri, 28 Mar 2025 09:02:54 -0500 Subject: [PATCH 05/10] update code samples --- .../500-databases/950-cloudflare-d1.mdx | 12 +++--- .../325-prisma-config-reference.mdx | 42 +++++++++++++++---- content/800-guides/070-cloudflare-d1.mdx | 12 +++--- 3 files changed, 45 insertions(+), 21 deletions(-) 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 b57bd7649c..8816fa552a 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 @@ -78,7 +78,7 @@ Then, set up a [migration driver adapter](/orm/reference/prisma-config-reference ```ts import path from 'node:path' -import { defineConfig } from 'prisma/config' +import type { PrismaConfig } from 'prisma' import { PrismaD1HTTP } from '@prisma/adapter-d1' // import your .env file @@ -90,19 +90,19 @@ type Env = { CLOUDFLARE_DATABASE_ID: string } -export default defineConfig({ +export default { earlyAccess: true, schema: path.join('prisma', 'schema.prisma'), - migrate: { - async adapter(env: Env) { + migration: { + 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 ``` ### Migrate your database 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 77b7476711..adc3b35274 100644 --- a/content/200-orm/500-reference/325-prisma-config-reference.mdx +++ b/content/200-orm/500-reference/325-prisma-config-reference.mdx @@ -48,6 +48,11 @@ export default { interface PrismaConfig { // Required while in Early Access earlyAccess: boolean + + // Optional: Configure Prisma Migrate behavior + migrate?: { + adapter?: (env: Env) => Promise + } // Optional: Configure schema location (default: ./prisma/schema.prisma) schema?: { @@ -72,14 +77,14 @@ interface PrismaConfig { Controls whether the config file is enabled. Must be set to `true` during Early Access. -### `schema` +### `migration` - Type: `object` - Required: No -- Default: `{ kind: 'single', filePath: './prisma/schema.prisma' }` +- Default: `{}` -Configures how Prisma locates and loads your schema file(s). See sub-options below for details. +Configures how Prisma Migrate communicates with your underlying database. See sub-options below for details. -#### `schema.adapter` +#### `migration.adapter` - Type: `(env: Env) => Promise` - Required: No - Default: none @@ -90,18 +95,37 @@ Example using the Prisma ORM D1 driver adapter: ```ts import type { PrismaConfig } from 'prisma' +import path from 'node:path' +import { PrismaD1HTTP } from '@prisma/adapter-d1' +import 'dotenv/config' + +type Env = { + CLOUDFLARE_D1_TOKEN: string + CLOUDFLARE_ACCOUNT_ID: string + CLOUDFLARE_DATABASE_ID: string +} export default { earlyAccess: true, - schema: { - adapter: async (env: Env) => { - await import { PrismaD1 } from '@prisma/adapter-d1' - return new PrismaD1(env.DB) + migration: { + 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 +} satisfies PrismaConfig ``` +### `schema` +- Type: `object` +- Required: No +- Default: `{ kind: 'single', filePath: './prisma/schema.prisma' }` + +Configures how Prisma ORM locates and loads your schema file(s). See sub-options below for details. + #### `schema.filePath` - Type: `string` - Required: Only when `schema.kind` is `'single'` diff --git a/content/800-guides/070-cloudflare-d1.mdx b/content/800-guides/070-cloudflare-d1.mdx index 7731a1af96..f40de14882 100644 --- a/content/800-guides/070-cloudflare-d1.mdx +++ b/content/800-guides/070-cloudflare-d1.mdx @@ -110,7 +110,7 @@ Ensure that you have a `prisma.config.ts` file set up in the root of your projec ```ts import path from 'node:path' -import { defineConfig } from 'prisma/config' +import type { PrismaConfig } from 'prisma' import { PrismaD1HTTP } from '@prisma/adapter-d1' // import your .env file @@ -122,19 +122,19 @@ type Env = { CLOUDFLARE_DATABASE_ID: string } -export default defineConfig({ +export default { earlyAccess: true, schema: path.join('prisma', 'schema.prisma'), - migrate: { - async adapter(env: Env) { + migration: { + 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 ``` This will allow `prisma migrate` to interact with your D1 database. From 097eb7405eb8ac88c000c951614f174497a518e8 Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Tue, 8 Apr 2025 14:53:21 +0200 Subject: [PATCH 06/10] update d1 and turso migreation docs --- .../050-overview/500-databases/890-neon.mdx | 18 --- .../050-overview/500-databases/900-turso.mdx | 80 +++++++++---- .../500-databases/950-cloudflare-d1.mdx | 106 ++++++++++++------ .../325-prisma-config-reference.mdx | 2 + 4 files changed, 134 insertions(+), 72 deletions(-) diff --git a/content/200-orm/050-overview/500-databases/890-neon.mdx b/content/200-orm/050-overview/500-databases/890-neon.mdx index 00d73ee239..bbbae4e2c6 100644 --- a/content/200-orm/050-overview/500-databases/890-neon.mdx +++ b/content/200-orm/050-overview/500-databases/890-neon.mdx @@ -6,32 +6,14 @@ tocDepth: 2 toc: true --- - - This guide explains how to: - [Connect Prisma ORM using Neon's connection pooling feature](#how-to-use-neons-connection-pooling) - [Resolve connection timeout issues](#resolving-connection-timeouts) - [Use Neon's serverless driver with Prisma ORM](#how-to-use-neons-serverless-driver-with-prisma-orm-preview) - - ## What is Neon? -Neon's logo - [Neon](https://neon.tech/) is a fully managed serverless PostgreSQL with a generous free tier. Neon separates storage and compute, and offers modern developer features such as serverless, branching, bottomless storage, and more. Neon is open source and written in Rust. Learn more about Neon [here](https://neon.tech/docs/introduction). 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 ac3b64b851..67f2968f57 100644 --- a/content/200-orm/050-overview/500-databases/900-turso.mdx +++ b/content/200-orm/050-overview/500-databases/900-turso.mdx @@ -13,11 +13,9 @@ Prisma ORM support for Turso is currently in [Early Access](/orm/more/releases#e [Turso](https://turso.tech/) is an edge-hosted, distributed database that's based on [libSQL](https://turso.tech/libsql), an open-source and open-contribution fork of [SQLite](https://sqlite.org/), enabling you to bring data closer to your application and minimize query latency. Turso can also be hosted on a remote server. - - +:::warning Support for Turso is available in [Early Access](/orm/more/releases#early-access) from Prisma ORM versions 5.4.2 and later. - - +::: ## Commonalities with other database providers @@ -78,7 +76,7 @@ turso db tokens create turso-prisma-db Update your `.env` file with the authentication token and connection string: -```text file=.env +```bash file=.env TURSO_AUTH_TOKEN="eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9..." TURSO_DATABASE_URL="libsql://turso-prisma-db-user.turso.io" ``` @@ -130,31 +128,73 @@ const prisma = new PrismaClient({ adapter }) You can use Prisma Client as you normally would with full type-safety in your project. -## How to manage schema changes +## Using Prisma Migrate via a driver adapter in `prisma.config.ts` (Early Access) + +As of [v6.6.0](https://pris.ly/release/6.6.0) and with a `prisma.config.ts` file, you can use `prisma db push` to make changes to your database schema. This is currently in [Early Access](/orm/more/releases#early-access). + +### 1. Install the LibSQL driver adapter + +Run this command in your terminal: -Prisma Migrate and Introspection workflows are currently not supported when working with Turso. This is because Turso uses HTTP to connect to your database, which Prisma Migrate doesn't support. +```termina +npm install @prisma/adapter-libsql +``` + +### 2. Set environment variables + +In order to set up the LibSQL adapter, you'll need to add a few secrets to a `.env` file: -To update your database schema: +- `LIBSQL_DATABASE_URL`: The connection URL of your Turso database instance. +- `LIBSQL_DATABASE_TOKEN`: The token of your Turso database instance. -1. Generate a migration file using `prisma migrate dev` against a local SQLite database: +You can then add these to your `.env` file or use them directly if they are stored in a different secret store: - ```terminal - npx prisma migrate dev --name init - ``` +```bash file=.env +LIBSQL_DATABASE_URL="..." +LIBSQL_DATABASE_TOKEN="..." +``` -2. Apply the migration using Turso's CLI: +### 3. Set up Prisma Config file - ```terminal - turso db shell turso-prisma-db < ./prisma/migrations/20230922132717_init/migration.sql - ``` +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#migrationadapter) to use `PrismaLibSQL`: - :::info +```ts file=prisma.config.ts +import path from 'node:path' +import { defineConfig } from 'prisma/config' +import { PrismaLibSQL } from '@prisma/adapter-libsql' - Replace `20230922132717_init` with the name of your migration. +// import your .env file +import 'dotenv/config' - ::: +type Env = { + LIBSQL_DATABASE_URL: string + LIBSQL_DATABASE_TOKEN: string +} -For subsequent migrations, repeat the above steps to apply changes to your database. This workflow does not support track the history of applied migrations to your remote database. +export default defineConfig({ + earlyAccess: true, + schema: path.join('prisma', 'schema.prisma'), + + migrate: { + async adapter(env) { + return new PrismaLibSQL({ + url: env.LIBSQL_DATABASE_URL, + authToken: env.LIBSQL_DATABASE_TOKEN, + }) + } + } +}) +``` + +### 4. Migrate your database + +Prisma Migrate now will run migrations against your remote Turso database based on the configuration provided in `prisma.config.ts`. + +To create your first migration with this workflow, run the following command: + +```terminal +npx prisma migrate dev --name init +``` ## Embedded Turso database replicas 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 8816fa552a..e6a21b8fc4 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 @@ -34,8 +34,9 @@ Many aspects of using Prisma ORM with D1 are just like using Prisma ORM with any There are a number of differences between D1 and SQLite to consider. You should be aware of the following when deciding to use D1 and Prisma ORM: + +- **Making schema changes**. As of [v6.6.0](https://pris.ly/release/6.6.0) and with a `prisma.config.ts` file, you can use `prisma db push`. However, if you prefer a Cloudflare first approach, you can use D1's [migration system](https://developers.cloudflare.com/d1/reference/migrations/) and the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for your migration workflows. See the [Migration workflow](#migration-workflow) and [Alternative migration workflow](#alternative-migration-workflow) sections below for more information. - **Local and remote D1 (SQLite) databases**. Cloudflare provides local and remote versions of D1. The [local](https://developers.cloudflare.com/d1/build-with-d1/local-development/) version is managed using the `--local` option of the `wrangler d1` CLI and is located in `.wrangler/state`. The [remote](https://developers.cloudflare.com/d1/build-with-d1/remote-development/) version is managed by Cloudflare and is accessed via HTTP. -- **Making schema changes**. With a `prisma.config.ts` file and a defined schema engine adapter, you can use Prisma Migrate's normal commands, like `prisma migrate dev`. However, if you would prefer a Cloudflare first approach, you can use D1's [migration system](https://developers.cloudflare.com/d1/reference/migrations/) and the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for your migration workflows. See the [Migration workflow](#migration-workflow) and [Alternative migration workflow](#alternative-migration-workflow) sections below for more information. ## How to connect to D1 in Cloudflare Workers or Cloudflare Pages @@ -43,40 +44,54 @@ When using Prisma ORM with D1, you need to use the `sqlite` database provider an If you want to deploy a Cloudflare Worker with D1 and Prisma ORM, follow these [step-by-step instructions](/guides/cloudflare-d1). -## Migration workflow +## Schema migrations with Prisma ORM on D1 -Our recommended migration workflow is to use a `prisma.config.ts` file to define a schema engine adapter and to use standard `prisma migrate` commands. +You can use two approaches for migrating your database schema with Prisma ORM and D1: +- Using `prisma db push` via a driver adapter in `prisma.config.ts` +- Using the Wrangler CLI -### Add needed environment variables +### Using Prisma Migrate via a driver adapter in `prisma.config.ts` (Early Access) + +:::warning + +This approach has been introduced in [Early Access](/orm/more/releases#early-access) in [v6.6.0](https://pris.ly/release/6.6.0); other commands like `prisma migrate dev` will be added soon. + +::: + +#### 1. Install the Prisma D1 driver adapter + +Run this command in your terminal: + +```terminal +npm install @prisma/adapter-d1 +``` + +#### 2. Set environment variables In order to set up the D1 adapter, you'll need to add a few secrets to a `.env` file: -- `DATABASE_URL`: A path to a SQLite database, used in local development -- `CLOUDFLARE_ACCOUNT_ID`: Your Cloudflare account ID, fetched via `npx wrangler whoami` + +- `CLOUDFLARE_ACCOUNT_ID`: Your Cloudflare account ID, fetched via `npx wrangler whoami`. - `CLOUDFLARE_DATABASE_ID`: Retrieved during D1 database creation. If you have an existing D1 database, you can use `npx wrangler d1 list` and `npx wrangler d1 info ` to get the ID. -- `CLOUDFLARE_D1_TOKEN`: This API token is used by Prisma ORM to communicate with your D1 instance directly. To create this, follow these steps: +- `CLOUDFLARE_D1_TOKEN`: This API token is used by Prisma ORM to communicate with your D1 instance directly. To create it, follow these steps: 1. Visit https://dash.cloudflare.com/profile/api-tokens - 2. Click "Create Token" - 3. Click "Custom token" template - 4. Fill out the template: Make sure you use a recognizable name and add the `Account / D1 / Edit` permission. - 5. Click "Continue to summary" and then "Create Token". + 2. Click **Create Token** + 3. Click **Custom token** template + 4. Fill out the template: Make sure you use a recognizable name and add the **Account / D1 / Edit** permission. + 5. Click **Continue to summary** and then **Create Token**. -You can then add these to your `.env` file or use them directly if they are stored in a different secret store. +You can then add these to your `.env` file or use them directly if they are stored in a different secret store: ```bash file=.env -DATABASE_URL="file:./prisma/db.sqlite" - CLOUDFLARE_ACCOUNT_ID="0773..." CLOUDFLARE_DATABASE_ID="01f30366-..." CLOUDFLARE_D1_TOKEN="F8Cg..." ``` -### Setting up Prisma config +#### 3. Set up Prisma Config file -First, make sure that you have [a `prisma.config.ts` file](/orm/reference/prisma-config-reference) for your project. +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#migrationadapter) to reference D1: -Then, set up a [migration driver adapter](/orm/reference/prisma-config-reference#migrationadapter) to reference D1. - -```ts +```ts file=prisma.config.ts import path from 'node:path' import type { PrismaConfig } from 'prisma' import { PrismaD1HTTP } from '@prisma/adapter-d1' @@ -93,7 +108,9 @@ type Env = { export default { earlyAccess: true, schema: path.join('prisma', 'schema.prisma'), - migration: { + + // add-start + migrate: { async adapter(env) { return new PrismaD1HTTP({ CLOUDFLARE_D1_TOKEN: env.CLOUDFLARE_D1_TOKEN, @@ -102,26 +119,43 @@ export default { }) }, }, + // add-end } satisfies PrismaConfig ``` -### Migrate your database +#### 4. Migrate your database -You should now be able to take advantage of `prisma migrate`. Use `prisma migrate dev --name init` in order to create your first migration and apply it to your D1 database. +Prisma Migrate now will run migrations against your remote D1 database based on the configuration provided in `prisma.config.ts`. -## Alternative migration workflow +To update the remote schema with this workflow, run the following command: -Cloudflare D1 comes with its own [migration system](https://developers.cloudflare.com/d1/reference/migrations/). While we recommend that you use [the prisma migrate workflow](#migration-workflow), this migration system via the `wrangler d1 migrations` command is available. +```terminal +npx prisma db push +``` + +:::note + +Note that for querying the database, you keep using the `PrismaD1` driver adapter from the `@prisma/adapter-d1` package: + +```ts +import { PrismaD1HTTP } from '@prisma/adapter-d1' +``` + +::: + +### Using the Wrangler CLI + +Cloudflare D1 comes with its own [migration system](https://developers.cloudflare.com/d1/reference/migrations/). While we recommend that you use the [native Prisma Migrate workflow](##using-prisma-migrate-via-a-driver-adapter-in-prismaconfigts), this migration system via the `wrangler d1 migrations` command is available. This command doesn't help you in figuring out the SQL statements for creating your database schema that need to be put _inside_ of these migration files though. If you want to query your database using Prisma Client, it's important that your database schema maps to your Prisma schema, this is why it's recommended to generate the SQL statements from your Prisma schema. When using D1, you can use the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for that purpose. -### Creating an initial migration +#### Creating an initial migration The workflow for creating an initial migration looks as follows. Assume you have a fresh D1 instance without any tables. -#### 1. Update your Prisma data model +##### 1. Update your Prisma data model This is your initial version of the Prisma schema that you want to map to your D1 instance: @@ -133,7 +167,7 @@ model User { } ``` -#### 2. Create migration file using `wrangler` CLI +##### 2. Create migration file using `wrangler` CLI Next, you need to create the migration file using the [`wrangler d1 migrations create`](https://developers.cloudflare.com/workers/wrangler/commands/#migrations-create) command: @@ -152,7 +186,7 @@ migrations/ However, before you can apply the migration to your D1 instance, you actually need to put a SQL statement into the currently empty `0001_create_user_table.sql` file. -#### 3. Generate SQL statements using `prisma migrate diff` +##### 3. Generate SQL statements using `prisma migrate diff` To generate the initial SQL statement, you can use the `prisma migrate diff` command which compares to _schemas_ (via its `--to-X` and `--from-X` options) and generates the steps that are needed to "evolve" from one to the other. These schemas can be either Prisma or SQL schemas. @@ -187,7 +221,7 @@ CREATE TABLE "User" ( CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); ``` -#### 4. Execute the migration using `wrangler d1 migrations apply` +##### 4. Execute the migration using `wrangler d1 migrations apply` Finally, you can apply the migration against your D1 instances. @@ -203,11 +237,11 @@ For the **remote** instance, run: npx wrangler d1 migrations apply __YOUR_DATABASE_NAME__ --remote ``` -### Evolve your schema with further migrations +#### Evolve your schema with further migrations For any further migrations, you can use the same workflow but instead of using `--from-empty`, you'll need to use `--from-local-d1` because your source schema for the `prisma migrate diff` command now is the current schema of that local D1 instance, while the target remains your (then updated) Prisma schema. -#### 1. Update your Prisma data model +##### 1. Update your Prisma data model Assume you have updated your Prisma schema with another model: @@ -227,7 +261,7 @@ model Post { } ``` -#### 2. Create migration file using `wrangler` CLI +##### 2. Create migration file using `wrangler` CLI Like before, you first need to create the migration file: @@ -245,7 +279,7 @@ migrations/ As before, you now need to put a SQL statement into the currently empty `0002_create_post_table.sql` file. -#### 3. Generate SQL statements using `prisma migrate diff` +##### 3. Generate SQL statements using `prisma migrate diff` As explained above, you now need to use `--from-local-d1` instead of `--from-empty` to specify a source schema: @@ -276,7 +310,7 @@ CREATE TABLE "Post" ( ); ``` -#### 4. Execute the migration using `wrangler d1 migrations apply` +##### 4. Execute the migration using `wrangler d1 migrations apply` Finally, you can apply the migration against your D1 instances. @@ -297,3 +331,7 @@ npx wrangler d1 migrations apply __YOUR_DATABASE_NAME__ --remote ### Transactions not supported Cloudflare D1 currently does not support transactions (see the [open feature request](https://github.com/cloudflare/workers-sdk/issues/2733)). As a result, Prisma ORM does not support transactions for Cloudflare D1. When using Prisma's D1 adapter, implicit & explicit transactions will be ignored and run as individual queries, which breaks the guarantees of the ACID properties of transactions. + +### Prisma Migrate only supports remote D1 databases + +The Wrangler CLI can distinguish between local and remote D1 (i.e. SQLite) database instances via the `--local` and `--remote` options. This distinction is currently not available with the [native Prisma Migrate workflow](#using-prisma-migrate-via-a-driver-adapter-in-prismaconfigts). \ No newline at end of file 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 adc3b35274..2ce81f44f8 100644 --- a/content/200-orm/500-reference/325-prisma-config-reference.mdx +++ b/content/200-orm/500-reference/325-prisma-config-reference.mdx @@ -17,6 +17,7 @@ This feature is currently in [Early Access](/orm/more/releases#early-access) and 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: Using the `defineConfig` helper: + ```ts import { defineConfig } from 'prisma/config' @@ -30,6 +31,7 @@ export default defineConfig({ ``` Or using TypeScript's `satisfies` operator with the `PrismaConfig` type: + ```ts import type { PrismaConfig } from 'prisma' From 4c8cf6700d70972bf9625c6425492777e498a676 Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Tue, 8 Apr 2025 14:57:50 +0200 Subject: [PATCH 07/10] update d1 and turso migreation docs --- .../050-overview/500-databases/900-turso.mdx | 16 ++++++++++++++-- .../500-databases/950-cloudflare-d1.mdx | 8 +++++++- 2 files changed, 21 insertions(+), 3 deletions(-) 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 67f2968f57..ca730886ba 100644 --- a/content/200-orm/050-overview/500-databases/900-turso.mdx +++ b/content/200-orm/050-overview/500-databases/900-turso.mdx @@ -130,7 +130,19 @@ You can use Prisma Client as you normally would with full type-safety in your pr ## Using Prisma Migrate via a driver adapter in `prisma.config.ts` (Early Access) -As of [v6.6.0](https://pris.ly/release/6.6.0) and with a `prisma.config.ts` file, you can use `prisma db push` to make changes to your database schema. This is currently in [Early Access](/orm/more/releases#early-access). +As of [v6.6.0](https://pris.ly/release/6.6.0) and with a `prisma.config.ts` file, you can use `prisma db push` to make changes to your database schema. + +:::warning + +This functionality has been introduced in [Early Access](/orm/more/releases#early-access) in [v6.6.0](https://pris.ly/release/6.6.0) and supports the following commands: + +- `prisma db push` +- `prisma db pull` +- `prisma migrate diff` + +Other commands like `prisma migrate dev` and `prisma migrate deploy` will be added soon. + +::: ### 1. Install the LibSQL driver adapter @@ -193,7 +205,7 @@ Prisma Migrate now will run migrations against your remote Turso database based To create your first migration with this workflow, run the following command: ```terminal -npx prisma migrate dev --name init +npx prisma db push ``` ## Embedded Turso database replicas 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 e6a21b8fc4..09a956bc74 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 @@ -54,7 +54,13 @@ You can use two approaches for migrating your database schema with Prisma ORM an :::warning -This approach has been introduced in [Early Access](/orm/more/releases#early-access) in [v6.6.0](https://pris.ly/release/6.6.0); other commands like `prisma migrate dev` will be added soon. +This functionality has been introduced in [Early Access](/orm/more/releases#early-access) in [v6.6.0](https://pris.ly/release/6.6.0) and supports the following commands: + +- `prisma db push` +- `prisma db pull` +- `prisma migrate diff` + +Other commands like `prisma migrate dev` and `prisma migrate deploy` will be added soon. ::: From e6fbe3576c84d2909c15073f237377f073166e8c Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Tue, 8 Apr 2025 15:08:11 +0200 Subject: [PATCH 08/10] fix broken anchors --- .../200-orm/050-overview/500-databases/950-cloudflare-d1.mdx | 4 ++-- content/200-orm/500-reference/200-prisma-cli-reference.mdx | 4 ++-- content/800-guides/070-cloudflare-d1.mdx | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) 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 09a956bc74..6c800724d4 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 @@ -34,8 +34,8 @@ Many aspects of using Prisma ORM with D1 are just like using Prisma ORM with any There are a number of differences between D1 and SQLite to consider. You should be aware of the following when deciding to use D1 and Prisma ORM: - -- **Making schema changes**. As of [v6.6.0](https://pris.ly/release/6.6.0) and with a `prisma.config.ts` file, you can use `prisma db push`. However, if you prefer a Cloudflare first approach, you can use D1's [migration system](https://developers.cloudflare.com/d1/reference/migrations/) and the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for your migration workflows. See the [Migration workflow](#migration-workflow) and [Alternative migration workflow](#alternative-migration-workflow) sections below for more information. + +- **Making schema changes**. As of [v6.6.0](https://pris.ly/release/6.6.0) and with a `prisma.config.ts` file, you can use `prisma db push`. However, if you prefer a Cloudflare first approach, you can use D1's [migration system](https://developers.cloudflare.com/d1/reference/migrations/) and the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for your migration workflows. See the [Migration workflow](#migration-workflow) and [Alternative migration workflow](#using-the-wrangler-cli) sections below for more information. - **Local and remote D1 (SQLite) databases**. Cloudflare provides local and remote versions of D1. The [local](https://developers.cloudflare.com/d1/build-with-d1/local-development/) version is managed using the `--local` option of the `wrangler d1` CLI and is located in `.wrangler/state`. The [remote](https://developers.cloudflare.com/d1/build-with-d1/remote-development/) version is managed by Cloudflare and is accessed via HTTP. ## How to connect to D1 in Cloudflare Workers or Cloudflare Pages diff --git a/content/200-orm/500-reference/200-prisma-cli-reference.mdx b/content/200-orm/500-reference/200-prisma-cli-reference.mdx index 805d2cfbd6..f1a6678397 100644 --- a/content/200-orm/500-reference/200-prisma-cli-reference.mdx +++ b/content/200-orm/500-reference/200-prisma-cli-reference.mdx @@ -1291,7 +1291,7 @@ One of the following `--from-...` options is required: | `--from-schema-datamodel` | Path to a Prisma schema file, uses the data model for the diff | | | `--from-schema-datasource` | Path to a Prisma schema file, uses the URL in the `datasource` block for the diff | | | `--from-empty` | Assume that you the data model you are migrating from is empty | | -| `--from-local-d1` | Path to a local D1 instance ([learn more](/orm/overview/databases/cloudflare-d1#alternative-migration-workflow)) | Available since [5.12.0](https://github.com/prisma/prisma/releases/tag/5.12.0) | +| `--from-local-d1` | Path to a local D1 instance ([learn more](/orm/overview/databases/cloudflare-d1#using-the-wrangler-cli)) | Available since [5.12.0](https://github.com/prisma/prisma/releases/tag/5.12.0) | One of the following `--to-...` options is required: @@ -1302,7 +1302,7 @@ One of the following `--to-...` options is required: | `--to-schema-datamodel` | Path to a Prisma schema file, uses the data model for the diff | | | `--to-schema-datasource` | Path to a Prisma schema file, uses the URL in the `datasource` block for the diff | | | `--to-empty` | Assume that you the data model you are migrating to is empty | | -| `--to-local-d1` | Path to a local D1 instance ([learn more](/orm/overview/databases/cloudflare-d1#alternative-migration-workflow)) | Available since [5.12.0](https://github.com/prisma/prisma/releases/tag/5.12.0) | +| `--to-local-d1` | Path to a local D1 instance ([learn more](/orm/overview/databases/cloudflare-d1#using-the-wrangler-cli)) | Available since [5.12.0](https://github.com/prisma/prisma/releases/tag/5.12.0) | Other options: diff --git a/content/800-guides/070-cloudflare-d1.mdx b/content/800-guides/070-cloudflare-d1.mdx index f40de14882..21da5b81b0 100644 --- a/content/800-guides/070-cloudflare-d1.mdx +++ b/content/800-guides/070-cloudflare-d1.mdx @@ -77,7 +77,7 @@ If you weren't able to grab this ID from the terminal output, you can also find :::note -We recommend using `prisma migrate` in order to keep your data in D1 migrated. However, if you would prefer to use Cloudflare's migration system, [that workflow is also available](/orm/overview/databases/cloudflare-d1#alternative-migration-workflow) +We recommend using `prisma migrate` in order to keep your data in D1 migrated. However, if you would prefer to use Cloudflare's migration system, [that workflow is also available](/orm/overview/databases/cloudflare-d1#using-the-wrangler-cli) ::: From 12b543fa5c2faee252deaa063bc01d8e4db38904 Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Tue, 8 Apr 2025 15:13:34 +0200 Subject: [PATCH 09/10] fix broken anchors --- .../200-orm/050-overview/500-databases/950-cloudflare-d1.mdx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 6c800724d4..553fcd4dd3 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 @@ -34,8 +34,7 @@ Many aspects of using Prisma ORM with D1 are just like using Prisma ORM with any There are a number of differences between D1 and SQLite to consider. You should be aware of the following when deciding to use D1 and Prisma ORM: - -- **Making schema changes**. As of [v6.6.0](https://pris.ly/release/6.6.0) and with a `prisma.config.ts` file, you can use `prisma db push`. However, if you prefer a Cloudflare first approach, you can use D1's [migration system](https://developers.cloudflare.com/d1/reference/migrations/) and the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for your migration workflows. See the [Migration workflow](#migration-workflow) and [Alternative migration workflow](#using-the-wrangler-cli) sections below for more information. +- **Making schema changes**. As of [v6.6.0](https://pris.ly/release/6.6.0) and with a `prisma.config.ts` file, you can use `prisma db push`. However, if you prefer a Cloudflare first approach, you can use D1's [migration system](https://developers.cloudflare.com/d1/reference/migrations/) and the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for your migration workflows. See the [Schema migrations with Prisma ORM on D1](#schema-migrations-with-prisma-orm-on-d1) section below for more information. - **Local and remote D1 (SQLite) databases**. Cloudflare provides local and remote versions of D1. The [local](https://developers.cloudflare.com/d1/build-with-d1/local-development/) version is managed using the `--local` option of the `wrangler d1` CLI and is located in `.wrangler/state`. The [remote](https://developers.cloudflare.com/d1/build-with-d1/remote-development/) version is managed by Cloudflare and is accessed via HTTP. ## How to connect to D1 in Cloudflare Workers or Cloudflare Pages From 2405eccfbb6a6f68ceb6f31dbca8becd60bf93d5 Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Tue, 8 Apr 2025 15:27:49 +0200 Subject: [PATCH 10/10] fix broken anchors --- .../200-orm/050-overview/500-databases/950-cloudflare-d1.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 553fcd4dd3..1ec1226f88 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 @@ -150,7 +150,7 @@ import { PrismaD1HTTP } from '@prisma/adapter-d1' ### Using the Wrangler CLI -Cloudflare D1 comes with its own [migration system](https://developers.cloudflare.com/d1/reference/migrations/). While we recommend that you use the [native Prisma Migrate workflow](##using-prisma-migrate-via-a-driver-adapter-in-prismaconfigts), this migration system via the `wrangler d1 migrations` command is available. +Cloudflare D1 comes with its own [migration system](https://developers.cloudflare.com/d1/reference/migrations/). While we recommend that you use the [native Prisma Migrate workflow](#using-prisma-migrate-via-a-driver-adapter-in-prismaconfigts-early-access), this migration system via the `wrangler d1 migrations` command is available. This command doesn't help you in figuring out the SQL statements for creating your database schema that need to be put _inside_ of these migration files though. If you want to query your database using Prisma Client, it's important that your database schema maps to your Prisma schema, this is why it's recommended to generate the SQL statements from your Prisma schema. @@ -339,4 +339,4 @@ Cloudflare D1 currently does not support transactions (see the [open feature req ### Prisma Migrate only supports remote D1 databases -The Wrangler CLI can distinguish between local and remote D1 (i.e. SQLite) database instances via the `--local` and `--remote` options. This distinction is currently not available with the [native Prisma Migrate workflow](#using-prisma-migrate-via-a-driver-adapter-in-prismaconfigts). \ No newline at end of file +The Wrangler CLI can distinguish between local and remote D1 (i.e. SQLite) database instances via the `--local` and `--remote` options. This distinction is currently not available with the [native Prisma Migrate workflow](#using-prisma-migrate-via-a-driver-adapter-in-prismaconfigts-early-access). \ No newline at end of file