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_eventsevent store - Provide
createDb(connectionString)returning a typedDbClient(Drizzle + Postgres) - Ship migrations under
src/migrations/(applied byjobs/db-migrate) - Re-export
eq,and,desc,sql, … so resolvers and handlers import query operators from a single package
Key Components
| File / Dir | Role |
|---|---|
src/client.ts | createDb() factory + DbClient type (Drizzle + Postgres connection) |
src/schema/index.ts | Re-exports every table schema |
src/schema/*.ts | Per-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+postgresdriver@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).