I am building an API using LoopBack 4. Is it possible to use relations as if they were actual properties?
The API stores events (e.g. concerts) in the events table in the database and event dates in the event_dates table. I have successfully added a hasMany relation to the Event model and a belongsTo relation to the EventDate model (one Event can have multiple EventDates) using this [1] instructions.
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?
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?
This is what I need to make the dates field available under /events right now (doesn't seem to be the right way):
const events = await this.eventRepository.find(filter);
for (let event of events) {
event.dates = await this.eventRepository.dates(eventId).find()
}
When I want to add a new event, I need to do this:
POST /events
POST /events/:id/event-dates
POST /events/:id/event-dates
...
Please note: I'm looking for solutions that are already available within the LoopBack framework. Implementing these things are not the problem, I just want this as short and maintainable as possible.