LibPQ.jl icon indicating copy to clipboard operation
LibPQ.jl copied to clipboard

How to handle UUID and JSONB

Open hasanAjsf opened this issue 6 years ago • 1 comments
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?

image

hasanAjsf avatar Sep 07 '19 16:09 hasanAjsf

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)))))

iamed2 avatar Oct 17 '19 16:10 iamed2