LibPQ.jl
LibPQ.jl copied to clipboard
How to handle UUID and JSONB
trafficstars
In my database, I've the below structure:
CREATE TABLE public.devices
(
id uuid,
data jsonb
)
When I run my query using this package, I got the below, how can I read the output properly?

By "properly", do you mean you want to read the UUIDs as Julia's UUID type?
execute(conn, "SELECT * FROM devices"; type_map=Dict(:uuid=>UUID), conversions=Dict((:uuid, UUID)=>(x)->UUID(String(x))))
I will add UUID as a built-in conversion type since it's a standard library anyway. After that it will be automatically parsed as a UUID.
JSON is more complicated. It's not clear what to parse JSON into, as it could be a string, number, array, or dictionary.
If you want to cover all your bases:
columntable(execute(conn, "SELECT * FROM devices"; type_map=Dict(:jsonb=>Any), conversions=Dict((:jsonb, Any)=>(x)->JSON.parse(String(x)))))