Allow omission or customisation of id generation #1519

Closed
opened 2026-03-13 08:44:56 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @chrisui on GitHub (Jul 18, 2025).

Is this suited for github?

  • Yes, this is suited for github

It's great to have an opinionated default but database ID's are sensitive to the whole system so better-auth should provide a mechanism to customise how these id's are created/used.

Describe the solution you'd like

Either on the auth config or database adapter config (for context, I use drizzle), have two option properties:

{
  generateIds?: boolean // defaults to true for today's behaviour
  idGenerator?: (model) => string | number
}

If generateIds is explicitly false then better-auth will not generate any random ID and instead rely upon the schema/creation to generate and return the ID. For example drizzle schemas can have $defaultFn().

If idGenerator is provided and generateIds is not explicitly false then instead of better-auth generating a random id it calls the function to generate - providing context such as the model name so that users can use typeid's if they wish.

Describe alternatives you've considered

n/a

Additional context

Related to https://github.com/better-auth/better-auth/issues/2480 however that issue is originally primarily about mapping fields rather than the underlying id generation (though people in that thread latter complain about latter behaviour).

Originally created by @chrisui on GitHub (Jul 18, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. It's great to have an opinionated default but database ID's are sensitive to the whole system so better-auth should provide a mechanism to customise how these id's are created/used. ### Describe the solution you'd like Either on the auth config or database adapter config (for context, I use drizzle), have two option properties: ``` { generateIds?: boolean // defaults to true for today's behaviour idGenerator?: (model) => string | number } ``` If `generateIds` is explicitly `false` then better-auth will not generate any random ID and instead rely upon the schema/creation to generate and return the ID. For example drizzle schemas can have `$defaultFn()`. If `idGenerator` is provided and `generateIds` is not explicitly `false` then instead of better-auth generating a random id it calls the function to generate - providing context such as the model name so that users can use typeid's if they wish. ### Describe alternatives you've considered n/a ### Additional context Related to https://github.com/better-auth/better-auth/issues/2480 however that issue is originally primarily about _mapping_ fields rather than the underlying id generation (though people in that thread latter complain about latter behaviour).
GiteaMirror added the enhancement label 2026-03-13 08:44:56 -05:00
Author
Owner

@chrisui commented on GitHub (Jul 18, 2025):

If anyone is using drizzle like me then here's a copy edit of the adapter that adds generateId's config.

https://gist.github.com/chrisui/b700330efa63e55a1cab197fc38cf5f7

Now with my id fields in schema configured like below i get typeid's in database with better-auth.

text('id')
    .$defaultFn(() => randomTypeID(type).toString())
    .primaryKey();
@chrisui commented on GitHub (Jul 18, 2025): If anyone is using drizzle like me then here's a copy edit of the adapter that adds generateId's config. https://gist.github.com/chrisui/b700330efa63e55a1cab197fc38cf5f7 Now with my id fields in schema configured like below i get typeid's in database with better-auth. ``` text('id') .$defaultFn(() => randomTypeID(type).toString()) .primaryKey(); ```
Author
Owner

@frectonz commented on GitHub (Aug 11, 2025):

This is already supported.

export const auth = betterAuth({
  database: {
    generateId(size?: number) {
      return "id"
    }
  }
})
@frectonz commented on GitHub (Aug 11, 2025): This is already supported. ```typescript export const auth = betterAuth({ database: { generateId(size?: number) { return "id" } } }) ```
Author
Owner

@chrisui commented on GitHub (Sep 9, 2025):

This is already supported.

export const auth = betterAuth({
database: {
generateId(size?: number) {
return "id"
}
}
})

Thanks. Works for now. (would still think co-location of id logic and schema is better to avoid centralising here)

Quote note for anyone finding this: it's actually [perhaps, now] nested in an advanced config object.

betterAuth({
  advanced: {
    database: {
      generateId() { ... }
    }
  }
})
@chrisui commented on GitHub (Sep 9, 2025): > This is already supported. > > export const auth = betterAuth({ > database: { > generateId(size?: number) { > return "id" > } > } > }) Thanks. Works for now. (would still think co-location of id logic and schema is better to avoid centralising here) Quote note for anyone finding this: it's actually [perhaps, now] nested in an `advanced` config object. ```ts betterAuth({ advanced: { database: { generateId() { ... } } } }) ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1519