Skip to content

Commit

Permalink
ucast-prisma: add example to README
Browse files Browse the repository at this point in the history
  • Loading branch information
srenatus committed Oct 31, 2024
1 parent 73b18d1 commit ce6cf75
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions packages/ucast-prisma/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,69 @@ This package contains helpers for using ucast conditions with Prisma queries.
> This is an experimental package and is subject to change.

## Usage

This package can be used to add filtering to Prisma queries from ucast conditions.

```diff
router.get("/tickets", async (req, res) => {
- const { allow, reason } = await authz.authorized(
+ const { allow, reason, conditions } = await authz.authorized(path, { action: "list" }, req);
if (!allow) return res.status(FORBIDDEN).json({ reason });

+ const filters = ucastToPrisma(conditions, "tickets");
const tickets = (
await prisma.tickets.findMany({
where: {
tenant: req.auth.tenant.id,
+ ...filters,
},
include: {
customers: true,
users: true,
},
})
).map((ticket) => toTicket(ticket));
return res.status(OK).json({ tickets });
});
```

The conditions returned by the OPA policy evaluation looks like this:

```json
{
"conditions": {
"or": [
{ "tickets.resolved": false },
{ "users.name": "ceasar" }
]
}
}
```

and the call to `ucastToPrisma(conditions, "tickets")` turns it into this
Prisma query:

```json
{
"OR": [
{
"resolved": {
"equals": false
}
},
{
"users": {
"name": {
"equals": "ceasar"
}
}
}
]
}
```


## Community

For questions, discussions and announcements related to Styra products, services and open source projects, please join
Expand Down

0 comments on commit ce6cf75

Please sign in to comment.