Skip to content

Commit

Permalink
first draft read replicas
Browse files Browse the repository at this point in the history
  • Loading branch information
ruheni committed Nov 17, 2023
1 parent 358926e commit 4a9b136
Showing 1 changed file with 55 additions and 0 deletions.
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).

0 comments on commit 4a9b136

Please sign in to comment.