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

fix(client): extensions query examples #4184

Merged
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const xprisma = prisma.$extends({
async findMany({ model, operation, args, query }) {
// set `age` and fill with the rest of `where`
args.where = { age: { gt: 18 }, ...args.where }
const users = await query(args)

return query(args)
},
},
Expand Down Expand Up @@ -99,9 +99,9 @@ const xprisma = prisma.$extends({
query: {
$allModels: {
async findMany({ model, operation, args, query }) {
// set `age` and fill with the rest of `where`
args.where = { age: { gt: 18 }, ...args.where }
const users = await query(args)
// set `take` and fill with the rest of `args`
args = { take: 100, ...args }

return query(args)
},
},
Expand Down Expand Up @@ -147,6 +147,34 @@ const xprisma = prisma.$extends({
})
```

### Mutate the result of a query
andrew-walford-prisma marked this conversation as resolved.
Show resolved Hide resolved
andrew-walford-prisma marked this conversation as resolved.
Show resolved Hide resolved

You can use `await` and then mutate the result of the `query` promise.

```ts
const xprisma = prisma.$extends({
query: {
user: {
async findFirst({ model, operation, args, query }) {
const user = await query(args)

if (user.password !== undefined) {
user.password = '******'
}

return user
andrew-walford-prisma marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
})
```

<Admonition type="info">

We include the above example to show that this is possible. However, for performance reasons we recommend that you use the [`result` component type](/concepts/components/prisma-client/client-extensions/result) to override existing fields. The `result` component type usually gives better performance in this situation because it computes only on access. The `query` component type computes after query execution.

</Admonition>

## Wrap a query into a batch transaction

You can wrap your extended queries into a [batch transaction](/guides/performance-and-optimization/prisma-client-transactions-guide). For example, you can use this to enact row-level security (RLS).
Expand Down