Hello, I have a json array and I would like to get...
# javascript
b
Hello, I have a json array and I would like to get the username of the author returned with the rest of the data
Copy code
[{"comment":"Hello","author":"2aa439fd-77b0-448f-8ab7-ad26a0a0b5b3"}]
s
Can you share your js code you are using for the query please?
b
Copy code
const { data } = await supabase
      .from("comments")
      .select(
        "id, inserted_at, data, karma, profiles:author ( id, username ), replies"
      )
      .order("inserted_at", { ascending: false })
      .eq("post", query.id);
I think the problem is this won't work
I think maybe I need to do something else, but this is my first time using postgres
s
Do you have your relationships setup properly inside the database?
b
This is what I have
It's a jsonb column
Not sure if that would work
s
No you can't get the author username from that, you would need to have that as a column itself, your tables could be structured different to get such a result.
Let me try and get some SQL for you to set this up in a way you can get all the data you need from the one query
What is the name of this table you have taken a screenshot of?
Copy code
sql
CREATE TABLE public.comments (
    id uuid primary key default uuid_generate_v4(),
    data text,
    inserted_at timestamp with time zone default now() not null,
    updated_at timestamp with time zone,
    author uuid references public.profiles not null,
    post int8,
    karma int4
)

CREATE TABLE public.replies (
    id uuid primary key default uuid_generate_v4(),
    author uuid references public.profiles not null,
    message text,
    comment_id uuid references public.comments not null,
    inserted_at timestamp with time zone default now() not null,
    updated_at timestamp with time zone
)
The assumption in here is that your reference to the author is to a table in your public schema called
profiles
, maybe your setup is not using that table so change that accordingly. With this you don't store replies inside a jsonb column as a reference to the original comment is stored in the replies table., now you should be able to run your JS code and get results
Copy code
js
const { data } = await supabase
    .from("comments")
    .select(
        `id, inserted_at, data, karma, 
        author( id, username ), 
        replies(
            message,
            author ( username:reply_username )
        )
    `)
    .order("inserted_at", { ascending: false })
    .eq("post", query.id);
I haven't tested the JS part of this code but it should work
b
Thank you!
Will that work with replies to replies?
s
No this won't work with that as it is, you would need to change the replies table to have an
reply_id
to reference itself. Depending on how complex this get you could require a pivot table that uses leaf nodes, but I don't think supabase-js library would now how to handle this, so you would have to write your own queries and use
.rpc
instead.
b
That sounds like a lot... I did see this https://github.com/lawrencecchen/threaded-comments but also want to try do it myself
s
You can take a look at the sql file in that repo to see how the developer is accomplishing this, you can see its similar to my first suggestion of storing the
reply_id
which in their case is called
parent_id
, but they also have a bunch of custom views they created in the database to make querying easier. These are more complex SQL concepts, but you can read more about them https://www.postgresqltutorial.com/postgresql-views/
b
Thank you for your help 🙂