Skip to content

Commit

Permalink
remove change in behavior to use replica for $queryRaw by default
Browse files Browse the repository at this point in the history
  • Loading branch information
casey-chow committed Oct 16, 2023
1 parent 83ddb70 commit c7954b6
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export DATABASE_URL=postgresql://prisma:prisma@localhost:6432/test
export REPLICA_URL=postgresql://prisma:prisma@localhost:7432/test
export REPLICA_URL=postgresql://prisma:prisma@localhost:6432/test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ prisma.$primary().user.findMany({ where: { ... }})
)
```
- If you use the read replicas extension with Prisma version below 5.1, any result extensions will not work.
- If you use `$queryRaw`, the query will by default route to the read replica, even for writes. To prevent this, make sure to use `.$primary()` on the client for any raw query writes.
- If you use `$queryRaw`, the query will by default route to the primary, even for `SELECT` statements.
8 changes: 4 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const readOperations = [
'groupBy',
'aggregate',
'count',
'$queryRaw',
'$queryRawUnsafe',
'$findRaw',
'$aggregateRaw',
'queryRaw',
'queryRawUnsafe',
'findRaw',
'aggregateRaw',
]

export const readReplicas = (options: ReplicasOptions, configureReplicaClient?: ConfigureReplicaCallback) =>
Expand Down
6 changes: 3 additions & 3 deletions tests/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ test('read query is executed against replica', async () => {
expect(logs).toEqual([{ server: 'replica', operation: 'findMany' }])
})

test('$queryRaw and $queryRawUnsafe are executed against replica', async () => {
test('$queryRaw and $queryRawUnsafe are executed against primary', async () => {
await expect(prisma.$queryRaw`SELECT ${'asdf'} as id`).resolves.toEqual([{ id: 'asdf' }])
await expect(prisma.$queryRawUnsafe('SELECT $1 as id', 'asdf')).resolves.toEqual([{ id: 'asdf' }])

expect(logs).toEqual([
{ server: 'replica', operation: '$queryRaw' },
{ server: 'replica', operation: '$queryRawUnsafe' },
{ server: 'primary', operation: '$queryRaw' },
{ server: 'primary', operation: '$queryRawUnsafe' },
])
})

Expand Down

0 comments on commit c7954b6

Please sign in to comment.