@styra/ucast-prisma

License NPM Version

This package contains helpers for using ucast conditions with Prisma queries.

[!WARNING] This is an experimental package and is subject to change.

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

+ 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:

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

Note that an expanded, more verbose format is supported, too:

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

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

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

For questions, discussions and announcements related to Styra products, services and open source projects, please join the Styra community on Slack!