loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.95k stars 1.07k forks source link

Spike: Resolver for inclusion of related models #2634

Closed bajtos closed 5 years ago

bajtos commented 5 years ago

Continue the works started in #2124, leverage the building blocks implemented as an outcome of #2592 and create a detailed proposal for implementing resolver for navigational properties.

Implementation wise, I think we want to end up with the following user experience:

export class TodoListRepository extends DefaultCrudRepository<
  TodoList,
  typeof TodoList.prototype.id
> {
  public readonly todos: HasManyRepositoryFactory<
    Todo,
    typeof TodoList.prototype.id
  >;

  constructor(
    @inject('datasources.db') dataSource: juggler.DataSource,
    @repository.getter(TodoRepository)
    protected todoRepositoryGetter: Getter<TodoRepository>,
  ) {
    super(TodoList, dataSource);
    this.todos = this._createHasManyRepositoryFactoryFor(
      'todos',
      todoRepositoryGetter,
    );
   ////// THE FOLLOWING LINE IS NEWLY ADDED
   this._registerHasManyInclusion('todos', todoRepositoryGetter);
  }
}

The question is how to implement _register**Inclusion APIs and process the registered inclusion handlers in find/findById implementations provided by DefaultCrudRepository. See https://github.com/strongloop/loopback-next/issues/1352#issuecomment-436197152 for more details on that.

It is important to consider the scenario where we want to fetch multiple source models and include their related models in the result, e.g. TodoList.find(1, {include: [{relation: 'todos'}]}). Under the hood, we must avoid SELECT N+1 issue. Inclusion handler must be able to fetch related models for multiple source-model ids in one query (using inq operator under the hood). The design proposed in https://github.com/strongloop/loopback-next/issues/1352#issuecomment-436197152 is already taking that into account, so does the existing implementation in juggler.

Acceptance criteria

AnanthGopal commented 5 years ago

Hi @bajtos

this._registerHasManyInclusion('todos', todoRepositoryGetter);

When I add this code I got the error "_registerHasManyInclusion" it's not defined. Please provide the full function for this _registerHasManyInclusion

shadyanwar commented 5 years ago

@AnanthGopal this is a spike. The feature is still being worked on and is not implemented or released yet.

bajtos commented 5 years ago

MVP scope

Post-MVP