@mistriwork/search-postgres
v1.4.2
Published
PostgreSQL tsvector + GIN search provider pack for mistri Apps, living beside your deployment's PostgreSQL database.
Maintainers
Readme
@mistriwork/search-postgres
PostgreSQL tsvector + GIN as the search provider for a mistri App's search blocks.
What it is
Search is an index, never a source of truth. The index holds a copy of the fields a resource declares searchable, keyed by the record's primary key, and answers a phrase with keys; the rows are re-read from the database under the reader's own grants. The pack holds only what is PostgreSQL's:
PostgresTsvectorIndex: one table per collection in the database the connection names, overpg(loaded lazily on first use). Each table holdsid text NOT NULL, each declared field astext,mistri_tenant text, one generatedtsvectorcolumn per field (to_tsvector('simple', coalesce(field, ''))) with a GIN index per vector column, and a unique index on(id, coalesce(mistri_tenant, '')). It ranks byts_rankwith an earlier asked field weighing more and ties falling toid, and rebuilds a table under an advisory lock when a declaration adds a field. Field lists are cached per process and refreshed from the catalog when another process rebuilds the table.postgresSearchConnectionOf&postgresSearch: validates connection entries and creates aSearchProviderwhoseclose()drains open pools.
How it plugs in
Pass the provider to createHandler({ search }) from @mistriwork/mistri/handler,
or plug it into a Registry with plugSearch from @mistriwork/mistri/search:
import { createHandler } from '@mistriwork/mistri/handler';
import { postgresSearch } from '@mistriwork/search-postgres';
const mistri = createHandler({
config: './mistri.json',
basePath: '/desk',
publicOrigin: 'https://example.com',
search: postgresSearch([{ name: 'pgsearch', url: '${DATABASE_URL}' }]),
});An App names the connection in its resource:
resources: { ticket: { search: { connection: 'pgsearch', fields: ['subject', 'requester.name'] } } }The pool is opened on first use. ${VAR} references in url expand when it
opens and are never logged. The index lives in PostgreSQL tables, so it
survives restarts with no reindex required.
What it is not
- A large catalog.
tsvectortables beside the deployment's database suit moderate tables. A large catalog wants a dedicated engine such as Typesense. - A schema-scoped runtime store. Provisioning is lazy on
ensureCollection; point the connection at a dedicated database or a role with a pinnedsearch_pathwhen the index should not share a schema with business tables.
