Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cerbos/orm-prisma

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cerbos/orm-prisma

An adapater library that takes a [Cerbos](https://cerbos.dev) Query Plan ([PlanResources API](https://docs.cerbos.dev/cerbos/latest/api/index.html#resources-query-plan)) response and converts it into a [Prisma](https://prisma.io) where class object. This

  • 0.1.4
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

Cerbos + Prisma ORM Adapter

An adapater library that takes a Cerbos Query Plan (PlanResources API) response and converts it into a Prisma where class object. This is designed to work alongside a project using the Cerbos Javascript SDK.

The following conditions are supported: and, or, eq, ne, lt, gt, lte, gte and in.

Requirements

  • Cerbos > v0.16
  • @cerbos/http or @cerbos/grpc client

Usage

npm install @cerbos/orm-prisma

This package exports a single function:

queryPlanToPrisma({ queryPlan, fieldNameMapper }): PrismaCondition

The function reqiures the full query plan from Cerbos to be passed in an object along with a fieldNameMapper.

A basic implementation can be as simple as:

import { GRPC as Cerbos } from "@cerbos/grpc";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();
const cerbos = new Cerbos("localhost:3592", { tls: false });

// Fetch the query plan from Cerbos passing in the principal
// resource type and action
const queryPlan = await cerbos.planResources({
  principal: {....},
  resource: { kind: "resourceKind" },
  action: "view"
})

// Generate the prisma filter from the query plan
const filters = queryPlanToPrisma({
  queryPlan,
  fieldNameMapper: {
    "request.resource.attr.aFieldName": "prismaModelFieldName"
  }
});

// Pass the filters in as where conditions
// If you have prexisting where conditions, you can pass them in an AND clause
const result = await prisma.myModel.findMany({
  where: {
    AND: filters
  },
});

console.log(result)

The fieldNameMapper is used to convert the field names in the query plan response to names of fields in the Prisma model - this can be done as a map or a function:

const filters = queryPlanToPrisma({
  queryPlan,
  fieldNameMapper: {
    "request.resource.attr.aFieldName": "prismaModelFieldName"
  }
});


//or

const filters = queryPlanToPrisma({
  queryPlan,
  fieldNameMapper: (fieldName: string): string => {
    if(fieldName.indexOf("request.resource.") > 0) {
      return fieldName.replace("request.resource.attr", "")
    }

    if(fieldName.indexOf("request.principal.") > 0) {
      return fieldName.replace("request.principal.attr", "")
    }
  }
});

Full Example

A full Prisma application making use of this adapater can be found at https://github.com/cerbos/express-prisma-cerbos

FAQs

Package last updated on 26 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc