https://github.com/prisma/prisma-read-replica-middleware
https://github.com/prisma/prisma-read-replica-middleware
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/prisma/prisma-read-replica-middleware
- Owner: prisma
- License: apache-2.0
- Created: 2022-02-25T21:56:29.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-26T09:07:46.000Z (almost 4 years ago)
- Last Synced: 2026-02-02T21:52:11.329Z (5 months ago)
- Language: TypeScript
- Size: 329 KB
- Stars: 4
- Watchers: 20
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Prisma Read Replica Middleware
We have provided this middleware as an example only, and without warranty. It is not intended for use in a production environment. Please consider the limitations documented below before adding it to your application.
Find implementation examples [here](https://github.com/prisma/middleware-examples/).
## Installation
1. `npm i @prisma/prisma-read-replica-middleware --save`
## Usage
1. Create a new file adjacent to your `schema.prisma` called `read-replica-schema.prisma`.
2. Inside `read-replica-schema.prisma` add _only_ `generator` and `datasource` blocks.
3. In the `generator` block, set `output` to `read-replica-client`.
4. In the `datasource` block, add the URL of your read replica database.
5. As part of your build process locally and in all environments, run `prisma-read-replica generate` to generate the Read Replica client.
6. Wherever you instantiate Prisma using `new PrismaClient()`, instantiate `PrismaReadReplica` and apply it with `$use`. For example:
```ts
import { Prisma, PrismaClient } from '@prisma/client'
import PrismaReadReplicaMiddleware from 'prisma-read-replica-middleware';
const modelsToExclude = ['User'];
prisma.$use(PrismaReadReplicaMiddleware(modelsToExclude));
```
## Limitations
1. This middleware does not perform migrations against a read replica database.
2. This middleware only intercepts the following actions: find, findMany, findUnique
3. This middleware does not account for custom providers.
4. This middleware does not account for nested reads because middleware does not have access to the underlying SQL query. In the following example the request will _not_ be sent to the Read Replica because `User` is an excluded model.
```ts
import { Prisma, PrismaClient } from '@prisma/client'
import PrismaReadReplicaMiddleware from 'prisma-read-replica';
const prisma = new PrismaClient({
log: ['query', 'info', 'warn', 'error'],
});
const modelsToExclude = ['User'];
prisma.$use(PrismaReadReplicaMiddleware(modelsToExclude));
const getUsers = await prisma.user.findMany({
where: {
email: {
contains: 'test',
},
},
include: {
post: true,
},
})
```