My data looks something like this in RethinkDb:
[appointments]
{
id: 1,
date: "2016-01-01",
stylistId: 1,
}
[stylists]
{
id: 1,
name: "Sue",
}
On the client, I want to fetch an appointment and have the stylist embedded in the appointment object like so:
{
id: 1,
date: "2016-01-01",
stylist: {
id: 1,
name: "Sue",
}
}
I can achieve something close using this query:
return this.hz('appointments')
.find(appointmentId)
.fetch()
.toPromise()
.then(appointment => {
return this.hz('stylists').find(appointment.stylistId)
.fetch()
.toPromise()
.then(stylist => {
appointment.stylist = stylist;
return appointment;
});
})
.then(appointment => this.appointment = appointment);
I'm wondering if there is a cleaner way to achieve the desired result. I know that RethinkDb has support for Joins but it does not look like it's exposed in Horizon. I also am not very good with RxJS so I'm wondering if the join can be more cleanly done there.
try mergeMap
and map
combination
const appointmentFind$ = (appointmentId) => hz('appointments').find(appointmentId).fetch();
const stylistFind$ = (stylistId) => hz('stylists').find(stylistId).fetch();
appointmentFind$(appointmentId)
.mergeMap((appointment) => stylistFind$(appointment.stylistId)
.map((stylist) => ({ ...appointment, stylist })))
.subscribe((x) => console.log(x));
I wrote it down here, so it's untested, but it should work somehow like this