BenXk
01/17/2022, 4:42 PM[{"comment":"Hello","author":"2aa439fd-77b0-448f-8ab7-ad26a0a0b5b3"}]
silentworks
01/17/2022, 8:17 PMBenXk
01/18/2022, 9:07 AMconst { 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);
BenXk
01/18/2022, 9:08 AMBenXk
01/18/2022, 9:08 AMsilentworks
01/18/2022, 10:36 AMBenXk
01/18/2022, 11:42 AMBenXk
01/18/2022, 11:42 AMBenXk
01/18/2022, 11:42 AMsilentworks
01/18/2022, 11:48 AMsilentworks
01/18/2022, 11:52 AMsilentworks
01/18/2022, 11:57 AMsilentworks
01/18/2022, 12:12 PMsql
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
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);
silentworks
01/18/2022, 12:12 PMBenXk
01/18/2022, 12:28 PMBenXk
01/18/2022, 12:28 PMsilentworks
01/18/2022, 3:41 PMreply_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.BenXk
01/18/2022, 4:15 PMsilentworks
01/19/2022, 1:22 PMreply_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/BenXk
01/21/2022, 1:26 PM