Store percentage value as integer

Hi, I realised once again how awesome Drizzle is! I want to store percentage value as integer. I ended up with this:
export const percentage = customType<{ data: number, driverData: number }>({
dataType() {
return 'integer'
},
toDriver(value: number) {
return Math.round(value * 100)
},
fromDriver(value: number) {
return value / 100
}
})
export const percentage = customType<{ data: number, driverData: number }>({
dataType() {
return 'integer'
},
toDriver(value: number) {
return Math.round(value * 100)
},
fromDriver(value: number) {
return value / 100
}
})
For my use case, it is correct as I don't want to store more than 2 decimals. Using the type decimal is inconvenient as it is stored as string with the Postgres driver. Hence this choice. But my question is about the caveats of this technique. Does the in & out conversion affect the performance in a noticeable way?
2 Replies
binajmen
binajmenOP•9mo ago
up 😇
Mykhailo
Mykhailo•9mo ago
Hello, @binajmen! It shouldn't affect the performance in a noticeable way. mode option in some types works the same, so it should be ok
Want results from more Discord servers?
Add your server