PGlite Integration State
Embedded Postgres state store for integration scripts. Tracks dedup IDs, pagination cursors, sync timestamps, and OAuth tokens without external dependencies.
Why PGlite
Integration scripts need persistent state that goes beyond what state.json handles.
When syncing 10K+ emails, you need per-message dedup. When paginating calendar events
across years of history, you need cursor storage. When refreshing OAuth tokens, you
need secure persistence. PGlite gives a real Postgres database (via WASM) stored
locally at ~/.kevin-wiki/integrations.pglite with zero setup.
Scope is deliberately narrow - 3 tables for integration bookkeeping. Not a full brain engine. Search stays with QMD - Local Wiki Search Engine. Content stays as markdown files. Source: scripts/lib/integration-db.ts
Schema
Three tables:
- integration_state - key-value pairs per integration (sync timestamps, cursors)
- seen_ids - dedup tracking per integration (email message IDs, meeting source IDs)
- oauth_tokens - encrypted token storage per provider (Google OAuth refresh tokens)
API
import { setState, getState, markSeen, hasSeenId, getSeenCount,
saveOAuthTokens, loadOAuthTokens, closeDB } from "./lib/integration-db.js";
await setState("calendar", "last_sync", "2026-04-12T10:00:00Z");
const lastSync = await getState("calendar", "last_sync");
if (await hasSeenId("email", messageId)) continue;
await markSeen("email", messageId, JSON.stringify({ subject }));
await saveOAuthTokens("google", { access_token, refresh_token, expires_at });
const tokens = await loadOAuthTokens("google");
await closeDB();
Data Location
~/.kevin-wiki/integrations.pglite - delete this directory to reset all integration state.
Timeline
- 2026-06-17 | Added inline provenance and listed the integration scripts that use the state layer. Source: scripts/lib/integration-db.ts; scripts/sync-calendar.ts; scripts/sync-email.ts; scripts/sync-meetings.ts
- 2026-04-12 | Created. Adapted from gbrain's PGlite approach but scoped narrowly to integration state only, keeping the filesystem-native wiki architecture. Source: scripts/lib/integration-db.ts