Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw on empty replica list #13

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export const readReplicas = (options: ReplicasOptions, configureReplicaClient?:
replicaUrls = [replicaUrls]
} else if (!Array.isArray(replicaUrls)) {
throw new Error(`Replica URLs must be a string or list of strings`)
} else if (replicaUrls.length === 0) {
throw new Error(`At least one replica URL must be specified`)
}

const replicaManager = new ReplicaManager({
Expand Down
21 changes: 17 additions & 4 deletions tests/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import execa from 'execa'
// @ts-ignore
import type { PrismaClient } from './client'
import { readReplicas } from '..'
import type { PrismaClient } from './client'

type LogEntry = { server: 'primary' | 'replica'; operation: string }

Expand All @@ -11,7 +11,7 @@ function createPrisma() {
const clientModule = require('./client')
const basePrisma = new clientModule.PrismaClient() as PrismaClient

return basePrisma
const prisma = basePrisma
.$extends(
readReplicas(
{
Expand All @@ -36,22 +36,35 @@ function createPrisma() {
},
},
})

return [basePrisma, prisma] as const
}

let basePrisma: ReturnType<typeof createPrisma>
let prisma: ReturnType<typeof createPrisma>

beforeAll(async () => {
await execa('pnpm', ['prisma', 'db', 'push', '--schema', 'tests/prisma/schema.prisma'], {
cwd: __dirname,
})

prisma = createPrisma()
;[basePrisma, prisma] = createPrisma()
})

beforeEach(async () => {
logs = []
})

test('client throws an error when given an empty read replica list', async () => {
const createInstance = () =>
basePrisma.$extends(
readReplicas({
url: [],
}),
)

expect(createInstance).toThrowError('At least one replica URL must be specified')
})

test('read query is executed against replica', async () => {
await prisma.user.findMany()

Expand Down