This project is destinated to study the RLS (Row Level Security) with 3 implementations:
- Prisma and PostgreSQL, using
Prisma Extensions
. - GraphQL directives.
- Nest.JS Guards
column | type |
---|---|
id | uuid |
name | string |
column | type |
---|---|
id | uuid |
name | string |
short_name | string |
description | string |
column | type |
---|---|
reference_user_id | uuid |
organization_id | uuid |
column | type |
---|---|
id | uuid |
name | string |
slug | string |
description | string |
published | boolean |
organization_id | uuid |
- User is only allowed to update, read or delete an Organization if he is OrganizationManager from that Organization
- User is only allowed to create, update, read or delete an Event if he is OrganizationManager from the Organization that is organizer from that Event
Clone this repository:
git clone https://github.com/KozielGPC/rls-prisma-postgres-example.git
Create a .env
file and install dependencies:
cd rls-prisma-postgres-example
cp .env.example .env
yarn install
Run the following command to start a new Postgres database in a Docker container:
docker-compose up -d
Run this command to apply migrations to the database:
yarn prisma migrate deploy
Run the following command to add seed data to the database:
yarn prisma db seed
To run the application you should run the following command:
yarn start:dev
Then you can access it at http://localhost:3000/graphql
To run the tests.spec.ts
file, run the following command:
yarn jest
Nest Guard requiring ADMIN
role but roles
header only contains USER
:
Directive to deny access to the field description
:
Without requesting description
field:
To-Do
Using the Prisma Extensions
implementation, there is a bug where you run a query with the RLS and then run a second with ByPass, it throws an error. It is better descripted in this issue, and the solution is to connect with two different prisma clients, but it connects using 26 connections from the connection pool (13 for each client). If you wanna see the conenction info, uncomment the lines of logs in the PrismaClient instances at main.ts