是否有可能使 hasMany 关系表现得像 LoopBack 4 中的实际属性
Is it possible to make hasMany relations behave like actual properties in LoopBack 4
我正在使用 LoopBack 4 构建一个 API。是否可以像使用实际属性一样使用关系?
API 将事件(例如音乐会)存储在数据库中的事件 table 中,将事件日期存储在 event_dates table 中。我已使用此 [1] 说明成功添加了一个与事件模型的 hasMany 关系和一个与 EventDate 模型的 belongsTo 关系(一个事件可以有多个 EventDates)。
虽然我可以使用 eventRepository.dates(eventId) 查询日期,但当我请求 http://localhost:3000/events 时却没有可用的日期 – 我如何在不询问 eventRepository.dates(eventId) 的情况下实现这一目标分开?
另一方面,我想 POST 和 PATCH 事件,而无需单独发布和修补事件日期 – 这可以通过几行代码实现吗?
这就是我现在需要使日期字段在 /events 下可用的方法(这似乎不是正确的方法):
const events = await this.eventRepository.find(filter);
for (let event of events) {
event.dates = await this.eventRepository.dates(eventId).find()
}
当我想添加一个新事件时,我需要这样做:
POST /events
POST /events/:id/event-dates
POST /events/:id/event-dates
...
请注意:我正在寻找 LoopBack 框架内已经可用的解决方案。实现这些东西不是问题,我只希望它尽可能简短和可维护。
While I can query the dates using eventRepository.dates(eventId), there aren't available when I request http://localhost:3000/events – How could I achieve this without asking eventRepository.dates(eventId) separately?
我们将此功能称为 "inclusion of related models",遗憾的是它尚未实现。您可以在此处跟踪进度:https://github.com/strongloop/loopback-next/issues/1352
On the other hand I would like to POST and PATCH events without posting and patching the event dates separately – Is this possible with a few lines of code?
如果我的理解正确,您希望通过单个 REST API 调用更新 Event
和 EventDate
实例。我们不支持该功能,老实说,我不确定我们是否会支持。
This is what I need to make the dates field available under /events right now (doesn't seem to be the right way).
您的解决方案容易受到所谓的 "SELECT 1+N PROBLEM"(参见 What is the "N+1 selects problem" in ORM (Object-Relational Mapping)?)的影响。如果您的事件有 N 个日期,那么您将进行 1+N 次数据库查询。
更好的解决方案是利用 LoopBack 的 inq
运算符:
const eventIds = events.map(e => e.id);
const dates = await this.datesRepository.find({where:{eventId:{inq: eventIds}}});
// copy "dates" entries to relevant "event" items
// by matching "dates[].eventId" against "events[].id"
如果查询的事件数量很多,那么您需要将eventIds
数组拆分成更小的块并多次调用this.datesRepository.find
。
无论如何,这就是我们在不久的将来要在 LoopBack 中实现的要点。
由于您正在寻找已在框架中实现的解决方案,因此您发布的代码片段差不多就是这样。
我正在使用 LoopBack 4 构建一个 API。是否可以像使用实际属性一样使用关系?
API 将事件(例如音乐会)存储在数据库中的事件 table 中,将事件日期存储在 event_dates table 中。我已使用此 [1] 说明成功添加了一个与事件模型的 hasMany 关系和一个与 EventDate 模型的 belongsTo 关系(一个事件可以有多个 EventDates)。
虽然我可以使用 eventRepository.dates(eventId) 查询日期,但当我请求 http://localhost:3000/events 时却没有可用的日期 – 我如何在不询问 eventRepository.dates(eventId) 的情况下实现这一目标分开?
另一方面,我想 POST 和 PATCH 事件,而无需单独发布和修补事件日期 – 这可以通过几行代码实现吗?
这就是我现在需要使日期字段在 /events 下可用的方法(这似乎不是正确的方法):
const events = await this.eventRepository.find(filter);
for (let event of events) {
event.dates = await this.eventRepository.dates(eventId).find()
}
当我想添加一个新事件时,我需要这样做:
POST /events
POST /events/:id/event-dates
POST /events/:id/event-dates
...
请注意:我正在寻找 LoopBack 框架内已经可用的解决方案。实现这些东西不是问题,我只希望它尽可能简短和可维护。
While I can query the dates using eventRepository.dates(eventId), there aren't available when I request http://localhost:3000/events – How could I achieve this without asking eventRepository.dates(eventId) separately?
我们将此功能称为 "inclusion of related models",遗憾的是它尚未实现。您可以在此处跟踪进度:https://github.com/strongloop/loopback-next/issues/1352
On the other hand I would like to POST and PATCH events without posting and patching the event dates separately – Is this possible with a few lines of code?
如果我的理解正确,您希望通过单个 REST API 调用更新 Event
和 EventDate
实例。我们不支持该功能,老实说,我不确定我们是否会支持。
This is what I need to make the dates field available under /events right now (doesn't seem to be the right way).
您的解决方案容易受到所谓的 "SELECT 1+N PROBLEM"(参见 What is the "N+1 selects problem" in ORM (Object-Relational Mapping)?)的影响。如果您的事件有 N 个日期,那么您将进行 1+N 次数据库查询。
更好的解决方案是利用 LoopBack 的 inq
运算符:
const eventIds = events.map(e => e.id);
const dates = await this.datesRepository.find({where:{eventId:{inq: eventIds}}});
// copy "dates" entries to relevant "event" items
// by matching "dates[].eventId" against "events[].id"
如果查询的事件数量很多,那么您需要将eventIds
数组拆分成更小的块并多次调用this.datesRepository.find
。
无论如何,这就是我们在不久的将来要在 LoopBack 中实现的要点。
由于您正在寻找已在框架中实现的解决方案,因此您发布的代码片段差不多就是这样。