You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SELECT p.*, u.username, u.name, u.profile_photo_url,
(SELECTCOUNT(*) FROM likes WHERE post_id =p.id) as likes_count,
(SELECTCOUNT(*) FROM comments WHERE post_id =p.id) as comments_count,
EXISTS(SELECT1FROM likes WHERE post_id =p.idAND user_id = $1) as is_liked
FROM posts p
JOIN follows f ONp.user_id=f.following_idJOIN users u ONp.user_id=u.idWHEREf.follower_id= $1ANDp.is_draft= FALSE
AND (p.time_locked_until IS NULLORp.time_locked_until<= NOW())
ORDER BYp.created_atDESCLIMIT20 OFFSET $2
Get Explore Feed (Global)
SELECT p.*, u.username, u.name, u.profile_photo_url,
(SELECTCOUNT(*) FROM likes WHERE post_id =p.id) as likes_count,
(SELECTCOUNT(*) FROM comments WHERE post_id =p.id) as comments_count,
EXISTS(SELECT1FROM likes WHERE post_id =p.idAND user_id = $1) as is_liked
FROM posts p
JOIN users u ONp.user_id=u.idWHEREp.is_draft= FALSE
AND (p.time_locked_until IS NULLORp.time_locked_until<= NOW())
ANDp.visibility='public'ORDER BYp.created_atDESCLIMIT20 OFFSET $2
Get Local Feed (1-3km radius)
SELECT p.*, u.username, u.name, u.profile_photo_url,
ST_Distance(p.location_coords::geography, ST_SetSRID(ST_MakePoint($2, $3), 4326)::geography) /1000as distance_km,
(SELECTCOUNT(*) FROM likes WHERE post_id =p.id) as likes_count,
(SELECTCOUNT(*) FROM comments WHERE post_id =p.id) as comments_count
FROM posts p
JOIN users u ONp.user_id=u.idWHEREp.location_coordsIS NOT NULLANDp.is_draft= FALSE
AND (p.time_locked_until IS NULLORp.time_locked_until<= NOW())
AND ST_DWithin(
p.location_coords::geography,
ST_SetSRID(ST_MakePoint($2, $3), 4326)::geography,
$4*1000
)
ORDER BYp.created_atDESCLIMIT20 OFFSET $5
Get User Posts
SELECT p.*,
(SELECTCOUNT(*) FROM likes WHERE post_id =p.id) as likes_count,
(SELECTCOUNT(*) FROM comments WHERE post_id =p.id) as comments_count
FROM posts p
WHEREp.user_id= $1ANDp.is_draft= FALSE
ORDER BYp.created_atDESCLIMIT20 OFFSET $2
Get Saved Posts
SELECT p.*, u.username, u.name, u.profile_photo_urlFROM posts p
JOIN saved_posts sp ONp.id=sp.post_idJOIN users u ONp.user_id=u.idWHEREsp.user_id= $1ORDER BYsp.created_atDESCLIMIT20 OFFSET $2
Get Liked Posts
SELECT p.*, u.username, u.name, u.profile_photo_urlFROM posts p
JOIN likes l ONp.id=l.post_idJOIN users u ONp.user_id=u.idWHEREl.user_id= $1ORDER BYl.created_atDESCLIMIT20 OFFSET $2
Delete Post
DELETEFROM posts WHERE id = $1AND user_id = $2
Duet Posts
Create Duet Post
-- First create the postINSERT INTO posts (user_id, caption, media_urls, media_type) VALUES ($1, $2, $3, 'photo') RETURNING id;
-- Then create duet entryINSERT INTO duet_posts (post_id, user1_id, user2_id, user1_media_url, user2_media_url)
VALUES ($1, $2, $3, $4, $5)
Story Chains
Create Story Chain
INSERT INTO story_chains (creator_id, title) VALUES ($1, $2) RETURNING *
SELECT c.*, u.username, u.profile_photo_url,
(SELECTCOUNT(*) FROM comments WHERE parent_comment_id =c.id) as replies_count
FROM comments c
JOIN users u ONc.user_id=u.idWHEREc.post_id= $1ANDc.parent_comment_id IS NULLORDER BYc.created_atDESCLIMIT20 OFFSET $2
Get Replies
SELECT c.*, u.username, u.profile_photo_urlFROM comments c
JOIN users u ONc.user_id=u.idWHEREc.parent_comment_id= $1ORDER BYc.created_atASC
Follows
Follow User
INSERT INTO follows (follower_id, following_id) VALUES ($1, $2) ON CONFLICT DO NOTHING
Unfollow User
DELETEFROM follows WHERE follower_id = $1AND following_id = $2
Get Followers
SELECT u.*FROM users u
JOIN follows f ONu.id=f.follower_idWHEREf.following_id= $1LIMIT20 OFFSET $2
Get Following
SELECT u.*FROM users u
JOIN follows f ONu.id=f.following_idWHEREf.follower_id= $1LIMIT20 OFFSET $2
Check if Following
SELECT EXISTS(SELECT1FROM follows WHERE follower_id = $1AND following_id = $2)
Follow Groups
Create Follow Group
INSERT INTO follow_groups (user_id, name) VALUES ($1, $2) RETURNING *
Add Member to Group
INSERT INTO follow_group_members (group_id, user_id) VALUES ($1, $2) ON CONFLICT DO NOTHING
Get User's Groups
SELECT fg.*,
(SELECTCOUNT(*) FROM follow_group_members WHERE group_id =fg.id) as members_count
FROM follow_groups fg
WHEREfg.user_id= $1
Get Group Feed
SELECT p.*, u.username, u.name, u.profile_photo_urlFROM posts p
JOIN users u ONp.user_id=u.idWHEREp.group_id= $1ANDp.visibility='group'ORDER BYp.created_atDESC
Friendship Levels
Get Friendship Level
SELECT*FROM friendship_levels
WHERE (user1_id = $1AND user2_id = $2) OR (user1_id = $2AND user2_id = $1)
SELECT c.*,
(SELECT json_agg(json_build_object('id', u.id, 'username', u.username, 'profile_photo_url', u.profile_photo_url))
FROM conversation_participants cp
JOIN users u ONcp.user_id=u.idWHEREcp.conversation_id=c.idANDcp.user_id!= $1) as participants,
(SELECT content FROM messages WHERE conversation_id =c.idORDER BY created_at DESCLIMIT1) as last_message
FROM conversations c
JOIN conversation_participants cp ONc.id=cp.conversation_idWHEREcp.user_id= $1ORDER BYc.created_atDESC
Get Messages (Real-time)
SELECT m.*, u.username, u.profile_photo_urlFROM messages m
JOIN users u ONm.sender_id=u.idWHEREm.conversation_id= $1AND (m.delete_at IS NULLORm.delete_at> NOW())
ORDER BYm.created_atASCLIMIT50 OFFSET $2
SELECT e.*, u.username,
ST_Distance(e.location_coords::geography, ST_SetSRID(ST_MakePoint($1, $2), 4326)::geography) /1000as distance_km,
(SELECTCOUNT(*) FROM event_attendees WHERE event_id =e.idAND status ='going') as attendees_count
FROM events e
JOIN users u ONe.creator_id=u.idWHERE ST_DWithin(
e.location_coords::geography,
ST_SetSRID(ST_MakePoint($1, $2), 4326)::geography,
$3*1000
)
ORDER BYe.event_dateASC
RSVP to Event
INSERT INTO event_attendees (event_id, user_id, status)
VALUES ($1, $2, $3)
ON CONFLICT (event_id, user_id) DO UPDATESET status = $3
Confessions
Create Confession
INSERT INTO confessions (content) VALUES ($1) RETURNING *
Get Confessions Feed
SELECT c.*,
(SELECTCOUNT(*) FROM confession_votes WHERE confession_id =c.idAND vote_type ='upvote') as upvotes,
(SELECTCOUNT(*) FROM confession_votes WHERE confession_id =c.idAND vote_type ='downvote') as downvotes
FROM confessions c
WHEREc.is_approved= TRUE
ORDER BYc.created_atDESCLIMIT20 OFFSET $1
Vote on Confession
INSERT INTO confession_votes (confession_id, user_id, vote_type)
VALUES ($1, $2, $3)
ON CONFLICT (confession_id, user_id) DO UPDATESET vote_type = $3
INSERT INTO challenge_votes (submission_id, user_id) VALUES ($1, $2) ON CONFLICT DO NOTHING;
UPDATE challenge_submissions SET votes_count = votes_count +1WHERE id = $1
Get Active Challenges
SELECT*FROM challenges
WHERE start_date <= NOW() AND end_date >= NOW()
ORDER BY start_date DESC
Get Challenge Submissions
SELECT cs.*, p.*, u.username, u.profile_photo_urlFROM challenge_submissions cs
JOIN posts p ONcs.post_id=p.idJOIN users u ONcs.user_id=u.idWHEREcs.challenge_id= $1ORDER BYcs.votes_countDESCLIMIT20 OFFSET $2
Private Locker
Add to Locker
INSERT INTO locker_posts (user_id, post_id, pin_hash) VALUES ($1, $2, $3)
Get Locker Posts (after PIN verification)
SELECT p.*FROM posts p
JOIN locker_posts lp ONp.id=lp.post_idWHERElp.user_id= $1ANDlp.pin_hash= $2
Storage Operations (Supabase Storage)
Upload Media
final file =File(path);
final fileName ='${DateTime.now().millisecondsSinceEpoch}_${basename(path)}';
await supabase.storage.from('media').upload('posts/$fileName', file);
final url = supabase.storage.from('media').getPublicUrl('posts/$fileName');