-
Notifications
You must be signed in to change notification settings - Fork 785
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
content/200-concepts/100-components/02-prisma-client/065-read-replicas.mdx
This file contains 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,55 @@ | ||
--- | ||
title: 'Read replicas' | ||
metaTitle: 'Read replicas' | ||
metaDescription: 'This page explains how to set up and use read replicas with Prisma Client' | ||
--- | ||
|
||
<TopBlock> | ||
|
||
You can add read replication in your application using the [`@prisma/extension-read-replicas`] Prisma Client extension. | ||
|
||
</TopBlock> | ||
|
||
Install the extension in your project: | ||
|
||
```terminal | ||
npm install @prisma/extension-read-replicas | ||
``` | ||
|
||
```ts | ||
import { PrismaClient } from '@prisma/client' | ||
import { readReplicas } from '@prisma/extension-read-replicas' | ||
|
||
const prisma = new PrismaClient().$extends( | ||
readReplicas({ | ||
url: process.env.DATABASE_URL_REPLICA, | ||
}) | ||
) | ||
``` | ||
|
||
```ts | ||
const prisma = new PrismaClient().$extends( | ||
readReplicas({ | ||
url: [ | ||
process.env.DATABASE_URL_REPLICA_1, | ||
process.env.DATABASE_URL_REPLICA_2, | ||
], | ||
}) | ||
) | ||
``` | ||
|
||
You can use the `$primary()` method to execute queries through against your primary database instance: | ||
|
||
```ts | ||
const posts = await prisma.$primary().post.findMany() | ||
``` | ||
|
||
You can use the `$replica()` method to explicitly execute your query against a replica instead of your primary database: | ||
|
||
```ts | ||
const result = await prisma.$replica().$queryRaw`SELECT ...` | ||
``` | ||
|
||
## Going further | ||
|
||
Try it out and share your feedback with us [here](https://github.com/prisma/extension-read-replicas/issues/new). |