Skip to content

Commit

Permalink
fix transaction extension example
Browse files Browse the repository at this point in the history
  • Loading branch information
jharrell committed Jan 10, 2025
1 parent effb6e7 commit 1786a14
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,20 @@ You can wrap your extended queries into a [batch transaction](/orm/prisma-client
The following example extends `findFirst` so that it runs in a batch transaction.

```ts
const prisma = new PrismaClient().$extends({
query: {
user: {
// Get the input `args` and a callback to `query`
async findFirst({ args, query, operation }) {
const [result] = await prisma.$transaction([query(args)]) // wrap the query in a batch transaction, and destructure the result to return an array
return result // return the first result found in the array
const transactionExtension = Prisma.defineExtension((prisma) =>
prisma.$extends({
query: {
user: {
// Get the input `args` and a callback to `query`
async findFirst({ args, query, operation }) {
const [result] = await prisma.$transaction([query(args)]) // wrap the query in a batch transaction, and destructure the result to return an array
return result // return the first result found in the array
},
},
},
},
})
})
)
const prisma = new PrismaClient().$extends(transactionExtension)
```

## Query extensions versus middlewares
Expand Down

0 comments on commit 1786a14

Please sign in to comment.