Scott Prins
04/21/2022, 6:33 PMconversations{
id,
created_at
}
conversation_members {
id,
user_id (FK: user.id)
conversation_id (FK: conversation.id)
joined_time,
left_time,
created_at
}
messages {
id
conversation_id (FK:conversation.id)
from_id (FK: user.id)
body,
timestamp
}
users {
id
first_name
last_name
}
I have tried this with Firebase with just a "Conversations" collection and a nested array of messages, but it was inefficient as hell and not scalable at all, so Relational database was my next option here, but I am having trouble writing the queries to make it happen effectivelygaryaustin
04/22/2022, 12:24 AMconst { data, error } = await supabase
.from('conversations')
.select('*, conversation_members!inner(*)')
.eq('conversation_members.user_id', userid)
Just a select with multiple filters for messages from user and conversation:
const { data, error } = await supabase
.from('messages')
.select('*')
.eq('conversation_id', convid)
.eq('from_id',userid)
or something close to those.Scott Prins
04/22/2022, 2:17 AMScott Prins
04/22/2022, 2:18 AMScott Prins
04/22/2022, 2:40 AMexport function getUserConversationsSB(_userId) {
const [{ data, error }] = useRealtime(CONVERSATIONS_TABLE, {
select: {
columns: '*, conversation_members!inner(*), users!conversation_members(*)',
filter: useFilter((q) => {
return q.eq('conversation_members.user_id', _userId);
}),
},
});
if (error) {
console.error(error);
}
return data;
}
garyaustin
04/22/2022, 2:46 AM