【问题标题】:Is there a better way to "join" data using Horizon?有没有更好的方法来使用 Horizo​​n 来“加入”数据?
【发布时间】:2016-10-14 03:12:57
【问题描述】:

我的数据在 RethinkDb 中看起来像这样:

[appointments]
{
    id: 1,
    date: "2016-01-01",
    stylistId: 1,
}

[stylists]
{
    id: 1,
    name: "Sue",
}

在客户端,我想获取一个约会并将造型师嵌入到约会对象中,如下所示:

{
    id: 1,
    date: "2016-01-01",
    stylist: {
        id: 1,
        name: "Sue",
    }
}

我可以使用这个查询实现一些接近的目标:

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);

我想知道是否有更清洁的方法来实现预期的结果。我知道 RethinkDb 支持加入,但它看起来不像在 Horizo​​n 中公开。我对 RxJS 也不是很好,所以我想知道连接是否可以在那里更干净地完成。

【问题讨论】:

    标签: javascript rxjs rethinkdb horizon


    【解决方案1】:

    试试mergeMap和map的组合

    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));
    

    我把它写在这里,所以它未经测试,但它应该像这样工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      • 2020-02-17
      • 2014-06-04
      • 1970-01-01
      • 2019-05-14
      相关资源
      最近更新 更多