How to use bigint safely with Supabase JS
Asked Answered
C

1

6

Postgres' bigint type holds 64 bit integers. But the Supabase JS library returns those values as JS numbers, which cannot safely store 64 bit integers AFAIK. What would be the correct way to handle 64 bit integers in Supabase?

Chemical answered 23/11, 2022 at 15:29 Comment(2)
Store it as a string?Needle
@DaneBrouwer Are you telling me to store it as a string in Postgres? But using a bigint would be a lot more performant than using strings right? I plan to use that column as the primary key too.Chemical
E
6

You are correct. This is because numbers in JS are float64, so if your number is more than Number.MAX_SAFE_INTEGER === 9007199254740991. It won't be represented correctly.

An alternative is to cast it to text during the call & manually convert it:

supabase.from('table_name').select('id, num::text');
BigInt(data);
Eggett answered 23/11, 2022 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.