Skip to content

visualbravo/zenstack-replicas

Repository files navigation

ZenStack Replicas

Distribute read operations across different database replicas with the ZenStack ORM.

NPM Version NPM Downloads Build Status Join the ZenStack server License: MIT

ℹ️ This project is not affiliated with or endorsed by the ZenStack team.

Requirements

  • ZenStack (version >= 3.3.0)
  • Node.js (version >= 20.0.0)

Installation

npm install @visualbravo/zenstack-replicas
pnpm add @visualbravo/zenstack-replicas
bun add @visualbravo/zenstack-replicas

Sample Usage

import { schema } from './zenstack/schema'
import { ZenStackClient } from '@zenstackhq/orm'
import { defineReplicasPlugin } from '@visualbravo/zenstack-replicas'

const replica1 = new ZenStackClient(schema, {
  ...
})

const replica2 = new ZenStackClient(schema, {
  ...
})

const primary = new ZenStackClient(schema, {
  ...
})

const client = primary.$use(
  defineReplicasPlugin({
    replicas: [replica1, replica2],
  }),
)

// replica1
await client.user.findMany()

// replica2
await client.user.findMany()

// replica1
await client.user.findMany()

// primary (write operation)
await client.user.create({
  data: {
    email: 'test@test.com',
  },
})

Note

Replicas are queried in a round-robin pattern.

License

MIT

About

Distribute read operations across different database replicas with the ZenStack ORM.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors