diff --git a/content/200-concepts/100-components/02-prisma-client/065-read-replicas.mdx b/content/200-concepts/100-components/02-prisma-client/065-read-replicas.mdx new file mode 100644 index 0000000000..23b0628ada --- /dev/null +++ b/content/200-concepts/100-components/02-prisma-client/065-read-replicas.mdx @@ -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' +--- + + + +You can add read replication in your application using the [`@prisma/extension-read-replicas`] Prisma Client extension. + + + +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).