-
Notifications
You must be signed in to change notification settings - Fork 989
add accelerate docs and organize static imports #5641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,7 @@ | |
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
||
| # wrangler project | ||
| .dev.vars | ||
| .wrangler/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
||
|  |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean in the actual code like here