Custom fields
custom-fieldsField definitions, options and typed values. A sensitive value is readable only with the field's permission. Inline layout stores the definition on the value row.
Work managementmanagedcustom
To do
Doing
Done
Install
The command also installs updated-at, memberships, permissions, the modules it needs.
npx better-supabase add custom-fieldsConfigure
The blocks entries in your config, with the default mode.
import { defineConfig } from "better-supabase/config";
export default defineConfig({
blocks: {
// Required by custom-fields; add installs them too.
"updated-at": {},
memberships: {},
permissions: {},
// Modes: "managed", "custom". The default is "managed".
"custom-fields": { mode: "managed" },
},
});Migrations
The files
npx better-supabase add custom-fields writes into your declarative schema with the default layout.9 files
supabase/schemas/900_better_supabase_061_custom_fields.sqlschema1070 lines-- better-supabase module: custom-fields
-- @bsb-block custom-fields@4 managed
-- @bsb-hash sha256:805d08d2562c4be3dbe0601df23c90cb8489010223d8e40e7780c841a201fe5e
-- Field definitions, options and typed values. A sensitive value is readable only with the field's permission. Inline layout stores the definition on the value row.
-- 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.
create or replace function "better_supabase"."bsb_valid_bsn"(value text)
returns boolean
language sql
immutable
strict
parallel safe
set search_path = ''
as $$
select case
when value !~ '^[0-9]{9}$' or value = '000000000' then false
else (
select sum(substr(value, i, 1)::integer * case when i = 9 then -1 else 10 - i end)
from generate_series(1, 9) as i
) % 11 = 0
end
$$;
revoke execute on function "better_supabase"."bsb_valid_bsn"(text) from public, anon;
grant execute on function "better_supabase"."bsb_valid_bsn"(text) to authenticated, service_role;
create or replace function "better_supabase"."bsb_valid_iban"(value text)
returns boolean
language sql
immutable
strict
parallel safe
set search_path = ''
as $$
select case
when value !~ '^[A-Z]{2}[0-9]{2}[A-Z0-9]{11,30}$' then false
else (
select string_agg(case when ascii(c.ch) between 65 and 90 then (ascii(c.ch) - 55)::text else c.ch end, '' order by c.n)
from regexp_split_to_table(substr(value, 5) || substr(value, 1, 4), '') with ordinality as c(ch, n)
)::numeric % 97 = 1
end
$$;
revoke execute on function "better_supabase"."bsb_valid_iban"(text) from public, anon;
grant execute on function "better_supabase"."bsb_valid_iban"(text) to authenticated, service_role;
-- A sensitive value is returned only when the caller holds the field's
-- permission, or custom_fields.sensitive.read when the field names none.
create table "better_supabase"."field_definitions" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"entity" text not null,
"key" text not null,
"label" text not null,
"type" text not null,
"required" boolean not null default false,
"sensitive" boolean not null default false,
"permission" text,
"encrypted" boolean not null default false,
"pack" text,
"pack_version" integer,
"rank" text collate "C" not null default 'V',
unique ("tenant_id", "entity", "key")
);
alter table "better_supabase"."field_definitions" add constraint "bsb_field_definitions_type"
check ("type" in ('text', 'number', 'date', 'bool', 'option', 'json', 'email', 'url', 'multi_option', 'user', 'rating', 'currency', 'bsn', 'iban', 'phone_e164'));
alter table "better_supabase"."field_definitions" enable row level security;
revoke all on "better_supabase"."field_definitions" from public, anon, authenticated;
grant select on "better_supabase"."field_definitions" to authenticated;
grant all on "better_supabase"."field_definitions" to service_role;
create table "better_supabase"."field_values" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"definition_id" uuid references "better_supabase"."field_definitions" ("id") on delete cascade,
"subject_type" text not null,
"subject_id" text not null,
"value_text" text,
"value_number" numeric,
"value_date" date,
"value_bool" boolean,
"option_id" uuid,
"value_json" jsonb
, unique ("tenant_id", "subject_type", "subject_id", "definition_id")
);
alter table "better_supabase"."field_values" enable row level security;
revoke all on "better_supabase"."field_values" from public, anon, authenticated;
grant select on "better_supabase"."field_values" to authenticated;
grant all on "better_supabase"."field_values" to service_role;
create index field_values_definition_idx on "better_supabase"."field_values" ("definition_id");
alter table "better_supabase"."field_definitions" add column "archived_at" timestamptz;
create or replace function "better_supabase"."upsert_field_definition"(tenant uuid, entity text, key text, label text, type text, sensitive boolean)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_id uuid;
v_inserted boolean;
v_last text;
v_rank 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 (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', upsert_field_definition.tenant, 'custom_fields.manage'), false)) then
raise exception 'not allowed to manage custom fields' using errcode = '42501', hint = 'CUSTOM_FIELD_FORBIDDEN';
end if;
select max(d."rank" collate "C") into v_last from "better_supabase"."field_definitions" d
where d."tenant_id" = upsert_field_definition.tenant and d."entity" = upsert_field_definition.entity;
v_rank := case
when v_last is null then 'V'
when length(substring(v_last from '^z*')) = length(v_last) then v_last || 'V'
else left(v_last, length(substring(v_last from '^z*'))) || substr('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', (strpos('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', substr(v_last, length(substring(v_last from '^z*')) + 1, 1)) - 1 + 62) / 2 + 1, 1)
end;
if exists (select 1 from "better_supabase"."field_definitions" d
where d."tenant_id" = upsert_field_definition.tenant and d."entity" = upsert_field_definition.entity
and d."key" = upsert_field_definition.key and d."archived_at" is not null) then
raise exception 'field is archived; restore it first' using errcode = '55000', hint = 'CUSTOM_FIELD_ARCHIVED';
end if;
insert into "better_supabase"."field_definitions" ("tenant_id", "entity", "key", "label", "type", "sensitive", "rank")
values (upsert_field_definition.tenant, upsert_field_definition.entity, upsert_field_definition.key, upsert_field_definition.label, upsert_field_definition.type, coalesce(upsert_field_definition.sensitive, false), v_rank)
on conflict ("tenant_id", "entity", "key")
do update set "label" = excluded."label", "sensitive" = excluded."sensitive"
returning "id", xmax = 0 into v_id, v_inserted;
if v_inserted then
null;
else
null;
end if;
return jsonb_build_object('id', v_id);
end;
$$;
revoke execute on function "better_supabase"."upsert_field_definition"(uuid, text, text, text, text, boolean) from public, anon;
grant execute on function "better_supabase"."upsert_field_definition"(uuid, text, text, text, text, boolean) to authenticated, service_role;
create or replace function "better_supabase"."get_field_values"(tenant uuid, subject_type text, subject_id text)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
declare
v_values jsonb;
begin
select coalesce(jsonb_agg(jsonb_build_object('id', v."id", 'text', v."value_text")), '[]'::jsonb)
into v_values
from "better_supabase"."field_values" v
left join "better_supabase"."field_definitions" d on d."id" = v."definition_id"
where v."tenant_id" = get_field_values.tenant
and v."subject_type" = get_field_values.subject_type
and v."subject_id" = get_field_values.subject_id
and (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', get_field_values.tenant, 'custom_fields.read'), false))
and (not coalesce(d."sensitive", false)
or coalesce(better_supabase.can('tenant', v."tenant_id", coalesce(d."permission", 'custom_fields.read_sensitive')), false));
return v_values;
end;
$$;
revoke execute on function "better_supabase"."get_field_values"(uuid, text, text) from public, anon;
grant execute on function "better_supabase"."get_field_values"(uuid, text, text) to authenticated, service_role;
create or replace function "better_supabase"."set_field_value"(tenant uuid, subject_type text, subject_id text, definition uuid, value text)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_id uuid;
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 (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', set_field_value.tenant, 'custom_fields.manage'), false)) then
raise exception 'not allowed to set custom fields' using errcode = '42501', hint = 'CUSTOM_FIELD_FORBIDDEN';
end if;
if exists (select 1 from "better_supabase"."field_definitions" d where d."id" = set_field_value.definition and d."archived_at" is not null) then
raise exception 'field is archived; restore it first' using errcode = '55000', hint = 'CUSTOM_FIELD_ARCHIVED';
end if;
if exists (select 1 from "better_supabase"."field_definitions" d where d."id" = set_field_value.definition and not (case d."type" when 'bsn' then "better_supabase"."bsb_valid_bsn"(set_field_value.value) when 'iban' then "better_supabase"."bsb_valid_iban"(set_field_value.value) when 'phone_e164' then set_field_value.value ~* '^\+[1-9][0-9]{1,14}$' else true end)) then
raise exception 'the value doesn''t match the field''s format' using errcode = '22023', hint = 'CUSTOM_FIELD_VALUE_INVALID';
end if;
insert into "better_supabase"."field_values" ("tenant_id", "definition_id", "subject_type", "subject_id", "value_text")
values (set_field_value.tenant, set_field_value.definition, set_field_value.subject_type, set_field_value.subject_id, set_field_value.value)
on conflict ("tenant_id", "subject_type", "subject_id", "definition_id")
do update set "value_text" = excluded."value_text"
returning "id" into v_id;
null;
return jsonb_build_object('id', v_id);
end;
$$;
revoke execute on function "better_supabase"."set_field_value"(uuid, text, text, uuid, text) from public, anon;
grant execute on function "better_supabase"."set_field_value"(uuid, text, text, uuid, text) to authenticated, service_role;
-- Changes the value of one field value row. The row's subject and field stay.
create or replace function "better_supabase"."update_field_value"(value_id uuid, value text)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_tenant uuid;
v_type text;
v_subject text;
v_definition uuid;
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 v."tenant_id", v."subject_type", v."subject_id", v."definition_id"
into v_tenant, v_type, v_subject, v_definition
from "better_supabase"."field_values" v where v."id" = update_field_value.value_id;
if not found then
raise exception 'field value not found' using errcode = 'P0002', hint = 'CUSTOM_FIELD_VALUE_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'custom_fields.manage'), false)) then
raise exception 'not allowed to set custom fields' using errcode = '42501', hint = 'CUSTOM_FIELD_FORBIDDEN';
end if;
if exists (select 1 from "better_supabase"."field_definitions" d where d."id" = v_definition and not (case d."type" when 'bsn' then "better_supabase"."bsb_valid_bsn"(update_field_value.value) when 'iban' then "better_supabase"."bsb_valid_iban"(update_field_value.value) when 'phone_e164' then update_field_value.value ~* '^\+[1-9][0-9]{1,14}$' else true end)) then
raise exception 'the value doesn''t match the field''s format' using errcode = '22023', hint = 'CUSTOM_FIELD_VALUE_INVALID';
end if;
update "better_supabase"."field_values" set "value_text" = update_field_value.value
where "id" = update_field_value.value_id;
null;
return jsonb_build_object('id', update_field_value.value_id);
end;
$$;
revoke execute on function "better_supabase"."update_field_value"(uuid, text) from public, anon;
grant execute on function "better_supabase"."update_field_value"(uuid, text) to authenticated, service_role;
-- Removes one field value.
create or replace function "better_supabase"."delete_field_value"(value_id uuid)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_tenant uuid;
v_type text;
v_subject 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;
select v."tenant_id", v."subject_type", v."subject_id"
into v_tenant, v_type, v_subject
from "better_supabase"."field_values" v where v."id" = delete_field_value.value_id;
if not found then
raise exception 'field value not found' using errcode = 'P0002', hint = 'CUSTOM_FIELD_VALUE_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'custom_fields.manage'), false)) then
raise exception 'not allowed to delete this field value' using errcode = '42501', hint = 'CUSTOM_FIELD_FORBIDDEN';
end if;
delete from "better_supabase"."field_values" where "id" = delete_field_value.value_id;
null;
return true;
end;
$$;
revoke execute on function "better_supabase"."delete_field_value"(uuid) from public, anon;
grant execute on function "better_supabase"."delete_field_value"(uuid) to authenticated, service_role;
-- Archives a field: it leaves list_field_definitions and upsert_field_definition and set_field_value refuse it. Its values stay and stay readable. Needs the manage key; archiving an archived field returns it unchanged.
create or replace function "better_supabase"."archive_field_definition"(definition uuid)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_tenant uuid;
v_archived timestamptz;
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 "tenant_id", "archived_at" into v_tenant, v_archived from "better_supabase"."field_definitions" where "id" = archive_field_definition.definition;
if not found then
raise exception 'field not found' using errcode = 'P0002', hint = 'CUSTOM_FIELD_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'custom_fields.manage'), false)) then
raise exception 'not allowed to manage custom fields' using errcode = '42501', hint = 'CUSTOM_FIELD_FORBIDDEN';
end if;
if (v_archived is not null) = true then
return jsonb_build_object('id', archive_field_definition.definition, 'archivedAt', v_archived);
end if;
update "better_supabase"."field_definitions" set "archived_at" = now() where "id" = archive_field_definition.definition
returning "archived_at" into v_archived;
null;
return jsonb_build_object('id', archive_field_definition.definition, 'archivedAt', v_archived);
end;
$$;
revoke execute on function "better_supabase"."archive_field_definition"(uuid) from public, anon;
grant execute on function "better_supabase"."archive_field_definition"(uuid) to authenticated, service_role;
-- Restores an archived field. Needs the manage key; restoring one that is not archived returns it unchanged.
create or replace function "better_supabase"."restore_field_definition"(definition uuid)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_tenant uuid;
v_archived timestamptz;
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 "tenant_id", "archived_at" into v_tenant, v_archived from "better_supabase"."field_definitions" where "id" = restore_field_definition.definition;
if not found then
raise exception 'field not found' using errcode = 'P0002', hint = 'CUSTOM_FIELD_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'custom_fields.manage'), false)) then
raise exception 'not allowed to manage custom fields' using errcode = '42501', hint = 'CUSTOM_FIELD_FORBIDDEN';
end if;
if (v_archived is not null) = false then
return jsonb_build_object('id', restore_field_definition.definition, 'archivedAt', v_archived);
end if;
update "better_supabase"."field_definitions" set "archived_at" = null where "id" = restore_field_definition.definition
returning "archived_at" into v_archived;
null;
return jsonb_build_object('id', restore_field_definition.definition, 'archivedAt', v_archived);
end;
$$;
revoke execute on function "better_supabase"."restore_field_definition"(uuid) from public, anon;
grant execute on function "better_supabase"."restore_field_definition"(uuid) to authenticated, service_role;
-- Deletes a field for good with every value stored under it; archive keeps
-- them. Needs 'custom_fields.manage', which falls back to the manage key.
create or replace function "better_supabase"."purge_field_definition"(definition uuid)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_tenant uuid;
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 "tenant_id" into v_tenant from "better_supabase"."field_definitions" where "id" = purge_field_definition.definition;
if not found then
raise exception 'field not found' using errcode = 'P0002', hint = 'CUSTOM_FIELD_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'custom_fields.manage'), false)) then
raise exception 'not allowed to purge custom fields' using errcode = '42501', hint = 'CUSTOM_FIELD_FORBIDDEN';
end if;
delete from "better_supabase"."field_definitions" where "id" = purge_field_definition.definition;
null;
return true;
end;
$$;
revoke execute on function "better_supabase"."purge_field_definition"(uuid) from public, anon;
grant execute on function "better_supabase"."purge_field_definition"(uuid) to authenticated, service_role;
-- The definitions of one entity in display order. Archived fields stay out
-- unless include_archived is true.
create or replace function "better_supabase"."list_field_definitions"(tenant uuid, entity text, include_archived boolean default false)
returns jsonb
language sql
stable
security definer
set search_path = ''
as $$
select coalesce(jsonb_agg(jsonb_build_object(
'id', d."id",
'entity', d."entity",
'key', d."key",
'label', d."label",
'type', d."type",
'required', d."required",
'sensitive', d."sensitive",
'encrypted', d."encrypted",
'pack', d."pack",
'packVersion', d."pack_version",
'archivedAt', d."archived_at",
'rank', d."rank"
) order by d."rank" collate "C", d."key"), '[]'::jsonb)
from "better_supabase"."field_definitions" d
where d."tenant_id" = list_field_definitions.tenant
and d."entity" = list_field_definitions.entity
and (coalesce(list_field_definitions.include_archived, false) or d."archived_at" is null)
and (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', list_field_definitions.tenant, 'custom_fields.read'), false))
$$;
revoke execute on function "better_supabase"."list_field_definitions"(uuid, text, boolean) from public, anon;
grant execute on function "better_supabase"."list_field_definitions"(uuid, text, boolean) to authenticated, service_role;
-- One entity's definitions as a keyset page in display order (ADR 1077).
-- filters is { entity, type (each a value or a list), sensitive (true keeps
-- sensitive fields), archived (true adds archived fields), search (key,
-- label), sort (key, label, type${rank === "" ? "" : ", rank"}), direction, facet, count, limit, after,
-- offset, paged, total, totalCap }.
create or replace function "better_supabase"."page_field_definitions"(tenant uuid, filters jsonb default '{}')
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
#variable_conflict use_variable
declare
v_filters jsonb := coalesce(page_field_definitions.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 not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', page_field_definitions.tenant, 'custom_fields.read'), false)) then
raise exception 'not allowed to read custom fields' using errcode = '42501', hint = 'CUSTOM_FIELD_FORBIDDEN';
end if;
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 = 'CUSTOM_FIELD_INVALID';
end if;
if v_facet is not null then
if v_facet not in ('entity', 'type') then
raise exception 'facet is one of entity, type' using errcode = '22023', hint = 'CUSTOM_FIELD_INVALID';
end if;
with s as (
select (case v_facet when 'entity' then (d."entity")::text when 'type' then (d."type"::text)::text end) as v
from "better_supabase"."field_definitions" d
where d."tenant_id" = page_field_definitions.tenant
and (not (v_filters -> 'entity' is not null and jsonb_typeof(v_filters -> 'entity') <> 'null' and not (jsonb_typeof(v_filters -> 'entity') = 'array' and jsonb_array_length(v_filters -> 'entity') = 0)) or (d."entity")::text = any((case when jsonb_typeof(v_filters -> 'entity') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'entity')) else array[v_filters ->> 'entity'] end)::text[]))
and (not (v_filters -> 'type' is not null and jsonb_typeof(v_filters -> 'type') <> 'null' and not (jsonb_typeof(v_filters -> 'type') = 'array' and jsonb_array_length(v_filters -> 'type') = 0)) or d."type"::text = any((case when jsonb_typeof(v_filters -> 'type') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'type')) else array[v_filters ->> 'type'] end)::text[]))
and (not coalesce(nullif(coalesce(v_filters #>> '{sensitive,0}', v_filters ->> 'sensitive'), '') = 'true', false) or d."sensitive")
and (coalesce(nullif(coalesce(v_filters #>> '{archived,0}', v_filters ->> 'archived'), '') = 'true', false) or d."archived_at" is null)
and (v_search is null or d."key" ilike v_search or d."label" 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"."field_definitions" d
where d."tenant_id" = page_field_definitions.tenant
and (not (v_filters -> 'entity' is not null and jsonb_typeof(v_filters -> 'entity') <> 'null' and not (jsonb_typeof(v_filters -> 'entity') = 'array' and jsonb_array_length(v_filters -> 'entity') = 0)) or (d."entity")::text = any((case when jsonb_typeof(v_filters -> 'entity') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'entity')) else array[v_filters ->> 'entity'] end)::text[]))
and (not (v_filters -> 'type' is not null and jsonb_typeof(v_filters -> 'type') <> 'null' and not (jsonb_typeof(v_filters -> 'type') = 'array' and jsonb_array_length(v_filters -> 'type') = 0)) or d."type"::text = any((case when jsonb_typeof(v_filters -> 'type') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'type')) else array[v_filters ->> 'type'] end)::text[]))
and (not coalesce(nullif(coalesce(v_filters #>> '{sensitive,0}', v_filters ->> 'sensitive'), '') = 'true', false) or d."sensitive")
and (coalesce(nullif(coalesce(v_filters #>> '{archived,0}', v_filters ->> 'archived'), '') = 'true', false) or d."archived_at" is null)
and (v_search is null or d."key" ilike v_search or d."label" 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 ('key', 'label', 'type', 'rank') then
raise exception 'sort is one of key, label, type, rank' using errcode = '22023', hint = 'CUSTOM_FIELD_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 = 'CUSTOM_FIELD_INVALID';
end if;
if v_sort is null 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) <> 4) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CUSTOM_FIELD_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, jsonb_build_object(
'id', d."id",
'entity', d."entity",
'key', d."key",
'label', d."label",
'type', d."type",
'required', d."required",
'sensitive', d."sensitive",
'encrypted', d."encrypted",
'pack', d."pack",
'packVersion', d."pack_version",
'archivedAt', d."archived_at",
'rank', d."rank"
) as j, jsonb_build_array((d."entity")::text, (d."rank" collate "C")::text, (d."key")::text, (d."id")::text) as k
from (
select * from "better_supabase"."field_definitions" d
where d."tenant_id" = page_field_definitions.tenant
and (not (v_filters -> 'entity' is not null and jsonb_typeof(v_filters -> 'entity') <> 'null' and not (jsonb_typeof(v_filters -> 'entity') = 'array' and jsonb_array_length(v_filters -> 'entity') = 0)) or (d."entity")::text = any((case when jsonb_typeof(v_filters -> 'entity') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'entity')) else array[v_filters ->> 'entity'] end)::text[]))
and (not (v_filters -> 'type' is not null and jsonb_typeof(v_filters -> 'type') <> 'null' and not (jsonb_typeof(v_filters -> 'type') = 'array' and jsonb_array_length(v_filters -> 'type') = 0)) or d."type"::text = any((case when jsonb_typeof(v_filters -> 'type') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'type')) else array[v_filters ->> 'type'] end)::text[]))
and (not coalesce(nullif(coalesce(v_filters #>> '{sensitive,0}', v_filters ->> 'sensitive'), '') = 'true', false) or d."sensitive")
and (coalesce(nullif(coalesce(v_filters #>> '{archived,0}', v_filters ->> 'archived'), '') = 'true', false) or d."archived_at" is null)
and (v_search is null or d."key" ilike v_search or d."label" ilike v_search)
and (v_after is null or (d."entity", d."rank" collate "C", d."key", d."id") > ((v_after ->> 0)::text, (v_after ->> 1)::text, (v_after ->> 2)::text, (v_after ->> 3)::uuid))
order by d."entity" asc, d."rank" collate "C" asc, d."key" asc, d."id" asc
limit v_limit + 1 offset v_offset
) d
) p;
elsif v_sort is null 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) <> 4) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CUSTOM_FIELD_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, jsonb_build_object(
'id', d."id",
'entity', d."entity",
'key', d."key",
'label', d."label",
'type', d."type",
'required', d."required",
'sensitive', d."sensitive",
'encrypted', d."encrypted",
'pack', d."pack",
'packVersion', d."pack_version",
'archivedAt', d."archived_at",
'rank', d."rank"
) as j, jsonb_build_array((d."entity")::text, (d."rank" collate "C")::text, (d."key")::text, (d."id")::text) as k
from (
select * from "better_supabase"."field_definitions" d
where d."tenant_id" = page_field_definitions.tenant
and (not (v_filters -> 'entity' is not null and jsonb_typeof(v_filters -> 'entity') <> 'null' and not (jsonb_typeof(v_filters -> 'entity') = 'array' and jsonb_array_length(v_filters -> 'entity') = 0)) or (d."entity")::text = any((case when jsonb_typeof(v_filters -> 'entity') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'entity')) else array[v_filters ->> 'entity'] end)::text[]))
and (not (v_filters -> 'type' is not null and jsonb_typeof(v_filters -> 'type') <> 'null' and not (jsonb_typeof(v_filters -> 'type') = 'array' and jsonb_array_length(v_filters -> 'type') = 0)) or d."type"::text = any((case when jsonb_typeof(v_filters -> 'type') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'type')) else array[v_filters ->> 'type'] end)::text[]))
and (not coalesce(nullif(coalesce(v_filters #>> '{sensitive,0}', v_filters ->> 'sensitive'), '') = 'true', false) or d."sensitive")
and (coalesce(nullif(coalesce(v_filters #>> '{archived,0}', v_filters ->> 'archived'), '') = 'true', false) or d."archived_at" is null)
and (v_search is null or d."key" ilike v_search or d."label" ilike v_search)
and (v_after is null or (d."entity" <= (v_after ->> 0)::text and ((d."entity" < (v_after ->> 0)::text) or (d."entity" = (v_after ->> 0)::text and d."rank" collate "C" > (v_after ->> 1)::text) or (d."entity" = (v_after ->> 0)::text and d."rank" collate "C" = (v_after ->> 1)::text and d."key" > (v_after ->> 2)::text) or (d."entity" = (v_after ->> 0)::text and d."rank" collate "C" = (v_after ->> 1)::text and d."key" = (v_after ->> 2)::text and d."id" > (v_after ->> 3)::uuid))))
order by d."entity" desc, d."rank" collate "C" asc, d."key" asc, d."id" asc
limit v_limit + 1 offset v_offset
) d
) p;
elsif v_sort = 'key' 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) <> 3) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CUSTOM_FIELD_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, jsonb_build_object(
'id', d."id",
'entity', d."entity",
'key', d."key",
'label', d."label",
'type', d."type",
'required', d."required",
'sensitive', d."sensitive",
'encrypted', d."encrypted",
'pack', d."pack",
'packVersion', d."pack_version",
'archivedAt', d."archived_at",
'rank', d."rank"
) as j, jsonb_build_array((d."entity")::text, (d."key")::text, (d."id")::text) as k
from (
select * from "better_supabase"."field_definitions" d
where d."tenant_id" = page_field_definitions.tenant
and (not (v_filters -> 'entity' is not null and jsonb_typeof(v_filters -> 'entity') <> 'null' and not (jsonb_typeof(v_filters -> 'entity') = 'array' and jsonb_array_length(v_filters -> 'entity') = 0)) or (d."entity")::text = any((case when jsonb_typeof(v_filters -> 'entity') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'entity')) else array[v_filters ->> 'entity'] end)::text[]))
and (not (v_filters -> 'type' is not null and jsonb_typeof(v_filters -> 'type') <> 'null' and not (jsonb_typeof(v_filters -> 'type') = 'array' and jsonb_array_length(v_filters -> 'type') = 0)) or d."type"::text = any((case when jsonb_typeof(v_filters -> 'type') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'type')) else array[v_filters ->> 'type'] end)::text[]))
and (not coalesce(nullif(coalesce(v_filters #>> '{sensitive,0}', v_filters ->> 'sensitive'), '') = 'true', false) or d."sensitive")
and (coalesce(nullif(coalesce(v_filters #>> '{archived,0}', v_filters ->> 'archived'), '') = 'true', false) or d."archived_at" is null)
and (v_search is null or d."key" ilike v_search or d."label" ilike v_search)
and (v_after is null or (d."entity", d."key", d."id") > ((v_after ->> 0)::text, (v_after ->> 1)::text, (v_after ->> 2)::uuid))
order by d."entity" asc, d."key" asc, d."id" asc
limit v_limit + 1 offset v_offset
) d
) p;
elsif v_sort = 'key' 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) <> 3) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CUSTOM_FIELD_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, jsonb_build_object(
'id', d."id",
'entity', d."entity",
'key', d."key",
'label', d."label",
'type', d."type",
'required', d."required",
'sensitive', d."sensitive",
'encrypted', d."encrypted",
'pack', d."pack",
'packVersion', d."pack_version",
'archivedAt', d."archived_at",
'rank', d."rank"
) as j, jsonb_build_array((d."entity")::text, (d."key")::text, (d."id")::text) as k
from (
select * from "better_supabase"."field_definitions" d
where d."tenant_id" = page_field_definitions.tenant
and (not (v_filters -> 'entity' is not null and jsonb_typeof(v_filters -> 'entity') <> 'null' and not (jsonb_typeof(v_filters -> 'entity') = 'array' and jsonb_array_length(v_filters -> 'entity') = 0)) or (d."entity")::text = any((case when jsonb_typeof(v_filters -> 'entity') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'entity')) else array[v_filters ->> 'entity'] end)::text[]))
and (not (v_filters -> 'type' is not null and jsonb_typeof(v_filters -> 'type') <> 'null' and not (jsonb_typeof(v_filters -> 'type') = 'array' and jsonb_array_length(v_filters -> 'type') = 0)) or d."type"::text = any((case when jsonb_typeof(v_filters -> 'type') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'type')) else array[v_filters ->> 'type'] end)::text[]))
and (not coalesce(nullif(coalesce(v_filters #>> '{sensitive,0}', v_filters ->> 'sensitive'), '') = 'true', false) or d."sensitive")
and (coalesce(nullif(coalesce(v_filters #>> '{archived,0}', v_filters ->> 'archived'), '') = 'true', false) or d."archived_at" is null)
and (v_search is null or d."key" ilike v_search or d."label" ilike v_search)
and (v_after is null or (d."entity" <= (v_after ->> 0)::text and ((d."entity" < (v_after ->> 0)::text) or (d."entity" = (v_after ->> 0)::text and d."key" > (v_after ->> 1)::text) or (d."entity" = (v_after ->> 0)::text and d."key" = (v_after ->> 1)::text and d."id" > (v_after ->> 2)::uuid))))
order by d."entity" desc, d."key" asc, d."id" asc
limit v_limit + 1 offset v_offset
) d
) p;
elsif v_sort = 'label' 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 = 'CUSTOM_FIELD_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, jsonb_build_object(
'id', d."id",
'entity', d."entity",
'key', d."key",
'label', d."label",
'type', d."type",
'required', d."required",
'sensitive', d."sensitive",
'encrypted', d."encrypted",
'pack', d."pack",
'packVersion', d."pack_version",
'archivedAt', d."archived_at",
'rank', d."rank"
) as j, jsonb_build_array((lower(d."label"))::text, (d."id")::text) as k
from (
select * from "better_supabase"."field_definitions" d
where d."tenant_id" = page_field_definitions.tenant
and (not (v_filters -> 'entity' is not null and jsonb_typeof(v_filters -> 'entity') <> 'null' and not (jsonb_typeof(v_filters -> 'entity') = 'array' and jsonb_array_length(v_filters -> 'entity') = 0)) or (d."entity")::text = any((case when jsonb_typeof(v_filters -> 'entity') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'entity')) else array[v_filters ->> 'entity'] end)::text[]))
and (not (v_filters -> 'type' is not null and jsonb_typeof(v_filters -> 'type') <> 'null' and not (jsonb_typeof(v_filters -> 'type') = 'array' and jsonb_array_length(v_filters -> 'type') = 0)) or d."type"::text = any((case when jsonb_typeof(v_filters -> 'type') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'type')) else array[v_filters ->> 'type'] end)::text[]))
and (not coalesce(nullif(coalesce(v_filters #>> '{sensitive,0}', v_filters ->> 'sensitive'), '') = 'true', false) or d."sensitive")
and (coalesce(nullif(coalesce(v_filters #>> '{archived,0}', v_filters ->> 'archived'), '') = 'true', false) or d."archived_at" is null)
and (v_search is null or d."key" ilike v_search or d."label" ilike v_search)
and (v_after is null or (lower(d."label"), d."id") > ((v_after ->> 0)::text, (v_after ->> 1)::uuid))
order by lower(d."label") asc, d."id" asc
limit v_limit + 1 offset v_offset
) d
) p;
elsif v_sort = 'label' 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 = 'CUSTOM_FIELD_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, jsonb_build_object(
'id', d."id",
'entity', d."entity",
'key', d."key",
'label', d."label",
'type', d."type",
'required', d."required",
'sensitive', d."sensitive",
'encrypted', d."encrypted",
'pack', d."pack",
'packVersion', d."pack_version",
'archivedAt', d."archived_at",
'rank', d."rank"
) as j, jsonb_build_array((lower(d."label"))::text, (d."id")::text) as k
from (
select * from "better_supabase"."field_definitions" d
where d."tenant_id" = page_field_definitions.tenant
and (not (v_filters -> 'entity' is not null and jsonb_typeof(v_filters -> 'entity') <> 'null' and not (jsonb_typeof(v_filters -> 'entity') = 'array' and jsonb_array_length(v_filters -> 'entity') = 0)) or (d."entity")::text = any((case when jsonb_typeof(v_filters -> 'entity') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'entity')) else array[v_filters ->> 'entity'] end)::text[]))
and (not (v_filters -> 'type' is not null and jsonb_typeof(v_filters -> 'type') <> 'null' and not (jsonb_typeof(v_filters -> 'type') = 'array' and jsonb_array_length(v_filters -> 'type') = 0)) or d."type"::text = any((case when jsonb_typeof(v_filters -> 'type') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'type')) else array[v_filters ->> 'type'] end)::text[]))
and (not coalesce(nullif(coalesce(v_filters #>> '{sensitive,0}', v_filters ->> 'sensitive'), '') = 'true', false) or d."sensitive")
and (coalesce(nullif(coalesce(v_filters #>> '{archived,0}', v_filters ->> 'archived'), '') = 'true', false) or d."archived_at" is null)
and (v_search is null or d."key" ilike v_search or d."label" ilike v_search)
and (v_after is null or (lower(d."label") <= (v_after ->> 0)::text and ((lower(d."label") < (v_after ->> 0)::text) or (lower(d."label") = (v_after ->> 0)::text and d."id" > (v_after ->> 1)::uuid))))
order by lower(d."label") desc, d."id" asc
limit v_limit + 1 offset v_offset
) d
) p;
elsif v_sort = 'type' 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 = 'CUSTOM_FIELD_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, jsonb_build_object(
'id', d."id",
'entity', d."entity",
'key', d."key",
'label', d."label",
'type', d."type",
'required', d."required",
'sensitive', d."sensitive",
'encrypted', d."encrypted",
'pack', d."pack",
'packVersion', d."pack_version",
'archivedAt', d."archived_at",
'rank', d."rank"
) as j, jsonb_build_array((d."type"::text)::text, (d."id")::text) as k
from (
select * from "better_supabase"."field_definitions" d
where d."tenant_id" = page_field_definitions.tenant
and (not (v_filters -> 'entity' is not null and jsonb_typeof(v_filters -> 'entity') <> 'null' and not (jsonb_typeof(v_filters -> 'entity') = 'array' and jsonb_array_length(v_filters -> 'entity') = 0)) or (d."entity")::text = any((case when jsonb_typeof(v_filters -> 'entity') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'entity')) else array[v_filters ->> 'entity'] end)::text[]))
and (not (v_filters -> 'type' is not null and jsonb_typeof(v_filters -> 'type') <> 'null' and not (jsonb_typeof(v_filters -> 'type') = 'array' and jsonb_array_length(v_filters -> 'type') = 0)) or d."type"::text = any((case when jsonb_typeof(v_filters -> 'type') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'type')) else array[v_filters ->> 'type'] end)::text[]))
and (not coalesce(nullif(coalesce(v_filters #>> '{sensitive,0}', v_filters ->> 'sensitive'), '') = 'true', false) or d."sensitive")
and (coalesce(nullif(coalesce(v_filters #>> '{archived,0}', v_filters ->> 'archived'), '') = 'true', false) or d."archived_at" is null)
and (v_search is null or d."key" ilike v_search or d."label" ilike v_search)
and (v_after is null or (d."type"::text, d."id") > ((v_after ->> 0)::text, (v_after ->> 1)::uuid))
order by d."type"::text asc, d."id" asc
limit v_limit + 1 offset v_offset
) d
) p;
elsif v_sort = 'type' 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 = 'CUSTOM_FIELD_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, jsonb_build_object(
'id', d."id",
'entity', d."entity",
'key', d."key",
'label', d."label",
'type', d."type",
'required', d."required",
'sensitive', d."sensitive",
'encrypted', d."encrypted",
'pack', d."pack",
'packVersion', d."pack_version",
'archivedAt', d."archived_at",
'rank', d."rank"
) as j, jsonb_build_array((d."type"::text)::text, (d."id")::text) as k
from (
select * from "better_supabase"."field_definitions" d
where d."tenant_id" = page_field_definitions.tenant
and (not (v_filters -> 'entity' is not null and jsonb_typeof(v_filters -> 'entity') <> 'null' and not (jsonb_typeof(v_filters -> 'entity') = 'array' and jsonb_array_length(v_filters -> 'entity') = 0)) or (d."entity")::text = any((case when jsonb_typeof(v_filters -> 'entity') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'entity')) else array[v_filters ->> 'entity'] end)::text[]))
and (not (v_filters -> 'type' is not null and jsonb_typeof(v_filters -> 'type') <> 'null' and not (jsonb_typeof(v_filters -> 'type') = 'array' and jsonb_array_length(v_filters -> 'type') = 0)) or d."type"::text = any((case when jsonb_typeof(v_filters -> 'type') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'type')) else array[v_filters ->> 'type'] end)::text[]))
and (not coalesce(nullif(coalesce(v_filters #>> '{sensitive,0}', v_filters ->> 'sensitive'), '') = 'true', false) or d."sensitive")
and (coalesce(nullif(coalesce(v_filters #>> '{archived,0}', v_filters ->> 'archived'), '') = 'true', false) or d."archived_at" is null)
and (v_search is null or d."key" ilike v_search or d."label" ilike v_search)
and (v_after is null or (d."type"::text <= (v_after ->> 0)::text and ((d."type"::text < (v_after ->> 0)::text) or (d."type"::text = (v_after ->> 0)::text and d."id" > (v_after ->> 1)::uuid))))
order by d."type"::text desc, d."id" asc
limit v_limit + 1 offset v_offset
) d
) p;
elsif v_sort = 'rank' 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) <> 4) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CUSTOM_FIELD_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, jsonb_build_object(
'id', d."id",
'entity', d."entity",
'key', d."key",
'label', d."label",
'type', d."type",
'required', d."required",
'sensitive', d."sensitive",
'encrypted', d."encrypted",
'pack', d."pack",
'packVersion', d."pack_version",
'archivedAt', d."archived_at",
'rank', d."rank"
) as j, jsonb_build_array((d."entity")::text, (d."rank" collate "C")::text, (d."key")::text, (d."id")::text) as k
from (
select * from "better_supabase"."field_definitions" d
where d."tenant_id" = page_field_definitions.tenant
and (not (v_filters -> 'entity' is not null and jsonb_typeof(v_filters -> 'entity') <> 'null' and not (jsonb_typeof(v_filters -> 'entity') = 'array' and jsonb_array_length(v_filters -> 'entity') = 0)) or (d."entity")::text = any((case when jsonb_typeof(v_filters -> 'entity') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'entity')) else array[v_filters ->> 'entity'] end)::text[]))
and (not (v_filters -> 'type' is not null and jsonb_typeof(v_filters -> 'type') <> 'null' and not (jsonb_typeof(v_filters -> 'type') = 'array' and jsonb_array_length(v_filters -> 'type') = 0)) or d."type"::text = any((case when jsonb_typeof(v_filters -> 'type') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'type')) else array[v_filters ->> 'type'] end)::text[]))
and (not coalesce(nullif(coalesce(v_filters #>> '{sensitive,0}', v_filters ->> 'sensitive'), '') = 'true', false) or d."sensitive")
and (coalesce(nullif(coalesce(v_filters #>> '{archived,0}', v_filters ->> 'archived'), '') = 'true', false) or d."archived_at" is null)
and (v_search is null or d."key" ilike v_search or d."label" ilike v_search)
and (v_after is null or (d."entity", d."rank" collate "C", d."key", d."id") > ((v_after ->> 0)::text, (v_after ->> 1)::text, (v_after ->> 2)::text, (v_after ->> 3)::uuid))
order by d."entity" asc, d."rank" collate "C" asc, d."key" asc, d."id" asc
limit v_limit + 1 offset v_offset
) d
) p;
elsif v_sort = 'rank' 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) <> 4) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CUSTOM_FIELD_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, jsonb_build_object(
'id', d."id",
'entity', d."entity",
'key', d."key",
'label', d."label",
'type', d."type",
'required', d."required",
'sensitive', d."sensitive",
'encrypted', d."encrypted",
'pack', d."pack",
'packVersion', d."pack_version",
'archivedAt', d."archived_at",
'rank', d."rank"
) as j, jsonb_build_array((d."entity")::text, (d."rank" collate "C")::text, (d."key")::text, (d."id")::text) as k
from (
select * from "better_supabase"."field_definitions" d
where d."tenant_id" = page_field_definitions.tenant
and (not (v_filters -> 'entity' is not null and jsonb_typeof(v_filters -> 'entity') <> 'null' and not (jsonb_typeof(v_filters -> 'entity') = 'array' and jsonb_array_length(v_filters -> 'entity') = 0)) or (d."entity")::text = any((case when jsonb_typeof(v_filters -> 'entity') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'entity')) else array[v_filters ->> 'entity'] end)::text[]))
and (not (v_filters -> 'type' is not null and jsonb_typeof(v_filters -> 'type') <> 'null' and not (jsonb_typeof(v_filters -> 'type') = 'array' and jsonb_array_length(v_filters -> 'type') = 0)) or d."type"::text = any((case when jsonb_typeof(v_filters -> 'type') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'type')) else array[v_filters ->> 'type'] end)::text[]))
and (not coalesce(nullif(coalesce(v_filters #>> '{sensitive,0}', v_filters ->> 'sensitive'), '') = 'true', false) or d."sensitive")
and (coalesce(nullif(coalesce(v_filters #>> '{archived,0}', v_filters ->> 'archived'), '') = 'true', false) or d."archived_at" is null)
and (v_search is null or d."key" ilike v_search or d."label" ilike v_search)
and (v_after is null or (d."entity" <= (v_after ->> 0)::text and ((d."entity" < (v_after ->> 0)::text) or (d."entity" = (v_after ->> 0)::text and d."rank" collate "C" > (v_after ->> 1)::text) or (d."entity" = (v_after ->> 0)::text and d."rank" collate "C" = (v_after ->> 1)::text and d."key" > (v_after ->> 2)::text) or (d."entity" = (v_after ->> 0)::text and d."rank" collate "C" = (v_after ->> 1)::text and d."key" = (v_after ->> 2)::text and d."id" > (v_after ->> 3)::uuid))))
order by d."entity" desc, d."rank" collate "C" asc, d."key" asc, d."id" asc
limit v_limit + 1 offset v_offset
) d
) 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"."field_definitions" d
where d."tenant_id" = page_field_definitions.tenant
and (not (v_filters -> 'entity' is not null and jsonb_typeof(v_filters -> 'entity') <> 'null' and not (jsonb_typeof(v_filters -> 'entity') = 'array' and jsonb_array_length(v_filters -> 'entity') = 0)) or (d."entity")::text = any((case when jsonb_typeof(v_filters -> 'entity') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'entity')) else array[v_filters ->> 'entity'] end)::text[]))
and (not (v_filters -> 'type' is not null and jsonb_typeof(v_filters -> 'type') <> 'null' and not (jsonb_typeof(v_filters -> 'type') = 'array' and jsonb_array_length(v_filters -> 'type') = 0)) or d."type"::text = any((case when jsonb_typeof(v_filters -> 'type') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'type')) else array[v_filters ->> 'type'] end)::text[]))
and (not coalesce(nullif(coalesce(v_filters #>> '{sensitive,0}', v_filters ->> 'sensitive'), '') = 'true', false) or d."sensitive")
and (coalesce(nullif(coalesce(v_filters #>> '{archived,0}', v_filters ->> 'archived'), '') = 'true', false) or d."archived_at" is null)
and (v_search is null or d."key" ilike v_search or d."label" 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_field_definitions"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."page_field_definitions"(uuid, jsonb) to authenticated, service_role;
-- One definition the caller may read, or null.
create or replace function "better_supabase"."get_field_definition"(id uuid)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
declare
v_row "better_supabase"."field_definitions"%rowtype;
begin
select * into v_row from "better_supabase"."field_definitions" d where d."id" = get_field_definition.id;
if not found then
return null;
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_row."tenant_id", 'custom_fields.read'), false)) then
raise exception 'not allowed to read custom fields' using errcode = '42501', hint = 'CUSTOM_FIELD_FORBIDDEN';
end if;
return jsonb_build_object(
'id', v_row."id",
'entity', v_row."entity",
'key', v_row."key",
'label', v_row."label",
'type', v_row."type",
'required', v_row."required",
'sensitive', v_row."sensitive",
'encrypted', v_row."encrypted",
'pack', v_row."pack",
'packVersion', v_row."pack_version",
'archivedAt', v_row."archived_at",
'rank', v_row."rank"
);
end;
$$;
revoke execute on function "better_supabase"."get_field_definition"(uuid) from public, anon;
grant execute on function "better_supabase"."get_field_definition"(uuid) to authenticated, service_role;
-- Sets a field's rank key, from rankBetween over its neighbours' keys.
create or replace function "better_supabase"."rank_field_definition"(definition uuid, rank text)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_tenant uuid;
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 rank_field_definition.rank is null or rank_field_definition.rank !~ '^[0-9A-Za-z]*[1-9A-Za-z]$' then
raise exception 'not a rank key' using errcode = '22023', hint = 'CUSTOM_FIELD_RANK_INVALID';
end if;
select "tenant_id" into v_tenant from "better_supabase"."field_definitions" where "id" = rank_field_definition.definition;
if not found then
raise exception 'field not found' using errcode = 'P0002', hint = 'CUSTOM_FIELD_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'custom_fields.manage'), false)) then
raise exception 'not allowed to manage custom fields' using errcode = '42501', hint = 'CUSTOM_FIELD_FORBIDDEN';
end if;
update "better_supabase"."field_definitions" set "rank" = rank_field_definition.rank where "id" = rank_field_definition.definition;
return true;
end;
$$;
revoke execute on function "better_supabase"."rank_field_definition"(uuid, text) from public, anon;
grant execute on function "better_supabase"."rank_field_definition"(uuid, text) to authenticated, service_role;
-- Installs a field pack (apiVersion 1): { key, version, subjects: { <type>:
-- [{ key, label, type, required?, sensitive?, permission? }] } }. A definition
-- is matched by (pack, key), so a second install changes nothing.
create or replace function "better_supabase"."install_field_pack"(tenant uuid, pack jsonb)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_key text := install_field_pack.pack ->> 'key';
v_version integer;
v_entity text;
v_fields jsonb;
v_field jsonb;
v_field_key text;
v_label text;
v_type text;
v_required boolean;
v_sensitive boolean;
v_permission text;
v_row record;
v_id uuid;
v_last text;
v_rank text;
v_created integer := 0;
v_updated integer := 0;
v_unchanged integer := 0;
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 (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', install_field_pack.tenant, 'custom_fields.manage'), false)) then
raise exception 'not allowed to manage custom fields' using errcode = '42501', hint = 'CUSTOM_FIELD_FORBIDDEN';
end if;
if jsonb_typeof(install_field_pack.pack) is distinct from 'object' or install_field_pack.pack ->> 'apiVersion' is distinct from '1' then
raise exception 'a field pack is an object with apiVersion 1' using errcode = '22023', hint = 'CUSTOM_FIELD_PACK_INVALID';
end if;
if v_key is null or v_key !~ '^[a-z][a-z0-9_.-]{0,62}$' then
raise exception 'a field pack needs a key in lower case' using errcode = '22023', hint = 'CUSTOM_FIELD_PACK_INVALID';
end if;
begin
v_version := (install_field_pack.pack ->> 'version')::integer;
exception when invalid_text_representation or numeric_value_out_of_range then
v_version := null;
end;
if v_version is null or v_version < 1 then
raise exception 'a field pack needs a version of 1 or more' using errcode = '22023', hint = 'CUSTOM_FIELD_PACK_INVALID';
end if;
if jsonb_typeof(install_field_pack.pack -> 'subjects') is distinct from 'object' then
raise exception 'a field pack needs subjects: { <type>: fields }' using errcode = '22023', hint = 'CUSTOM_FIELD_PACK_INVALID';
end if;
for v_entity, v_fields in select s.key, s.value from jsonb_each(install_field_pack.pack -> 'subjects') s order by s.key loop
if jsonb_typeof(v_fields) is distinct from 'array' then
raise exception 'the fields of a subject type are a list' using errcode = '22023', hint = 'CUSTOM_FIELD_PACK_INVALID';
end if;
for v_field in select f.value from jsonb_array_elements(v_fields) f loop
if jsonb_typeof(v_field) is distinct from 'object' then
raise exception 'a field is an object of key, label and type' using errcode = '22023', hint = 'CUSTOM_FIELD_PACK_INVALID';
end if;
v_field_key := v_field ->> 'key';
v_label := v_field ->> 'label';
v_type := v_field ->> 'type';
v_permission := nullif(v_field ->> 'permission', '');
if v_field_key is null or v_field_key !~ '^[a-z][a-z0-9_.-]{0,62}$' or v_label is null or btrim(v_label) = '' then
raise exception 'a field needs a key in lower case and a label' using errcode = '22023', hint = 'CUSTOM_FIELD_PACK_INVALID';
end if;
if v_type is null or v_type not in ('text', 'number', 'date', 'bool', 'option', 'json', 'email', 'url', 'multi_option', 'user', 'rating', 'currency', 'bsn', 'iban', 'phone_e164') then
raise exception 'a field needs one of the custom field types' using errcode = '22023', hint = 'CUSTOM_FIELD_PACK_INVALID';
end if;
if (v_field ? 'required' and jsonb_typeof(v_field -> 'required') <> 'boolean') or (v_field ? 'sensitive' and jsonb_typeof(v_field -> 'sensitive') <> 'boolean') then
raise exception 'required and sensitive are booleans' using errcode = '22023', hint = 'CUSTOM_FIELD_PACK_INVALID';
end if;
v_required := coalesce((v_field ->> 'required')::boolean, false);
v_sensitive := coalesce((v_field ->> 'sensitive')::boolean, false);
select d."id" as id, d."pack" as pack, d."pack_version" as version, d."label" as label, d."type" as type,
d."required" as required, d."sensitive" as sensitive, d."permission" as permission
into v_row
from "better_supabase"."field_definitions" d
where d."tenant_id" = install_field_pack.tenant and d."entity" = v_entity and d."key" = v_field_key;
if not found then
select max(d."rank" collate "C") into v_last from "better_supabase"."field_definitions" d
where d."tenant_id" = install_field_pack.tenant and d."entity" = v_entity;
v_rank := case
when v_last is null then 'V'
when length(substring(v_last from '^z*')) = length(v_last) then v_last || 'V'
else left(v_last, length(substring(v_last from '^z*'))) || substr('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', (strpos('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', substr(v_last, length(substring(v_last from '^z*')) + 1, 1)) - 1 + 62) / 2 + 1, 1)
end;
insert into "better_supabase"."field_definitions" ("tenant_id", "entity", "key", "label", "type", "required", "sensitive", "permission", "pack", "pack_version", "rank")
values (install_field_pack.tenant, v_entity, v_field_key, v_label, v_type, v_required, v_sensitive, v_permission, v_key, v_version, v_rank)
returning "id" into v_id;
v_created := v_created + 1;
null;
elsif v_row.pack is distinct from v_key then
raise exception 'the field already exists and doesn''t belong to this pack' using errcode = '23505', hint = 'CUSTOM_FIELD_PACK_CONFLICT';
elsif v_row.label = v_label and v_row.type = v_type and v_row.required = v_required and v_row.sensitive = v_sensitive
and v_row.permission is not distinct from v_permission and v_row.version is not distinct from v_version then
v_unchanged := v_unchanged + 1;
else
update "better_supabase"."field_definitions" set "label" = v_label, "type" = v_type, "required" = v_required, "sensitive" = v_sensitive,
"permission" = v_permission, "pack_version" = v_version
where "id" = v_row.id;
v_updated := v_updated + 1;
null;
end if;
end loop;
end loop;
return jsonb_build_object('pack', v_key, 'version', v_version, 'created', v_created, 'updated', v_updated, 'unchanged', v_unchanged);
end;
$$;
revoke execute on function "better_supabase"."install_field_pack"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."install_field_pack"(uuid, jsonb) to authenticated, 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"."field_definitions" as restrictive
for select to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_insert on "better_supabase"."field_definitions" 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"."field_definitions" 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"."field_definitions" 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"."field_values" as restrictive
for select to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_insert on "better_supabase"."field_values" 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"."field_values" 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"."field_values" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
Client
This block has no React entry yet. Its TypeScript side is in better-supabase/blocks/custom-fields, which runs on the server.
Events
With the outbox installed, the module writes these in the same transaction as the change.
- field_definition.archived
- field_definition.created
- field_definition.purged
- field_definition.restored
- field_definition.updated
- field_value.deleted
- field_value.set
Dependencies
- Required: the install command adds it too
- Works with: optional
Recipes
More in Work management
Tagstags Tenant tags with a kind, and a link from a tag to a subject. Over the app's tables it can use one link table per subject.React hooks State machinestate-machine Allowed status transitions as the caller, a history row and a status.changed event. A machine can also write a stages timeline and keep a lifecycle row (pause, flags, pinned version). Each machine is a subject table named in options.machines, or a config.recordTypes type. Saved viewssaved-views Named, ranked list states (filters, sort, columns, grouping, density) for one user, a team or the whole tenant.React hooks Relationsrelations Typed links between subjects (blocks, parent, relates, or your own kinds), with inverse names, cycle checks, one-target kinds, walks and rollups. BulkActions actionsbulk-actions One action over many rows: up to a limit in one call with a result per row, and larger sets as a background run on the jobs queue that runs as the user who started it. Searchsearch A search_documents index kept up by triggers from each config.recordTypes search entry, with search(q) as the caller (websearch, trigram prefixes, can_subject, hybrid with embeddings), search_page for browsing and cursor paging with facet filters, and recents and favorites from the share-links user state.React hooks