Skip to content

policy ​

Builders for the row-level access rule of each operation in a table's policies. Guide: Policies.

ts
import { policy } from 'accessnexus';

Each builder returns a plain rule object ({ kind, ... }). The SupabaseAdapter compiles it to a Postgres RLS policy.

How a rule becomes a policy ​

One policy per operation, named <prefix>_<table>_<operation> (prefix default accessnexus), preceded by DROP POLICY IF EXISTS (unless adapterOptions.idempotent is false):

OperationClauses
selectUSING (<rule>)
insertWITH CHECK (<check of rule>)
updateUSING (<rule>) and an explicit WITH CHECK (<check of rule>)
deleteUSING (<rule>)

For every builder except withCheck, the check expression equals the rule expression, so an UPDATE can never move a row out of the rule. An operation left out of policies has no policy and is denied (RLS is on).

sql
DROP POLICY IF EXISTS "accessnexus_posts_update" ON posts;
CREATE POLICY "accessnexus_posts_update"
  ON posts FOR UPDATE
  TO authenticated
  USING (
    (SELECT auth.uid()) = author_id
  )
  WITH CHECK (
    (SELECT auth.uid()) = author_id
  );

Target role (TO ...) ​

AccessNexus picks the role; the predicate always decides access.

OperationTO public whenOtherwise
selectsome branch can be true without a signed-in user: policy.public(), policy.custom(...), or a matchesField with no auth. call on either side (any needs one such branch, all needs every branch)TO authenticated
insert, update, deletean explicit policy.public() decides it (inside any, or inside all together only with branches that can pass without a user)TO authenticated

auth.uid() and auth.jwt() are always emitted as (SELECT auth.uid()) / (SELECT auth.jwt()) so Postgres evaluates them once per query (initPlan), not once per row.

Summary ​

BuilderRule kindSQL predicate
isAuthenticated()authenticated(SELECT auth.uid()) IS NOT NULL
public()publicTRUE
none()noneFALSE
isOwner(column)owner(SELECT auth.uid()) = <column>
hasRole(role)role((SELECT auth.jwt()) -> 'app_metadata' ->> 'role') = '<role>'
belongsTo(column, through?)belongs_to(SELECT <prefix>_<table>_belongs_to_<n>_fn(<column>))
sharesTenant(column, membership)shares_tenant(SELECT <prefix>_<table>_shares_tenant_<n>_fn(<column>))
matchesField(a, b)matches_field<a> = <b>
custom(expression, adapter?)custom<expression> verbatim
any(rules)any(<r1>) OR (<r2>) ...
all(rules)all(<r1>) AND (<r2>) ...
withCheck(using, check, options?)with_checkUSING (<using>), WITH CHECK ((<using>) AND (<check>))

isAuthenticated ​

Any signed-in user.

ts
isAuthenticated(): AuthenticatedRule; // { kind: 'authenticated' }

SQL: (SELECT auth.uid()) IS NOT NULL. Role: authenticated.

ts
import { policy } from 'accessnexus';

export const policies = { select: policy.isAuthenticated() };

WARNING

On Supabase anyone can sign up. Do not use isAuthenticated() for select on a table with private columns (email, phone, address), and do not use it for insert on a table keyed by the user id (validate warns USER_ROW_INSERT_OPEN).

public ​

Everyone, including anonymous visitors.

ts
public(): PublicRule; // { kind: 'public' }

SQL: TRUE. Role: public.

ts
import { policy } from 'accessnexus';

export const policies = { select: policy.public() };

WARNING

On insert, update or delete this lets anyone holding your public anon key write the table. validate warns PUBLIC_WRITE.

none ​

Nobody through the API. The service role, SECURITY DEFINER functions and triggers still bypass RLS.

ts
none(): NoneRule; // { kind: 'none' }

SQL: FALSE. Role: authenticated.

ts
import { policy } from 'accessnexus';

export const policies = { update: policy.none(), delete: policy.none() };

isOwner ​

The row belongs to the current user.

ts
isOwner(column: string): OwnerRule; // { kind: 'owner', column }
ParameterTypeDescription
columnstringColumn holding the owner's user id. Must be uuid (OWNER_COLUMN_NOT_UUID).

SQL: (SELECT auth.uid()) = <column>. Role: authenticated. The column is indexed automatically (idx_<table>_<column>_rls).

ts
import { policy } from 'accessnexus';

export const policies = {
  select: policy.isOwner('user_id'),
  insert: policy.isOwner('user_id'),
  update: policy.isOwner('user_id'),
  delete: policy.isOwner('user_id'),
};

hasRole ​

The user's global app role, read from the JWT app_metadata.role claim.

ts
hasRole(role: string): RoleRule; // { kind: 'role', role }
ParameterTypeDescription
rolestringExpected app_metadata.role value. Quoted as a SQL literal.

SQL: ((SELECT auth.jwt()) -> 'app_metadata' ->> 'role') = '<role>'. Role: authenticated.

app_metadata is writable only server-side, e.g. supabase.auth.admin.updateUserById(id, { app_metadata: { role: 'admin' } }) with the service role, or a custom access token hook. The top-level JWT role claim (the Postgres role, anon / authenticated) is not used.

ts
import { policy } from 'accessnexus';

// Platform staff read the audit trail
export const policies = { select: policy.hasRole('admin') };

WARNING

hasRole is not a role inside an org or team. For tenant roles use { scope: 'tenant', roles: [...] } in definePermissions or belongsTo(..., { where: { role: [...] } }). validate warns GLOBAL_ROLE_IN_TENANT_TABLE when a table mixes hasRole with membership checks.

belongsTo ​

The current user is a member of the row's group (org, team, shop).

ts
belongsTo(
  column: string,
  through?: {
    table: string;
    foreignKey: string;
    localKey: string;
    where?: Record<string, string | string[]>;
  },
): BelongsToRule; // { kind: 'belongs_to', column, through }
ParameterTypeDescription
columnstringColumn on this table holding the group id. Must be uuid (the helper takes p_value uuid).
through.tablestringMembership table, e.g. 'org_members'.
through.foreignKeystringMembership column holding the group id.
through.localKeystringMembership column holding the user id.
through.whereRecord<string, string | string[]>Extra filters on the membership row. An array becomes IN (...), a string = '...'. Values are quoted literals.

Each distinct belongsTo on a table compiles to a SECURITY DEFINER helper (one per signature, numbered from 1), which avoids RLS recursion even on the membership table itself:

sql
CREATE OR REPLACE FUNCTION accessnexus_posts_belongs_to_1_fn(p_value uuid)
RETURNS boolean
LANGUAGE sql
SECURITY DEFINER
SET search_path = pg_catalog, public
AS $$
  SELECT EXISTS (SELECT 1 FROM public.org_members WHERE org_id = p_value AND user_id = (SELECT auth.uid()) AND role IN ('admin'));
$$;
REVOKE EXECUTE ON FUNCTION accessnexus_posts_belongs_to_1_fn(uuid) FROM PUBLIC, anon;
GRANT EXECUTE ON FUNCTION accessnexus_posts_belongs_to_1_fn(uuid) TO authenticated;

The policy predicate is (SELECT accessnexus_posts_belongs_to_1_fn(org_id)). When a policy using the helper is granted TO public, the helper is also granted to anon (it returns false without a user).

Without through, the helper checks profiles.<column> of the current user: SELECT EXISTS (SELECT 1 FROM public.profiles WHERE <column> = p_value AND id = (SELECT auth.uid())).

ts
import { policy } from 'accessnexus';

const members = { table: 'org_members', foreignKey: 'org_id', localKey: 'user_id' };

export const policies = {
  select: policy.belongsTo('org_id', members),
  delete: policy.belongsTo('org_id', { ...members, where: { role: ['admin'] } }),
};

WARNING

belongsTo(column) without through trusts profiles.<column>. If users can insert their profile row or update that column, they can set it to any tenant id and join that tenant. validate rejects this (BELONGS_TO_PROFILE_WRITABLE). Prefer through; to keep the profiles lookup, use insert: policy.none() on profiles and trigger.lockColumns({ columns: [column] }).

validate checks that column, foreignKey, localKey and where columns exist, and that where values belong to an enum column's values (INVALID_ENUM_VALUE).

sharesTenant ​

The row's user shares at least one tenant with the current user. Typical use: profiles visible to co-members of any org.

ts
sharesTenant(
  column: string,
  membership: { table: string; userColumn: string; tenantColumn: string },
): SharesTenantRule; // { kind: 'shares_tenant', column, membership }
ParameterTypeDescription
columnstringColumn on this table holding a user id, e.g. 'id' on profiles. Must be uuid (the helper takes p_value uuid).
membership.tablestringMembership table, e.g. 'org_members'.
membership.userColumnstringMembership column holding the user id.
membership.tenantColumnstringMembership column holding the tenant id.

Unlike belongsTo, which asks whether the current user is a member of the row's group, sharesTenant asks whether the user that column points to is in some tenant the current user also belongs to. It compiles to a SECURITY DEFINER STABLE helper (numbered with the table's other membership helpers), so there is no RLS recursion and Postgres can evaluate it once per value:

sql
CREATE OR REPLACE FUNCTION accessnexus_profiles_shares_tenant_1_fn(p_value uuid)
RETURNS boolean
LANGUAGE sql
STABLE
SECURITY DEFINER
SET search_path = pg_catalog, public
AS $$
  SELECT EXISTS (
    SELECT 1 FROM public.org_members me
    JOIN public.org_members them ON them.org_id = me.org_id
    WHERE me.user_id = (SELECT auth.uid()) AND them.user_id = p_value
  );
$$;
REVOKE EXECUTE ON FUNCTION accessnexus_profiles_shares_tenant_1_fn(uuid) FROM PUBLIC, anon;
GRANT EXECUTE ON FUNCTION accessnexus_profiles_shares_tenant_1_fn(uuid) TO authenticated;

The policy predicate is (SELECT accessnexus_profiles_shares_tenant_1_fn(id)). As with belongsTo, a helper used by a policy granted TO public is also granted to anon (it returns false without a user), and column counts as a policy column for the automatic index.

ts
import { policy } from 'accessnexus';

// profiles: your own row, plus the profiles of people you share an org with
export const profilePolicies = {
  select: policy.any([
    policy.isOwner('id'),
    policy.sharesTenant('id', { table: 'org_members', userColumn: 'user_id', tenantColumn: 'org_id' }),
  ]),
};

TIP

Never write a membership self-join in policy.custom for this: it recurses through the membership table's own RLS. Use sharesTenant.

matchesField ​

Two values must be equal.

ts
matchesField(fieldA: string, fieldB: string): MatchesFieldRule; // { kind: 'matches_field', fieldA, fieldB }
ParameterTypeDescription
fieldAstringColumn name or an auth call such as 'auth.uid()'.
fieldBstringColumn name or an auth call.

SQL: <fieldA> = <fieldB>. A bare auth.<fn>(...) side is wrapped as (SELECT auth.<fn>(...)). Role for select: public when neither side contains auth., else authenticated. Plain column names are checked by validate and indexed.

ts
import { policy } from 'accessnexus';

export const policies = { insert: policy.matchesField('author_id', 'auth.uid()') };
// author_id = (SELECT auth.uid())

custom ​

Raw SQL predicate, emitted verbatim.

ts
custom(expression: string, adapter?: string): CustomRule; // { kind: 'custom', expression, adapter }
ParameterTypeDescription
expressionstringSQL boolean expression evaluated per row. Must be non-empty (INVALID_POLICY_RULE).
adapterstringOptional adapter hint. Stored on the rule; the Supabase adapter ignores it.

Role for select: public (AccessNexus cannot tell whether the expression needs a user).

ts
import { policy } from 'accessnexus';

export const policies = { select: policy.custom("status = 'published' AND deleted_at IS NULL") };

WARNING

The expression is not quoted or validated as SQL. Never interpolate user input into it. Write (SELECT auth.uid()), never a bare auth.uid() (UNWRAPPED_AUTH_CALL warning). Do not re-implement membership checks with subqueries on the same table; use belongsTo (RLS recursion).

any ​

OR: passes when any rule passes.

ts
any(rules: PolicyRule[]): AnyRule; // { kind: 'any', rules }
ParameterTypeDescription
rulesPolicyRule[]At least one rule (EMPTY_COMBINATOR).

SQL: each rule in parentheses, joined by OR; a single rule is emitted without wrapping.

ts
import { policy } from 'accessnexus';

export const policies = {
  select: policy.any([policy.isOwner('author_id'), policy.custom("status = 'published'")]),
};

all ​

AND: passes only when every rule passes.

ts
all(rules: PolicyRule[]): AllRule; // { kind: 'all', rules }
ParameterTypeDescription
rulesPolicyRule[]At least one rule (EMPTY_COMBINATOR).

SQL: each rule in parentheses, joined by AND.

Not to be confused with the all key of a table's policies (policies: { all: rule }), which applies one rule to every operation. See Policies.

ts
import { policy } from 'accessnexus';

export const policies = {
  update: policy.all([policy.isOwner('user_id'), policy.custom('locked = false')]),
};

withCheck ​

Separate rules for the rows a user may target (USING) and the row being written (WITH CHECK).

ts
withCheck(
  using: PolicyRule,
  check: PolicyRule,
  options?: { mode?: WithCheckMode },
): WithCheckRule; // { kind: 'with_check', using, check, mode? }

type WithCheckMode = 'and' | 'replace';
ParameterTypeDefaultDescription
usingPolicyRulerequiredWhich existing rows the user may target. Also required of the new row unless mode: 'replace'.
checkPolicyRulerequiredCondition on the written row.
options.mode'and' | 'replace''and''and': WITH CHECK ((<using>) AND (<check>)). 'replace': WITH CHECK (<check>).
OperationUSINGWITH CHECK ('and')WITH CHECK ('replace')
select, deleteusingnonenone
insertnone(using) AND (check)check
updateusing(using) AND (check)check

The mode key is only present on the rule when passed. Nested withCheck inside any / all contributes its using to USING and its check expression to WITH CHECK.

ts
import { policy } from 'accessnexus';

export const policies = {
  // Authors edit their comments; every edit must set edited = true
  update: policy.withCheck(policy.isOwner('author_id'), policy.custom('edited = true')),
};

// The written row may leave the `using` condition (publishing a draft)
export const publishDraft = policy.withCheck(
  policy.all([policy.isOwner('author_id'), policy.custom("status = 'draft'")]),
  policy.all([policy.isOwner('author_id'), policy.custom("status IN ('draft', 'published')")]),
  { mode: 'replace' },
);

WARNING

mode: 'replace' drops using from WITH CHECK: an UPDATE may then move a row out of the user's ownership or tenant unless check repeats that condition. Keep the default unless the new row must legitimately leave using, and restate the ownership condition in check.

Types ​

TypeShape
PolicyOperation'select' | 'insert' | 'update' | 'delete'
PolicyRule{ readonly kind: string } (base of every rule)
TablePolicies{ [op in PolicyOperation]?: PolicyRule }
WithCheckMode'and' | 'replace'

The concrete rule interfaces (AuthenticatedRule, OwnerRule, BelongsToRule, ...) are the return types shown above; only PolicyRule, PolicyOperation, TablePolicies and WithCheckMode are exported from accessnexus.

Released under the MIT License.