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(),
}