[GH-ISSUE #1369] Database schema generator: option to add an index to an additional database field #17345

Closed
opened 2026-04-15 15:28:03 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @sharknoon on GitHub (Feb 6, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1369

Is this suited for github?

  • Yes, this is suited for github

I have added a username field to the User table. I need to query the users based on their usernames. Therefore I would like to add an index to the username, to improve performance and have better scaling with more users.

Describe the solution you'd like

export const auth = betterAuth({
        // ...
	user: {
		additionalFields: {
			username: {
				type: 'string',
				index: true, // <-- new field
                                // ...
			}
		}
	},
        // ...
)}

Describe alternatives you've considered

The alternative would be to modify the generated database schema, but that would be overridden every time I regenerate the auth schema with npx @better-auth/cli generate

Additional context

Intention to add this feature: https://www.better-auth.com/docs/guides/optimizing-for-performance#recommended-fields-to-index
Indexes for Drizzle: https://orm.drizzle.team/docs/indexes-constraints#indexes
Indexes for Prisma: https://www.prisma.io/docs/orm/prisma-schema/data-model/indexes

Originally created by @sharknoon on GitHub (Feb 6, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1369 ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. I have added a username field to the User table. I need to query the users based on their usernames. Therefore I would like to add an index to the username, to improve performance and have better scaling with more users. ### Describe the solution you'd like ```typescript export const auth = betterAuth({ // ... user: { additionalFields: { username: { type: 'string', index: true, // <-- new field // ... } } }, // ... )} ``` ### Describe alternatives you've considered The alternative would be to modify the generated database schema, but that would be overridden every time I regenerate the auth schema with `npx @better-auth/cli generate` ### Additional context Intention to add this feature: https://www.better-auth.com/docs/guides/optimizing-for-performance#recommended-fields-to-index Indexes for Drizzle: https://orm.drizzle.team/docs/indexes-constraints#indexes Indexes for Prisma: https://www.prisma.io/docs/orm/prisma-schema/data-model/indexes
GiteaMirror added the locked label 2026-04-15 15:28:03 -05:00
Author
Owner

@florianmartens commented on GitHub (Mar 20, 2025):

At least for me better-auth does not generate a single index in my database. I already wondered what's up with that.

I suspected that they did not want to make any assumptions on how the data is queried? Which is a bit odd to me since the library controls much of the querying with their own client and server functions.

But I guess both prisma and dizzle create their migrations as pure sql. Just create an empty migration and add the indexes in that migration file. This way you don't have to recreate indexes when you regenerate the schema file. AFAIK neither Drizzle nor Prisma optimize queries bases on indexes defined in schema files. So it should be fine.

<!-- gh-comment-id:2739955699 --> @florianmartens commented on GitHub (Mar 20, 2025): At least for me better-auth does not generate a single index in my database. I already wondered what's up with that. I suspected that they did not want to make any assumptions on how the data is queried? Which is a bit odd to me since the library controls much of the querying with their own client and server functions. But I guess both prisma and dizzle create their migrations as pure sql. Just create an empty migration and add the indexes in that migration file. This way you don't have to recreate indexes when you regenerate the schema file. AFAIK neither Drizzle nor Prisma optimize queries bases on indexes defined in schema files. So it should be fine.
Author
Owner

@dosubot[bot] commented on GitHub (Jun 19, 2025):

Hi, @sharknoon. I'm Dosu, and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • You requested a feature to add an index to the username field in the User table to improve query performance.
  • Proposed using TypeScript for schema configuration to avoid manual modifications being overwritten.
  • @florianmartens noted that the current setup doesn't generate indexes and suggested using an empty migration to add them.
  • Neither Drizzle nor Prisma optimize queries based on schema-defined indexes.

Next Steps:

  • Please confirm if this issue is still relevant to the latest version of the better-auth repository by commenting here.
  • If no updates are provided, the issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

<!-- gh-comment-id:2988587742 --> @dosubot[bot] commented on GitHub (Jun 19, 2025): Hi, @sharknoon. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale. **Issue Summary:** - You requested a feature to add an index to the username field in the User table to improve query performance. - Proposed using TypeScript for schema configuration to avoid manual modifications being overwritten. - @florianmartens noted that the current setup doesn't generate indexes and suggested using an empty migration to add them. - Neither Drizzle nor Prisma optimize queries based on schema-defined indexes. **Next Steps:** - Please confirm if this issue is still relevant to the latest version of the better-auth repository by commenting here. - If no updates are provided, the issue will be automatically closed in 7 days. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#17345