Is there a better way to “join” data using Horizon?

丶灬走出姿态 提交于 2019-12-08 06:56:48

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/40034343/is-there-a-better-way-to-join-data-using-horizon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!