Mac
05/12/2022, 6:29 PMsnapshots
and tracks
. They're connected in a many-to-many relationship via a join table called snapshots_tracks
. Apart from containing proper foreign keys, it has two extra columns named added_at
and position
.
Now, I'm using the JavaScript SDK to find a snapshot
with an id equal to given snapshotId
, but I'd like my response to include a tracks
array with each track containing the aforementioned properites as well as all the other `tracks`' columns.
While I'm aware that PostgREST is capable of detecting M-M relationships, I couldn't quite figure out how to craft a query described above. Here's what I have so far:
ts
const { data } = await supabase
.from<Snapshot>('snapshots')
.select(
`
id,
captured_at,
description,
num_followers,
tracks(*)
`
)
.throwOnError(true)
.eq('id', snapshotId)
.limit(1)
.single();
burggraf
05/12/2022, 6:31 PMburggraf
05/12/2022, 7:29 PMburggraf
05/12/2022, 7:29 PMburggraf
05/12/2022, 7:29 PMburggraf
05/12/2022, 7:30 PM.rpc()
burggraf
05/12/2022, 7:31 PMMac
05/12/2022, 7:33 PMentry_meta
is an array with a single object containing these propertiesburggraf
05/12/2022, 7:33 PMburggraf
05/12/2022, 7:33 PMMac
05/12/2022, 7:36 PMMac
05/13/2022, 7:34 PMtrack
object like so:
ts
// "data" is a single snapshot record object
await Promise.all(
data.tracks.map(async (track) => {
const { data: foundEntry } = await supabase
.rpc<SnapshotTrackEntry>('get_snapshot_track_entry', {
snapshot_id_arg: snapshotId,
track_id_arg: track.id
})
.throwOnError(true)
.limit(1)
.single();
track.entry = foundEntry;
})
);
burggraf
05/13/2022, 7:42 PM