Appearance
Search
Document-store includes a keyword-based search system. Register a search function per type to extract searchable content, then query with store.find().
Registering search functions
The search callback in registerType extracts keywords from a document. It returns a KeywordMap — an object where keys are section names and values are string arrays:
typescript
store.registerType('bookmark', {
search: async (doc) => ({
content: [doc.title, doc.url],
tags: doc.tags || [],
})
});The search function is called every time a document is created or edited. The extracted keywords are stored alongside the rendered document.
Searching
Use store.find() to search across documents:
typescript
// Search all sections
const hashes = await store.find('example');
// Search a specific section
const hashes = await store.find('example', 'content');
const tagMatches = await store.find('javascript', 'tags');Returns an array of matching document hashes.
Keyword extraction
A common pattern is a helper that tokenizes text into keywords:
typescript
function getKeywords(...texts: (string | undefined)[]): string[] {
return texts
.filter(Boolean)
.join(' ')
.toLowerCase()
.split(/\s+/)
.filter(w => w.length > 1);
}
store.registerType('post', {
search: async (doc) => ({
content: getKeywords(doc.title, doc.body),
tags: doc.tags || [],
})
});Sections
Sections let you organize keywords by category. Search within a section to narrow results:
typescript
store.registerType('video', {
search: async (doc) => ({
content: getKeywords(doc.title, doc.description),
tags: doc.tags || [],
creator: [doc.creatorName],
})
});
// Find videos tagged 'music'
await store.find('music', 'tags');
// Find videos by creator name
await store.find('alice', 'creator');How it works
- On
add()oredit(), the store calls your search function with the rendered document - The resulting
KeywordMapis stored in the persistence layer alongside the document store.find()delegates to the persistence layer's search implementation- SQLite uses
LIKEqueries on the stored keywords JSON - MongoDB can use text indexes on the keywords field
The search is intentionally simple — it's useful for basic lookups and filtering. For advanced full-text search, query your database directly using its native capabilities.