fix: preserve transaction state in $use, $unuse, and $unuseAll#2497
fix: preserve transaction state in $use, $unuse, and $unuseAll#2497pkudinov wants to merge 1 commit intozenstackhq:devfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughClient cloning methods ( Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip CodeRabbit can approve the review once all CodeRabbit's comments are resolved.Enable the |
When calling $use(), $unuse(), or $unuseAll() on a transaction client, the returned client would escape the active transaction because the constructor always creates a fresh Kysely instance from kyselyProps. Propagate the transaction Kysely instance to the new client when the current client is inside a transaction. Fixes zenstackhq#2494
03736ed to
ce30ea2
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/e2e/orm/client-api/transaction.test.ts (1)
136-148: Consider using a valid plugin structure.The plugin object uses
handle, which isn't a recognized plugin hook based on theClientImplimplementation (which looks foronQuery,onProcedure,client,result). While this works as a no-op plugin (unrecognized properties are ignored), using the correct interface would make the test more representative:🔧 Suggested improvement
it('$use preserves transaction isolation', async () => { await expect( client.$transaction(async (tx) => { - await (tx as any).$use({ id: 'noop', handle: (_node: any, proceed: any) => proceed(_node) }).user.create({ + await (tx as any).$use({ id: 'noop' }).user.create({ data: { email: 'u1@test.com' }, });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/e2e/orm/client-api/transaction.test.ts` around lines 136 - 148, The test uses client.$use with a plugin object that defines a nonstandard "handle" property; update the plugin to use a valid plugin hook (e.g., onQuery or onProcedure) so it matches the ClientImpl plugin shape and remains a no-op—replace the object passed to (tx as any).$use (id: 'noop', handle: ...) with one that provides a recognized hook like onQuery/onProcedure that simply calls proceed, keeping the id 'noop' and the overall behavior under client.$transaction intact.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@tests/e2e/orm/client-api/transaction.test.ts`:
- Around line 136-148: The test uses client.$use with a plugin object that
defines a nonstandard "handle" property; update the plugin to use a valid plugin
hook (e.g., onQuery or onProcedure) so it matches the ClientImpl plugin shape
and remains a no-op—replace the object passed to (tx as any).$use (id: 'noop',
handle: ...) with one that provides a recognized hook like onQuery/onProcedure
that simply calls proceed, keeping the id 'noop' and the overall behavior under
client.$transaction intact.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 4a081a18-ec8d-4f80-8906-472b677ced06
📒 Files selected for processing (2)
packages/orm/src/client/client-impl.tstests/e2e/orm/client-api/transaction.test.ts
Fixes #2494
Problem
Calling
$use(),$unuse(), or$unuseAll()on a transaction client returns a new client that runs queries outside the active transaction.The constructor always creates a fresh Kysely instance from
kyselyProps(this.kysely = new Kysely(this.kyselyProps)), but the transaction state lives intxClient.kyselywhich was set to the KyselyTransactionobject after construction. The new client never receives this.Fix
After constructing the new client in
$use(),$unuse(), and$unuseAll(), propagate the transaction Kysely instance:Tests
Three new tests in
tests/e2e/orm/client-api/transaction.test.tsverify that$unuseAll(),$unuse(), and$use()on a transaction client stay in the same transaction by checking that writes roll back when the transaction fails.Summary by CodeRabbit
Bug Fixes
Tests