Skip to content

fix(#768): eliminate redundant DB reads in transaction-documents.service#857

Open
jethrojohn739-max wants to merge 1 commit into
MettaChain:mainfrom
jethrojohn739-max:fix/768-transaction-documents-redundant-reads
Open

fix(#768): eliminate redundant DB reads in transaction-documents.service#857
jethrojohn739-max wants to merge 1 commit into
MettaChain:mainfrom
jethrojohn739-max:fix/768-transaction-documents-redundant-reads

Conversation

@jethrojohn739-max

Copy link
Copy Markdown

Summary

Closes #768 [E7] — audit and eliminate redundant DB reads in transaction-documents.service.ts.

Problem

Every public method in TransactionDocumentsService followed a two-query sequential pattern:

  1. ensureTransactionExists(transactionId) → full transaction row read
  2. A separate document.findFirst / findMany → second round-trip

getVersions was worst: 3 sequential queries (tx check → doc existence → documentVersion.findMany with uploadedBy).

Fix

Replaced all sequential read chains with Prisma $transaction([...]) batches so the transaction access-select and the document query run in the same database connection checkout.

Method Before After
attach 2 queries (tx full row + doc create) 1 read (tx select only) + writes
list 2 sequential reads 1 batched round-trip
findOne 2 sequential reads 1 batched round-trip
addVersion 2 sequential reads + write batch 1 batched read + write batch
getVersions 3 sequential reads 1 batched round-trip
remove 2 sequential reads + delete 1 batched read + delete

Additional improvements:

  • transaction.findUnique now uses select: { id, buyerId, sellerId } — fetches only the access-control fields, not the full row.
  • Removed @ts-nocheck from both transaction-documents.service.ts and dto/transaction-document.dto.ts.
  • Replaced as any casts with correct Prisma-typed field assignments.
  • Added typed aliases: TxAccess, DocumentWithVersions, VersionWithUploader, DocumentTypeValue.
  • Removed the now-redundant private ensureTransactionExists method.

Testing

  • npm run build passes with zero TypeScript errors under strict mode.

…ments.service

- Consolidate sequential ensureTransactionExists + document queries into
  batched ([tx select, doc findFirst]) round-trips for
  findOne, addVersion, getVersions, and remove — reducing reads per call
  from 2 to 1 batched operation.
- list: batches transaction access-select + document.findMany in a single
   batch (was 2 sequential queries).
- getVersions: was 3 sequential queries (tx check, doc existence, versions
  findMany with uploadedBy); now 1 batched round-trip with versions and
  uploader embedded via include.
- Use select: { id, buyerId, sellerId } on transaction queries to fetch
  only the access-control fields needed, reducing data transfer.
- Remove @ts-nocheck from service and dto; add proper TypeScript types
  (TxAccess, DocumentWithVersions, VersionWithUploader, DocumentTypeValue).
- Replace as any casts with correct Prisma-typed field assignments.
@drips-wave

drips-wave Bot commented Jun 30, 2026

Copy link
Copy Markdown

@jethrojohn739-max 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

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.

[E7] transaction-documents.service.ts — audit for redundant reads when iterating versions

1 participant