所以我试图通过 查询一个表userId
,然后从我从第一个查询中获得的数组中查询另一个表。
DB 看起来像这样(一个用户可以有很多成员):
members:
id | userId | channelId | name | ...
channels:
id | ...
但问题是我的第一个查询返回一个我需要循环的数组。我将如何使用钩子在 React 中实现这一点?
const useChats = () => {
const [chats, setChats] = useState()
const client = useClient()
const memberFilter = useFilter<DB.Member>((query) =>
// hard coded for now
query.eq('userId', '94a9aa95-c794-404a-9a37-7f424b215ebb').limit(10)
)
const [{ data: memberData }] = useRealtime<DB.Member>('members', {
select: {
columns: 'id, channelId, name, image, read',
filter: memberFilter,
},
})
const channelFilter = useFilter<DB.Channel>((query) =>
// issue is here... how do i query `channel:id` for each member entry?
// memberData is of type `DB.Member[] | null | undefined`
query.eq('id', memberData[0].channelId).limit(10)
)
const [{ data: channelData }] = useRealtime<DB.Channel>('channels', {
select: {
columns: 'id, lastActive, lastMessage',
filter: channelFilter,
},
})
return chats
}
export { useChats }
类型:
export namespace DB {
interface Member {
id: string
userId: string
channelId: string
name: string
image: string
read: boolean
}
interface Channel {
id: string
lastActive: string
lastMessage?: string
}
}
用于react-supabase
查询