Skip to content

feat(tenants): replace inline InMemoryTenantRepository with Postgres repository and migration - #1660

Open
devogechukwu wants to merge 1 commit into
CalloraOrg:mainfrom
devogechukwu:feat/postgres-tenant-repository
Open

devogechukwu wants to merge 1 commit into
CalloraOrg:mainfrom
devogechukwu:feat/postgres-tenant-repository

Conversation

@devogechukwu

Copy link
Copy Markdown

Summary

Replaces the inline InMemoryTenantRepository in src/routes/tenants.ts with a dedicated repository module (src/repositories/tenantRepository.ts), a PostgreSQL-backed PostgresTenantRepository, and a database migration so tenants persist across restarts and are shared across instances while keeping the HTTP contract unchanged.

Changes

  • src/repositories/tenantRepository.ts: Moved TenantRecord, TenantRepository, slugifyTenantName, and InMemoryTenantRepository out of src/routes/tenants.ts, and added PostgresTenantRepository.
    • list(actorId) returns only tenants created by actorId.
    • create(input, actorId) maps duplicate slug violations (SQLSTATE 23505 in Postgres / slug collision in memory) to ConflictError (HTTP 409, code CONFLICT).
    • update(tenantId, input, actorId) scopes updates to WHERE id = $id AND created_by = $actorId and throws NotFoundError('Tenant not found') (HTTP 404, code NOT_FOUND) for both unknown and cross-actor IDs.
  • migrations/0034_tenants.sql & migrations/0034_tenants.down.sql: Added tenants table (id TEXT PRIMARY KEY, name, slug UNIQUE, contact_email, plan CHECK (plan IN ('starter', 'growth', 'enterprise')), metadata JSONB, created_by, created_at, updated_at, and idx_tenants_created_by index) plus rollback migration.
  • src/routes/tenants.ts: Re-exports TenantRecord and TenantRepository, adds optional pool to TenantsRouterDeps, and defaults to PostgresTenantRepository when a pool or DATABASE_URL (outside demo mode) is available, keeping deps.tenantRepository injection and all HTTP envelopes/ETag/validation unchanged.
  • src/repositories/tenantRepository.test.ts: Added tests covering both InMemoryTenantRepository and PostgresTenantRepository (via pg-mem), including metadata JSON round-trip, missing optional fields, slug uniqueness (409 CONFLICT), partial update merging, cross-actor vs unknown 404, and restart persistence.

Testing

  • Local node_modules was not installed in this environment; CI will execute tenantRepository.test.ts, quarantined tenant route tests, npm run db:check-migrations, and npm run typecheck.

Closes #1560

@drips-wave

drips-wave Bot commented Nov 23, 2026

Copy link
Copy Markdown

@devogechukwu Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@devogechukwu devogechukwu changed the title feat(tenants): replace inline in-memory repository with Postgres repo… feat(tenants): replace inline InMemoryTenantRepository with Postgres repository and migration Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace the inline InMemoryTenantRepository in src/routes/tenants.ts with a Postgres tenants repository and migration

1 participant