Suggestion: improve consistency across IDs #58

Closed
opened 2026-03-13 07:30:27 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @rokitgg on GitHub (Oct 9, 2024).

Description

Currently, there are inconsistencies in the string length of user and table IDs across the API. This can led to unexpected situations when, for example, a certain column might only allow for a maximum amount of characters to be inserted.

Imagine this scenario using drizzle-orm:

const users = pgTable("user", (t) => ({
   id: t.char({ length: 21 }).primaryKey();
  })
// some insert operations might fail while others don't!

Current Situation

I've observed varying approaches to ID generation across different parts of the codebase. For example:

  1. In user creation:

    const createdUser = await ctx.context.internalAdapter.createUser({
      id: generateRandomString(32, alphabet("a-z", "0-9", "A-Z")),
      // ...other fields
    });
    
  2. In a general ID generation function (utils/id.ts), where the default is set to 21 characters:

    export const generateId = (size?: number) => {
      return generateRandomString(size || 21, alphabet("a-z", "0-9", "A-Z"));
    };
    

Proposal

  1. Implement a common ID generation function that is used consistently across the entire codebase (already exists).
  2. Decide on a fixed length for IDs (e.g., 32 characters) to ensure uniformity.

Benefits

  • Improved code consistency and readability
  • Simplified database operations and queries
  • Reduced potential for conflicts or errors due to ID inconsistencies
  • Easier maintenance and debugging

Questions for Discussion

  • Is there a specific reason for the current variation in ID lengths?
Originally created by @rokitgg on GitHub (Oct 9, 2024). ### Description Currently, there are inconsistencies in the string length of user and table IDs across the API. This can led to unexpected situations when, for example, a certain column might only allow for a maximum amount of characters to be inserted. Imagine this scenario using drizzle-orm: ```typescript const users = pgTable("user", (t) => ({ id: t.char({ length: 21 }).primaryKey(); }) // some insert operations might fail while others don't! ``` ### Current Situation I've observed varying approaches to ID generation across different parts of the codebase. For example: 1. In user creation: ```typescript const createdUser = await ctx.context.internalAdapter.createUser({ id: generateRandomString(32, alphabet("a-z", "0-9", "A-Z")), // ...other fields }); ``` 2. In a general ID generation function (utils/id.ts), where the default is set to 21 characters: ```typescript export const generateId = (size?: number) => { return generateRandomString(size || 21, alphabet("a-z", "0-9", "A-Z")); }; ``` ### Proposal 1. Implement a common ID generation function that is used consistently across the entire codebase (already exists). 2. Decide on a fixed length for IDs (e.g., 32 characters) to ensure uniformity. ### Benefits - Improved code consistency and readability - Simplified database operations and queries - Reduced potential for conflicts or errors due to ID inconsistencies - Easier maintenance and debugging ### Questions for Discussion - Is there a specific reason for the current variation in ID lengths?
Author
Owner

@Bekacru commented on GitHub (Oct 9, 2024):

Great suggestions! The variation was caused because we're refactoring to use generateId for all ID-related generations. I've just opened a PR that should address the issue.

@Bekacru commented on GitHub (Oct 9, 2024): Great suggestions! The variation was caused because we're refactoring to use generateId for all ID-related generations. I've just opened a PR that should address the issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#58