Skip to content

refactor(storage): extract SlowLogRepository across all three storage adapters#191

Open
amitkojha05 wants to merge 1 commit into
BetterDB-inc:masterfrom
amitkojha05:refactor/extract-slowlog-repos
Open

refactor(storage): extract SlowLogRepository across all three storage adapters#191
amitkojha05 wants to merge 1 commit into
BetterDB-inc:masterfrom
amitkojha05:refactor/extract-slowlog-repos

Conversation

@amitkojha05
Copy link
Copy Markdown
Contributor

@amitkojha05 amitkojha05 commented May 12, 2026

Summary

Extracts slow log query logic from the three storage adapters into dedicated SlowLogRepository classes, continuing the repository extraction started in #125 (webhooks).

New files:

  • repositories/slowlog.postgres.repository.ts
  • repositories/slowlog.sqlite.repository.ts
  • repositories/slowlog.memory.repository.ts

Modified files:

  • postgres.adapter.ts — delegates to SlowLogPostgresRepository
  • sqlite.adapter.ts — delegates to SlowLogSqliteRepository
  • memory.adapter.ts — delegates to SlowLogMemoryRepository

Changes

The TODO comment in postgres.adapter.ts (left in #125) lists slowlog as the next domain to extract:

// Domain-specific repositories (webhooks extracted). Remaining domains to extract:
// ACL, anomaly, slowlog, commandlog, latency, ...

This removes ~200 lines of inline query logic from each SQL adapter and isolates slow log storage behind testable, single-responsibility classes.

Implementation

Same pattern as #125:

  • Repository constructors take non-nullable connections (Pool, Database.Database) — the type system guarantees a live connection
  • Adapters retain if (!this.pool) / if (!this.db) lifecycle guards and delegate after initialization
  • Memory repository is self-contained (owns its own StoredSlowLogEntry[])
  • entries.length === 0 short-circuit lives in both the adapter (preserving original behaviour) and the repository (for standalon testability)
// Adapter — owns lifecycle, keeps guard
async saveSlowLogEntries(entries: StoredSlowLogEntry[], connectionId: string): Promise<number> {
  if (!this.pool || entries.length === 0) return 0;
  return this.slowlogRepo.saveSlowLogEntries(entries, connectionId);
}

// Repository — non-nullable Pool, pure query logic
constructor(private readonly pool: Pool, private readonly mappers: RowMappers) {}

async saveSlowLogEntries(entries: StoredSlowLogEntry[], connectionId: string): Promise<number> {
  if (entries.length === 0) return 0;
  // ... SQL ...
}

Testing

Pure extraction — zero SQL changes, zero logic changes, zero interface changes. All existing tests pass unchanged.

CI note

CI note: the anomaly.service.spec.ts failure (getClusterInfo mock missing at line 733) was introduced in #142 (feat: sentinel/cluster failover — commit 2fa1c46) and is pre-existing on master. Unrelated to this SlowLog extraction.

@amitkojha05 amitkojha05 force-pushed the refactor/extract-slowlog-repos branch from 78f20c3 to 891a582 Compare May 12, 2026 21:11
@amitkojha05
Copy link
Copy Markdown
Contributor Author

Hi @KIvanow ,PR is up — extracts SlowLogRepository across all three adapters, continuing the pattern from #125.
Happy to get your feedback.

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.

1 participant