perf(share): replace per-row DB loops with batch operations in shareCap#1738
Open
MinitJain wants to merge 1 commit intoCapSoftware:mainfrom
Open
perf(share): replace per-row DB loops with batch operations in shareCap#1738MinitJain wants to merge 1 commit intoCapSoftware:mainfrom
MinitJain wants to merge 1 commit intoCapSoftware:mainfrom
Conversation
Sequential await db().delete/insert inside for-loops caused N round-trips per sharing update. Replaced with single batch delete using inArray and single batch insert with all entries collected upfront. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
shareCapwas issuing oneDELETEand oneINSERTper organization/space in sequentialforloops — N DB round-trips per call.Replaced with:
DELETE ... WHERE organizationId IN (...)for orgs to removeINSERTwith all new org entries collected upfrontBefore / After
Before: O(n) DB calls where n = number of orgs/spaces being updated
After: Max 4 DB calls total (1 delete + 1 insert for orgs, 1 delete + 1 insert for spaces), regardless of n
Test plan
🤖 Generated with Claude Code
Greptile Summary
This PR replaces sequential per-row
DELETE/INSERTloops inshareCapwith batch Drizzle ORM operations (inArray-based delete + bulk insert), reducing DB round-trips from O(n) to a constant max of 4 calls. The refactoring preserves identical semantics — the set-reconciliation logic (delete current minus desired, insert desired minus current) is unchanged for both org shares and space video associations.Confidence Score: 5/5
Safe to merge — batch refactor is semantically equivalent to the original loops with no logic regressions.
All changed code is a clean set-reconciliation batch optimization. The empty-array guard (length > 0) prevents invalid inArray calls, organizationId is NOT NULL in the schema so no null-safety issues, and the filter logic (delete current minus desired, insert desired minus current) is identical to the original loops. No P0 or P1 findings.
No files require special attention.
Important Files Changed
Sequence Diagram
sequenceDiagram participant Client participant shareCap participant DB Client->>shareCap: shareCap({ capId, spaceIds, public }) shareCap->>DB: SELECT user, cap, orgMemberships shareCap->>DB: SELECT directOrgIds (inArray orgs) shareCap->>DB: SELECT spacesData (inArray spaces) shareCap->>DB: SELECT currentSharedOrganizations WHERE videoId=capId alt orgIdsToRemove.length > 0 shareCap->>DB: DELETE sharedVideos WHERE videoId=capId AND orgId IN (...) end alt newOrgEntries.length > 0 shareCap->>DB: INSERT INTO sharedVideos (bulk) end shareCap->>DB: SELECT currentSpaceVideos WHERE videoId=capId alt spaceIdsToRemove.length > 0 shareCap->>DB: DELETE spaceVideos WHERE videoId=capId AND spaceId IN (...) end alt newSpaceEntries.length > 0 shareCap->>DB: INSERT INTO spaceVideos (bulk) end opt isPublic is boolean shareCap->>DB: UPDATE videos SET public=isPublic WHERE id=capId end shareCap-->>Client: { success: true }Reviews (1): Last reviewed commit: "perf(share): replace per-row DB loops wi..." | Re-trigger Greptile