patterntypescriptMajor
Three-mode architecture: public, hybrid, private with split read/write DB clients
Viewed 0 times
read-write-splithybrid-modeprivate-modepublic-modedual-clientdatabase-routing
Problem
A knowledge base app needs three privacy modes: (1) public — search and submit to shared cloud DB, (2) hybrid — search the public DB but keep submissions local, (3) private — everything local. The challenge is that hybrid mode requires reading from one database (Turso cloud) while writing to another (local SQLite). A single getDb() client cannot handle this split.
Solution
Split the singleton client into getReadDb() and getWriteDb(). Each function checks HIVEBRAIN_MODE (public/hybrid/private) and creates the appropriate client. In public mode both point to Turso. In hybrid mode read=Turso, write=local SQLite. In private mode both are local. Keep getDb() as an alias for getReadDb() for backward compatibility. All read functions (search, getAllEntries, getEntry, getStats) use getReadDb(). All write functions (insertEntry, trackView, trackSearch, addVote, addRevision) use getWriteDb(). Functions that read data written by write functions (getAnalytics, getRevisions, getVoteForIp) must also use getWriteDb(). In initDb(), run schema initialization on both clients when they differ (hybrid mode needs the local DB schema too).
Why
Users need granular control over what data leaves their machine. A binary public/private choice is insufficient — hybrid mode lets users benefit from community knowledge while keeping their own discoveries private.
Context
Building a knowledge base with MCP integration where users can choose their privacy level
Revisions (0)
No revisions yet.