© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
1 reply
Sylvain

Using BIN_TO_UUID / UUID_TO_BIN

I’m trying to understand the best way to use
BIN_TO_UUID
BIN_TO_UUID
and
UUID_TO_BIN
UUID_TO_BIN
(MySQL).

The below is working fine...

export const users = mysqlTable('User', {
 uuid: binary('uuid', { length: 16 }).default(sql`(UUID_TO_BIN(UUID(), 1))`).primaryKey(),
 username: varchar('username', { length: 256 }).notNull(),
}

const select = await db
 .select({ uuid: sql<string>`BIN_TO_UUID(${users.uuid}, 1)` })
 .from(users)

const update = await db
 .update(users)
 .set({ username: 'foo' })
 .where(eq(users.uuid, sql<string>`UUID_TO_BIN(${myUserUuid}, 1)`))
export const users = mysqlTable('User', {
 uuid: binary('uuid', { length: 16 }).default(sql`(UUID_TO_BIN(UUID(), 1))`).primaryKey(),
 username: varchar('username', { length: 256 }).notNull(),
}

const select = await db
 .select({ uuid: sql<string>`BIN_TO_UUID(${users.uuid}, 1)` })
 .from(users)

const update = await db
 .update(users)
 .set({ username: 'foo' })
 .where(eq(users.uuid, sql<string>`UUID_TO_BIN(${myUserUuid}, 1)`))


...but I was wondering if there was a better approach, so that the
UUID<>BIN
UUID<>BIN
conversion would automatically apply - without having to manually specify it on each query?

Here is an example of what I’m looking for (
fromSQL
fromSQL
and
toSQL
toSQL
are made up functions):

const customBinary = customType<{
 data: string; driverData: string; config: { length?: number }
}>({
 dataType(config) {
  return typeof config?.length !== 'undefined' ? `binary(${config.length})` : `binary`
 },
 // automatically apply on select + return
 fromSQL(value: string): string {
  return `BIN_TO_UUID(${value}, 1)`
 },
 // automatically apply on insert/update + select filters
 toSQL(value: string): string {
  return `UUID_TO_BIN(${value}, 1)`
 }
});

export const users = mysqlTable('User', {
 uuid: customBinary('uuid', { length: 16 }).default(sql`(UUID_TO_BIN(UUID(), 1))`).primaryKey(),
 username: varchar('username', { length: 256 }).notNull(),
}
const customBinary = customType<{
 data: string; driverData: string; config: { length?: number }
}>({
 dataType(config) {
  return typeof config?.length !== 'undefined' ? `binary(${config.length})` : `binary`
 },
 // automatically apply on select + return
 fromSQL(value: string): string {
  return `BIN_TO_UUID(${value}, 1)`
 },
 // automatically apply on insert/update + select filters
 toSQL(value: string): string {
  return `UUID_TO_BIN(${value}, 1)`
 }
});

export const users = mysqlTable('User', {
 uuid: customBinary('uuid', { length: 16 }).default(sql`(UUID_TO_BIN(UUID(), 1))`).primaryKey(),
 username: varchar('username', { length: 256 }).notNull(),
}


Is there any way to do something similar right now?
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

Using UUID v7 with Drizzle?
Drizzle TeamDTDrizzle Team / help
16mo ago
Custom UUID
Drizzle TeamDTDrizzle Team / help
3y ago
add a prefix to UUID?
Drizzle TeamDTDrizzle Team / help
2y ago