© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago
DiamondDragon

Dynamic function get use typed column names using getTableConfig

I have both UUIDs and NanoIDs on every one of my tables. I am considering adding utils to map from uuid -> nanoID & vice versa.

how would i make a dynamic function like this? basically under the assumption every table has a
id
id
and
shortId
shortId
column which is the nanoid

export async function getUuidFromShortId(table: PgTable, shortId: string) {
    const { columns, name, schema } = getTableConfig(table)
    const [result] = await db
        .select({ id: columns.id })
        .from(name)
        .where(eq(columns.shortId, shortId))
        .limit(1)
    return result[0]?.id ?? null
}
export async function getUuidFromShortId(table: PgTable, shortId: string) {
    const { columns, name, schema } = getTableConfig(table)
    const [result] = await db
        .select({ id: columns.id })
        .from(name)
        .where(eq(columns.shortId, shortId))
        .limit(1)
    return result[0]?.id ?? null
}
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Dynamic Table Names
Drizzle TeamDTDrizzle Team / help
12mo ago
Issue with truncating column names using `with`
Drizzle TeamDTDrizzle Team / help
2y ago
How to get column names from Drizzle Table?
Drizzle TeamDTDrizzle Team / help
2y ago
Mapping/Renaming column names for MariaDB when using RETURNING *
Drizzle TeamDTDrizzle Team / help
3y ago