Git Product home page Git Product logo

crls's Introduction

CRLS

๐Ÿ”’ Dead easy column and row-level security

npm npm bundle size npm install size maintainability vulnerabilities dependencies

๐Ÿš€ Quick Start

Install

# Use your favorite package manager!
pnpm add -E crls

Import

// ESM / TypeScript
import crls from 'crls'

// or CommonJS
// const crls = require('crls')

Start filtering data

import crls from 'crls'

type Post = {
  id: number
  title: string
  author: string
}

type Context = {
  username: string
}

const data: Array<Post> = [
  { id: 1, title: 'A blog post!', author: 'luke' },
  { id: 2, title: 'Another blog post!', author: 'luke' },
  { id: 3, title: 'My blog post!!!', author: 'notluke' },
]

const withCRLS = crls<Post, Context>(data, (row, context) => {
  // Users cannot see posts that they haven't authored
  if (row.author !== context.username) return false
  // If the user is "luke", they cannot see post IDs
  else if (context.username === 'luke') return new Set(['title', 'author'])
  // If the user is the author, and they aren't "luke"
  else return true
})

const lukePosts = withRLS({ username: 'luke' })
// => [{ title: "A blog post!", author: "luke" }, { title: "Another blog post!", author: "luke" }]

const notLukePosts = withRLS({ username: 'notluke' })
// => [{ id: 3, title: "My blog post!!!", author: "notluke" }]

const bobPosts = withRLS({ username: 'bob' })
// => []

View full documentation at crls.js.org!

๐Ÿ“ƒ License

CRLS is licensed under the MIT License.

crls's People

Contributors

lukecarr avatar renovate[bot] avatar

Watchers

 avatar

crls's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

devcontainer
.devcontainer/devcontainer.json
  • mcr.microsoft.com/devcontainers/typescript-node 18
github-actions
.github/workflows/docs.yml
  • actions/checkout v3
  • pnpm/action-setup v2.2.4
  • actions/setup-node v3
  • peaceiris/actions-gh-pages v3
.github/workflows/test.yml
  • actions/checkout v3
  • pnpm/action-setup v2.2.4
  • actions/setup-node v3
npm
package.json
  • pnpm 7.14.2

  • Check this box to trigger a request for Renovate to run again on this repository

Support Node.js Stream API

We should export a TransformStream that allows crls to be used within a stream pipeline.

Because this sort of logic is probably niche, it should be included in a separate export (crls/stream) that is tree-shakeable.

Simplify security expression

Right now, the security expression is evaluated as two predicates: one for rows (returning true for the row to be included, false for the row to be excluded), and one for columns (returning true for all columns to be included, or a set of column names to include).

These expression can be combined into a single parameter passed to the library's default export. The combined expression should return true (if the row and all columns are included), or a set of column names (implying that the row, albeit partial, isn't excluded), or false (if the row is excluded).

function crls <T extends object, U = unknown> (
    data: T[] | (() => Promise<T[]>),
    security: (row: T, context: U) => true | Set<keyof T> | false
): ((context: U) => Partial<T>[]) | ((context: U) => Promise<Partial<T>[]>);

Next.js example application

We should include a reference Next.js application to demonstrate how crls works in the real world.

The example should probably use Prisma because of it's popularity & widespread use with Next.js, and some authentication library (e.g. Clerk, Magic).

The app should be placed in a directory named examples/nextjs-prisma-clerk (or whatever libraries are chosen), and use PNPM as the package manager (to align with the library).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.