fix: replace ALS-based db scoping with closure-based scoped client#17
Merged
fix: replace ALS-based db scoping with closure-based scoped client#17
Conversation
The Prisma 7 pg adapter breaks AsyncLocalStorage propagation after async queries, making the ALS-based tenant scoping unreliable. A module-level fallback was tried but caused cross-tenant data leaks in production due to race conditions between concurrent requests. Replace the entire approach with a closure-based scoped db client: - Add createScopedDb(congregationId) factory that captures the ID in a closure instead of reading from AsyncLocalStorage - authenticateAndAuthorize() now returns a scoped db client - All service functions receive db: ScopedDb as first parameter instead of importing the global db - LimitService constructor takes db as first parameter - Background worker uses createScopedDb() directly - Update all 29 test files to pass db to service function calls - Update architecture docs to reflect the new pattern (OSS-focused)
df48454 to
3d3f931
Compare
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
AsyncLocalStorageafter async queries, making ALS-based tenant scoping unreliable. A previous module-level fallback caused cross-tenant data leaks in production due to race conditions between concurrent requests.createScopedDb(congregationId)creates a Prisma$extendsclient that capturescongregationIdin a JavaScript closure — completely immune to ALS propagation issues.authenticateAndAuthorize()now returns a scopeddbclient alongsidecurrentUser,congregation,session, andcan().db: ScopedDbas their first parameter instead of importing the globaldb.LimitServiceconstructor takesdbas first parameter.createScopedDb()directly for tenant isolation.Supersedes #13 and #12 which used ALS-based workarounds.
Test plan
pnpm test:unit— 322 tests passpnpm test:typecheck— cleanpnpm test:lint— clean