有没有更好的“加入"方式?数据使用地平线? [英] Is there a better way to "join" data using Horizon?
问题描述
我的数据在 RethinkDb 中看起来像这样:
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);
我想知道是否有更简洁的方法来实现预期的结果.我知道 RethinkDb 支持 Joins,但它看起来不像它在 Horizon 中公开.我对 RxJS 也不太擅长,所以我想知道是否可以在那里更干净地完成连接.
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.
推荐答案
尝试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));
我把它写在这里,所以它未经测试,但它应该像这样工作
I wrote it down here, so it's untested, but it should work somehow like this
这篇关于有没有更好的“加入"方式?数据使用地平线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!