← All @molecule/* packages · App templates
@molecule/api-search-postgresProvider bond · search · API (Node) · v1.0.1 · Apache-2.0
PostgreSQL full-text search provider for molecule.dev
npm install @molecule/api-search-postgresnpm · Source on GitHub · Implements @molecule/api-search
@molecule/api-search-postgres is a provider bond on the API (Node) side: it implements the search core interface (@molecule/api-search) with a concrete vendor or library behind it.
Your code calls the core; you wire this provider once at startup. Swapping vendors later is one line in that wiring, not a rewrite.
import { setProvider } from '@molecule/api-search'
import { provider } from '@molecule/api-search-postgres'
setProvider(provider)Works with: @molecule/api-database, @molecule/api-search
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
PostgreSQL full-text search provider for molecule.dev.
Implements the SearchProvider interface using PostgreSQL's built-in
tsvector/tsquery full-text search capabilities. No external search
engine required — uses the existing database bond.
import { setProvider } from '@molecule/api-search'
import { provider } from '@molecule/api-search-postgres'
setProvider(provider)
provider
npm install @molecule/api-search-postgres @molecule/api-database @molecule/api-search
BulkIndexResultResult of a bulk index operation.
interface BulkIndexResult {
/**
* Number of documents successfully indexed.
*/
indexed: number
/**
* Number of documents that failed to index.
*/
failed: number
/**
* Errors encountered during bulk indexing, keyed by document id.
*/
errors: Record<string, string>
}
FacetCountA single facet count entry.
interface FacetCount {
/**
* The facet value.
*/
value: string
/**
* Number of documents matching this facet value.
*/
count: number
}
IndexDocumentA document to be indexed in a bulk operation.
interface IndexDocument {
/**
* Unique identifier for the document.
*/
id: string
/**
* The document fields and values.
*/
document: Record<string, unknown>
}
IndexSchemaSchema definition for a search index, describing the fields and their roles.
interface IndexSchema {
/**
* Map of field names to their types.
*/
fields: Record<string, FieldType>
/**
* Fields that are searchable via full-text queries.
*/
searchableFields?: string[]
/**
* Fields that can be used in filter expressions.
*/
filterableFields?: string[]
/**
* Fields that can be used for sorting results.
*/
sortableFields?: string[]
}
PostgresSearchOptionsConfiguration options for the PostgreSQL search provider.
interface PostgresSearchOptions {
/**
* PostgreSQL text search configuration (language).
*
* @default 'english'
*/
searchConfig?: string
/**
* Table prefix for search index tables.
*
* @default 'search_'
*/
tablePrefix?: string
/**
* Whether to use GIN indexes for faster text search.
*
* @default true
*/
useGinIndex?: boolean
}
SearchHitA single search result hit.
interface SearchHit {
/**
* Document identifier.
*/
id: string
/**
* Relevance score.
*/
score: number
/**
* The matched document fields.
*/
document: Record<string, unknown>
/**
* Highlighted field snippets, keyed by field name.
*/
highlights?: Record<string, string[]>
}
SearchProviderSearch provider interface.
All search providers must implement this interface to provide full-text search, indexing, and suggestion capabilities.
interface SearchProvider {
/**
* Creates a search index with an optional schema.
*
* @param name - Index name.
* @param schema - Optional schema describing field types and roles.
*/
createIndex(name: string, schema?: IndexSchema): Promise<void>
/**
* Deletes a search index and all its documents.
*
* @param name - Index name to delete.
*/
deleteIndex(name: string): Promise<void>
/**
* Indexes a single document.
*
* @param indexName - Target index name.
* @param id - Unique document identifier.
* @param document - The document fields and values.
*/
index(indexName: string, id: string, document: Record<string, unknown>): Promise<void>
/**
* Indexes multiple documents in a single operation.
*
* @param indexName - Target index name.
* @param documents - Array of documents to index.
* @returns Result with indexed/failed counts and errors.
*/
bulkIndex(indexName: string, documents: IndexDocument[]): Promise<BulkIndexResult>
/**
* Executes a full-text search query against an index.
*
* @param indexName - Index to search.
* @param query - Search query with text, filters, pagination, etc.
* @returns Search results with hits, total count, facets, and timing.
*/
search(indexName: string, query: SearchQuery): Promise<SearchResult>
/**
* Deletes a document from an index by id.
*
* @param indexName - Index containing the document.
* @param id - Document identifier to delete.
*/
delete(indexName: string, id: string): Promise<void>
/**
* Returns typeahead/autocomplete suggestions for a partial query.
*
* @param indexName - Index to generate suggestions from.
* @param query - Partial text to complete.
* @param options - Suggestion options (limit, fields, fuzzy).
* @returns Array of suggestions sorted by relevance.
*/
suggest(indexName: string, query: string, options?: SuggestOptions): Promise<Suggestion[]>
/**
* Retrieves a single document from an index by id.
*
* @param indexName - Index containing the document.
* @param id - Document identifier.
* @returns The document fields, or `null` if not found.
*/
getDocument(indexName: string, id: string): Promise<Record<string, unknown> | null>
}
SearchQueryA full-text search query with optional filters, facets, sorting, and pagination.
interface SearchQuery {
/**
* The search text.
*
* Empty or whitespace-only text is "browse" mode: bond implementations
* MUST match ALL documents (subject to `filters`, `sort`, and pagination)
* rather than erroring or returning zero hits. Every bundled bond
* (Elasticsearch, Meilisearch, Typesense, PostgreSQL) follows this
* contract, so swapping providers doesn't silently change what an empty
* search box shows.
*/
text: string
/**
* Filter expressions to narrow results.
*/
filters?: Record<string, unknown>
/**
* Fields to compute facet counts for.
*/
facets?: string[]
/**
* Sort fields and directions.
*/
sort?: SortField[]
/**
* Page number (1-based).
*/
page?: number
/**
* Number of results per page.
*/
perPage?: number
/**
* Whether to include highlighted snippets in results.
*/
highlight?: boolean
}
SearchResultThe result of a search query, including hits, pagination, facets, and timing.
interface SearchResult {
/**
* Matched documents.
*/
hits: SearchHit[]
/**
* Total number of matching documents.
*/
total: number
/**
* Current page number.
*/
page: number
/**
* Number of results per page.
*/
perPage: number
/**
* Facet counts keyed by field name.
*/
facets?: Record<string, FacetCount[]>
/**
* Time taken to process the query in milliseconds.
*/
processingTimeMs: number
}
SortFieldA field to sort search results by.
interface SortField {
/**
* The field name to sort on.
*/
field: string
/**
* Sort direction.
*/
direction: SortDirection
}
SuggestionA single autocomplete suggestion.
interface Suggestion {
/**
* The suggested text.
*/
text: string
/**
* Relevance score for ranking suggestions.
*/
score: number
/**
* Optional highlighted version of the suggestion.
*/
highlighted?: string
}
SuggestOptionsOptions for typeahead / autocomplete suggestions.
interface SuggestOptions {
/**
* Maximum number of suggestions to return.
*/
limit?: number
/**
* Fields to generate suggestions from.
*/
fields?: string[]
/**
* Whether to apply fuzzy matching.
*/
fuzzy?: boolean
}
FieldTypeField type for index schema definitions.
type FieldType = 'text' | 'keyword' | 'number' | 'boolean' | 'date' | 'geo'
SortDirectionSort direction for search results.
type SortDirection = 'asc' | 'desc'
createProvider(options)Creates a PostgreSQL full-text search provider instance.
function createProvider(options?: PostgresSearchOptions): SearchProvider
options — Provider configuration options.Returns: A fully configured SearchProvider implementation.
providerDefault lazily-initialized PostgreSQL search provider. Uses the bonded database pool for queries.
const provider: SearchProvider
Implements @molecule/api-search interface.
Setup function to register this provider with the core interface:
import { setProvider } from '@molecule/api-search'
import { provider } from '@molecule/api-search-postgres'
export function setupSearchPostgres(): void {
setProvider(provider)
}
Peer dependencies:
@molecule/api-database ^1.0.1@molecule/api-search ^1.0.1@molecule/api-database@molecule/api-searchProvider-specific behavior to know before debugging:
SearchQuery.text contract, consistent with the meilisearch/typesense
bonds. suggest()'s partial-query parameter is different: empty input
there still returns [] (no autocomplete suggestions), not every document.widget cas matches "widget case"). Operators
typed by users (!, (, :, &, quotes) are treated as literal text, never
as tsquery syntax.GROUP BY document->>field query per requested
facet field (values coerced to text, capped at the top 100 by count —
SearchResult.facets[field]). Budget for that extra round trip when
requesting facets on a hot path or many fields at once.document->>field = value, field bound as a query parameter — not a
sanitized identifier, so punctuation in field names like 'product-type'
works). Nested paths and range filters are not supported.IndexSchema.fields passed to createIndex() (number →
double precision, date → timestamptz, boolean → boolean) — the
type map is recorded in a companion <prefix>schema_meta table at
createIndex() time. Fields NOT in the schema (or indices created
without one) still compare as text, so lexicographic ordering
("10" < "9") is still possible for undeclared fields.content column (the same plain text
index()/bulkIndex() extract for full-text matching), not the raw
JSON document — so snippets never contain JSON braces/keys/quotes. They
still come back under a single key, _content, rather than per-field
like the engine-backed bonds. Rows written before this column existed
backfill it (empty string) on the next createIndex() call and get real
content on their next index()/bulkIndex() write.Integration checklist — drive the real UI (live preview, no mocks), adapt each item to this app's actual screens/flows, and check every box off one by one. A box you can't check is an integration bug to fix — not a skip:
text matches
ALL documents by contract) — not zero results and not an error.