patternModerate
Adding platform features to a knowledge base — schema migrations, search sorting, entry templates, and auto-related entries
Viewed 0 times
canonical entriessearch sortingfreshness detectionsolution verificationusage contextentry templatesrelated entriesbidirectional linkingtoken efficiency
Problem
When evolving a simple knowledge base into a platform with features like search sorting, canonical entries, solution verification, freshness tracking, and auto-related entries, the implementation requires careful coordination across schema, API, and MCP layers while maintaining backward compatibility and token efficiency for AI consumers.
Solution
Structure the implementation in layers: (1) Schema migrations using a safe migrateAddColumn pattern with try/catch for idempotency. (2) Pure functions for computed properties like freshness (computed on read, not stored separately). (3) API extensions that use optional parameters and strip empty/default values to avoid breaking existing consumers. (4) For auto-related entries, use FTS to find similar entries on insert and create bidirectional links. Key patterns: Use stripEmpty to omit default values from responses (saves tokens), compute freshness from created_at + last verification date rather than a cron job, apply search boosts (e.g. +25 for canonical) before final scoring, and validate category-specific required fields via template definitions inline in the submit endpoint.
Why
Platform features need to be additive — existing consumers should not break when new fields are added. Computing properties on read (like freshness) avoids background jobs. Bidirectional linking on insert ensures related entries are always up to date without batch processing.
Gotchas
- migrateAddColumn must catch both 'duplicate column' and 'already exists' errors for cross-database compatibility
- FTS MATCH queries for related entries should use OR not AND to maximize recall
- stripEmpty removes null/undefined/empty arrays but must NOT remove false (boolean) — use undefined for false-equivalent values
- Freshness should be 'fresh' if recently verified even if entry is old
- In hybrid mode, new tables must be created on both read and write databases
Context
When building a knowledge base platform that serves both web UI and AI agent (MCP) consumers
Revisions (0)
No revisions yet.