Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# wrangler project
.dev.vars
.wrangler/
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ With **Prisma Migrate**, Prisma's integrated database migration tool, the workfl
1. Migrate your development database using the `prisma migrate dev` CLI command
1. Use Prisma Client in your application code to access your database

![Typical workflow with Prisma Migrate](/img/prisma-migrate-development-workflow.png)
![Typical workflow with Prisma Migrate](/img/orm/prisma-migrate-development-workflow.png)

To learn more about the Prisma Migrate workflow, see:

Expand All @@ -268,6 +268,6 @@ The typical workflow when using **SQL migrations and introspection** is slightly
1. (Re-)generate Prisma Client
1. Use Prisma Client in your application code to access your database

![Introspect workflow](/img/prisma-evolve-app-workflow.png)
![Introspect workflow](/img/orm/prisma-evolve-app-workflow.png)

To learn more about the introspection workflow, please refer the [introspection section](/orm/prisma-schema/introspection).
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ To run a Microsoft SQL Server locally on a Windows machine:

1. Use Windows Authentication to log in to Microsoft SQL Server Management Studio (expand the **Server Name** dropdown and click **<Browse for more...>** to find your database engine):

![The New Query button in SQL Server Management Studio](/img/connect-sql-server.png)
![The New Query button in SQL Server Management Studio](/img/orm/connect-sql-server.png)

</TopBlock>

Expand Down
4 changes: 2 additions & 2 deletions docs/200-orm/100-prisma-schema/50-introspection.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ However, it can also be [used _repeatedly_ in an application](#introspection-wit

Introspection has one main function: Populate your Prisma schema with a data model that reflects the current database schema.

![Introspect your database with Prisma](/img/prisma-db-pull-generate-schema.png)
![Introspect your database with Prisma](/img/orm/prisma-db-pull-generate-schema.png)

Here's an overview of its main functions on SQL databases:

Expand Down Expand Up @@ -63,7 +63,7 @@ The typical workflow for projects that are not using Prisma Migrate, but instead

Note that as you evolve the application, [this process can be repeated for an indefinite number of times](#introspection-with-an-existing-schema).

![Introspect workflow](/img/prisma-evolve-app-workflow.png)
![Introspect workflow](/img/orm/prisma-evolve-app-workflow.png)

## Rules and conventions

Expand Down
12 changes: 6 additions & 6 deletions docs/200-orm/200-prisma-client/100-queries/055-pagination.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const results = await prisma.post.findMany({
})
```

![](/img/offset-skip-take.png)
![](/img/orm/offset-skip-take.png)

To implement pages of results, you would just `skip` the number of pages multiplied by the number of results you show per page.

Expand Down Expand Up @@ -103,7 +103,7 @@ const firstQueryResults = await prisma.post.findMany({

The following diagram shows the IDs of the first 4 results - or page 1. The cursor for the next query is **29**:

![](/img/cursor-1.png)
![](/img/orm/cursor-1.png)

The second query returns the first 4 `Post` records that contain the word `"Prisma"` **after the supplied cursor** (in other words - IDs that are larger than **29**):

Expand All @@ -130,23 +130,23 @@ const myCursor = lastPostInResults.id // Example: 52

The following diagram shows the first 4 `Post` records **after** the record with ID **29**. In this example, the new cursor is **52**:

![](/img/cursor-2.png)
![](/img/orm/cursor-2.png)

### FAQ

#### Do I always have to skip: 1?

If you do not `skip: 1`, your result set will include your previous cursor. The first query returns four results and the cursor is **29**:

![](/img/cursor-1.png)
![](/img/orm/cursor-1.png)

Without `skip: 1`, the second query returns 4 results after (and _including_) the cursor:

![](/img/cursor-3.png)
![](/img/orm/cursor-3.png)

If you `skip: 1`, the cursor is not included:

![](/img/cursor-2.png)
![](/img/orm/cursor-2.png)

You can choose to `skip: 1` or not depending on the pagination behavior that you want.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ The pipeline should handle deployment to staging and production environments, an

When you add Prisma Migrate to an **existing database**, you must [baseline](/orm/prisma-migrate/workflows/baselining) the production database. Baselining is performed **once**, and can be done from a local instance.

![](/img/baseline-production-from-local.png)
![](/img/orm/baseline-production-from-local.png)
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ npx prisma db pull

Here's a graphical illustration for what happens when `db pull` is invoked:

![Introspect your database with Prisma](/img/prisma-db-pull-generate-schema.png)
![Introspect your database with Prisma](/img/orm/prisma-db-pull-generate-schema.png)

For the above Prisma 1 datamodel, this results in the following Prisma 2 schema (note that the models have been reordered to match the initial order of the Prisma 1 datamodel):

Expand Down
29 changes: 29 additions & 0 deletions docs/300-accelerate/100-what-is-accelerate.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: 'What is Accelerate'
metaTitle: 'What is Accelerate'
metaDescription: 'Learn about Accelerate, a global cache and serverless connection pool that makes your database queries faster.'
tocDepth: 3
toc: true
---

<TopBlock>

[Accelerate](https://www.prisma.io/data-platform/accelerate) is a global database cache available in 300 locations that you can use to achieve up to 1000x faster database queries.

Its main features are:

- a global cache
- scalable connection pool for serverless and edge applications
- usage of Prisma Client at the edge (e.g. in Cloudflare Workers or Vercel Edge Functions)

The goal of Accelerate is to improve response times and reduce database load. It works by caching data at the edge using established caching patterns that you control.

While Accelerate is beneficial for all types of applications, being at the edge provides additional benefits to edge function environments like [Vercel Edge Functions](https://vercel.com/docs/concepts/functions/edge-functions), [Cloudflare Workers](https://workers.cloudflare.com/), and [Deno Deploy](https://deno.com/deploy). Cache hits can be served from data centers near the user regardless of the region of the database.

</TopBlock>

## See Accelerate in action

We built a small sample application, [Accelerate Speed Test](https://accelerate-speed-test.prisma.io/). The app compares the performance of cached and uncached queries side by side. The app is [open source](https://github.com/prisma/accelerate-speed-test) and you can clone it to try it yourself.

![Screenshot of the Accelerate Speed Test app showing cached query performance](/img/accelerate/accelerate.png)
193 changes: 193 additions & 0 deletions docs/300-accelerate/200-getting-started.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
---
title: 'Getting started'
metaTitle: 'Getting started with Accelerate'
metaDescription: 'Learn how to get up and running with Accelerate.'
tocDepth: 3
toc: true
---

<TopBlock>

</TopBlock>

## Prerequisites

To get started with Accelerate, you will need the following:

- A GitHub account.
- A project that uses [Prisma Client](/orm/prisma-client) `4.16.1` or higher. If your project is using interactive transactions, you need to use `5.1.1` or higher. (We always recommend using the latest version of Prisma.)
- A hosted PostgreSQL, MySQL, PlanetScale, CockroachDB, or MongoDB database.

## 1. Enable Accelerate in a project

In order to enable Accelerate, you can log in to [Prisma Data Platform](https://pris.ly/pdp) and create a new project. Follow the instructions in the UI to add Accelerate.

At the end of the setup process, you'll obtain a connection string that connects to Accelerate. This connection string also contains an API key that you need to use when configuring Prisma Client to use Accelerate.

## 2. Use Accelerate in your application

To get started using Accelerate, we recommend using the [latest version of Prisma ORM](https://github.com/prisma/prisma/releases/).

### 2.1. Update your database connection string

After enabling Accelerate in your project and creating a new API key, you should be given an Accelerate connection string.

To use this connection string, update the `datasource` block's `url` field in your Prisma schema:

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

Most likely, as shown above, your database connection string in defined in a `.env` file rather than hard-coded into the schema file.

Update that variable to use the new Accelerate connection string:

```env file=.env
# __API_KEY__ is a unique API key that Accelerate generates and automatically assigns to a project.
DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=__API_KEY__"

# Previous connection string
# DATABASE_URL="postgresql://user:password@host:port/db_name?schema=public"
```

Prisma Migrate and Introspection do not work with a `prisma://` connection string. In order to continue using these features add a new variable to the `.env` file named `DIRECT_DATABASE_URL` whose value is the direct database connection string:

<Admonition>

As of Prisma version `5.2.0` you can use Prisma Studio with the Accelerate connection string.

</Admonition>

```env file=.env highlight=3;add
DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=__API_KEY__"
DIRECT_DATABASE_URL="postgresql://user:password@host:port/db_name?schema=public"
```

Then in your Prisma schema's `datasource` block add a field named `directUrl` with the following:

```prisma highlight=4;add
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_DATABASE_URL")
}
```

Migrations and introspections will use the `directUrl` connection string rather than the one defined in `url` when this configuration is provided.

> `directUrl` is useful for you to carry out migrations and introspections. However, you don't need `directUrl` to use Accelerate in your application.

### 2.2. Install the Accelerate Prisma Client extension

Run the following command to install the Accelerate extension for Prisma Client:

```terminal
npm install @prisma/extension-accelerate
```

### 2.3. Generate Prisma Client for Accelerate

If you're using Prisma version `5.2.0` or greater, Prisma Client will automatically determine how it should connect to the database depending on the protocol in the database connection string. If the connection string in the `DATABASE_URL` starts with `prisma://`, Prisma Client will try to connect to your database using Prisma Accelerate.

When using Prisma Accelerate in long-running application servers, such as a server deployed on AWS EC2, you can generate the Prisma Client by executing the following command:

```terminal
npx prisma generate
```

When using Prisma Accelerate in a Serverless or an Edge application, we recommend you to run the following command to generate Prisma Client:

```terminal
npx prisma generate --no-engine
```

<Admonition type="info">

The `--no-engine` flag prevents a Query Engine file from being included in the generated Prisma Client, this ensures the bundle size of your application remains small.

</Admonition>

<Admonition type="warning">

If your Prisma version is below `5.2.0`, generate Prisma Client with the `--accelerate` option:

```terminal
npx prisma generate --accelerate
```

If your Prisma version is below `5.0.0`, generate Prisma Client with the `--data-proxy` option:
</Admonition>

### 2.4. Extend your Prisma Client instance to add the Accelerate extension

To use Accelerate, you must extend Prisma Client with the Accelerate extension.
Extend your Prisma Client instance to add the Accelerate extension:

```ts
import { PrismaClient } from '@prisma/client'
import { withAccelerate } from '@prisma/extension-accelerate'

const prisma = new PrismaClient().$extends(withAccelerate())
```

If you are going to deploy to an edge runtime (like Cloudflare Workers, Vercel Edge Functions, Deno Deploy, or Netlify Edge Functions), use our edge client instead:

```ts
import { PrismaClient } from '@prisma/client/edge'
import { withAccelerate } from '@prisma/extension-accelerate'

const prisma = new PrismaClient().$extends(withAccelerate())
```

If VS Code does not recognize the `$extends` method, refer to [this section](/accelerate/faq#vs-code-does-not-recognize-the-extends-method) on how to resolve the issue.

<Admonition type="info">

If you are using [Prisma Middleware](/orm/prisma-client/client-extensions/middleware) in your application, make sure they are added before any Prisma Client extensions (like Accelerate). For example:

```ts
const prisma = new PrismaClient().$use(middleware).$extends(withAccelerate())
```

</Admonition>

### 2.5. Use Accelerate in your database queries

The `withAccelerate` extension primarily does two things:

- Gives you access to the `cacheStrategy` field within each applicable model method that allows you to define a cache strategy per-query.
- Routes all of your queries through a connection pooler.

#### No cache strategy to only use connection pool

If you simply want to take advantage of Accelerate's connection pooling feature without applying a cache strategy, you may run your query the same way you would have without Accelerate.

By enabling Accelerate and supplying the Accelerate connection string, your queries now use the connection pooler by default.

#### Define a cache strategy

Update a query with the new `cacheStrategy` property which allows you to define a cache strategy for that specific query:

```ts
const user = await prisma.user.findMany({
where: {
email: {
contains: 'alice@prisma.io',
},
},
cacheStrategy: { swr: 60, ttl: 60 },
})
```

In the example above, `swr: 60` and `ttl: 60` means Accelerate will serve cached data for 60 seconds and then another 60 seconds while Accelerate fetches fresh data in the background.

You should now see improved performance for your cached queries.

<Admonition type="info">

For information about which strategy best serves your application, see [Select a cache strategy](/accelerate/caching#selecting-a-cache-strategy).

</Admonition>
Loading