Documentation

Security & RLS

The direct-connection security model. Read this before using pg-fluent in production or migrating from Supabase.

Server-side only

pg-fluent is a server-side library. Database credentials must never reach browsers or untrusted clients. Exposing a direct database connection string to client-side code is a critical security risk.

RLS warning (read before migrating)

pg-fluent runs statements using the connection's own database role — the same way psql or any direct driver does. It does not set role or request.jwt.claims, and it does not emulate Supabase's anon / authenticated roles.

Consequently, Row Level Security policies written for Supabase's anon/authenticated roles do not apply automatically. If you connect as the table owner or any role with BYPASSRLS, RLS is skipped entirely.

If you need per-user policy enforcement:

  • Connect as a least-privilege, non-owner role for which RLS is enabled.
  • Set the required claims/role context yourself before querying as appropriate for your application. On pooled connections, use transaction-local settings (for example SET LOCAL) or explicitly reset role/claims before releasing the connection to avoid leaking session state.
  • Keep the database client on the server and expose only application-level endpoints to untrusted clients.

Injection safety

Every value is bound as a parameter. User-controlled structure — including select, order, or()/filter() bodies, JSON keys/paths, on_conflict, columns, embed hints, and identifiers — is validated against strict allowlists/grammar and quoted via a single identifier-quoting path. It is never concatenated into SQL text.

The only way to execute caller-supplied SQL text is the explicitly named db.unsafe() API. Its text is trusted by design; parameters are still bound.

// Safe: interpolation is bound as a parameter
await db.sql`select * from users where id = ${userId}`

// Safe structural composition
const table = db.identifier('users')
await db.sql`select id from ${table}`

// Explicit escape hatch: caller-supplied SQL text
await db.unsafe('select * from users where id = $1', [userId])

Credentials and TLS

  • Prefer connectionString from an environment variable (process.env.DATABASE_URL).
  • Passwords and connection strings are redacted from pg-fluent logs/error envelopes. Avoid serializing or logging the client or db.$pool directly.
  • TLS is configurable. For non-local connections, configure TLS explicitly (ssl / rejectUnauthorized).
  • Use a least-privilege role. Avoid table-owner, superuser, or BYPASSRLS roles for application traffic.

Mutation safety

Unfiltered UPDATE/DELETE requests without .select() are refused client-side by default (PGFLUENT_UNFILTERED_MUTATION). Only opt in intentionally with allowUnfilteredMutations: true or .allowUnfiltered().

Use .maxAffected(n) to cap the blast radius of a write and roll back when the affected row count exceeds the expected limit.