Sylvain
Sylvain
DTDrizzle Team
Created by Sylvain on 5/22/2023 in #help
Using BIN_TO_UUID / UUID_TO_BIN
Nevermind... I found a better way to achieve what I was looking for by using custom types with the binary-uuid package (https://www.npmjs.com/package/binary-uuid):
import { fromBinaryUUID } from 'binary-uuid'
import { sql, type SQL } from 'drizzle-orm';

export const binary = customType<{
data: string; driverData: string; config: { length?: number }
}>({
dataType(config) {
return typeof config?.length !== 'undefined' ? `binary(${config.length})` : `binary`
},
fromDriver(value: string): string {
const buff = value.startsWith('base64:type254:')
? Buffer.from(value.split(":")[2], "base64")
: Buffer.from(value, 'binary')

return fromBinaryUUID(buff)
},
toDriver(value: string): SQL<unknown> {
return sql`UUID_TO_BIN(${value}, 1)`
},
});
import { fromBinaryUUID } from 'binary-uuid'
import { sql, type SQL } from 'drizzle-orm';

export const binary = customType<{
data: string; driverData: string; config: { length?: number }
}>({
dataType(config) {
return typeof config?.length !== 'undefined' ? `binary(${config.length})` : `binary`
},
fromDriver(value: string): string {
const buff = value.startsWith('base64:type254:')
? Buffer.from(value.split(":")[2], "base64")
: Buffer.from(value, 'binary')

return fromBinaryUUID(buff)
},
toDriver(value: string): SQL<unknown> {
return sql`UUID_TO_BIN(${value}, 1)`
},
});
2 replies