Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@prisma/extension-read-replicas
Advanced tools
This [Prisma Client Extension](https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions) adds read replica support to your Prisma Client. Under the hood, this extension creates additional Prisma Clients for the read replica database
Readme
This Prisma Client Extension adds read replica support to your Prisma Client. Under the hood, this extension creates additional Prisma Clients for the read replica database connection strings, and then routes read queries to these Clients instead of using the primary Prisma Client.
Requires Prisma 5.2+.
Depending on the package manager of your choice:
npm
npm install @prisma/extension-read-replicas
yarn
yarn add @prisma/extension-read-replicas
pnpm
pnpm add @prisma/extension-read-replicas
import { PrismaClient } from '@prisma/client'
import { readReplicas } from '@prisma/extension-read-replicas'
const prisma = new PrismaClient().$extends(
readReplicas({
url: 'postgresql://replica.example.com:5432/db',
}),
)
All non-transactional read queries will now be executed against the defined replica.
Write queries and transactions will be executed against the primary server.
Note: queryRaw
and executeRaw
are always executed against the primary server by default since
the extension can not know for sure if a particular raw query would read or write to the database.
Use the $replica()
method to explicitly route the request to a read replica.
You can also initialize the extension with an array of replica connection strings:
const prisma = new PrismaClient().$extends(
readReplicas({
url: [
'postgresql://replica-1.example.com:5432/db',
'postgresql://replica-2.example.com:5432/db',
],
}),
)
In this case, a replica for each read query will be selected randomly.
If you want to execute a read query against the primary server, you can use the $primary()
method on your extended client:
prisma.$primary().user.findMany({ where: { ... }})
Sometimes you might want to do the opposite and route the request to a replica even though
it will be routed to primary by default. In that case, you can use the $replica()
method:
prisma.$replica().$queryRaw`SELECT ...`
### Caveats and limitations
- At the moment, if you are using this read replicas extension alongside other extensions, this extension should be applied last:
```ts
const prisma = new PrismaClient()
.$extends(withAccelerate())
.$extends(rlsExtension())
.$extends(
readReplicas({
url: 'postgresql://replica.example.com:5432/db',
}),
)
FAQs
This [Prisma Client Extension](https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions) adds read replica support to your Prisma Client. Under the hood, this extension creates additional Prisma Clients for the read replica database
The npm package @prisma/extension-read-replicas receives a total of 28,565 weekly downloads. As such, @prisma/extension-read-replicas popularity was classified as popular.
We found that @prisma/extension-read-replicas demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.