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.
Installation
npm i @prisma/prisma-read-replica-middleware --save
Usage
- Create a new file adjacent to your
schema.prisma
called read-replica-schema.prisma
. - Inside
read-replica-schema.prisma
add only generator
and datasource
blocks. - In the
generator
block, set output
to read-replica-client
. - In the
datasource
block, add the URL of your read replica database. - As part of your build process locally and in all environments, run
prisma-read-replica generate
to generate the Read Replica client. - Wherever you instantiate Prisma using
new PrismaClient()
, instantiate PrismaReadReplica
and apply it with $use
. For example:
import { Prisma, PrismaClient } from '@prisma/client'
import PrismaReadReplicaMiddleware from 'prisma-read-replica-middleware';
const modelsToExclude = ['User'];
prisma.$use(PrismaReadReplicaMiddleware(modelsToExclude));
Limitations
- This middleware does not perform migrations against a read replica database.
- This middleware only intercepts the following actions: find, findMany, findUnique
- This middleware does not account for custom providers.
- 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.
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,
},
})