Skip to content

refactor: improve storage.ts cohesion by extracting modules #131

@coji

Description

@coji

Problem

storage.ts is 1083 lines with mixed concerns: type definitions, pure data transformers, backend-specific logic (SQLite vs PostgreSQL), and cross-cutting mutex infrastructure.

Extraction Opportunities

1. Data transformers → transformers.ts

  • validateLabels(), rowToRun(), rowToStep(), rowToLog(), toClientRun()
  • Pure functions, zero DB dependency, ~80 lines
  • Testable in isolation

2. claimNext backend split

  • claimNext() is 140+ lines with two completely divergent paths:
    • PostgreSQL: FOR UPDATE SKIP LOCKED + advisory locks + retry loop (lines 716–799)
    • SQLite: simple subquery + UPDATE (lines 801–836)
  • Could become claim-sqlite.ts and claim-postgres.ts
  • Biggest readability win

3. Mutex → keep inline (too small to extract)

  • createWriteMutex() is 18 lines, only used once
  • Not worth a separate file

Priority

Medium. Do alongside #124 (getRun JOIN optimization) since both touch the same file.

References

Found during /simplify cohesion review on PR #130

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions