Attachments
attachmentsFiles linked to records in a tenant: a record per file, a private bucket whose policies only accept an upload a pending record expects, and reads only for scanned files. confirm_attachment() emits attachment.uploaded for the app's scan job. options.publicBuckets serves chosen buckets by public URL, for media such as product images.
CollaborationReact hooksmanagedcustom
A
AdaCan you check the totals, @Sam?
S
SamFixed, thanks.
+2Resolved
Install
The command also installs updated-at, memberships, permissions, the modules it needs.
npx better-supabase add attachmentsConfigure
The blocks entries in your config, with the default mode.
import { defineConfig } from "better-supabase/config";
export default defineConfig({
blocks: {
// Required by attachments; add installs them too.
"updated-at": {},
memberships: {},
permissions: {},
// Modes: "managed", "custom". The default is "managed".
attachments: { mode: "managed" },
},
});Migrations
The files
npx better-supabase add attachments writes into your declarative schema with the default layout.9 files
supabase/schemas/900_better_supabase_034_attachments.sqlschema1415 lines-- better-supabase module: attachments
-- @bsb-block attachments@10 managed
-- @bsb-hash sha256:19fb6ec6aa825b7fe40d5158dab2fd0036310109c8d48e00d19348fbe8380a8f
-- Files linked to records in a tenant: a record per file, a private bucket whose policies only accept an upload a pending record expects, and reads only for scanned files. confirm_attachment() emits attachment.uploaded for the app's scan job. options.publicBuckets serves chosen buckets by public URL, for media such as product images.
-- Managed by `better-supabase add`; re-running it rewrites this file unless it was edited by hand.
-- Change it through `blocks` in better-supabase.config.ts and the module's SQL hooks.
-- Files linked to records. Each object lives at options.path (default
-- {organization_id}/attachments/{id}) in the attachments bucket, or its
-- subject's bucket; the record is created first, so the storage policies
-- below only accept an upload a pending record expects.
-- Document types (ADR 0157): a key and a name per organization, and the fields
-- a file of that type carries in its metadata: { <key>: { type, required?,
-- options? } } with the types text, number, amount, date, expiry, boolean, select.
create table "better_supabase"."attachment_document_types" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"key" text not null constraint bsb_attachment_document_types_key_check check ("key" ~ '^[a-z][a-z0-9_.-]{0,62}$'),
"name" text not null constraint bsb_attachment_document_types_name_check check (length(btrim("name")) between 1 and 200),
"fields" jsonb not null default '{}'::jsonb constraint bsb_attachment_document_types_fields_check check (jsonb_typeof("fields") = 'object'),
"created_at" timestamptz not null default now(),
"updated_at" timestamptz not null default now(),
unique ("tenant_id", "key")
);
alter table "better_supabase"."attachment_document_types" enable row level security;
revoke all on "better_supabase"."attachment_document_types" from public, anon, authenticated;
grant select on "better_supabase"."attachment_document_types" to authenticated;
grant all on "better_supabase"."attachment_document_types" to service_role;
create policy bsb_attachment_document_types_read on "better_supabase"."attachment_document_types"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('attachments.read')));
-- The reason a document type's field schema is invalid, or null.
create or replace function "better_supabase"."attachment_schema_error"(fields jsonb)
returns text
language plpgsql
immutable
set search_path = ''
as $$
declare
v_key text;
v_def jsonb;
v_type text;
begin
if jsonb_typeof(attachment_schema_error.fields) is distinct from 'object' then
return 'fields must be an object keyed by field key';
end if;
for v_key, v_def in select f.key, f.value from jsonb_each(attachment_schema_error.fields) f order by f.key loop
if v_key !~ '^[a-z][a-z0-9_]{0,62}$' then
return 'field key ' || v_key || ' must be lower case letters, digits and underscores';
end if;
if jsonb_typeof(v_def) is distinct from 'object' then
return v_key || ' must be an object with a type';
end if;
v_type := v_def ->> 'type';
if v_type is null or v_type not in ('text', 'number', 'amount', 'date', 'expiry', 'boolean', 'select') then
return v_key || ' needs one of the types text, number, amount, date, expiry, boolean, select';
end if;
if v_def ? 'required' and jsonb_typeof(v_def -> 'required') <> 'boolean' then
return v_key || ': required must be a boolean';
end if;
if v_type = 'select' and (jsonb_typeof(v_def -> 'options') is distinct from 'array' or jsonb_array_length(v_def -> 'options') = 0
or exists (select 1 from jsonb_array_elements(v_def -> 'options') o where jsonb_typeof(o.value) <> 'string')) then
return v_key || ': a select field needs a list of text options';
end if;
end loop;
return null;
end;
$$;
-- The reason a record's metadata doesn't match a document type's fields, or
-- null. A key the type doesn't declare is left alone.
create or replace function "better_supabase"."attachment_fields_error"(fields jsonb, data jsonb)
returns text
language plpgsql
stable
set search_path = ''
as $$
declare
v_data jsonb := coalesce(attachment_fields_error.data, '{}'::jsonb);
v_key text;
v_def jsonb;
v_value jsonb;
v_type text;
v_date date;
begin
if jsonb_typeof(v_data) is distinct from 'object' then
return 'metadata must be an object';
end if;
for v_key, v_def in select f.key, f.value from jsonb_each(attachment_fields_error.fields) f order by f.key loop
v_value := v_data -> v_key;
v_type := v_def ->> 'type';
if v_value is null or jsonb_typeof(v_value) = 'null' then
if coalesce((v_def ->> 'required')::boolean, false) then
return v_key || ' is required';
end if;
continue;
end if;
if v_type = 'text' then
if jsonb_typeof(v_value) <> 'string' then
return v_key || ' must be text';
end if;
elsif v_type = 'number' then
if jsonb_typeof(v_value) <> 'number' then
return v_key || ' must be a number';
end if;
elsif v_type = 'boolean' then
if jsonb_typeof(v_value) <> 'boolean' then
return v_key || ' must be true or false';
end if;
elsif v_type in ('date', 'expiry') then
if jsonb_typeof(v_value) <> 'string' or (v_value #>> '{}') !~ '^[0-9]{4}-[0-9]{2}-[0-9]{2}$' then
return v_key || ' must be a date as YYYY-MM-DD';
end if;
begin
v_date := (v_value #>> '{}')::date;
exception when others then
return v_key || ' must be a real date';
end;
if to_char(v_date, 'YYYY-MM-DD') <> (v_value #>> '{}') then
return v_key || ' must be a real date';
end if;
elsif v_type = 'amount' then
if jsonb_typeof(v_value) <> 'object'
or jsonb_typeof(v_value -> 'amount') not in ('string', 'number')
or (v_value ->> 'amount') !~ '^-?[0-9]{1,18}$'
or coalesce(v_value ->> 'currency', '') !~ '^[A-Z]{3}$' then
return v_key || ' must be { amount: minor units as an integer, currency: ISO code }';
end if;
elsif v_type = 'select' then
if jsonb_typeof(v_value) <> 'string' or not (v_def -> 'options') @> jsonb_build_array(v_value) then
return v_key || ' must be one of its options';
end if;
end if;
end loop;
return null;
end;
$$;
revoke execute on function "better_supabase"."attachment_schema_error"(jsonb) from public, anon;
revoke execute on function "better_supabase"."attachment_fields_error"(jsonb, jsonb) from public, anon;
grant execute on function "better_supabase"."attachment_schema_error"(jsonb) to authenticated, service_role;
grant execute on function "better_supabase"."attachment_fields_error"(jsonb, jsonb) to authenticated, service_role;
-- Creates or changes the document type with key in an organization.
create or replace function "better_supabase"."define_attachment_document_type"(tenant uuid, key text, name text, fields jsonb default '{}'::jsonb)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_row "better_supabase"."attachment_document_types";
v_error text;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(better_supabase.can('tenant', define_attachment_document_type.tenant, 'attachments.manage'), false)) then
raise exception 'not allowed to manage document types' using errcode = '42501', hint = 'ATTACHMENT_FORBIDDEN';
end if;
v_error := "better_supabase"."attachment_schema_error"(coalesce(define_attachment_document_type.fields, '{}'::jsonb));
if v_error is not null then
raise exception '%', v_error using errcode = '22023', hint = 'ATTACHMENT_DOCUMENT_SCHEMA_INVALID';
end if;
insert into "better_supabase"."attachment_document_types" as t ("tenant_id", "key", "name", "fields")
values (define_attachment_document_type.tenant, define_attachment_document_type.key, define_attachment_document_type.name, coalesce(define_attachment_document_type.fields, '{}'::jsonb))
on conflict ("tenant_id", "key") do update
set "name" = excluded."name", "fields" = excluded."fields", "updated_at" = now()
returning * into v_row;
return to_jsonb(v_row);
end;
$$;
-- The document types of an organization, by key.
create or replace function "better_supabase"."list_attachment_document_types"(tenant uuid)
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select coalesce(jsonb_agg(to_jsonb(t) order by t."key"), '[]'::jsonb)
from "better_supabase"."attachment_document_types" t
where t."tenant_id" = list_attachment_document_types.tenant
$$;
-- Deletes a document type that no attachment uses.
create or replace function "better_supabase"."delete_attachment_document_type"(tenant uuid, key text)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin') or coalesce(better_supabase.can('tenant', delete_attachment_document_type.tenant, 'attachments.manage'), false)) then
raise exception 'not allowed to manage document types' using errcode = '42501', hint = 'ATTACHMENT_FORBIDDEN';
end if;
delete from "better_supabase"."attachment_document_types" t
where t."tenant_id" = delete_attachment_document_type.tenant and t."key" = delete_attachment_document_type.key;
return found;
exception when foreign_key_violation then
raise exception 'attachments still use this document type' using errcode = '23503', hint = 'ATTACHMENT_DOCUMENT_TYPE_IN_USE';
end;
$$;
revoke execute on function "better_supabase"."define_attachment_document_type"(uuid, text, text, jsonb) from public, anon;
revoke execute on function "better_supabase"."list_attachment_document_types"(uuid) from public, anon;
revoke execute on function "better_supabase"."delete_attachment_document_type"(uuid, text) from public, anon;
grant execute on function "better_supabase"."define_attachment_document_type"(uuid, text, text, jsonb) to authenticated, service_role;
grant execute on function "better_supabase"."list_attachment_document_types"(uuid) to authenticated, service_role;
grant execute on function "better_supabase"."delete_attachment_document_type"(uuid, text) to authenticated, service_role;
create table "better_supabase"."attachments" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"subject_type" text constraint bsb_attachments_subject_type_check check ("subject_type" ~ '^[a-z][a-z0-9_-]{0,62}$'),
"subject_id" text constraint bsb_attachments_subject_id_check check (length("subject_id") >= 1 and length("subject_id") <= 200),
"bucket" text not null default 'attachments',
"object_path" text,
"name" text not null constraint bsb_attachments_name_check check (length("name") >= 1 and length("name") <= 255),
"mime_type" text not null constraint bsb_attachments_mime_type_check check ("mime_type" ~ '^[a-z0-9.+-]+/[a-z0-9.+-]+$'),
"size" bigint not null,
"status" text not null default 'pending' constraint bsb_attachments_status_check check ("status" in ('pending', 'clean', 'infected', 'failed')),
"scan_detail" text,
"uploaded_by" uuid references auth.users (id) on delete set null default auth.uid(),
"created_at" timestamptz not null default clock_timestamp(),
"uploaded_at" timestamptz,
"scanned_at" timestamptz,
constraint bsb_attachments_check check (("subject_type" is null) = ("subject_id" is null))
);
-- The current version (replace_attachment() adds one) and the image or
-- document size the client measured: width and height in pixels, pages.
alter table "better_supabase"."attachments" add column "version" integer not null default 1 constraint bsb_attachments_version_check check ("version" >= 1);
-- The SHA-256 of the file's bytes, hex, recorded by the scan step from the
-- stored object. A client never supplies it.
alter table "better_supabase"."attachments" add column "sha256" text constraint bsb_attachments_sha256_check check ("sha256" ~ '^[0-9a-f]{64}$');
alter table "better_supabase"."attachments" add column "hash_verified_at" timestamptz;
alter table "better_supabase"."attachments" add column "width" integer constraint bsb_attachments_width_check check ("width" > 0);
alter table "better_supabase"."attachments" add column "height" integer constraint bsb_attachments_height_check check ("height" > 0);
alter table "better_supabase"."attachments" add column "pages" integer constraint bsb_attachments_pages_check check ("pages" > 0);
-- App data about the file (a caption, the source, a page count), set on create.
alter table "better_supabase"."attachments" add column "metadata" jsonb not null default '{}' constraint bsb_attachments_metadata_check check (jsonb_typeof("metadata") = 'object');
-- Where a record came from and what it is (ADR 0157). storage_status follows
-- the upload: stored once uploaded_at is set, pending_upload until then, and
-- missing for a record whose file never arrived.
alter table "better_supabase"."attachments" add column "document_type_id" uuid references "better_supabase"."attachment_document_types" ("id") on delete restrict;
alter table "better_supabase"."attachments" add column "source" text constraint bsb_attachments_source_check check (length("source") >= 1 and length("source") <= 200);
alter table "better_supabase"."attachments" add column "external_id" text constraint bsb_attachments_external_id_check check (length("external_id") >= 1 and length("external_id") <= 500);
alter table "better_supabase"."attachments" add column "storage_status" text not null default 'pending_upload' constraint bsb_attachments_storage_status_check check ("storage_status" in ('stored', 'pending_upload', 'missing'));
alter table "better_supabase"."attachments" add column "source_path" text constraint bsb_attachments_source_path_check check (length("source_path") >= 1 and length("source_path") <= 2000);
alter table "better_supabase"."attachments" add constraint "bsb_attachments_external"
check (("source" is null) = ("external_id" is null));
create unique index attachments_external_idx on "better_supabase"."attachments" ("tenant_id", "source", "external_id") where "source" is not null;
create index attachments_document_type_idx on "better_supabase"."attachments" ("document_type_id") where "document_type_id" is not null;
create unique index "bsb_attachments_tenant_id_key" on "better_supabase"."attachments" ("tenant_id", "id");
-- The bucket, size limit and MIME types come from the module options, so a
-- re-run applies changed options to an existing table.
-- "object_path" is a plain column a trigger fills: logical replication (PowerSync)
-- skips generated columns. An install from before keeps its values when the
-- expression is dropped.
create or replace function "better_supabase"."attachment_object_path"()
returns trigger
language plpgsql
security invoker
set search_path = ''
as $$
begin
select "tenant_id"::text || '/attachments/' || "id"::text into new."object_path" from (select new.*) as r;
return new;
end;
$$;
revoke execute on function "better_supabase"."attachment_object_path"() from public, anon, authenticated;
create trigger bsb_attachments_object_path
before insert or update on "better_supabase"."attachments"
for each row execute function "better_supabase"."attachment_object_path"();
-- options.path: the data file recomputes the path of a row that no longer
-- matches the template, so a changed template rewrites it.
comment on column "better_supabase"."attachments"."object_path" is 'better-supabase path: {organization_id}/attachments/{id}';
alter table "better_supabase"."attachments" alter column "bucket" set default 'attachments';
alter table "better_supabase"."attachments" add constraint "bsb_attachments_bucket"
check ("bucket" in ('attachments')) not valid;
alter table "better_supabase"."attachments" add constraint "bsb_attachments_size"
check ("size" >= 0 and "size" <= 52428800);
create unique index attachments_object_idx on "better_supabase"."attachments" ("bucket", "object_path");
create index attachments_subject_idx on "better_supabase"."attachments" ("tenant_id", "subject_type", "subject_id");
create index attachments_subject_lookup_idx on "better_supabase"."attachments" ("subject_type", "subject_id");
create index attachments_uploaded_by_idx on "better_supabase"."attachments" ("uploaded_by");
create or replace function "better_supabase"."attachment_document_guard"()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
declare
v_types text[];
v_fields jsonb;
v_error text;
begin
if new."storage_status" in ('stored', 'pending_upload') or new."uploaded_at" is not null then
new."storage_status" := case when new."uploaded_at" is not null then 'stored' else
case when new."storage_status" = 'missing' then 'missing' else 'pending_upload' end end;
end if;
if new."source" is not null and (tg_op = 'INSERT' or new."source" is distinct from old."source" or new."external_id" is distinct from old."external_id") then
if not (new."source" = any (array[]::text[])) then
raise exception 'the source is not in options.sources and has no import run' using errcode = '22023', hint = 'ATTACHMENT_SOURCE_UNKNOWN';
end if;
if new."tenant_id" is not null and exists (
select 1 from "better_supabase"."attachments" x
where x."tenant_id" = new."tenant_id" and x."source" = new."source" and x."external_id" = new."external_id" and x."id" <> new."id"
) then
raise exception 'an attachment of this source already has this external id' using errcode = '23505', hint = 'ATTACHMENT_EXTERNAL_ID_TAKEN';
end if;
end if;
if new."document_type_id" is not null and (tg_op = 'INSERT' or new."document_type_id" is distinct from old."document_type_id" or new."metadata" is distinct from old."metadata") then
select y."fields" into v_fields from "better_supabase"."attachment_document_types" y
where y."id" = new."document_type_id" and y."tenant_id" = new."tenant_id";
if not found then
raise exception 'the document type doesn''t exist in this organization' using errcode = '22023', hint = 'ATTACHMENT_DOCUMENT_TYPE_UNKNOWN';
end if;
v_error := "better_supabase"."attachment_fields_error"(v_fields, new."metadata");
if v_error is not null then
raise exception '%', v_error using errcode = '22023', hint = 'ATTACHMENT_FIELD_INVALID';
end if;
end if;
return new;
end;
$$;
revoke execute on function "better_supabase"."attachment_document_guard"() from public, anon, authenticated;
create trigger bsb_attachments_documents
before insert or update on "better_supabase"."attachments"
for each row execute function "better_supabase"."attachment_document_guard"();
alter table "better_supabase"."attachments" enable row level security;
revoke all on "better_supabase"."attachments" from public, anon, authenticated;
grant select, delete on "better_supabase"."attachments" to authenticated;
grant insert ("tenant_id", "bucket", "subject_type", "subject_id", "name", "mime_type", "size", "metadata", "width", "height", "pages", "document_type_id", "source", "external_id", "source_path") on "better_supabase"."attachments" to authenticated;
grant all on "better_supabase"."attachments" to service_role;
-- Whether the caller may read a subject: its row is visible to them (the
-- subject table's own policies apply) and they hold its permission. Files
-- without a subject pass; with on, an unlisted type fails.
-- plpgsql, so a subject table a later module creates (messages) binds late.
create or replace function "better_supabase"."attachment_subject_readable"(subject_type text, subject_id text, tenant uuid)
returns boolean
language plpgsql
stable
security invoker
set search_path = ''
as $$
begin
return true;
end;
$$;
revoke execute on function "better_supabase"."attachment_subject_readable"(text, text, uuid) from public, anon;
grant execute on function "better_supabase"."attachment_subject_readable"(text, text, uuid) to authenticated, service_role;
create policy bsb_attachments_read on "better_supabase"."attachments"
for select to authenticated
using ("better_supabase"."attachments"."tenant_id" in (select better_supabase.tenant_ids_with('attachments.read')) and true);
create policy bsb_attachments_insert on "better_supabase"."attachments"
for insert to authenticated
with check (("uploaded_by" = (select auth.uid())
and "better_supabase"."attachments"."tenant_id" in (select better_supabase.tenant_ids_with('attachments.upload'))
and true) and (select better_supabase.actor_may_write()));
create policy bsb_attachments_delete on "better_supabase"."attachments"
for delete to authenticated
using (("uploaded_by" = (select auth.uid()) or "tenant_id" in (select better_supabase.tenant_ids_with('attachments.manage'))) and (select better_supabase.actor_may_write()));
-- Tells the tenant's topic which subject's files changed. The payload carries
-- ids only; files without a tenant broadcast nothing.
create or replace function "better_supabase"."attachments_changed"()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."attachments"%rowtype;
begin
if tg_op = 'DELETE' then
v_row := old;
else
v_row := new;
end if;
if v_row."tenant_id" is null then
return null;
end if;
if to_regprocedure('realtime.send(jsonb, text, text, boolean)') is not null then
perform realtime.send(jsonb_build_object('subjectType', v_row."subject_type", 'subjectId', v_row."subject_id"), 'changed', 'attachments:' || v_row."tenant_id"::text, true);
end if;
return null;
end;
$$;
revoke execute on function "better_supabase"."attachments_changed"() from public, anon, authenticated;
create trigger "bsb_attachments_changed"
after insert or update or delete on "better_supabase"."attachments"
for each row execute function "better_supabase"."attachments_changed"();
-- Members join attachments:{tenant}; payloads name the subject whose files changed.
create policy bsb_attachments_receive on realtime.messages
for select to authenticated
using (realtime.messages.extension = 'broadcast'
and (select realtime.topic()) like 'attachments:%'
and substr((select realtime.topic()), 13) in (select x::text from better_supabase.tenant_ids_with('attachments.read') x));
-- The earlier versions of each file, written by replace_attachment() and
-- restore_attachment_version(). A reader of the file reads its versions; the
-- rows change only through those functions.
create table "better_supabase"."attachment_versions" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"attachment_id" uuid not null references "better_supabase"."attachments" ("id") on delete cascade,
"version" integer not null constraint bsb_attachment_versions_version_check check ("version" >= 1),
"bucket" text not null,
"object_path" text not null,
"name" text not null,
"mime_type" text not null,
"size" bigint not null,
"width" integer,
"height" integer,
"pages" integer,
"status" text not null constraint bsb_attachment_versions_status_check check ("status" in ('pending', 'clean', 'infected', 'failed')),
"scan_detail" text,
"uploaded_by" uuid references auth.users (id) on delete set null,
"uploaded_at" timestamptz,
"scanned_at" timestamptz,
"sha256" text,
"hash_verified_at" timestamptz,
"archived_at" timestamptz not null default now(),
unique ("attachment_id", "version"),
unique ("bucket", "object_path")
);
create index attachment_versions_uploaded_by_idx on "better_supabase"."attachment_versions" ("uploaded_by");
alter table "better_supabase"."attachment_versions" enable row level security;
revoke all on "better_supabase"."attachment_versions" from public, anon, authenticated;
grant select on "better_supabase"."attachment_versions" to authenticated;
grant all on "better_supabase"."attachment_versions" to service_role;
create policy bsb_attachment_versions_read on "better_supabase"."attachment_versions"
for select to authenticated
using (exists (select 1 from "better_supabase"."attachments" a where a."id" = "attachment_id"));
-- Whether the caller may run action (insert, select or delete) on the
-- object at path in bucket: an upload needs the caller's pending record, a
-- read a clean file or the uploader or attachments.manage, a delete
-- the uploader or attachments.manage. It reads the table as its owner, so
-- the storage policies don't depend on the table's own policies.
create or replace function "better_supabase"."attachment_object_allowed"(bucket text, path text, action text)
returns boolean
language sql
stable
security definer
set search_path = ''
as $$
select exists (
select 1 from "better_supabase"."attachments" a
where a."bucket" = attachment_object_allowed.bucket and a."object_path" = any (array[attachment_object_allowed.path, regexp_replace(attachment_object_allowed.path, '-v[0-9]+$', '')]) and (case when a."version" > 1 then a."object_path" || '-v' || a."version"::text else a."object_path" end) = attachment_object_allowed.path
and case attachment_object_allowed.action
when 'insert' then a."status" = 'pending' and a."uploaded_at" is null and a."uploaded_by" = auth.uid()
when 'select' then (a."status" = 'clean' and coalesce(better_supabase.can('tenant', a."tenant_id", 'attachments.read'), false))
or a."uploaded_by" = auth.uid() or coalesce(better_supabase.can('tenant', a."tenant_id", 'attachments.manage'), false)
when 'delete' then a."uploaded_by" = auth.uid() or coalesce(better_supabase.can('tenant', a."tenant_id", 'attachments.manage'), false)
else false
end
) or exists (
select 1 from "better_supabase"."attachment_versions" v
join "better_supabase"."attachments" a on a."id" = v."attachment_id"
where v."bucket" = attachment_object_allowed.bucket and v."object_path" = attachment_object_allowed.path
and case attachment_object_allowed.action
when 'select' then (v."status" = 'clean' and coalesce(better_supabase.can('tenant', a."tenant_id", 'attachments.read'), false))
or v."uploaded_by" = auth.uid() or coalesce(better_supabase.can('tenant', a."tenant_id", 'attachments.manage'), false)
when 'delete' then a."uploaded_by" = auth.uid() or coalesce(better_supabase.can('tenant', a."tenant_id", 'attachments.manage'), false)
else false
end
)
$$;
revoke execute on function "better_supabase"."attachment_object_allowed"(text, text, text) from public, anon;
grant execute on function "better_supabase"."attachment_object_allowed"(text, text, text) to authenticated, service_role;
-- Whether the caller sees the object's record through the table's own read
-- policy, which checks the subject. It runs as the caller (security invoker):
-- attachment_object_allowed runs as its owner, so it can't apply the subject
-- table's policies itself.
create or replace function "better_supabase"."attachment_object_visible"(bucket text, path text)
returns boolean
language sql
stable
security invoker
set search_path = ''
as $$
select exists (
select 1 from "better_supabase"."attachments" a
where a."bucket" = attachment_object_visible.bucket and a."object_path" = any (array[attachment_object_visible.path, regexp_replace(attachment_object_visible.path, '-v[0-9]+$', '')]) and (case when a."version" > 1 then a."object_path" || '-v' || a."version"::text else a."object_path" end) = attachment_object_visible.path
) or exists (
select 1 from "better_supabase"."attachment_versions" v
where v."bucket" = attachment_object_visible.bucket and v."object_path" = attachment_object_visible.path
)
$$;
revoke execute on function "better_supabase"."attachment_object_visible"(text, text) from public, anon;
grant execute on function "better_supabase"."attachment_object_visible"(text, text) to authenticated, service_role;
create policy bsb_attachments_insert on storage.objects
for insert to authenticated
with check ((bucket_id in ('attachments') and "better_supabase"."attachment_object_allowed"(bucket_id, name, 'insert')) and (select better_supabase.actor_may_write()));
create policy bsb_attachments_select on storage.objects
for select to authenticated
using (bucket_id in ('attachments') and "better_supabase"."attachment_object_allowed"(bucket_id, name, 'select'));
create policy bsb_attachments_delete on storage.objects
for delete to authenticated
using ((bucket_id in ('attachments') and "better_supabase"."attachment_object_allowed"(bucket_id, name, 'delete')) and (select better_supabase.actor_may_write()));
-- The caller's new record, for a signed upload URL to its object_path.
create or replace function "better_supabase"."create_attachment"(
tenant uuid,
name text,
mime_type text,
size bigint,
subject_type text default null,
subject_id text default null,
metadata jsonb default null,
width integer default null,
height integer default null,
pages integer default null,
document_type text default null,
source text default null,
external_id text default null,
source_path text default null
)
returns jsonb
language plpgsql
security invoker
set search_path = ''
as $$
declare
v_row "better_supabase"."attachments";
v_columns text;
begin
v_row := jsonb_populate_record(null::"better_supabase"."attachments", (
select coalesce(jsonb_object_agg(e.field_key, e.field_value), '{}'::jsonb)
from jsonb_each('{}'::jsonb) as e (field_key, field_value)
where e.field_key = any (array[]::text[])
));
v_row."tenant_id" := create_attachment.tenant;
v_row."bucket" := 'attachments';
v_row."name" := create_attachment.name;
v_row."mime_type" := lower(create_attachment.mime_type);
v_row."size" := create_attachment.size;
v_row."subject_type" := create_attachment.subject_type;
v_row."subject_id" := create_attachment.subject_id;
v_row."metadata" := coalesce(create_attachment.metadata, '{}');
v_row."width" := create_attachment.width;
v_row."height" := create_attachment.height;
v_row."pages" := create_attachment.pages;
if create_attachment.document_type is not null then
select y."id" into v_row."document_type_id" from "better_supabase"."attachment_document_types" y
where y."tenant_id" = create_attachment.tenant and y."key" = create_attachment.document_type;
if not found then
raise exception 'the document type doesn''t exist in this organization' using errcode = '22023', hint = 'ATTACHMENT_DOCUMENT_TYPE_UNKNOWN';
end if;
end if;
v_row."source" := create_attachment.source;
v_row."external_id" := create_attachment.external_id;
v_row."source_path" := create_attachment.source_path;
select string_agg(column_name, ', ')
into v_columns
from unnest(array['"tenant_id"', '"bucket"', '"name"', '"mime_type"', '"size"', '"subject_type"', '"subject_id"', '"metadata"', '"width"', '"height"', '"pages"', '"document_type_id"', '"source"', '"external_id"', '"source_path"']
|| array(select quote_ident(k) from jsonb_object_keys('{}'::jsonb) as k
where k = any (array[]::text[]))) as column_name;
execute format('insert into "better_supabase"."attachments" as x (%1$s) select %1$s from (select ($1).*) r returning x.*', v_columns)
into v_row
using v_row;
return (to_jsonb(v_row) || jsonb_build_object('object_path', (case when v_row."version" > 1 then v_row."object_path" || '-v' || v_row."version"::text else v_row."object_path" end)));
end;
$$;
-- Marks the upload done once the object exists, taking its size and type
-- from Storage, and writes attachment.uploaded for the scan job.
-- For a file in another store (Vercel Blob), only the service role may pass object.
create or replace function "better_supabase"."confirm_attachment"(id uuid, object jsonb default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."attachments";
v_meta jsonb;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."attachments" a where a."id" = confirm_attachment.id for update;
if v_row."id" is null or (v_row."uploaded_by" is distinct from auth.uid() and not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin'))) then
raise exception 'No pending attachment of yours has this id' using errcode = 'P0002', hint = 'ATTACHMENT_NOT_FOUND';
end if;
if v_row."uploaded_at" is not null then
return (to_jsonb(v_row) || jsonb_build_object('object_path', (case when v_row."version" > 1 then v_row."object_path" || '-v' || v_row."version"::text else v_row."object_path" end)));
end if;
select o.metadata into v_meta from storage.objects o
where o.bucket_id = v_row."bucket" and o.name = (case when v_row."version" > 1 then v_row."object_path" || '-v' || v_row."version"::text else v_row."object_path" end);
if not found then
if confirm_attachment.object is null or not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin')) then
raise exception 'The file was not uploaded yet' using errcode = 'P0002', hint = 'ATTACHMENT_NOT_UPLOADED';
end if;
if (case when jsonb_typeof(confirm_attachment.object -> 'size') = 'number'
then (confirm_attachment.object ->> 'size')::numeric < 0
or (confirm_attachment.object ->> 'size')::numeric <> trunc((confirm_attachment.object ->> 'size')::numeric)
else true end)
or coalesce(confirm_attachment.object ->> 'mimetype', '') !~ '^[^/[:space:]]+/[^[:space:]]+$' then
raise exception 'object needs a whole size of 0 or more and a MIME type' using errcode = '22023', hint = 'ATTACHMENT_OBJECT_INVALID';
end if;
v_meta := jsonb_build_object('size', confirm_attachment.object -> 'size', 'mimetype', lower(confirm_attachment.object ->> 'mimetype'));
if exists (
select 1 from storage.buckets b
where b.id = v_row."bucket"
and ((b.file_size_limit is not null and (v_meta ->> 'size')::bigint > b.file_size_limit)
or (coalesce(cardinality(b.allowed_mime_types), 0) > 0
and not exists (select 1 from unnest(b.allowed_mime_types) m where v_meta ->> 'mimetype' like replace(lower(m), '*', '%'))))
) then
raise exception 'The file breaks the size limit or MIME types of its bucket' using errcode = '22023', hint = 'ATTACHMENT_OBJECT_REFUSED';
end if;
end if;
update "better_supabase"."attachments" a set
"uploaded_at" = now(),
"size" = coalesce((v_meta ->> 'size')::bigint, a."size"),
"mime_type" = coalesce(lower(v_meta ->> 'mimetype'), a."mime_type")
where a."id" = v_row."id"
returning * into v_row;
null;
return (to_jsonb(v_row) || jsonb_build_object('object_path', (case when v_row."version" > 1 then v_row."object_path" || '-v' || v_row."version"::text else v_row."object_path" end)));
end;
$$;
-- The caller takes the upload of a record whose file hasn't arrived (pending
-- upload or missing): they become its uploader, upload to its object_path and
-- call confirm_attachment. The service role leaves the uploader as it is.
create or replace function "better_supabase"."claim_attachment_upload"(id uuid)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_row "better_supabase"."attachments";
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."attachments" a where a."id" = claim_attachment_upload.id for update;
if v_row."id" is null then
raise exception 'No attachment has this id' using errcode = 'P0002', hint = 'ATTACHMENT_NOT_FOUND';
end if;
if not (coalesce(better_supabase.can('tenant', v_row."tenant_id", 'attachments.upload'), false) or coalesce(better_supabase.can('tenant', v_row."tenant_id", 'attachments.manage'), false) or (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin'))) then
raise exception 'not allowed to upload this attachment' using errcode = '42501', hint = 'ATTACHMENT_FORBIDDEN';
end if;
if v_row."uploaded_at" is not null or v_row."storage_status" = 'stored' then
raise exception 'the file is already stored' using errcode = 'PT409', hint = 'ATTACHMENT_NOT_CLAIMABLE';
end if;
update "better_supabase"."attachments" a set
"storage_status" = 'pending_upload',
"uploaded_by" = case when (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin')) then a."uploaded_by" else auth.uid() end
where a."id" = v_row."id"
returning * into v_row;
return (to_jsonb(v_row) || jsonb_build_object('object_path', (case when v_row."version" > 1 then v_row."object_path" || '-v' || v_row."version"::text else v_row."object_path" end)));
end;
$$;
-- Marks a record whose file will not arrive.
create or replace function "better_supabase"."mark_attachment_missing"(id uuid)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_row "better_supabase"."attachments";
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."attachments" a where a."id" = mark_attachment_missing.id for update;
if v_row."id" is null then
raise exception 'No attachment has this id' using errcode = 'P0002', hint = 'ATTACHMENT_NOT_FOUND';
end if;
if not (coalesce(better_supabase.can('tenant', v_row."tenant_id", 'attachments.manage'), false) or (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin'))) then
raise exception 'not allowed to manage this attachment' using errcode = '42501', hint = 'ATTACHMENT_FORBIDDEN';
end if;
if v_row."uploaded_at" is not null or v_row."storage_status" = 'stored' then
raise exception 'the file is already stored' using errcode = 'PT409', hint = 'ATTACHMENT_NOT_CLAIMABLE';
end if;
update "better_supabase"."attachments" a set "storage_status" = 'missing' where a."id" = v_row."id"
returning * into v_row;
return (to_jsonb(v_row) || jsonb_build_object('object_path', (case when v_row."version" > 1 then v_row."object_path" || '-v' || v_row."version"::text else v_row."object_path" end)));
end;
$$;
revoke execute on function "better_supabase"."claim_attachment_upload"(uuid) from public, anon;
revoke execute on function "better_supabase"."mark_attachment_missing"(uuid) from public, anon;
grant execute on function "better_supabase"."claim_attachment_upload"(uuid) to authenticated, service_role;
grant execute on function "better_supabase"."mark_attachment_missing"(uuid) to authenticated, service_role;
-- Records a scan result (service role). A clean or infected file keeps its
-- status, so a replayed job changes nothing.
create or replace function "better_supabase"."set_attachment_status"(id uuid, status text, detail text default null, sha256 text default null)
returns jsonb
language plpgsql
security invoker
set search_path = ''
as $$
declare
v_row "better_supabase"."attachments";
begin
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin')) then
raise exception 'Only the service role records scan results' using errcode = '42501', hint = 'ATTACHMENT_FORBIDDEN';
end if;
if set_attachment_status.status not in ('clean', 'infected', 'failed') then
raise exception 'status must be clean, infected or failed' using errcode = '22023', hint = 'ATTACHMENT_STATUS_UNKNOWN';
end if;
if set_attachment_status.sha256 is not null and set_attachment_status.sha256 !~ '^[0-9a-f]{64}$' then
raise exception 'sha256 must be 64 lowercase hex characters' using errcode = '22023', hint = 'ATTACHMENT_HASH_INVALID';
end if;
update "better_supabase"."attachments" a set
"status" = set_attachment_status.status,
"scan_detail" = set_attachment_status.detail,
"scanned_at" = now(),
"sha256" = coalesce(set_attachment_status.sha256, a."sha256"),
"hash_verified_at" = case when set_attachment_status.sha256 is null then a."hash_verified_at" else now() end
where a."id" = set_attachment_status.id
and a."uploaded_at" is not null
and a."status" in ('pending', 'failed')
returning * into v_row;
if v_row."id" is null then
select * into v_row from "better_supabase"."attachments" a where a."id" = set_attachment_status.id;
return case when v_row."id" is null then null else (to_jsonb(v_row) || jsonb_build_object('object_path', (case when v_row."version" > 1 then v_row."object_path" || '-v' || v_row."version"::text else v_row."object_path" end))) end;
end if;
insert into "better_supabase"."scanned_objects" ("bucket", "object_path", "status", "scan_detail", "scanned_at")
values (v_row."bucket", (case when v_row."version" > 1 then v_row."object_path" || '-v' || v_row."version"::text else v_row."object_path" end), v_row."status", v_row."scan_detail", now())
on conflict ("bucket", "object_path") do update
set "status" = excluded."status", "scan_detail" = excluded."scan_detail", "scanned_at" = excluded."scanned_at";
null;
return (to_jsonb(v_row) || jsonb_build_object('object_path', (case when v_row."version" > 1 then v_row."object_path" || '-v' || v_row."version"::text else v_row."object_path" end)));
end;
$$;
create or replace function "better_supabase"."list_attachments"(tenant uuid, subject_type text default null, subject_id text default null)
returns jsonb
language plpgsql
stable
security invoker
set search_path = ''
as $$
declare
v_files jsonb;
begin
with page as (
select * from "better_supabase"."attachments" y
where (y."tenant_id" = list_attachments.tenant or (y."tenant_id" is null and list_attachments.tenant is null))
and (list_attachments.subject_type is null or y."subject_type" = list_attachments.subject_type)
and (list_attachments.subject_id is null or y."subject_id" = list_attachments.subject_id)
)
select coalesce(jsonb_agg((to_jsonb(a) || jsonb_build_object('object_path', (case when a."version" > 1 then a."object_path" || '-v' || a."version"::text else a."object_path" end))) order by a."created_at", a."id"), '[]'::jsonb) into v_files
from page a;
return v_files;
end;
$$;
create or replace function "better_supabase"."attachment_counts"(tenant uuid, subject_type text, subject_ids text[])
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select coalesce(jsonb_object_agg(s.id, coalesce(n.total, 0)), '{}'::jsonb)
from (select distinct u as id from unnest(attachment_counts.subject_ids) u where u is not null limit 200) s
left join lateral (
select count(*) as total from "better_supabase"."attachments" y
where (y."tenant_id" = attachment_counts.tenant or (y."tenant_id" is null and attachment_counts.tenant is null))
and y."subject_type" = attachment_counts.subject_type
and y."subject_id" = s.id
) n on true
$$;
-- A keyset page of files, newest first (ADR 1077). Row security decides what
-- the caller sees. filters is { subjectType, subjectId, status, documentTypeId (each a value
-- or a list), search, sort (createdAt, name, size), direction, facet, count,
-- limit, after, offset, paged, total, totalCap }.
create or replace function "better_supabase"."page_attachments"(tenant uuid, filters jsonb default '{}')
returns jsonb
language plpgsql
stable
security invoker
set search_path = ''
as $$
#variable_conflict use_variable
declare
v_filters jsonb := coalesce(page_attachments.filters, '{}'::jsonb);
v_search text := case when nullif(trim(v_filters ->> 'search'), '') is null then null
else '%' || replace(replace(replace(trim(v_filters ->> 'search'), '\', '\\'), '%', '\%'), '_', '\_') || '%' end;
v_limit integer := least(greatest(coalesce((v_filters ->> 'limit')::integer, 50), 1), 200);
v_offset integer := greatest(coalesce((v_filters ->> 'offset')::integer, 0), 0);
v_after jsonb := v_filters -> 'after';
v_paged boolean := coalesce((v_filters ->> 'paged')::boolean, false);
v_total_cap integer := least(greatest(coalesce((v_filters ->> 'totalCap')::integer, 10000), 1), 100000);
v_items jsonb;
v_more boolean;
v_next jsonb;
v_total integer;
v_sort text := nullif(v_filters ->> 'sort', '');
v_dir text := nullif(v_filters ->> 'direction', '');
v_facet text := nullif(v_filters ->> 'facet', '');
v_count boolean := coalesce((v_filters ->> 'count')::boolean, false);
v_result jsonb;
begin
if v_filters ? 'after' and v_filters ? 'offset' then
raise exception 'after and offset are two ways to page; pass one' using errcode = '22023', hint = 'ATTACHMENT_INVALID';
end if;
if v_facet is not null then
if v_facet not in ('status', 'subjectType') then
raise exception 'facet is one of status, subjectType' using errcode = '22023', hint = 'ATTACHMENT_INVALID';
end if;
with s as (
select (case v_facet when 'status' then (x."status"::text)::text when 'subjectType' then (x."subject_type")::text end) as v
from "better_supabase"."attachments" x
where (x."tenant_id" = page_attachments.tenant or (x."tenant_id" is null and page_attachments.tenant is null))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (x."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (x."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'documentTypeId' is not null and jsonb_typeof(v_filters -> 'documentTypeId') <> 'null' and not (jsonb_typeof(v_filters -> 'documentTypeId') = 'array' and jsonb_array_length(v_filters -> 'documentTypeId') = 0)) or x."document_type_id" = any((case when jsonb_typeof(v_filters -> 'documentTypeId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'documentTypeId')) else array[v_filters ->> 'documentTypeId'] end)::uuid[]))
and (v_search is null or x."name" ilike v_search)
limit v_total_cap + 1
)
select jsonb_build_object('facet', v_facet, 'values', coalesce(jsonb_agg(jsonb_build_object('value', g.v, 'count', jsonb_build_object('value', g.n, 'exact', g.exact)) order by g.n desc, g.v), '[]'::jsonb))
into v_result
from (
select s.v, count(*) as n, (select count(*) from s) <= v_total_cap as exact
from s where s.v is not null group by s.v order by n desc, s.v limit 100
) g;
return v_result;
end if;
if v_count then
select count(*) into v_total from (
select 1 from "better_supabase"."attachments" x
where (x."tenant_id" = page_attachments.tenant or (x."tenant_id" is null and page_attachments.tenant is null))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (x."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (x."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'documentTypeId' is not null and jsonb_typeof(v_filters -> 'documentTypeId') <> 'null' and not (jsonb_typeof(v_filters -> 'documentTypeId') = 'array' and jsonb_array_length(v_filters -> 'documentTypeId') = 0)) or x."document_type_id" = any((case when jsonb_typeof(v_filters -> 'documentTypeId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'documentTypeId')) else array[v_filters ->> 'documentTypeId'] end)::uuid[]))
and (v_search is null or x."name" ilike v_search)
limit v_total_cap + 1
) c;
return jsonb_build_object('value', least(v_total, v_total_cap), 'exact', v_total <= v_total_cap);
end if;
if v_sort is not null and v_sort not in ('createdAt', 'name', 'size') then
raise exception 'sort is one of createdAt, name, size' using errcode = '22023', hint = 'ATTACHMENT_INVALID';
end if;
if v_dir is not null and v_dir not in ('asc', 'desc') then
raise exception 'direction is asc or desc' using errcode = '22023', hint = 'ATTACHMENT_INVALID';
end if;
if v_sort is null and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ATTACHMENT_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) || jsonb_build_object('object_path', (case when x."version" > 1 then x."object_path" || '-v' || x."version"::text else x."object_path" end))) as j, jsonb_build_array((x."created_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."attachments" x
where (x."tenant_id" = page_attachments.tenant or (x."tenant_id" is null and page_attachments.tenant is null))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (x."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (x."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'documentTypeId' is not null and jsonb_typeof(v_filters -> 'documentTypeId') <> 'null' and not (jsonb_typeof(v_filters -> 'documentTypeId') = 'array' and jsonb_array_length(v_filters -> 'documentTypeId') = 0)) or x."document_type_id" = any((case when jsonb_typeof(v_filters -> 'documentTypeId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'documentTypeId')) else array[v_filters ->> 'documentTypeId'] end)::uuid[]))
and (v_search is null or x."name" ilike v_search)
and (v_after is null or (x."created_at", x."id") > ((v_after ->> 0)::timestamptz, (v_after ->> 1)::uuid))
order by x."created_at" asc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort is null and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ATTACHMENT_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) || jsonb_build_object('object_path', (case when x."version" > 1 then x."object_path" || '-v' || x."version"::text else x."object_path" end))) as j, jsonb_build_array((x."created_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."attachments" x
where (x."tenant_id" = page_attachments.tenant or (x."tenant_id" is null and page_attachments.tenant is null))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (x."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (x."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'documentTypeId' is not null and jsonb_typeof(v_filters -> 'documentTypeId') <> 'null' and not (jsonb_typeof(v_filters -> 'documentTypeId') = 'array' and jsonb_array_length(v_filters -> 'documentTypeId') = 0)) or x."document_type_id" = any((case when jsonb_typeof(v_filters -> 'documentTypeId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'documentTypeId')) else array[v_filters ->> 'documentTypeId'] end)::uuid[]))
and (v_search is null or x."name" ilike v_search)
and (v_after is null or (x."created_at" <= (v_after ->> 0)::timestamptz and ((x."created_at" < (v_after ->> 0)::timestamptz) or (x."created_at" = (v_after ->> 0)::timestamptz and x."id" > (v_after ->> 1)::uuid))))
order by x."created_at" desc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'createdAt' and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ATTACHMENT_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) || jsonb_build_object('object_path', (case when x."version" > 1 then x."object_path" || '-v' || x."version"::text else x."object_path" end))) as j, jsonb_build_array((x."created_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."attachments" x
where (x."tenant_id" = page_attachments.tenant or (x."tenant_id" is null and page_attachments.tenant is null))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (x."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (x."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'documentTypeId' is not null and jsonb_typeof(v_filters -> 'documentTypeId') <> 'null' and not (jsonb_typeof(v_filters -> 'documentTypeId') = 'array' and jsonb_array_length(v_filters -> 'documentTypeId') = 0)) or x."document_type_id" = any((case when jsonb_typeof(v_filters -> 'documentTypeId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'documentTypeId')) else array[v_filters ->> 'documentTypeId'] end)::uuid[]))
and (v_search is null or x."name" ilike v_search)
and (v_after is null or (x."created_at", x."id") > ((v_after ->> 0)::timestamptz, (v_after ->> 1)::uuid))
order by x."created_at" asc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'createdAt' and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ATTACHMENT_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) || jsonb_build_object('object_path', (case when x."version" > 1 then x."object_path" || '-v' || x."version"::text else x."object_path" end))) as j, jsonb_build_array((x."created_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."attachments" x
where (x."tenant_id" = page_attachments.tenant or (x."tenant_id" is null and page_attachments.tenant is null))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (x."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (x."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'documentTypeId' is not null and jsonb_typeof(v_filters -> 'documentTypeId') <> 'null' and not (jsonb_typeof(v_filters -> 'documentTypeId') = 'array' and jsonb_array_length(v_filters -> 'documentTypeId') = 0)) or x."document_type_id" = any((case when jsonb_typeof(v_filters -> 'documentTypeId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'documentTypeId')) else array[v_filters ->> 'documentTypeId'] end)::uuid[]))
and (v_search is null or x."name" ilike v_search)
and (v_after is null or (x."created_at" <= (v_after ->> 0)::timestamptz and ((x."created_at" < (v_after ->> 0)::timestamptz) or (x."created_at" = (v_after ->> 0)::timestamptz and x."id" > (v_after ->> 1)::uuid))))
order by x."created_at" desc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'name' and coalesce(v_dir, 'asc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ATTACHMENT_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) || jsonb_build_object('object_path', (case when x."version" > 1 then x."object_path" || '-v' || x."version"::text else x."object_path" end))) as j, jsonb_build_array((lower(x."name"))::text, (x."id")::text) as k
from (
select * from "better_supabase"."attachments" x
where (x."tenant_id" = page_attachments.tenant or (x."tenant_id" is null and page_attachments.tenant is null))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (x."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (x."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'documentTypeId' is not null and jsonb_typeof(v_filters -> 'documentTypeId') <> 'null' and not (jsonb_typeof(v_filters -> 'documentTypeId') = 'array' and jsonb_array_length(v_filters -> 'documentTypeId') = 0)) or x."document_type_id" = any((case when jsonb_typeof(v_filters -> 'documentTypeId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'documentTypeId')) else array[v_filters ->> 'documentTypeId'] end)::uuid[]))
and (v_search is null or x."name" ilike v_search)
and (v_after is null or (lower(x."name"), x."id") > ((v_after ->> 0)::text, (v_after ->> 1)::uuid))
order by lower(x."name") asc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'name' and coalesce(v_dir, 'asc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ATTACHMENT_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) || jsonb_build_object('object_path', (case when x."version" > 1 then x."object_path" || '-v' || x."version"::text else x."object_path" end))) as j, jsonb_build_array((lower(x."name"))::text, (x."id")::text) as k
from (
select * from "better_supabase"."attachments" x
where (x."tenant_id" = page_attachments.tenant or (x."tenant_id" is null and page_attachments.tenant is null))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (x."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (x."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'documentTypeId' is not null and jsonb_typeof(v_filters -> 'documentTypeId') <> 'null' and not (jsonb_typeof(v_filters -> 'documentTypeId') = 'array' and jsonb_array_length(v_filters -> 'documentTypeId') = 0)) or x."document_type_id" = any((case when jsonb_typeof(v_filters -> 'documentTypeId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'documentTypeId')) else array[v_filters ->> 'documentTypeId'] end)::uuid[]))
and (v_search is null or x."name" ilike v_search)
and (v_after is null or (lower(x."name") <= (v_after ->> 0)::text and ((lower(x."name") < (v_after ->> 0)::text) or (lower(x."name") = (v_after ->> 0)::text and x."id" > (v_after ->> 1)::uuid))))
order by lower(x."name") desc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'size' and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ATTACHMENT_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) || jsonb_build_object('object_path', (case when x."version" > 1 then x."object_path" || '-v' || x."version"::text else x."object_path" end))) as j, jsonb_build_array((coalesce(x."size", 0))::text, (x."id")::text) as k
from (
select * from "better_supabase"."attachments" x
where (x."tenant_id" = page_attachments.tenant or (x."tenant_id" is null and page_attachments.tenant is null))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (x."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (x."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'documentTypeId' is not null and jsonb_typeof(v_filters -> 'documentTypeId') <> 'null' and not (jsonb_typeof(v_filters -> 'documentTypeId') = 'array' and jsonb_array_length(v_filters -> 'documentTypeId') = 0)) or x."document_type_id" = any((case when jsonb_typeof(v_filters -> 'documentTypeId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'documentTypeId')) else array[v_filters ->> 'documentTypeId'] end)::uuid[]))
and (v_search is null or x."name" ilike v_search)
and (v_after is null or (coalesce(x."size", 0), x."id") > ((v_after ->> 0)::bigint, (v_after ->> 1)::uuid))
order by coalesce(x."size", 0) asc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'size' and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ATTACHMENT_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, (to_jsonb(x) || jsonb_build_object('object_path', (case when x."version" > 1 then x."object_path" || '-v' || x."version"::text else x."object_path" end))) as j, jsonb_build_array((coalesce(x."size", 0))::text, (x."id")::text) as k
from (
select * from "better_supabase"."attachments" x
where (x."tenant_id" = page_attachments.tenant or (x."tenant_id" is null and page_attachments.tenant is null))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (x."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (x."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'documentTypeId' is not null and jsonb_typeof(v_filters -> 'documentTypeId') <> 'null' and not (jsonb_typeof(v_filters -> 'documentTypeId') = 'array' and jsonb_array_length(v_filters -> 'documentTypeId') = 0)) or x."document_type_id" = any((case when jsonb_typeof(v_filters -> 'documentTypeId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'documentTypeId')) else array[v_filters ->> 'documentTypeId'] end)::uuid[]))
and (v_search is null or x."name" ilike v_search)
and (v_after is null or (coalesce(x."size", 0) <= (v_after ->> 0)::bigint and ((coalesce(x."size", 0) < (v_after ->> 0)::bigint) or (coalesce(x."size", 0) = (v_after ->> 0)::bigint and x."id" > (v_after ->> 1)::uuid))))
order by coalesce(x."size", 0) desc, x."id" asc
limit v_limit + 1 offset v_offset
) x
) p;
end if;
if not v_paged then
return v_items;
end if;
if coalesce((v_filters ->> 'total')::boolean, false) then
select count(*) into v_total from (
select 1 from "better_supabase"."attachments" x
where (x."tenant_id" = page_attachments.tenant or (x."tenant_id" is null and page_attachments.tenant is null))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (x."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (x."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or x."status"::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'documentTypeId' is not null and jsonb_typeof(v_filters -> 'documentTypeId') <> 'null' and not (jsonb_typeof(v_filters -> 'documentTypeId') = 'array' and jsonb_array_length(v_filters -> 'documentTypeId') = 0)) or x."document_type_id" = any((case when jsonb_typeof(v_filters -> 'documentTypeId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'documentTypeId')) else array[v_filters ->> 'documentTypeId'] end)::uuid[]))
and (v_search is null or x."name" ilike v_search)
limit v_total_cap + 1
) c;
end if;
return jsonb_build_object('items', v_items, 'hasMore', v_more, 'nextCursor', case when v_more then v_next end)
|| case when v_total is null then '{}'::jsonb
else jsonb_build_object('total', least(v_total, v_total_cap), 'totalCapped', v_total > v_total_cap) end;
end;
$$;
revoke execute on function "better_supabase"."page_attachments"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."page_attachments"(uuid, jsonb) to authenticated, service_role;
create or replace function "better_supabase"."get_attachment"(id uuid)
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select (to_jsonb(a) || jsonb_build_object('object_path', (case when a."version" > 1 then a."object_path" || '-v' || a."version"::text else a."object_path" end))) from "better_supabase"."attachments" a where a."id" = get_attachment.id
$$;
-- Starts a new version of a file: the current one, once uploaded, moves to
-- attachment_versions, and the caller uploads the new file to the returned
-- object_path and confirms it, which scans it again. A file whose upload
-- never finished is replaced in place.
create or replace function "better_supabase"."replace_attachment"(
id uuid,
name text,
mime_type text,
size bigint,
width integer default null,
height integer default null,
pages integer default null
)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_row "better_supabase"."attachments";
v_next integer;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."attachments" a where a."id" = replace_attachment.id for update;
if v_row."id" is null or not ((coalesce(v_row."uploaded_by" = auth.uid(), false) and coalesce(better_supabase.can('tenant', v_row."tenant_id", 'attachments.upload'), false)) or coalesce(better_supabase.can('tenant', v_row."tenant_id", 'attachments.manage'), false) or (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin'))) then
raise exception 'No attachment you may replace has this id' using errcode = 'P0002', hint = 'ATTACHMENT_NOT_FOUND';
end if;
if v_row."uploaded_at" is not null then
insert into "better_supabase"."attachment_versions" ("tenant_id", "attachment_id", "version", "bucket", "object_path", "name", "mime_type", "size", "width", "height", "pages", "status", "scan_detail", "uploaded_by", "uploaded_at", "scanned_at", "sha256", "hash_verified_at")
values (v_row."tenant_id", v_row."id", v_row."version", v_row."bucket", (case when v_row."version" > 1 then v_row."object_path" || '-v' || v_row."version"::text else v_row."object_path" end), v_row."name", v_row."mime_type", v_row."size", v_row."width", v_row."height", v_row."pages", v_row."status", v_row."scan_detail", v_row."uploaded_by", v_row."uploaded_at", v_row."scanned_at", v_row."sha256", v_row."hash_verified_at");
end if;
select greatest(v_row."version" - case when v_row."uploaded_at" is null then 1 else 0 end, coalesce(max(v."version"), 0)) + 1
into v_next
from "better_supabase"."attachment_versions" v where v."attachment_id" = v_row."id";
update "better_supabase"."attachments" a set
"version" = v_next,
"name" = replace_attachment.name,
"mime_type" = lower(replace_attachment.mime_type),
"size" = replace_attachment.size,
"width" = replace_attachment.width,
"height" = replace_attachment.height,
"pages" = replace_attachment.pages,
"status" = 'pending',
"scan_detail" = null,
"uploaded_by" = coalesce(auth.uid(), a."uploaded_by"),
"uploaded_at" = null,
"scanned_at" = null,
"sha256" = null,
"hash_verified_at" = null
where a."id" = v_row."id"
returning * into v_row;
null;
return (to_jsonb(v_row) || jsonb_build_object('object_path', (case when v_row."version" > 1 then v_row."object_path" || '-v' || v_row."version"::text else v_row."object_path" end)));
end;
$$;
-- Makes an earlier version current again; the current one, once uploaded,
-- becomes an earlier version. No object moves.
create or replace function "better_supabase"."restore_attachment_version"(id uuid, version integer)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_row "better_supabase"."attachments";
v_old "better_supabase"."attachment_versions";
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_row from "better_supabase"."attachments" a where a."id" = restore_attachment_version.id for update;
if v_row."id" is null or not ((coalesce(v_row."uploaded_by" = auth.uid(), false) and coalesce(better_supabase.can('tenant', v_row."tenant_id", 'attachments.upload'), false)) or coalesce(better_supabase.can('tenant', v_row."tenant_id", 'attachments.manage'), false) or (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin'))) then
raise exception 'No attachment you may restore has this id' using errcode = 'P0002', hint = 'ATTACHMENT_NOT_FOUND';
end if;
select * into v_old from "better_supabase"."attachment_versions" v
where v."attachment_id" = v_row."id" and v."version" = restore_attachment_version.version
for update;
if v_old."id" is null then
raise exception 'The file has no earlier version with this number' using errcode = 'P0002', hint = 'ATTACHMENT_VERSION_NOT_FOUND';
end if;
delete from "better_supabase"."attachment_versions" v where v."id" = v_old."id";
if v_row."uploaded_at" is not null then
insert into "better_supabase"."attachment_versions" ("tenant_id", "attachment_id", "version", "bucket", "object_path", "name", "mime_type", "size", "width", "height", "pages", "status", "scan_detail", "uploaded_by", "uploaded_at", "scanned_at", "sha256", "hash_verified_at")
values (v_row."tenant_id", v_row."id", v_row."version", v_row."bucket", (case when v_row."version" > 1 then v_row."object_path" || '-v' || v_row."version"::text else v_row."object_path" end), v_row."name", v_row."mime_type", v_row."size", v_row."width", v_row."height", v_row."pages", v_row."status", v_row."scan_detail", v_row."uploaded_by", v_row."uploaded_at", v_row."scanned_at", v_row."sha256", v_row."hash_verified_at");
end if;
update "better_supabase"."attachments" a set
"version" = v_old."version",
"name" = v_old."name",
"mime_type" = v_old."mime_type",
"size" = v_old."size",
"width" = v_old."width",
"height" = v_old."height",
"pages" = v_old."pages",
"status" = v_old."status",
"scan_detail" = v_old."scan_detail",
"uploaded_by" = v_old."uploaded_by",
"uploaded_at" = v_old."uploaded_at",
"scanned_at" = v_old."scanned_at",
"sha256" = v_old."sha256",
"hash_verified_at" = v_old."hash_verified_at"
where a."id" = v_row."id"
returning * into v_row;
null;
return (to_jsonb(v_row) || jsonb_build_object('object_path', (case when v_row."version" > 1 then v_row."object_path" || '-v' || v_row."version"::text else v_row."object_path" end)));
end;
$$;
-- A file's earlier versions, newest first.
create or replace function "better_supabase"."list_attachment_versions"(id uuid)
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select coalesce(jsonb_agg(to_jsonb(v) order by v."version" desc), '[]'::jsonb)
from "better_supabase"."attachment_versions" v where v."attachment_id" = list_attachment_versions.id
$$;
revoke execute on function "better_supabase"."replace_attachment"(uuid, text, text, bigint, integer, integer, integer) from public, anon;
revoke execute on function "better_supabase"."restore_attachment_version"(uuid, integer) from public, anon;
revoke execute on function "better_supabase"."list_attachment_versions"(uuid) from public, anon;
grant execute on function "better_supabase"."replace_attachment"(uuid, text, text, bigint, integer, integer, integer) to authenticated, service_role;
grant execute on function "better_supabase"."restore_attachment_version"(uuid, integer) to authenticated, service_role;
grant execute on function "better_supabase"."list_attachment_versions"(uuid) to authenticated, service_role;
create or replace function "better_supabase"."delete_attachment"(id uuid)
returns boolean
language sql
security invoker
set search_path = ''
as $$
with removed as (
delete from "better_supabase"."attachments" a where a."id" = delete_attachment.id returning 1
)
select exists (select 1 from removed)
$$;
-- The scan gate for any bucket: one row per scanned object, object_clean()
-- for storage policies, and set_object_scan() for the scanner. With
-- options.scanBuckets, a new object in those buckets gets a pending row and
-- writes object.uploaded for the scan job.
create table "better_supabase"."scanned_objects" (
"bucket" text not null,
"object_path" text not null,
"status" text not null default 'pending' constraint bsb_scanned_objects_status_check check ("status" in ('pending', 'clean', 'infected', 'failed')),
"scan_detail" text,
"created_at" timestamptz not null default now(),
"scanned_at" timestamptz,
primary key ("bucket", "object_path")
);
alter table "better_supabase"."scanned_objects" enable row level security;
revoke all on "better_supabase"."scanned_objects" from public, anon, authenticated;
grant all on "better_supabase"."scanned_objects" to service_role;
-- For storage policies: whether the object was scanned and found clean.
-- using (bucket_id = 'files' and better_supabase.object_clean(bucket_id, name) and ...)
create or replace function "better_supabase"."object_clean"(bucket text, path text)
returns boolean
language sql
stable
security definer
set search_path = ''
as $$
select exists (
select 1 from "better_supabase"."scanned_objects" s
where s."bucket" = object_clean.bucket and s."object_path" = object_clean.path and s."status" = 'clean'
)
$$;
-- The scan row of an object, or null (service role).
create or replace function "better_supabase"."object_scan"(bucket text, path text)
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select to_jsonb(s.*) from "better_supabase"."scanned_objects" s
where s."bucket" = object_scan.bucket and s."object_path" = object_scan.path
$$;
-- Records a scan result for any object (service role). A clean or infected
-- object keeps its status, so a replayed job changes nothing.
create or replace function "better_supabase"."set_object_scan"(bucket text, path text, status text, detail text default null)
returns jsonb
language plpgsql
security invoker
set search_path = ''
as $$
#variable_conflict use_column
declare
v_row "better_supabase"."scanned_objects";
begin
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin')) then
raise exception 'Only the service role records scan results' using errcode = '42501', hint = 'ATTACHMENT_FORBIDDEN';
end if;
if set_object_scan.status not in ('pending', 'clean', 'infected', 'failed') then
raise exception 'status must be pending, clean, infected or failed' using errcode = '22023', hint = 'ATTACHMENT_STATUS_UNKNOWN';
end if;
insert into "better_supabase"."scanned_objects" as s ("bucket", "object_path", "status", "scan_detail", "scanned_at")
values (set_object_scan.bucket, set_object_scan.path, set_object_scan.status, set_object_scan.detail,
case when set_object_scan.status = 'pending' then null else now() end)
on conflict ("bucket", "object_path") do update
set "status" = excluded."status", "scan_detail" = excluded."scan_detail", "scanned_at" = excluded."scanned_at"
where s."status" in ('pending', 'failed')
returning * into v_row;
if v_row."bucket" is null then
select * into v_row from "better_supabase"."scanned_objects" s where s."bucket" = set_object_scan.bucket and s."object_path" = set_object_scan.path;
end if;
return to_jsonb(v_row);
end;
$$;
revoke execute on function "better_supabase"."object_clean"(text, text) from public, anon;
revoke execute on function "better_supabase"."object_scan"(text, text) from public, anon, authenticated;
revoke execute on function "better_supabase"."set_object_scan"(text, text, text, text) from public, anon, authenticated;
grant execute on function "better_supabase"."object_clean"(text, text) to authenticated, service_role;
grant execute on function "better_supabase"."object_scan"(text, text) to service_role;
grant execute on function "better_supabase"."set_object_scan"(text, text, text, text) to service_role;
revoke execute on function "better_supabase"."create_attachment"(uuid, text, text, bigint, text, text, jsonb, integer, integer, integer, text, text, text, text) from public, anon;
revoke execute on function "better_supabase"."confirm_attachment"(uuid, jsonb) from public, anon;
revoke execute on function "better_supabase"."set_attachment_status"(uuid, text, text, text) from public, anon, authenticated;
revoke execute on function "better_supabase"."list_attachments"(uuid, text, text) from public, anon;
revoke execute on function "better_supabase"."get_attachment"(uuid) from public, anon;
revoke execute on function "better_supabase"."delete_attachment"(uuid) from public, anon;
grant execute on function "better_supabase"."create_attachment"(uuid, text, text, bigint, text, text, jsonb, integer, integer, integer, text, text, text, text) to authenticated, service_role;
grant execute on function "better_supabase"."confirm_attachment"(uuid, jsonb) to authenticated, service_role;
grant execute on function "better_supabase"."set_attachment_status"(uuid, text, text, text) to service_role;
grant execute on function "better_supabase"."list_attachments"(uuid, text, text) to authenticated, service_role;
revoke execute on function "better_supabase"."attachment_counts"(uuid, text, text[]) from public, anon;
grant execute on function "better_supabase"."attachment_counts"(uuid, text, text[]) to authenticated, service_role;
grant execute on function "better_supabase"."get_attachment"(uuid) to authenticated, service_role;
grant execute on function "better_supabase"."delete_attachment"(uuid) to authenticated, service_role;
-- Seeds the rows every organization starts with for attachments. Safe to run
-- again: a row whose key exists is kept. It is not an entry point.
create or replace function "better_supabase"."seed_attachments_defaults"(p_tenant uuid, p_variant text default null, p_locale text default 'en')
returns void
language plpgsql
security definer
set search_path = ''
as $$
begin
insert into "better_supabase"."attachment_document_types" ("tenant_id", "key", "name", "fields")
select p_tenant, 'contract', 'Contract', '{"counterparty":{"type":"text","required":true},"signed_on":{"type":"date"},"expires_on":{"type":"expiry"}}'::jsonb
where not exists (select 1 from "better_supabase"."attachment_document_types" x where x."tenant_id" = p_tenant and x."key" is not distinct from 'contract');
insert into "better_supabase"."attachment_document_types" ("tenant_id", "key", "name", "fields")
select p_tenant, 'invoice', 'Invoice', '{"number":{"type":"text","required":true},"issued_on":{"type":"date"},"amount":{"type":"amount"}}'::jsonb
where not exists (select 1 from "better_supabase"."attachment_document_types" x where x."tenant_id" = p_tenant and x."key" is not distinct from 'invoice');
insert into "better_supabase"."attachment_document_types" ("tenant_id", "key", "name", "fields")
select p_tenant, 'receipt', 'Receipt', '{"merchant":{"type":"text"},"paid_on":{"type":"date"},"amount":{"type":"amount"}}'::jsonb
where not exists (select 1 from "better_supabase"."attachment_document_types" x where x."tenant_id" = p_tenant and x."key" is not distinct from 'receipt');
insert into "better_supabase"."attachment_document_types" ("tenant_id", "key", "name", "fields")
select p_tenant, 'identity', 'Identity document', '{"number":{"type":"text"},"expires_on":{"type":"expiry"}}'::jsonb
where not exists (select 1 from "better_supabase"."attachment_document_types" x where x."tenant_id" = p_tenant and x."key" is not distinct from 'identity');
insert into "better_supabase"."attachment_document_types" ("tenant_id", "key", "name", "fields")
select p_tenant, 'report', 'Report', '{}'::jsonb
where not exists (select 1 from "better_supabase"."attachment_document_types" x where x."tenant_id" = p_tenant and x."key" is not distinct from 'report');
insert into "better_supabase"."attachment_document_types" ("tenant_id", "key", "name", "fields")
select p_tenant, 'other', 'Other', '{}'::jsonb
where not exists (select 1 from "better_supabase"."attachment_document_types" x where x."tenant_id" = p_tenant and x."key" is not distinct from 'other');
end;
$$;
revoke execute on function "better_supabase"."seed_attachments_defaults"(uuid, text, text) from public, anon, authenticated;
grant execute on function "better_supabase"."seed_attachments_defaults"(uuid, text, text) to service_role;
-- Tenant isolation (ADR 0099): permissive policies combine with or, so one
-- the app adds would widen the block's. These restrictive policies keep each
-- table to the caller's organizations and what the block's policies grant.
create policy bsb_tenant_scope_select on "better_supabase"."attachments" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("better_supabase"."attachments"."tenant_id" in (select better_supabase.tenant_ids_with('attachments.read')) and true)
);
create policy bsb_tenant_scope_insert on "better_supabase"."attachments" as restrictive
for insert to public
with check (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("uploaded_by" = (select auth.uid())
and "better_supabase"."attachments"."tenant_id" in (select better_supabase.tenant_ids_with('attachments.upload'))
and true)
);
create policy bsb_tenant_scope_update on "better_supabase"."attachments" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."attachments" as restrictive
for delete to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("uploaded_by" = (select auth.uid()) or "tenant_id" in (select better_supabase.tenant_ids_with('attachments.manage')))
);
create policy bsb_tenant_scope_select on "better_supabase"."attachment_document_types" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('attachments.read')))
);
create policy bsb_tenant_scope_insert on "better_supabase"."attachment_document_types" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."attachment_document_types" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."attachment_document_types" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."attachment_versions" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or (exists (select 1 from "better_supabase"."attachments" a where a."id" = "attachment_id"))
);
create policy bsb_tenant_scope_insert on "better_supabase"."attachment_versions" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."attachment_versions" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."attachment_versions" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
Client
The React hooks are the registry item @better-supabase/use-attachments, which shadcn copies into your app. They return data and actions and render no UI, so you build the interface from your own components.
Events
With the outbox installed, the module writes these in the same transaction as the change.
- attachment.replaced
- attachment.restored
- attachment.scanned
- attachment.uploaded
Dependencies
- Required: the install command adds it too
- Works with: optional
Requires
Recipes
More in Collaboration
Commentscomments Comments on any record as its conversation on the messaging core: whoever reads the record reads them, with replies, mentions and mention groups, anchors, files, edits with history, pins, resolved threads and read markers. The author and the mentioned follow the record and its followers hear of new comments (with notifications), and a comment becomes a work item (with tasks).React hooks Share linksshare-links Hashed share links, view counts and per-user state. The token is returned once. Resolving a link hashes the token the caller presents, so a stored plaintext token keeps working after it is hashed. A link can take a password, checked with bcrypt and rate-limited per link, and turn off downloads. Reactionsreactions Emoji reactions and up or down votes on any subject and on messages, with per-tenant custom emoji, counts and a vote score per subject under the subject's own read rules and a realtime topic per tenant.React hooks Moderationmoderation Reports on any subject, a review queue, decisions (approve, hide, remove, restore, ban) with a log, user bans and an automatic hide threshold. Reviews moderate through it, and it hides and removes messages, comments included. Guestsguests People outside the organization (clients, contractors) invited by email who read, comment on and upload to the records they are granted, and see the invoices and bookings of the party they act for. Never members, never through an authorization library. Messagingmessaging Conversations and messages shared by channels, record discussions and the inbox: channels, direct and group messages, one conversation per record, members with read markers and notification levels, threads, mentions, edits with history, pins, resolved threads, scheduled sends, files and reactions. Members, the tenant or the record's readers read a conversation; Realtime pings carry ids only.React hooks