A full-featured CRM built with React, shadcn/ui, and Supabase.
Replace the search_index view with a full-text global_search RPC
François Zaninotto74c55db07298
diff --git a/doc/src/content/docs/users/global-search.mdx b/doc/src/content/docs/users/global-search.mdxindex 9fb56f07..5bca1b56 100644--- a/doc/src/content/docs/users/global-search.mdx+++ b/doc/src/content/docs/users/global-search.mdx@@ -15,23 +15,26 @@ On mobile, tap the magnifier in the header. It is available on every screen.## What is searched-| Record | Searched fields |-| ------------------------ | ------------------------------------------------------------------------ |-| Companies | name, sector, description, website, phone, zip code, city, state |-| Contacts | first and last name, job title, background, company name, emails, phones |-| Deals | name, category, description, company name |-| Tasks | text, type, contact name |-| Notes (contact and deal) | note text, attachment file names, parent contact or deal name |+| Record | Searched fields |+| ------------------------ | ---------------------------------------------------------------- |+| Companies | name, sector, description, website, phone, zip code, city, state |+| Contacts | first and last name, job title, background, emails, phones |+| Deals | name, category, description |+| Tasks | text, type |+| Notes (contact and deal) | note text, attachment file names |Archived deals and completed tasks are left out, along with notes attached to anarchived deal.## Searching with several words-Every word must appear somewhere in the same record. Searching-`sourdough Anderson` returns the notes about sourdough written on Thomas-Anderson, and not the ones written on anybody else — the parent contact's name-counts as part of the note.+Every word must appear in the same record, as the start of a word: `tho and`+finds Thomas Anderson, but `homas` finds nothing. Accents and case are ignored,+so `helene` finds Hélène.++Only the record's own fields count: searching `sourdough Anderson` does not+find a note about sourdough written on Thomas Anderson, since his name belongs+to the contact, not to the note.## Good to knowdiff --git a/src/components/atomic-crm/providers/commons/search.ts b/src/components/atomic-crm/providers/commons/search.tsindex 6033b388..3b839efe 100644--- a/src/components/atomic-crm/providers/commons/search.ts+++ b/src/components/atomic-crm/providers/commons/search.ts@@ -55,17 +55,16 @@ const byDateDesc = (a: SearchResult, b: SearchResult) =>(b.date ?? "").localeCompare(a.date ?? "");/**- * Emulates the `search_index` database view for the FakeRest provider, by- * running FakeRest's own `q` search on each searchable resource and mapping- * the matches to the view's shape.+ * Emulates the `global_search` database function for the FakeRest provider,+ * by running FakeRest's own `q` search on each searchable resource and mapping+ * the matches to the function's result shape.*- * FIXME: this is an approximation of the view, not a reimplementation.+ * FIXME: this is an approximation of the function, not a reimplementation.* - one query per resource instead of one;- * - FakeRest matches the whole term as a single substring, where Postgres ANDs- * one `ilike` per word;- * - the result SET differs, not only the field values: this path takes the top- * 50 per resource and caps at 250, while Supabase takes one global top 50, so- * the demo build can surface a record production would not return.+ * - FakeRest matches the whole term as a single substring, where Postgres+ * matches each word as a word prefix, ignoring accents;+ * - FakeRest searches its own `q` fields, which differ from the fields+ * indexed by `global_search`.*/export async function getSearchResults(dataProvider: DataProvider,diff --git a/src/components/atomic-crm/providers/fakerest/dataProvider.ts b/src/components/atomic-crm/providers/fakerest/dataProvider.tsindex 898b133f..3c355c28 100644--- a/src/components/atomic-crm/providers/fakerest/dataProvider.ts+++ b/src/components/atomic-crm/providers/fakerest/dataProvider.ts@@ -17,6 +17,7 @@ import type {Sale,SalesFormData,SearchResourceName,+ SearchResult,SignUpData,Task,} from "../../types";@@ -141,11 +142,6 @@ const preserveAttachmentMimeType = <})),});-const parseResourceFilter = (- value: unknown,-): SearchResourceName[] | undefined =>- Array.isArray(value) ? (value as SearchResourceName[]) : undefined;-export const createDataProvider = ({db = generateData(),latency = 300,@@ -177,17 +173,6 @@ export const createDataProvider = ({const dataProviderWithCustomMethod: CrmDataProvider = {...baseDataProvider,async getList(resource: string, params: any) {- if (resource === "search_index") {- const { filter = {}, pagination } = params;- const all = await getSearchResults(- withSupabaseFilterAdapter(baseDataProvider),- filter.q ?? "",- parseResourceFilter(filter.resource_eq_any),- );- const { page, perPage } = pagination;- const start = (page - 1) * perPage;- return { data: all.slice(start, start + perPage), total: all.length };- }if (resource === "activity_log") {const { filter = {}, pagination } = params;const all = await getActivityLog(@@ -312,6 +297,18 @@ export const createDataProvider = ({return true;},+ globalSearch: async (+ query: string,+ resources: readonly SearchResourceName[],+ maxResults: number,+ ): Promise<SearchResult[]> => {+ const results = await getSearchResults(+ withSupabaseFilterAdapter(baseDataProvider),+ query,+ [...resources],+ );+ return results.slice(0, maxResults);+ },mergeContacts: async (sourceId: Identifier, targetId: Identifier) => {return mergeContacts(sourceId, targetId, baseDataProvider);},diff --git a/src/components/atomic-crm/providers/supabase/dataProvider.ts b/src/components/atomic-crm/providers/supabase/dataProvider.tsindex 73f376f8..45d5bb23 100644--- a/src/components/atomic-crm/providers/supabase/dataProvider.ts+++ b/src/components/atomic-crm/providers/supabase/dataProvider.ts@@ -13,6 +13,8 @@ import type {RAFile,Sale,SalesFormData,+ SearchResourceName,+ SearchResult,SignUpData,} from "../../types";import type { ConfigurationContextValue } from "../../root/ConfigurationContext";@@ -209,6 +211,22 @@ const getDataProviderWithCustomMethods = () => {async isInitialized() {return getIsInitialized();},+ async globalSearch(+ query: string,+ resources: readonly SearchResourceName[],+ maxResults: number,+ ): Promise<SearchResult[]> {+ const { data, error } = await getSupabaseClient().rpc("global_search", {+ query,+ resources,+ max_results: maxResults,+ });+ if (error) {+ console.error("global_search.error", error);+ throw new Error("Failed to search");+ }+ return data ?? [];+ },async mergeContacts(sourceId: Identifier, targetId: Identifier) {const { data, error } = await getSupabaseClient().functions.invoke("merge_contacts",@@ -356,27 +374,6 @@ const lifeCycleCallbacks: ResourceCallbacks[] = [return applyFullTextSearch(["name", "category", "description"])(params);},},- {- resource: "search_index",- beforeGetList: async (params) => {- if (!params.filter?.q) {- return params;- }- const { q, ...filter } = params.filter;- // A single `ilike` column: ra-data-postgrest splits the term on spaces- // and emits one `ilike` per word. qs.stringify uses the indices format,- // so the query string carries `content[0]=ilike.*a*&content[1]=ilike.*b*`;- // PostgREST strips the `[n]` subscripts and ANDs the repeated column, so- // "Thomas TF1" matches only rows whose content contains both words.- return {- ...params,- filter: {- ...filter,- "content@ilike": q,- },- };- },- },];export const getDataProvider = () => {diff --git a/src/components/atomic-crm/search/useGlobalSearch.ts b/src/components/atomic-crm/search/useGlobalSearch.tsindex c36024cb..1767fcbd 100644--- a/src/components/atomic-crm/search/useGlobalSearch.ts+++ b/src/components/atomic-crm/search/useGlobalSearch.ts@@ -1,22 +1,14 @@-import { useGetList } from "ra-core";+import { useQuery } from "@tanstack/react-query";+import { useDataProvider } from "ra-core";import { useEffect, useState } from "react";-import type { SearchResourceName, SearchResult } from "../types";+import type { CrmDataProvider } from "../providers/types";+import type { SearchResourceName } from "../types";export const MIN_SEARCH_LENGTH = 2;const DEBOUNCE_MS = 300;const MAX_RESULTS = 50;-const DISPLAYED_COLUMNS = [- "id",- "resource",- "record_id",- "title",- "subtitle",- "contact_id",- "deal_id",-] as const;-export const useDebouncedValue = <T>(value: T, delay: number): T => {const [debouncedValue, setDebouncedValue] = useState(value);@@ -29,7 +21,7 @@ export const useDebouncedValue = <T>(value: T, delay: number): T => {};/**- * Query the `search_index` view for a debounced search term.+ * Run the global search for a debounced search term.** Stays idle until the term reaches MIN_SEARCH_LENGTH, so opening the dialog* does not fire a request. `resources` restricts the query server-side, so the@@ -39,22 +31,16 @@ export const useGlobalSearch = (query: string,resources: readonly SearchResourceName[],) => {+ const dataProvider = useDataProvider<CrmDataProvider>();const debouncedQuery = useDebouncedValue(query.trim(), DEBOUNCE_MS);const isEnabled = debouncedQuery.length >= MIN_SEARCH_LENGTH;- const { data, isPending, error } = useGetList<SearchResult>(- "search_index",- {- filter: {- q: debouncedQuery,- "resource@in": `(${resources.join(",")})`,- },- pagination: { page: 1, perPage: MAX_RESULTS },- sort: { field: "date", order: "DESC" },- meta: { columns: [...DISPLAYED_COLUMNS] },- },- { enabled: isEnabled },- );+ const { data, isPending, error } = useQuery({+ queryKey: ["globalSearch", debouncedQuery, resources],+ queryFn: () =>+ dataProvider.globalSearch(debouncedQuery, resources, MAX_RESULTS),+ enabled: isEnabled,+ });return {results: isEnabled ? (data ?? []) : [],diff --git a/supabase/migrations/20260910140459_global_search.sql b/supabase/migrations/20260910140459_global_search.sqldeleted file mode 100644index e0e9e016..00000000--- a/supabase/migrations/20260910140459_global_search.sql+++ /dev/null@@ -1,99 +0,0 @@-alter table "public"."tasks" add column "created_at" timestamp with time zone not null default now();---- Existing rows have no creation time to recover, so fall back to the best--- available proxy: when the task was completed, a due date that has already--- passed, or the contact's first_seen. `least(..., now())` keeps the result in--- the past -- search_index sorts on this column, and a future date would put--- every open task above every note, contact and deal. A bare `now()` would be--- no better: it would tie every open task at the top of that same sort.-update "public"."tasks" t-set "created_at" = least(- coalesce(- t."done_date",- case when t."due_date" < now() then t."due_date" end,- c."first_seen"- ),- now()-)-from "public"."contacts" c-where c."id" = t."contact_id";--create or replace view "public"."search_index" with (security_invoker = on) as SELECT ('company.'::text || c.id) AS id,- 'companies'::text AS resource,- c.id AS record_id,- c.name AS title,- c.sector AS subtitle,- concat_ws(' '::text, c.name, c.sector, c.description, c.website, c.phone_number, c.zipcode, c.city, c.state_abbr) AS content,- NULL::bigint AS contact_id,- NULL::bigint AS deal_id,- c.created_at AS date- FROM public.companies c-UNION ALL- SELECT ('contact.'::text || co.id) AS id,- 'contacts'::text AS resource,- co.id AS record_id,- concat_ws(' '::text, co.first_name, co.last_name) AS title,- cc.name AS subtitle,- concat_ws(' '::text, co.first_name, co.last_name, co.title, co.background, cc.name, (jsonb_path_query_array(co.email_jsonb, '$[*]."email"'::jsonpath))::text, (jsonb_path_query_array(co.phone_jsonb, '$[*]."number"'::jsonpath))::text) AS content,- co.id AS contact_id,- NULL::bigint AS deal_id,- co.first_seen AS date- FROM (public.contacts co- LEFT JOIN public.companies cc ON ((cc.id = co.company_id)))-UNION ALL- SELECT ('deal.'::text || d.id) AS id,- 'deals'::text AS resource,- d.id AS record_id,- d.name AS title,- dc.name AS subtitle,- concat_ws(' '::text, d.name, d.category, d.description, dc.name) AS content,- NULL::bigint AS contact_id,- d.id AS deal_id,- d.created_at AS date- FROM (public.deals d- LEFT JOIN public.companies dc ON ((dc.id = d.company_id)))- WHERE (d.archived_at IS NULL)-UNION ALL- SELECT ('task.'::text || t.id) AS id,- 'tasks'::text AS resource,- t.id AS record_id,- "left"(t.text, 200) AS title,- concat_ws(' '::text, tco.first_name, tco.last_name) AS subtitle,- concat_ws(' '::text, t.text, t.type, tco.first_name, tco.last_name) AS content,- t.contact_id,- NULL::bigint AS deal_id,- t.created_at AS date- FROM (public.tasks t- LEFT JOIN public.contacts tco ON ((tco.id = t.contact_id)))- WHERE (t.done_date IS NULL)-UNION ALL- SELECT ('contactNote.'::text || cn.id) AS id,- 'contact_notes'::text AS resource,- cn.id AS record_id,- "left"(cn.text, 200) AS title,- concat_ws(' '::text, nco.first_name, nco.last_name) AS subtitle,- concat_ws(' '::text, cn.text, nco.first_name, nco.last_name, ( SELECT string_agg((a.a ->> 'title'::text), ' '::text) AS string_agg- FROM unnest(cn.attachments) a(a))) AS content,- cn.contact_id,- NULL::bigint AS deal_id,- cn.date- FROM (public.contact_notes cn- LEFT JOIN public.contacts nco ON ((nco.id = cn.contact_id)))-UNION ALL- SELECT ('dealNote.'::text || dn.id) AS id,- 'deal_notes'::text AS resource,- dn.id AS record_id,- "left"(dn.text, 200) AS title,- nd.name AS subtitle,- concat_ws(' '::text, dn.text, nd.name, ( SELECT string_agg((a.a ->> 'title'::text), ' '::text) AS string_agg- FROM unnest(dn.attachments) a(a))) AS content,- NULL::bigint AS contact_id,- dn.deal_id,- dn.date- FROM (public.deal_notes dn- LEFT JOIN public.deals nd ON ((nd.id = dn.deal_id)))- WHERE (nd.archived_at IS NULL);--grant all on table public.search_index to anon;-grant all on table public.search_index to authenticated;-grant all on table public.search_index to service_role;diff --git a/supabase/migrations/20261010054346_global_search.sql b/supabase/migrations/20261010054346_global_search.sqlnew file mode 100644index 00000000..a9dbb62b--- /dev/null+++ b/supabase/migrations/20261010054346_global_search.sql@@ -0,0 +1,205 @@+alter table "public"."tasks" add column "created_at" timestamp with time zone not null default now();++-- Existing rows have no creation time to recover, so fall back to the best+-- available proxy: when the task was completed, a due date that has already+-- passed, or the contact's first_seen. `least(..., now())` keeps the result in+-- the past -- global_search sorts on this column, and a future date would put+-- every open task above every note, contact and deal. A bare `now()` would be+-- no better: it would tie every open task at the top of that same sort.+update "public"."tasks" t+set "created_at" = least(+ coalesce(+ t."done_date",+ case when t."due_date" < now() then t."due_date" end,+ c."first_seen"+ ),+ now()+)+from "public"."contacts" c+where c."id" = t."contact_id";++create extension if not exists "unaccent" with schema "extensions";++-- unaccent() is only STABLE (its dictionary could change), which generated+-- columns reject. The dictionary is fixed here, so the wrapper is immutable.+create or replace function public.search_unaccent(value text) returns text+ language sql immutable strict parallel safe+ set search_path to ''+ as $$ select extensions.unaccent('extensions.unaccent'::regdictionary, value) $$;++-- Documents and queries are normalized the same way: unaccented, lowercased,+-- split on anything that is not a letter or a digit. So "hélè[email protected]"+-- indexes as helene, dupont, acme, fr and any of those prefixes finds it.+create or replace function public.search_document(variadic parts text[]) returns tsvector+ language sql immutable parallel safe+ set search_path to ''+ as $$+ select to_tsvector(+ 'simple'::regconfig,+ regexp_replace(public.search_unaccent(array_to_string(parts, ' ')), '[^[:alnum:]]+', ' ', 'g')+ )+ $$;++-- Every word of the query must match, each one as a prefix: "tho dup" finds+-- "Thomas Dupont". Returns null (matching nothing) for a blank query.+create or replace function public.search_query(query text) returns tsquery+ language sql immutable strict parallel safe+ set search_path to ''+ as $$+ select to_tsquery('simple'::regconfig, string_agg(word || ':*', ' & '))+ from regexp_split_to_table(lower(public.search_unaccent(query)), '[^[:alnum:]]+') as word+ where word <> ''+ $$;++create or replace function public.search_attachment_titles(attachments jsonb[]) returns text+ language sql immutable parallel safe+ set search_path to ''+ as $$ select string_agg(attachment ->> 'title', ' ') from unnest(attachments) as attachment $$;++alter table public.companies add column fts tsvector generated always as (+ public.search_document(name, sector, description, website::text, phone_number, zipcode, city, state_abbr)+) stored;++alter table public.contacts add column fts tsvector generated always as (+ public.search_document(+ first_name,+ last_name,+ title,+ background,+ jsonb_path_query_array(email_jsonb, '$[*]."email"')::text,+ jsonb_path_query_array(phone_jsonb, '$[*]."number"')::text+ )+) stored;++alter table public.deals add column fts tsvector generated always as (+ public.search_document(name, category, description)+) stored;++alter table public.tasks add column fts tsvector generated always as (+ public.search_document(text, type)+) stored;++alter table public.contact_notes add column fts tsvector generated always as (+ public.search_document(text, public.search_attachment_titles(attachments))+) stored;++alter table public.deal_notes add column fts tsvector generated always as (+ public.search_document(text, public.search_attachment_titles(attachments))+) stored;++-- Partial indexes mirror what global_search() returns: open tasks, live deals.+create index companies_fts_idx on public.companies using gin (fts);+create index contacts_fts_idx on public.contacts using gin (fts);+create index deals_fts_idx on public.deals using gin (fts) where archived_at is null;+create index tasks_fts_idx on public.tasks using gin (fts) where done_date is null;+create index contact_notes_fts_idx on public.contact_notes using gin (fts);+create index deal_notes_fts_idx on public.deal_notes using gin (fts);++-- A word found in most rows (a common word, a 2-letter prefix) makes the GIN+-- path fetch and sort every match. Walking a date index backwards instead stops+-- at the first max_results matches. The planner picks between the two per+-- query, which needs fine-grained lexeme statistics on the large tables.+create index contacts_first_seen_idx on public.contacts using btree (first_seen desc nulls last);+create index tasks_open_created_at_idx on public.tasks using btree (created_at desc) where done_date is null;+create index contact_notes_date_idx on public.contact_notes using btree (date desc nulls last);+create index deal_notes_date_idx on public.deal_notes using btree (date desc nulls last);++alter table public.contacts alter column fts set statistics 10000;+alter table public.tasks alter column fts set statistics 10000;+alter table public.contact_notes alter column fts set statistics 10000;+alter table public.deal_notes alter column fts set statistics 10000;++-- Each branch takes its own newest max_results matches, so the union never+-- holds more than 6 * max_results rows, and the lookups that build display+-- labels only run on those. The SQL is built per call with the tsquery inlined+-- as a literal: with a parameter the planner could not tell a rare word (use+-- the GIN index) from a common one (walk the date index).+create or replace function public.global_search(+ query text,+ resources text[] default null,+ max_results integer default 50+) returns table (+ id text,+ resource text,+ record_id bigint,+ title text,+ subtitle text,+ contact_id bigint,+ deal_id bigint,+ date timestamp with time zone+)+ language plpgsql stable security invoker+ set search_path to ''+ as $$+declare+ tsq tsquery := public.search_query(query);+ branch_templates constant jsonb := jsonb_build_object(+ 'companies', $q$+ select 'company.' || c.id, 'companies', c.id, c.name, c.sector, null::bigint, null::bigint, c.created_at+ from public.companies c+ where c.fts @@ %1$L::tsquery+ order by c.created_at desc+ limit %2$s $q$,+ 'contacts', $q$+ select 'contact.' || co.id, 'contacts', co.id, concat_ws(' ', co.first_name, co.last_name),+ (select cc.name from public.companies cc where cc.id = co.company_id), co.id, null::bigint, co.first_seen+ from public.contacts co+ where co.fts @@ %1$L::tsquery+ order by co.first_seen desc nulls last+ limit %2$s $q$,+ 'deals', $q$+ select 'deal.' || d.id, 'deals', d.id, d.name,+ (select dc.name from public.companies dc where dc.id = d.company_id), null::bigint, d.id, d.created_at+ from public.deals d+ where d.archived_at is null and d.fts @@ %1$L::tsquery+ order by d.created_at desc+ limit %2$s $q$,+ 'tasks', $q$+ select 'task.' || t.id, 'tasks', t.id, left(t.text, 200),+ (select concat_ws(' ', tc.first_name, tc.last_name) from public.contacts tc where tc.id = t.contact_id),+ t.contact_id, null::bigint, t.created_at+ from public.tasks t+ where t.done_date is null and t.fts @@ %1$L::tsquery+ order by t.created_at desc+ limit %2$s $q$,+ 'contact_notes', $q$+ select 'contactNote.' || cn.id, 'contact_notes', cn.id, left(cn.text, 200),+ (select concat_ws(' ', nc.first_name, nc.last_name) from public.contacts nc where nc.id = cn.contact_id),+ cn.contact_id, null::bigint, cn.date+ from public.contact_notes cn+ where cn.fts @@ %1$L::tsquery+ order by cn.date desc nulls last+ limit %2$s $q$,+ 'deal_notes', $q$+ select 'dealNote.' || dn.id, 'deal_notes', dn.id, left(dn.text, 200), nd.name, null::bigint, dn.deal_id, dn.date+ from public.deal_notes dn+ join public.deals nd on nd.id = dn.deal_id+ where nd.archived_at is null and dn.fts @@ %1$L::tsquery+ order by dn.date desc nulls last+ limit %2$s $q$+ );+ branches text[];+begin+ if tsq is null or max_results < 1 then+ return;+ end if;++ select array_agg(format('(%s)', format(template, tsq::text, max_results)))+ into branches+ from jsonb_each_text(branch_templates) as b(name, template)+ where resources is null or b.name = any(resources);++ if branches is null then+ return;+ end if;++ return query execute format(+ 'select * from (%s) results order by 8 desc nulls last limit %s',+ array_to_string(branches, ' union all '),+ max_results+ );+end+$$;++revoke execute on function public.global_search(text, text[], integer) from public, anon;+grant execute on function public.global_search(text, text[], integer) to authenticated, service_role;diff --git a/supabase/schemas/03_views.sql b/supabase/schemas/03_views.sqlindex a43b80e9..1b2ec74f 100644--- a/supabase/schemas/03_views.sql+++ b/supabase/schemas/03_views.sql@@ -132,114 +132,3 @@ select count(sub.id) as is_initializedfrom (select sales.id from public.sales limit 1) sub;--create or replace view public.search_index with (security_invoker = on) as-select- ('company.' || c.id) as id,- 'companies' as resource,- c.id as record_id,- c.name as title,- c.sector as subtitle,- concat_ws(- ' ',- c.name,- c.sector,- c.description,- c.website,- c.phone_number,- c.zipcode,- c.city,- c.state_abbr- ) as content,- null::bigint as contact_id,- null::bigint as deal_id,- c.created_at as date-from public.companies c-union all-select- ('contact.' || co.id) as id,- 'contacts' as resource,- co.id as record_id,- concat_ws(' ', co.first_name, co.last_name) as title,- cc.name as subtitle,- concat_ws(- ' ',- co.first_name,- co.last_name,- co.title,- co.background,- cc.name,- (jsonb_path_query_array(co.email_jsonb, '$[*]."email"'))::text,- (jsonb_path_query_array(co.phone_jsonb, '$[*]."number"'))::text- ) as content,- co.id as contact_id,- null::bigint as deal_id,- co.first_seen as date-from public.contacts co- left join public.companies cc on cc.id = co.company_id-union all-select- ('deal.' || d.id) as id,- 'deals' as resource,- d.id as record_id,- d.name as title,- dc.name as subtitle,- concat_ws(' ', d.name, d.category, d.description, dc.name) as content,- null::bigint as contact_id,- d.id as deal_id,- d.created_at as date-from public.deals d- left join public.companies dc on dc.id = d.company_id-where d.archived_at is null-union all-select- ('task.' || t.id) as id,- 'tasks' as resource,- t.id as record_id,- left(t.text, 200) as title,- concat_ws(' ', tco.first_name, tco.last_name) as subtitle,- concat_ws(' ', t.text, t.type, tco.first_name, tco.last_name) as content,- t.contact_id,- null::bigint as deal_id,- t.created_at as date-from public.tasks t- left join public.contacts tco on tco.id = t.contact_id-where t.done_date is null-union all-select- ('contactNote.' || cn.id) as id,- 'contact_notes' as resource,- cn.id as record_id,- left(cn.text, 200) as title,- concat_ws(' ', nco.first_name, nco.last_name) as subtitle,- concat_ws(- ' ',- cn.text,- nco.first_name,- nco.last_name,- (select string_agg(a ->> 'title', ' ') from unnest(cn.attachments) as a)- ) as content,- cn.contact_id,- null::bigint as deal_id,- cn.date-from public.contact_notes cn- left join public.contacts nco on nco.id = cn.contact_id-union all-select- ('dealNote.' || dn.id) as id,- 'deal_notes' as resource,- dn.id as record_id,- left(dn.text, 200) as title,- nd.name as subtitle,- concat_ws(- ' ',- dn.text,- nd.name,- (select string_agg(a ->> 'title', ' ') from unnest(dn.attachments) as a)- ) as content,- null::bigint as contact_id,- dn.deal_id,- dn.date-from public.deal_notes dn- left join public.deals nd on nd.id = dn.deal_id-where nd.archived_at is null;diff --git a/supabase/schemas/06_grants.sql b/supabase/schemas/06_grants.sqlindex 7d2f7aac..865c9dbc 100644--- a/supabase/schemas/06_grants.sql+++ b/supabase/schemas/06_grants.sql@@ -123,10 +123,6 @@ grant all on table public.init_state to anon;grant all on table public.init_state to authenticated;grant all on table public.init_state to service_role;-grant all on table public.search_index to anon;-grant all on table public.search_index to authenticated;-grant all on table public.search_index to service_role;--- Sequence grantsgrant all on sequence public.companies_id_seq to anon;grant all on sequence public.companies_id_seq to authenticated;diff --git a/supabase/schemas/08_search.sql b/supabase/schemas/08_search.sqlnew file mode 100644index 00000000..56228c64--- /dev/null+++ b/supabase/schemas/08_search.sql@@ -0,0 +1,192 @@+--+-- Global search+-- Each searchable table carries a generated tsvector column with a GIN index,+-- and global_search() queries them through RLS (security invoker), so search+-- visibility always follows the table policies.+--++create extension if not exists "unaccent" with schema "extensions";++-- unaccent() is only STABLE (its dictionary could change), which generated+-- columns reject. The dictionary is fixed here, so the wrapper is immutable.+create or replace function public.search_unaccent(value text) returns text+ language sql immutable strict parallel safe+ set search_path to ''+ as $$ select extensions.unaccent('extensions.unaccent'::regdictionary, value) $$;++-- Documents and queries are normalized the same way: unaccented, lowercased,+-- split on anything that is not a letter or a digit. So "hélè[email protected]"+-- indexes as helene, dupont, acme, fr and any of those prefixes finds it.+create or replace function public.search_document(variadic parts text[]) returns tsvector+ language sql immutable parallel safe+ set search_path to ''+ as $$+ select to_tsvector(+ 'simple'::regconfig,+ regexp_replace(public.search_unaccent(array_to_string(parts, ' ')), '[^[:alnum:]]+', ' ', 'g')+ )+ $$;++-- Every word of the query must match, each one as a prefix: "tho dup" finds+-- "Thomas Dupont". Returns null (matching nothing) for a blank query.+create or replace function public.search_query(query text) returns tsquery+ language sql immutable strict parallel safe+ set search_path to ''+ as $$+ select to_tsquery('simple'::regconfig, string_agg(word || ':*', ' & '))+ from regexp_split_to_table(lower(public.search_unaccent(query)), '[^[:alnum:]]+') as word+ where word <> ''+ $$;++create or replace function public.search_attachment_titles(attachments jsonb[]) returns text+ language sql immutable parallel safe+ set search_path to ''+ as $$ select string_agg(attachment ->> 'title', ' ') from unnest(attachments) as attachment $$;++alter table public.companies add column fts tsvector generated always as (+ public.search_document(name, sector, description, website::text, phone_number, zipcode, city, state_abbr)+) stored;++alter table public.contacts add column fts tsvector generated always as (+ public.search_document(+ first_name,+ last_name,+ title,+ background,+ jsonb_path_query_array(email_jsonb, '$[*]."email"')::text,+ jsonb_path_query_array(phone_jsonb, '$[*]."number"')::text+ )+) stored;++alter table public.deals add column fts tsvector generated always as (+ public.search_document(name, category, description)+) stored;++alter table public.tasks add column fts tsvector generated always as (+ public.search_document(text, type)+) stored;++alter table public.contact_notes add column fts tsvector generated always as (+ public.search_document(text, public.search_attachment_titles(attachments))+) stored;++alter table public.deal_notes add column fts tsvector generated always as (+ public.search_document(text, public.search_attachment_titles(attachments))+) stored;++-- Partial indexes mirror what global_search() returns: open tasks, live deals.+create index companies_fts_idx on public.companies using gin (fts);+create index contacts_fts_idx on public.contacts using gin (fts);+create index deals_fts_idx on public.deals using gin (fts) where archived_at is null;+create index tasks_fts_idx on public.tasks using gin (fts) where done_date is null;+create index contact_notes_fts_idx on public.contact_notes using gin (fts);+create index deal_notes_fts_idx on public.deal_notes using gin (fts);++-- A word found in most rows (a common word, a 2-letter prefix) makes the GIN+-- path fetch and sort every match. Walking a date index backwards instead stops+-- at the first max_results matches. The planner picks between the two per+-- query, which needs fine-grained lexeme statistics on the large tables.+create index contacts_first_seen_idx on public.contacts using btree (first_seen desc nulls last);+create index tasks_open_created_at_idx on public.tasks using btree (created_at desc) where done_date is null;+create index contact_notes_date_idx on public.contact_notes using btree (date desc nulls last);+create index deal_notes_date_idx on public.deal_notes using btree (date desc nulls last);++alter table public.contacts alter column fts set statistics 10000;+alter table public.tasks alter column fts set statistics 10000;+alter table public.contact_notes alter column fts set statistics 10000;+alter table public.deal_notes alter column fts set statistics 10000;++-- Each branch takes its own newest max_results matches, so the union never+-- holds more than 6 * max_results rows, and the lookups that build display+-- labels only run on those. The SQL is built per call with the tsquery inlined+-- as a literal: with a parameter the planner could not tell a rare word (use+-- the GIN index) from a common one (walk the date index).+create or replace function public.global_search(+ query text,+ resources text[] default null,+ max_results integer default 50+) returns table (+ id text,+ resource text,+ record_id bigint,+ title text,+ subtitle text,+ contact_id bigint,+ deal_id bigint,+ date timestamp with time zone+)+ language plpgsql stable security invoker+ set search_path to ''+ as $$+declare+ tsq tsquery := public.search_query(query);+ branch_templates constant jsonb := jsonb_build_object(+ 'companies', $q$+ select 'company.' || c.id, 'companies', c.id, c.name, c.sector, null::bigint, null::bigint, c.created_at+ from public.companies c+ where c.fts @@ %1$L::tsquery+ order by c.created_at desc+ limit %2$s $q$,+ 'contacts', $q$+ select 'contact.' || co.id, 'contacts', co.id, concat_ws(' ', co.first_name, co.last_name),+ (select cc.name from public.companies cc where cc.id = co.company_id), co.id, null::bigint, co.first_seen+ from public.contacts co+ where co.fts @@ %1$L::tsquery+ order by co.first_seen desc nulls last+ limit %2$s $q$,+ 'deals', $q$+ select 'deal.' || d.id, 'deals', d.id, d.name,+ (select dc.name from public.companies dc where dc.id = d.company_id), null::bigint, d.id, d.created_at+ from public.deals d+ where d.archived_at is null and d.fts @@ %1$L::tsquery+ order by d.created_at desc+ limit %2$s $q$,+ 'tasks', $q$+ select 'task.' || t.id, 'tasks', t.id, left(t.text, 200),+ (select concat_ws(' ', tc.first_name, tc.last_name) from public.contacts tc where tc.id = t.contact_id),+ t.contact_id, null::bigint, t.created_at+ from public.tasks t+ where t.done_date is null and t.fts @@ %1$L::tsquery+ order by t.created_at desc+ limit %2$s $q$,+ 'contact_notes', $q$+ select 'contactNote.' || cn.id, 'contact_notes', cn.id, left(cn.text, 200),+ (select concat_ws(' ', nc.first_name, nc.last_name) from public.contacts nc where nc.id = cn.contact_id),+ cn.contact_id, null::bigint, cn.date+ from public.contact_notes cn+ where cn.fts @@ %1$L::tsquery+ order by cn.date desc nulls last+ limit %2$s $q$,+ 'deal_notes', $q$+ select 'dealNote.' || dn.id, 'deal_notes', dn.id, left(dn.text, 200), nd.name, null::bigint, dn.deal_id, dn.date+ from public.deal_notes dn+ join public.deals nd on nd.id = dn.deal_id+ where nd.archived_at is null and dn.fts @@ %1$L::tsquery+ order by dn.date desc nulls last+ limit %2$s $q$+ );+ branches text[];+begin+ if tsq is null or max_results < 1 then+ return;+ end if;++ select array_agg(format('(%s)', format(template, tsq::text, max_results)))+ into branches+ from jsonb_each_text(branch_templates) as b(name, template)+ where resources is null or b.name = any(resources);++ if branches is null then+ return;+ end if;++ return query execute format(+ 'select * from (%s) results order by 8 desc nulls last limit %s',+ array_to_string(branches, ' union all '),+ max_results+ );+end+$$;++revoke execute on function public.global_search(text, text[], integer) from public, anon;+grant execute on function public.global_search(text, text[], integer) to authenticated, service_role;