Code Monkey home page Code Monkey logo

extension-read-replicas's Introduction

@prisma/extension-read-replicas

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.

Requirements

Requires Prisma 5.2+.

Installation

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

Usage

Initialization

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.

Multiple replicas

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.

Bypassing the replicas

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: { ... }})

Forcing request to go through a replica

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',
      }),
    )
  • If you use the read replicas extension with Prisma version below 5.1, any result extensions will not work.

extension-read-replicas's People

Contributors

sevinf avatar janpio avatar casey-chow avatar mwskwong avatar jolg42 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.