How aspen-cloud/triplit works
Purpose and primary entry points
Triplit is a TypeScript monorepo for a local-first, real-time database: clients maintain local data, execute live relational queries, optimistically mutate data offline, and synchronize authorized deltas with a server. Core public entry points are packages/db/src/index.ts, packages/client/src/index.ts, and packages/server/src/index.ts. Developer/project entry points are the CLI at packages/cli/src/index.ts and scaffolder at packages/create-triplit-app/src/index.ts.
Database kernel: data, transactions, and change propagation
@triplit/db is the runtime-independent database engine. It combines entity storage, transaction semantics, metadata, an outbox-oriented store, hybrid clocks, and write buffering to support concurrent writers and durable local/server implementations. Key boundaries: logical entity/query APIs versus pluggable KV storage; buffered local writes versus outbound synchronization.
• packages/db/src/db.ts
• packages/db/src/db-transaction.ts
• packages/db/src/entity-store-with-outbox.ts
Schema, authorization, and query execution
Schemas define collection shapes, relations, types, and role-based permissions. Queries are built/prepared, simplified and compiled into plans, then evaluated against the entity store; incremental view maintenance updates live results after mutations. Server-side permissions are a critical trust boundary for reads and writes.
• packages/db/src/schema/builder.ts
• packages/db/src/query-planner/query-compiler.ts
• packages/db/src/ivm/ivm.ts
Client local-first sync runtime
TriplitClient exposes query, subscription, fetch, and mutation APIs over a local DB. Its sync engine coordinates persisted outbox changes, connection state, remote patches, retries/rollback behavior, and subscriptions. Transport is explicitly separated into HTTP and WebSocket implementations, while storage adapters support browser, native, Node, and in-memory use.
• packages/client/src/client/triplit-client.ts
• packages/client/src/sync-engine.ts
• packages/client/src/transport/websocket-transport.ts
Sync server and protocol core
server-core owns protocol-agnostic sessions, token handling, sync connections, server orchestration, and webhooks. The server package adapts this core to Hono/Node and platform-specific runtimes, applies token/admin middleware, exposes public routes, and selects persistence. Thus protocol/domain behavior stays distinct from HTTP and deployment adapters.
• packages/server-core/src/triplit-server.ts
• packages/server-core/src/sync-connection.ts
• packages/server/src/server.ts
Runtimes and persistence infrastructure
The database storage boundary supports memory, IndexedDB, LMDB, SQLite, Expo SQLite, Bun SQLite, SQLite workers, and Cloudflare Durable Objects. Server distribution targets Node/Docker, Bun, and Cloudflare Workers; Cloudflare has standard and hibernating worker modes. This makes deployment/runtime selection an adapter concern rather than a database-core concern.
• packages/db/src/kv-store/storage/
• packages/server/src/cloudflare.ts
• packages/cf-worker-server/src/hibernation.ts
User-facing integrations and operations
Framework packages translate reactive client subscriptions into React, Angular, Vue, Svelte, Solid, TanStack, and React Native idioms. The CLI manages local development, auth/project state, schema pushes/diffs, seeding, snapshots, webhooks, and worker deployment. The Console is a separate Vite UI for server/data/schema administration, backed by its server component and Triplit client configuration.
• packages/react/src/use-query.ts
• packages/cli/src/commands/dev.tsx
• packages/console/src/App.tsx
Main entry points: db.ts Database · server.ts Hono/Node server · triplit-client.ts TriplitClient · triplit-server.ts Server core
Components 18
Database kernel
Runtime-independent local database and query engine.
- Database · Entity, transaction, metadata, clock, and write-buffer runtime.
packages/db/src/db.ts - Transactions
packages/db/src/db-transaction.ts - Entity store + outbox · Buffers local writes for outbound synchronization.
packages/db/src/entity-store-with-outbox.ts - KV storage adapters · Memory, browser, native, Node, SQLite, and Durable Object persistence.
- Schema & permissions · Defines collections, relations, types, and role permissions.
packages/db/src/schema/builder.ts - Query compiler · Compiles prepared queries into executable plans.
packages/db/src/query-planner/query-compiler.ts - Incremental views · Maintains live results after mutations.
packages/db/src/ivm/ivm.ts
Client runtime
Local-first client APIs, synchronization, and transports.
- TriplitClient · Query, subscription, fetch, and mutation facade over a local DB.
packages/client/src/client/triplit-client.ts - Sync engine · Coordinates outbox changes, patches, retries, rollback, and subscriptions.
packages/client/src/sync-engine.ts - WebSocket transport · Real-time client transport; HTTP is a parallel adapter.
packages/client/src/transport/websocket-transport.ts
Server protocol & runtimes
Protocol core separated from HTTP and deployment adapters.
- Server core · Protocol-agnostic sessions, tokens, orchestration, and webhooks.
packages/server-core/src/triplit-server.ts - Sync connections · Authorized delta synchronization connection handling.
packages/server-core/src/sync-connection.ts - Hono/Node server · Applies middleware, routes, and persistence selection.
packages/server/src/server.ts - Cloudflare runtime · Cloudflare Workers server mode.
packages/server/src/cloudflare.ts - Hibernating worker · Durable Object hibernation mode.
packages/cf-worker-server/src/hibernation.ts
Integrations & operations
Framework bindings and developer administration tools.
- React integration · Maps client subscriptions to React query hooks.
packages/react/src/use-query.ts - CLI development · Runs local development and operational workflows.
packages/cli/src/commands/dev.tsx - Console · Vite administration UI for server, data, and schema.
packages/console/src/App.tsx
Connections 25
- Database → Transactions: executes
- Transactions → Entity store + outbox: writes
- Database → KV storage adapters: persists through
- Entity store + outbox → KV storage adapters: durably stores
- Schema & permissions → Database: governs entities
- Query compiler → Schema & permissions: uses relations
- Query compiler → Database: evaluates against
- Incremental views → Query compiler: maintains plans
- Incremental views → Entity store + outbox: reacts to mutations
- TriplitClient → Database: uses local
- TriplitClient → Sync engine: coordinates through
- Sync engine → Entity store + outbox: drains and applies
- Sync engine → WebSocket transport: connects via
- WebSocket transport → Sync connections: sync protocol
- Sync connections → Server core: belongs to
- Server core → Database: serves authorized deltas
- Server core → Schema & permissions: enforces permissions · Read and write authorization trust boundary.
- Hono/Node server → Server core: adapts
- Hono/Node server → KV storage adapters: selects persistence
- Cloudflare runtime → Hono/Node server: deploys
- Hibernating worker → Cloudflare runtime: extends
- React integration → TriplitClient: subscribes through
- CLI development → Hono/Node server: runs and manages
- Console → TriplitClient: administers via
- Console → Hono/Node server: targets