Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c737d9d
update query compiler docs
nikolasburk Jun 10, 2025
4913ed7
update query compiler docs
nikolasburk Jun 10, 2025
42ae700
add docs about manual backups
nikolasburk Jun 10, 2025
f269143
local development updates
nikolasburk Jun 10, 2025
4ca19c0
update vs code extension docs
nikolasburk Jun 10, 2025
42e8e43
add shard key docs for planetscale
nikolasburk Jun 11, 2025
2b88406
document remote mcp server
nikolasburk Jun 11, 2025
7dee3ee
add usage docs for remote mcp server
nikolasburk Jun 11, 2025
968c672
remove pg-worker mentions
nikolasburk Jun 11, 2025
d0ca539
Merge branch 'main' into docs/6.10.0
nikolasburk Jun 12, 2025
b8a0a49
remove note about manual backups
nikolasburk Jun 12, 2025
02a54cd
Merge branch 'docs/6.10.0' of github.com:prisma/docs into docs/6.10.0
nikolasburk Jun 12, 2025
72d7ca0
update imports for new prisma-client generator
nikolasburk Jun 13, 2025
c192a46
add import notes for models and enums for new generator
nikolasburk Jun 13, 2025
f15348b
add note about cf ai playground to test the remote mcp server
nikolasburk Jun 13, 2025
b2e263a
add note about listing tools
nikolasburk Jun 13, 2025
4f9c51c
add sample prompts
nikolasburk Jun 13, 2025
d50729c
add note about preview feature for shard keys
nikolasburk Jun 16, 2025
3300c8c
update preview features and mssql driver adapter docs
nikolasburk Jun 16, 2025
0091b25
add note about push to cloud
nikolasburk Jun 16, 2025
7d9b834
Add Warp to the MCP server guide (#6961)
bholmesdev Jun 17, 2025
5642f29
update prisma dev output
nikolasburk Jun 17, 2025
d60a8f4
Merge branch 'docs/6.10.0' of github.com:prisma/docs into docs/6.10.0
nikolasburk Jun 17, 2025
77937c2
Merge branch 'main' of github.com:prisma/docs into docs/6.10.0
nikolasburk Jun 17, 2025
17fc0f3
updae cspell.json
nikolasburk Jun 17, 2025
16ab033
fix typo
nikolasburk Jun 17, 2025
b80ab30
add planetscale to qc page
nikolasburk Jun 17, 2025
a20a496
fix broken console links
nikolasburk Jun 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions content/200-orm/050-overview/500-databases/850-planetscale.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ tocDepth: 3
toc: true
---

<TopBlock>

Prisma and [PlanetScale](https://planetscale.com/) together provide a development arena that optimizes rapid, type-safe development of data access applications, using Prisma's ORM and PlanetScale's highly scalable MySQL-based platform.

This document discusses the concepts behind using Prisma ORM and PlanetScale, explains the commonalities and differences between PlanetScale and other database providers, and leads you through the process for configuring your application to integrate with PlanetScale.

</TopBlock>

## What is PlanetScale?

PlanetScale uses the [Vitess](https://vitess.io/) database clustering system to provide a MySQL-compatible database platform. Features include:
Expand Down Expand Up @@ -71,7 +67,7 @@ In Prisma ORM versions 3.1.1 and later, you can [emulate relations in Prisma Cli

To enable emulation of relations in Prisma Client, set the `relationMode` field to `"prisma"` in the `datasource` block:

```prisma file=schema.prisma showLineNumbers
```prisma file=schema.prisma
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
Expand All @@ -91,7 +87,7 @@ If you use relations in your Prisma schema with the default `"foreignKeys"` opti

When [you emulate relations in Prisma Client](#option-1-emulate-relations-in-prisma-client), you need to create your own indexes. As an example of a situation where you would want to add an index, take this schema for a blog with posts and comments:

```prisma file=schema.prisma showLineNumbers
```prisma file=schema.prisma
model Post {
id Int @id @default(autoincrement())
title String
Expand All @@ -112,7 +108,7 @@ The `postId` field in the `Comment` model refers to the corresponding `id` field

To avoid this, you can define an index on the `postId` field using [Prisma ORM's `@@index` argument](/orm/reference/prisma-schema-reference#index):

```prisma file=schema.prisma highlight=15;add showLineNumbers
```prisma file=schema.prisma highlight=15;add
model Post {
id Int @id @default(autoincrement())
title String
Expand Down Expand Up @@ -150,7 +146,7 @@ You can then use Prisma ORM and define relations in your Prisma schema without t

In that case, you can define a relation as with other database that supports foreign key constraints, for example:

```prisma file=schema.prisma showLineNumbers
```prisma file=schema.prisma
model Post {
id Int @id @default(autoincrement())
title String
Expand Down Expand Up @@ -182,7 +178,7 @@ As an example, let's say you decide to decide to add a new `excerpt` field to th

Next, add the following to your `schema.prisma` file:

```prisma file=schema.prisma highlight=5;edit showLineNumbers
```prisma file=schema.prisma highlight=5;edit
model Post {
id Int @id @default(autoincrement())
title String
Expand Down Expand Up @@ -213,13 +209,13 @@ Once you are happy with your changes on your development branch, you can open a

For more examples, see PlanetScale's tutorial on [automatic migrations with Prisma ORM](https://planetscale.com/docs/prisma/automatic-prisma-migrations) using `db push`.

## How to add in missing relations after Introspection
## How to add in missing relations after introspection

> **Note**: This section is only relevant if you use `relationMode = "prisma"` to emulate foreign key constraints with Prisma ORM. If you enabled foreign key constraints in your PlanetScale database, you can ignore this section.

After introspecting with `npx prisma db pull`, the schema you get may be missing some relations. For example, the following schema is missing a relation between the `User` and `Post` models:

```prisma file=schema.prisma showLineNumbers
```prisma file=schema.prisma
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
Expand All @@ -239,7 +235,7 @@ model User {

In this case you need to add the relation in manually:

```prisma file=schema.prisma highlight=6,16;add showLineNumbers
```prisma file=schema.prisma highlight=6,16;add
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
Expand All @@ -263,6 +259,30 @@ model User {

For a more detailed example, see the [Getting Started guide for PlanetScale](/getting-started/setup-prisma/add-to-existing-project/relational-databases/introspection-typescript-planetscale).

## How to define shard keys in your Prisma schema

[Sharding](https://planetscale.com/docs/vitess/sharding) is a popular technique to scale up when database load grows. As of [v6.10.0](https://github.com/prisma/prisma/releases/tag/6.10.0), Prisma ORM supports sharding on PlanetScale natively via the [`@shardKey`](/orm/reference/prisma-schema-reference#shardkey) and [`@@shardKey`](/orm/reference/prisma-schema-reference#shardkey-1) attributes in the Prisma schema which you can apply to the fields in your models that should serve as shard keys in your database setup.

**Single-column shard key**

```prisma
model User {
id String @default(uuid())
region String @shardKey
}
```

**Multi-column shard key**

```prisma
model User {
id String @default(uuid())
country String
customerId String
@@shardKey([country, customerId])
}
```

## How to use the PlanetScale serverless driver with Prisma ORM (Preview)

The [PlanetScale serverless driver](https://planetscale.com/docs/tutorials/planetscale-serverless-driver) provides a way of communicating with your database and executing queries over HTTP.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metaDescription: "Learn how to use Prisma ORM without Rust engines"
sidebar_class_name: preview-badge
---

As of [v6.7.0](https://pris.ly/release/6.7.0), you can use Prisma ORM without [Rust engine](/orm/more/under-the-hood/engines) binaries on PostgreSQL, SQLite and D1 databases.
As of [v6.7.0](https://pris.ly/release/6.7.0), you can use Prisma ORM without [Rust engine](/orm/more/under-the-hood/engines) binaries on PostgreSQL, SQLite, D1, MySQL, PlanetScale & MSSQL databases.

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

Expand Down Expand Up @@ -47,51 +47,64 @@ npx prisma generate

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

For **PostgreSQL**:

<TabbedContent code>
<TabItem value="PostgreSQL">
```terminal
npm install @prisma/adapter-pg
```

For **SQLite**:

</TabItem>
<TabItem value="SQLite">
```terminal
npm install @prisma/adapter-better-sqlite3
```

For **D1**:

</TabItem>
<TabItem value="D1">
```terminal
npm install @prisma/adapter-d1
```
</TabItem>
<TabItem value="MySQL">
```terminal
npm install @prisma/adapter-mysql2
```
</TabItem>
<TabItem value="PlanetScale">
```terminal
npm install @prisma/adapter-planetscale
```
</TabItem>
<TabItem value="MSSQL">
```terminal
npm install @prisma/adapter-tedious
```
</TabItem>
</TabbedContent>

### 4. Instantiate Prisma Client

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

For **PostgreSQL**:

```ts
<TabbedContent code>
<TabItem value="PostgreSQL">
```typescript
import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from './generated/prisma'

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

For **SQLite**:

```ts
</TabItem>
<TabItem value="SQLite">
```typescript
import { PrismaBetterSQLite3 } from '@prisma/adapter-better-sqlite3';
import { PrismaClient } from './generated/prisma';

const adapter = new PrismaBetterSQLite3({ url: process.env.DATABASE_URL })
const prisma = new PrismaClient({ adapter });
```

For **D1**:

```ts
</TabItem>
<TabItem value="D1">
```typescript
import { PrismaClient } from '@prisma/client'
import { PrismaD1 } from '@prisma/adapter-d1'

Expand All @@ -112,6 +125,57 @@ export default {
},
}
```
</TabItem>
<TabItem value="MySQL">
```typescript
import { PrismaMySQL2 } from '@prisma/adapter-mysql2';
import { PrismaClient } from './generated/prisma';

const adapter = new PrismaMySQL2({
host: process.env.HOST,
user: process.env.USER,
database: process.env.DB_NAME,
})
const prisma = new PrismaClient({ adapter });
```
</TabItem>
<TabItem value="PlanetScale">
```typescript
import { PrismaPlanetScale } from '@prisma/adapter-planetscale'
import { PrismaClient } from '@prisma/client'
import { fetch as undiciFetch } from 'undici'

const adapter = new PrismaPlanetScale({ url: process.env.DATABASE_URL, fetch: undiciFetch })
const prisma = new PrismaClient({ adapter })
```
</TabItem>
<TabItem value="MSSQL">
```typescript
import { PrismaMSSQL } from '@prisma/adapter-mssql';
import { PrismaClient } from './generated/prisma';

const sqlConfig = {
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
server: process.env.HOST,
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000
},
options: {
encrypt: true, // for azure
trustServerCertificate: false // change to true for local dev / self-signed certs
}
}

const adapter = new PrismaMSSQL(sqlConfig)
const prisma = new PrismaClient({ adapter });
```
</TabItem>
</TabbedContent>


### 5. Query your database

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ This command requires you to be authenticated, and will ask you to log in to you

Cloudflare has a [size limit of 3 MB for Workers on the free plan](https://developers.cloudflare.com/workers/platform/limits/). If your application bundle with Prisma ORM exceeds that size, we recommend upgrading to a paid Worker plan or using Prisma Accelerate to deploy your application.

If you're running into this problem with `pg` and the `@prisma/adapter-pg` package, you can replace the `pg` with the custom [`@prisma/pg-worker`](https://github.com/prisma/prisma/tree/main/packages/pg-worker) package and use the [`@prisma/adapter-pg-worker`](https://github.com/prisma/prisma/tree/main/packages/adapter-pg-worker) adapter that belongs to it.

`@prisma/pg-worker` is an optimized and lightweight version of `pg` that is designed to be used in a Worker. It is a drop-in replacement for `pg` and is fully compatible with Prisma ORM.

### Deploying a Next.js app to Cloudflare Pages with `@cloudflare/next-on-pages`

Cloudflare offers an option to run Next.js apps on Cloudflare Pages with [`@cloudflare/next-on-pages`](https://github.com/cloudflare/next-on-pages), see the [docs](https://developers.cloudflare.com/pages/framework-guides/nextjs/ssr/get-started/) for instructions.
Expand Down Expand Up @@ -216,10 +212,6 @@ If you are using a traditional PostgreSQL database that's accessed via TCP and t
- use the `@prisma/adapter-pg` database adapter (via the `driverAdapters` Preview feature)
- set `node_compat = true` in `wrangler.toml` (see the [Cloudflare docs](https://developers.cloudflare.com/workers/runtime-apis/nodejs/))

If you are running into a size issue and can't deploy your application because of that, you can use our slimmer variant of the `pg` driver package [`@prisma/pg-worker`](https://github.com/prisma/prisma/tree/main/packages/pg-worker) and the [`@prisma/adapter-pg-worker`](https://github.com/prisma/prisma/tree/main/packages/adapter-pg-worker) adapter that belongs to it.

`@prisma/pg-worker` is an optimized and lightweight version of `pg` that is designed to be used in a Worker. It is a drop-in replacement for `pg` and is fully compatible with Prisma ORM.

#### 1. Configure Prisma schema & database connection

:::note
Expand Down
24 changes: 24 additions & 0 deletions content/200-orm/500-reference/100-prisma-schema-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2979,6 +2979,30 @@ For more information about using the `multiSchema` feature, refer to [this guide

</Admonition>

### `@shardKey`

The `@shardKey` attribute is only compatible with [PlanetScale](http://planetscale.com/) databases. It enables you define a [shard key](https://planetscale.com/docs/vitess/sharding) on a field of your model:

```prisma
model User {
id String @default(uuid())
region String @shardKey
}
```

### `@@shardKey`

The `@shardKey` attribute is only compatible with [PlanetScale](http://planetscale.com/) databases. It enables you define a [shard key](https://planetscale.com/docs/vitess/sharding) on multiple fields of your model:

```prisma
model User {
id String @default(uuid())
country String
customerId String
@@shardKey([country, customerId])
}
```

## Attribute functions

### `auto()`
Expand Down
8 changes: 8 additions & 0 deletions content/250-postgres/100-introduction/250-overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ Learn more on our [pricing page](https://www.prisma.io/pricing).
Prisma Postgres allows you to set limits to ensure you never get a surprise bill. You'll receive alerts when you reach 75% of your set limit, and if you reach 100%, your database will be paused. This ensures you'll never have an unexpected bill, and you can always be in complete control of your spending.
Spend limits are available on the Pro plan and higher. Please note that the spend limit must be set higher than the base cost of the selected plan. For example, if you're on the Pro plan, your spend limit should exceed the base plan cost of $49.

### Restarting your database when changing your subscription

When changing your subscription from Starter to Pro/Business or from Pro/Business to Start, your database instance is being restarted. This may cause a downtime of ~1second.

:::note
This is temporary. In the future, there won't be any downtime when up- or downgrading a plan.
:::

## Bundling with Prisma Accelerate

Prisma Postgres comes bundled with [Prisma Accelerate](/accelerate).
Expand Down
Loading