[PR #4532] [MERGED] feat: database transaction support #5431

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

📋 Pull Request Information

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

Base: feat-database-transaction-supportHead: feat-database-transaction-support


📝 Commits (10+)

  • 340c6b5 fix: drizzle adapter transactions
  • 8d98137 feat: prisma adapter transactions
  • b506db2 fix: infer transaction return type
  • a58a07f feat: mongodb adapter transactions
  • 895e443 feat: opt out of transactions
  • 7d5a593 chore: update internal adapter
  • fb77649 chore: remove unused imports
  • de5677a chore: update init snapshot
  • c7072e1 chore: add logs
  • 407a769 chore: add basic tests

📊 Changes

15 files changed (+554 additions, -234 deletions)

View changed files

📝 docs/content/docs/adapters/mongo.mdx (+1 -1)
📝 docs/content/docs/guides/create-a-db-adapter.mdx (+4 -0)
📝 packages/better-auth/src/__snapshots__/init.test.ts.snap (+2 -0)
📝 packages/better-auth/src/adapters/create-adapter/index.ts (+22 -14)
📝 packages/better-auth/src/adapters/create-adapter/types.ts (+7 -4)
📝 packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts (+47 -13)
📝 packages/better-auth/src/adapters/kysely-adapter/kysely-adapter.ts (+21 -13)
📝 packages/better-auth/src/adapters/memory-adapter/adapter.memory.test.ts (+4 -0)
📝 packages/better-auth/src/adapters/mongodb-adapter/adapter.mongo-db.test.ts (+11 -3)
📝 packages/better-auth/src/adapters/mongodb-adapter/mongodb-adapter.ts (+143 -80)
📝 packages/better-auth/src/adapters/prisma-adapter/prisma-adapter.ts (+56 -16)
📝 packages/better-auth/src/adapters/test.ts (+79 -0)
📝 packages/better-auth/src/db/internal-adapter.ts (+145 -84)
📝 packages/better-auth/src/db/with-hooks.ts (+7 -3)
📝 packages/better-auth/src/types/adapter.ts (+5 -3)

📄 Description

Related: #4414
Closes: #4062

Todo

  • Fix MongoDB transactions not rolling back
    • config.session was not enough as its required to create a fresh session for every transaction.
      Removed config.session and added config.client instead.
    • disabled transaction tests for now. MongoDB transactions require a replica set or a sharded cluster
  • Documentation

Summary by cubic

Add transaction support to the database adapters so auth operations can run within a single DB transaction. Implements this for Prisma and Drizzle and standardizes adapter creation.

  • New Features

    • Provide a transaction(cb) hook that runs the callback with a transactional adapter instance.
    • Prisma: use $transaction and pass a tx-scoped adapter to the callback.
    • Drizzle: use db.transaction and pass a tx-scoped adapter to the callback.
    • Introduce CreateAdapterOptions and lazy BetterAuthOptions to safely build tx-scoped adapters; update Kysely to use the new type.
  • Bug Fixes

    • Drizzle: ensure all operations inside transaction() use the transaction connection.

🔄 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/4532 **Author:** [@jslno](https://github.com/jslno) **Created:** 9/8/2025 **Status:** ✅ Merged **Merged:** 9/9/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `feat-database-transaction-support` ← **Head:** `feat-database-transaction-support` --- ### 📝 Commits (10+) - [`340c6b5`](https://github.com/better-auth/better-auth/commit/340c6b5ade1dadea674b0e2ac8e3058162d2e257) fix: drizzle adapter transactions - [`8d98137`](https://github.com/better-auth/better-auth/commit/8d98137421ea9a54410040c5f8578f41a2a8c6c5) feat: prisma adapter transactions - [`b506db2`](https://github.com/better-auth/better-auth/commit/b506db27c7d29a2493610e0c2ec7beee70c503f3) fix: infer transaction return type - [`a58a07f`](https://github.com/better-auth/better-auth/commit/a58a07ffca11d62ab72a8cb27d205cb63d3d3d4d) feat: mongodb adapter transactions - [`895e443`](https://github.com/better-auth/better-auth/commit/895e443efb8495aa5ecb42098ad822fecc1e3a38) feat: opt out of transactions - [`7d5a593`](https://github.com/better-auth/better-auth/commit/7d5a593d3868fe35da2d03ccc1ad24b3687a04a6) chore: update internal adapter - [`fb77649`](https://github.com/better-auth/better-auth/commit/fb77649fc8cf3f8ef366e511bfcbd8c5610b6e42) chore: remove unused imports - [`de5677a`](https://github.com/better-auth/better-auth/commit/de5677a5b4c596f7f3161f67c3770e28fc2722f2) chore: update init snapshot - [`c7072e1`](https://github.com/better-auth/better-auth/commit/c7072e1f018d82b85414d4c988b4f7618a67c47b) chore: add logs - [`407a769`](https://github.com/better-auth/better-auth/commit/407a7693562bc4c24f252db1e9c7ed22a417b022) chore: add basic tests ### 📊 Changes **15 files changed** (+554 additions, -234 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/adapters/mongo.mdx` (+1 -1) 📝 `docs/content/docs/guides/create-a-db-adapter.mdx` (+4 -0) 📝 `packages/better-auth/src/__snapshots__/init.test.ts.snap` (+2 -0) 📝 `packages/better-auth/src/adapters/create-adapter/index.ts` (+22 -14) 📝 `packages/better-auth/src/adapters/create-adapter/types.ts` (+7 -4) 📝 `packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts` (+47 -13) 📝 `packages/better-auth/src/adapters/kysely-adapter/kysely-adapter.ts` (+21 -13) 📝 `packages/better-auth/src/adapters/memory-adapter/adapter.memory.test.ts` (+4 -0) 📝 `packages/better-auth/src/adapters/mongodb-adapter/adapter.mongo-db.test.ts` (+11 -3) 📝 `packages/better-auth/src/adapters/mongodb-adapter/mongodb-adapter.ts` (+143 -80) 📝 `packages/better-auth/src/adapters/prisma-adapter/prisma-adapter.ts` (+56 -16) 📝 `packages/better-auth/src/adapters/test.ts` (+79 -0) 📝 `packages/better-auth/src/db/internal-adapter.ts` (+145 -84) 📝 `packages/better-auth/src/db/with-hooks.ts` (+7 -3) 📝 `packages/better-auth/src/types/adapter.ts` (+5 -3) </details> ### 📄 Description Related: #4414 Closes: #4062 ## Todo - [x] Fix MongoDB transactions not rolling back - `config.session` was not enough as its required to create a fresh session for every transaction. Removed `config.session` and added `config.client` instead. - disabled transaction tests for now. MongoDB transactions require a replica set or a sharded cluster - [x] Documentation <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Add transaction support to the database adapters so auth operations can run within a single DB transaction. Implements this for Prisma and Drizzle and standardizes adapter creation. - **New Features** - Provide a transaction(cb) hook that runs the callback with a transactional adapter instance. - Prisma: use $transaction and pass a tx-scoped adapter to the callback. - Drizzle: use db.transaction and pass a tx-scoped adapter to the callback. - Introduce CreateAdapterOptions and lazy BetterAuthOptions to safely build tx-scoped adapters; update Kysely to use the new type. - **Bug Fixes** - Drizzle: ensure all operations inside transaction() use the transaction connection. <!-- 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:22:35 -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#5431