Skip to content

db — Architecture

The @mnemose/db package is the data-access layer for Mnemose. It owns the Drizzle ORM schema for every Postgres table (event store + read models), the migrations directory, the createDb() client factory, and re-exports commonly used Drizzle query operators.

Responsibilities

  • Define the Drizzle schema for all tenant-scoped tables (kernels, sessions, sandbox, KMS, IAM, SEO, etc.) and the append-only domain_events event store
  • Provide createDb(connectionString) returning a typed DbClient (Drizzle + Postgres)
  • Ship migrations under src/migrations/ (applied by jobs/db-migrate)
  • Re-export eq, and, desc, sql, … so resolvers and handlers import query operators from a single package

Key Components

File / DirRole
src/client.tscreateDb() factory + DbClient type (Drizzle + Postgres connection)
src/schema/index.tsRe-exports every table schema
src/schema/*.tsPer-aggregate Drizzle table definitions (agent-kernels, domain-events, seo, …)
src/migrations/Versioned SQL migrations applied by the db-migrate Cloud Run Job

Dependencies

  • drizzle-orm + postgres driver
  • @mnemose/domain — identifier + enum types used in column definitions

Data Flow

gateway resolver / command handler / agent
↓ createDb(DATABASE_URL) → DbClient
↓ db.select(...).from(table).where(eq(tenant_id, ...)) (read model)
↓ db.insert(domainEvents).values(event) (event store — append only)

The domain_events table is the authoritative audit trail — no UPDATE / DELETE paths exist in the application layer. Tenant isolation is enforced at the application layer (with RLS as a DB-level backstop per Stripe G).