Skip to content

Commit

Permalink
ucast-prisma: update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
srenatus committed Dec 4, 2024
1 parent c31016a commit cc9119d
Showing 1 changed file with 46 additions and 27 deletions.
73 changes: 46 additions & 27 deletions packages/ucast-prisma/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,68 @@ This package contains helpers for using ucast conditions with Prisma queries.
> [!WARNING]
> 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 });
});
+ import { ucastToPrisma } from "@styra/ucast-prisma";

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: filters,
include: {
customers: true,
users: true,
},
})
).map((ticket) => toTicket(ticket));
return res.status(OK).json({ tickets });
});
```

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

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

The conditions returned by the OPA policy evaluation looks like this:
Note that an expanded, more verbose format is supported, too:

```json
{
"conditions": {
"or": [
{ "tickets.resolved": false },
{ "users.name": "ceasar" }
"type": "compound",
"operator": "or",
"value": [
{
"type": "field",
"operator": "eq",
"field": "tickets.resolved",
"value": false
},
{
"type": "field",
"operator": "eq",
"field": "users.name",
"value": "caesar"
}
]
}
}
```

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

```json
Expand All @@ -71,7 +91,6 @@ Prisma query:
}
```


## Community

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

0 comments on commit cc9119d

Please sign in to comment.