Skip to content

nanoid support custom alphabet #2348

@oriole-pub

Description

@oriole-pub

Most of the time, we need to define a special business ID field for models, such as post_id or user_id. We often choose UUID or nanoid for generating these IDs. However, when using nanoid, we found that it cannot meet our requirements because it defaults to generating IDs with uppercase letters and numbers. Sometimes we prefer IDs in lowercase letters and numbers, which nanoid cannot handle. As a result, we are forced to set the ID field as non‑nullable and generate a compliant ID in the code layer when creating records.

This is not an ideal practice. Providing a custom alphabet feature for nanoid would help reduce this mental overhead. Moreover, I believe the optimal API design would be:

  • Keep the usage as is:
    @default(nanoid(16))

  • Add an optional parameter when creating the client:

export const db = new ZenStackClient(schema, {
    dialect: new SqliteDialect({
        database: new SQLite(':memory:'),
    }),
    nanoidCustomAlphabet: "1234567890abcdef"
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions