glapi-rls-guide

Implement PostgreSQL Row Level Security policies and session variables in GLAPI multi-tenant services.

Updated May 10, 2025
One-click install
npx skills add https://github.com/adteco/glapi --skill glapi-rls-guide
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: glapi-rls-guide
Source: https://github.com/adteco/glapi/tree/main/.claude/skills/glapi-rls-guide
Command: npx skills add https://github.com/adteco/glapi --skill glapi-rls-guide

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

GLAPI's multi-tenant databases need reliable Row Level Security and contextual session variables so that organization-specific queries cannot leak data between tenants, especially when repositories or services might inadvertently use global connections.

Core Features & Use Cases

  • Session Variable Enforcement: Every contextual database connection sets app.current_organization_id before executing queries, ensuring policies compare organization_id values accurately even across inserts, selects, updates, and deletes.
  • Context Module & Middleware: The context helpers and authenticated tRPC middleware create RLS-aware connections, validate organization UUIDs, and release clients safely at the end of each request.
  • Repository Patterns & Troubleshooting: Services and repositories accept a contextual db, follow the ctx.db path, and include debug patterns for diagnosing new row violates row-level security policy errors, illustrating how to add RLS for new tables while keeping migrations aligned.

Quick Start

Use the GLAPI RLS guide to outline the steps for enabling PostgreSQL session variables, RLS policies, and contextual database connections when adding a new multi-tenant table.

Frequently Asked Questions about glapi-rls-guide

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement PostgreSQL row level security for multi-tenant data isolation?

PostgreSQL row level security for multi-tenant isolation requires setting session variables like `app.current_organization_id` on contextual database connections. RLS policies compare this variable against the table's organization_id column to prevent cross-tenant data leakage across inserts, selects, updates, and deletes.

Why does my multi-tenant query fail with a new row violates row-level security policy error?

A `new row violates row-level security policy` error occurs when the database connection lacks the required session variable or the RLS policy restricts the insert. Debugging involves verifying contextual session variable initialization and ensuring the repository uses the contextual `ctx.db` connection.

How do I set up authenticated tRPC middleware to enforce row level security?

Authenticated tRPC middleware enforces row level security by creating RLS-aware contextual database connections. The middleware validates organization UUIDs, sets the required session variables before executing queries, and safely releases the database client at the end of each request.

Do I need to enable row level security on every new table in a multi-tenant database?

You need to enable row level security on every new multi-tenant table to maintain data isolation. This requires adding RLS policy definitions and updating migration files to ensure policies compare the session variable against the new table's organization_id column.

Can I use global database connections for multi-tenant repositories?

Global database connections bypass row level security policies and risk cross-tenant data leakage. Repositories and services must instead accept a contextual `db` object following the `ctx.db` path to ensure queries execute within the authenticated session variable context.