[PR #6634] [CLOSED] chore(kysely): Improve mysql post-insert retrieval strategies #6796

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6634
Author: @ping-maxwell
Created: 12/9/2025
Status: Closed

Base: canaryHead: refactor/improve-kysely-mysql-withReturns


📝 Commits (10+)

  • 8b863c5 refactor(kysely): Improve mysql withReturns
  • 45bc58f Update kysely-adapter.ts
  • 8f89f42 fix(kysely-mysql): handle race conditions with unique fields for concurrent inserts
  • c56f8a0 fix(adapter): handle race conditions in concurrent row creations
  • 9d3dd67 chore: comments
  • 15a57eb chore: remove logs
  • 14a68cd Merge branch 'canary' into refactor/improve-kysely-mysql-withReturns
  • 0a0f516 Merge branch 'canary' into refactor/improve-kysely-mysql-withReturns
  • 20f250f Merge branch 'canary' into refactor/improve-kysely-mysql-withReturns
  • 42f1633 Merge branch 'canary' into refactor/improve-kysely-mysql-withReturns

📊 Changes

5 files changed (+157 additions, -23 deletions)

View changed files

📝 docs/content/docs/concepts/database.mdx (+9 -0)
📝 packages/better-auth/src/adapters/drizzle-adapter/test/adapter.drizzle.mysql.test.ts (+6 -1)
📝 packages/better-auth/src/adapters/kysely-adapter/kysely-adapter.ts (+110 -17)
📝 packages/better-auth/src/adapters/kysely-adapter/test/adapter.kysely.mysql.test.ts (+6 -1)
📝 packages/better-auth/src/adapters/tests/basic.ts (+26 -4)

📄 Description

When using kysely + mysql then inserting data through the adapter, you may not receive data as the result of the insert. This is because MySQL does not support the RETURNING SQL clause, and thus it does not return any data that was inserted.

It's most troubling when we do not have access to inserting ids programmatically, this can be the case when users use generateId: false or other auth config options like generateId: "uuid" and possibly others.

This forces us to use different query strategies to attempt to get the last-inserted data, however there are still cases where we either can't get that data, or it's unreliable due to something like race-conditions.

This PR introduces a new vector to searching for fields to reliably fetch that inserted data, by finding all unique fields in a given schema for a model, then we can then see if the provided insert values contain data for those unique fields, and use that as a reference to find the inserted data.

tldr changes:

  • New strategies to ensure last-inserted row retrieval.
  • New unit-tests to test current row-creation race-conditions.

Summary by cubic

Improve MySQL post-insert retrieval in the Kysely adapter by preferring unique fields and using LAST_INSERT_ID() when auto-increment IDs are enabled. Also handles updates by using unique fields from the WHERE clause, reducing concurrency issues.

  • Refactors
    • Detect unique fields from the schema and WHERE clause; prefer the new value if the field is updated; use it to query the row.
    • Use LAST_INSERT_ID() when advanced.database.generateId is "serial".
    • Fall back to createdAt; if unavailable, try matching all insert values; return null if no reliable selector is found.
    • No API changes; reduces missed or incorrect results due to MySQL’s lack of RETURNING.

Written for commit 244c2822ff. Summary will update on new commits.


🔄 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/6634 **Author:** [@ping-maxwell](https://github.com/ping-maxwell) **Created:** 12/9/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `refactor/improve-kysely-mysql-withReturns` --- ### 📝 Commits (10+) - [`8b863c5`](https://github.com/better-auth/better-auth/commit/8b863c56851f18716916d208483ac7a0f575ff47) refactor(kysely): Improve mysql withReturns - [`45bc58f`](https://github.com/better-auth/better-auth/commit/45bc58f97e682dcad6d1b3750798de3c80e6a818) Update kysely-adapter.ts - [`8f89f42`](https://github.com/better-auth/better-auth/commit/8f89f425b739d9fd1ebedd66f609fcac7eea119b) fix(kysely-mysql): handle race conditions with unique fields for concurrent inserts - [`c56f8a0`](https://github.com/better-auth/better-auth/commit/c56f8a0a99aa4db77c391d8bfe4d5c739a933704) fix(adapter): handle race conditions in concurrent row creations - [`9d3dd67`](https://github.com/better-auth/better-auth/commit/9d3dd673333df4d6df058c8cadd7716458db155b) chore: comments - [`15a57eb`](https://github.com/better-auth/better-auth/commit/15a57ebdef4c4ceb1208971db2cda829638ed55f) chore: remove logs - [`14a68cd`](https://github.com/better-auth/better-auth/commit/14a68cde103dc5618e546ebf0ab40b61fd4d97f9) Merge branch 'canary' into refactor/improve-kysely-mysql-withReturns - [`0a0f516`](https://github.com/better-auth/better-auth/commit/0a0f5161ad57deb445aa1bf01968e3a370e38fe9) Merge branch 'canary' into refactor/improve-kysely-mysql-withReturns - [`20f250f`](https://github.com/better-auth/better-auth/commit/20f250fc2eecd33b50bdff396e9f78c97a621c16) Merge branch 'canary' into refactor/improve-kysely-mysql-withReturns - [`42f1633`](https://github.com/better-auth/better-auth/commit/42f16334f2d501a13a967e7da1f5fe73c4c8236c) Merge branch 'canary' into refactor/improve-kysely-mysql-withReturns ### 📊 Changes **5 files changed** (+157 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/concepts/database.mdx` (+9 -0) 📝 `packages/better-auth/src/adapters/drizzle-adapter/test/adapter.drizzle.mysql.test.ts` (+6 -1) 📝 `packages/better-auth/src/adapters/kysely-adapter/kysely-adapter.ts` (+110 -17) 📝 `packages/better-auth/src/adapters/kysely-adapter/test/adapter.kysely.mysql.test.ts` (+6 -1) 📝 `packages/better-auth/src/adapters/tests/basic.ts` (+26 -4) </details> ### 📄 Description When using kysely + mysql then inserting data through the adapter, you may **not** receive data as the result of the insert. This is because MySQL does not support the `RETURNING` SQL clause, and thus it does not return any data that was inserted. It's most troubling when we do not have access to inserting `id`s programmatically, this can be the case when users use `generateId: false` or other auth config options like `generateId: "uuid"` and possibly others. This forces us to use different query strategies to attempt to get the last-inserted data, however there are still cases where we either can't get that data, or it's unreliable due to something like race-conditions. This PR introduces a new vector to searching for fields to reliably fetch that inserted data, by finding all unique fields in a given schema for a model, then we can then see if the provided insert values contain data for those unique fields, and use that as a reference to find the inserted data. tldr changes: * New strategies to ensure last-inserted row retrieval. * New unit-tests to test current row-creation race-conditions. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Improve MySQL post-insert retrieval in the Kysely adapter by preferring unique fields and using LAST_INSERT_ID() when auto-increment IDs are enabled. Also handles updates by using unique fields from the WHERE clause, reducing concurrency issues. - **Refactors** - Detect unique fields from the schema and WHERE clause; prefer the new value if the field is updated; use it to query the row. - Use LAST_INSERT_ID() when advanced.database.generateId is "serial". - Fall back to createdAt; if unavailable, try matching all insert values; return null if no reliable selector is found. - No API changes; reduces missed or incorrect results due to MySQL’s lack of RETURNING. <sup>Written for commit 244c2822ffd719925fd4972bed98a791e12757c5. Summary will update on new commits.</sup> <!-- 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 13:12:04 -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#6796