[PR #4711] [MERGED] feat: bypass transaction with async local storage #5542

Closed
opened 2026-03-13 12:26:38 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4711
Author: @himself65
Created: 9/16/2025
Status: Merged
Merged: 9/16/2025
Merged by: @himself65

Base: canaryHead: himself65/2025/09/16/trx


📝 Commits (10+)

📊 Changes

8 files changed (+427 additions, -308 deletions)

View changed files

📝 docs/content/docs/installation.mdx (+36 -1)
📝 packages/better-auth/src/api/routes/sign-up.test.ts (+26 -0)
📝 packages/better-auth/src/api/routes/sign-up.ts (+159 -154)
📝 packages/better-auth/src/auth.ts (+2 -1)
packages/better-auth/src/context/transaction.ts (+92 -0)
📝 packages/better-auth/src/db/internal-adapter.ts (+107 -145)
📝 packages/better-auth/src/db/with-hooks.ts (+4 -7)
📝 packages/better-auth/src/index.ts (+1 -0)

📄 Description

Note that this will be a breaking change in the next major release. We are enable AsyncLocalStorage from node:async_hooks in the next major version

Related: https://github.com/better-auth/better-auth/pull/4554
Closes: https://github.com/better-auth/better-auth/pull/4215
Fixes: https://github.com/better-auth/better-auth/issues/4193

Summary by cubic

Adds AsyncLocalStorage-backed adapter context so DB calls automatically use the current adapter/transaction without passing trxAdapter. Request handling now runs inside this context for consistent behavior across the stack.

  • New Features

    • Introduced context/transaction helpers: runWithAdapter, runWithTransaction, getCurrentAdapter.
    • Safely loads AsyncLocalStorage via dynamic import to work on Edge runtimes (e.g., Cloudflare, Vercel Edge).
    • Wrapped request handler in auth.ts with runWithAdapter so downstream DB calls pick up the active adapter automatically.
  • Migration

    • Removed trxAdapter parameters from internal adapter and with-hooks functions. If you relied on them, wrap your flow with runWithTransaction(adapter, () => { ... }).
    • No changes needed if you only use the public betterAuth API.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/4711 **Author:** [@himself65](https://github.com/himself65) **Created:** 9/16/2025 **Status:** ✅ Merged **Merged:** 9/16/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `himself65/2025/09/16/trx` --- ### 📝 Commits (10+) - [`348b91c`](https://github.com/better-auth/better-auth/commit/348b91c0e417b4a52df2e45bec2b6a0442f221a5) feat: bypass transaction with async local storage - [`d2d32d2`](https://github.com/better-auth/better-auth/commit/d2d32d2f72a6458824d1c230ec0651c229101f1e) feat: export function - [`f6dc544`](https://github.com/better-auth/better-auth/commit/f6dc5442f7881a656bddad54874baa4fe380c772) fix: return type - [`4162aba`](https://github.com/better-auth/better-auth/commit/4162abae4e4eeaec0025bcc20d07e26e95d86233) fix: catch - [`edd3f5a`](https://github.com/better-auth/better-auth/commit/edd3f5a08ee4670f0dfdb042f1ca53378e4badc0) fix: catch - [`67c79e8`](https://github.com/better-auth/better-auth/commit/67c79e8e4e4adace613bb356dfc58f5cae07452d) fix: db adapter - [`3abfd9c`](https://github.com/better-auth/better-auth/commit/3abfd9c1a1480624600976c19022f6d27a906782) docs: update - [`e2cf98f`](https://github.com/better-auth/better-auth/commit/e2cf98f518333830dc21bb2a48d371336c3dd660) fix: throw - [`3c9448b`](https://github.com/better-auth/better-auth/commit/3c9448b23150681ba8ab9f5f8a10ae87cb428170) docs: update - [`e07eb5c`](https://github.com/better-auth/better-auth/commit/e07eb5cc397500edd8c53053bf6e4b16811fe2b0) feat: code ### 📊 Changes **8 files changed** (+427 additions, -308 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/installation.mdx` (+36 -1) 📝 `packages/better-auth/src/api/routes/sign-up.test.ts` (+26 -0) 📝 `packages/better-auth/src/api/routes/sign-up.ts` (+159 -154) 📝 `packages/better-auth/src/auth.ts` (+2 -1) ➕ `packages/better-auth/src/context/transaction.ts` (+92 -0) 📝 `packages/better-auth/src/db/internal-adapter.ts` (+107 -145) 📝 `packages/better-auth/src/db/with-hooks.ts` (+4 -7) 📝 `packages/better-auth/src/index.ts` (+1 -0) </details> ### 📄 Description Note that this will be a breaking change in the next major release. We are enable `AsyncLocalStorage` from `node:async_hooks` in the next major version Related: https://github.com/better-auth/better-auth/pull/4554 Closes: https://github.com/better-auth/better-auth/pull/4215 Fixes: https://github.com/better-auth/better-auth/issues/4193 <!-- This is an auto-generated description by cubic. --> ## Summary by cubic Adds AsyncLocalStorage-backed adapter context so DB calls automatically use the current adapter/transaction without passing trxAdapter. Request handling now runs inside this context for consistent behavior across the stack. - **New Features** - Introduced context/transaction helpers: runWithAdapter, runWithTransaction, getCurrentAdapter. - Safely loads AsyncLocalStorage via dynamic import to work on Edge runtimes (e.g., Cloudflare, Vercel Edge). - Wrapped request handler in auth.ts with runWithAdapter so downstream DB calls pick up the active adapter automatically. - **Migration** - Removed trxAdapter parameters from internal adapter and with-hooks functions. If you relied on them, wrap your flow with runWithTransaction(adapter, () => { ... }). - No changes needed if you only use the public betterAuth API. <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 12:26:38 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#5542