Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spike] HasMany relation to same Entity #1682

Closed
jannyHou opened this issue Sep 7, 2018 · 4 comments
Closed

[Spike] HasMany relation to same Entity #1682

jannyHou opened this issue Sep 7, 2018 · 4 comments
Assignees
Labels
Relations Model relations (has many, etc.)

Comments

@jannyHou
Copy link
Contributor

jannyHou commented Sep 7, 2018

Description / Steps to reproduce / Feature proposal

This is a spike story for #1571. Based on the discussion in the original story, we need a quick spike to

  • Create a test case to mock the scenario - Entity creates a hasMany relation to itself.
  • See if it fails. If yes:
    • Troubleshoot the reason, e.g. infinite dereference of circular dependencies or other reasons.
    • Discover the gap between the potential fix and the current code.

Timebox it to 2 days

@danysz
Copy link

danysz commented Jan 15, 2019

When we will have a fix for it?

@nabdelgadir
Copy link
Contributor

nabdelgadir commented Feb 18, 2019

I modified the Todo model by adding the following two properties:

@hasMany(() => Todo, {keyTo: 'parentId'})
todos?: Todo[];

@belongsTo(() => Todo)
parentId?: number;

And the Todo repository as follows:

import {
  DefaultCrudRepository,
  juggler,
  HasManyRepositoryFactory,
  BelongsToAccessor,
} from '@loopback/repository';
import {Todo} from '../models';
import {inject, Getter} from '@loopback/core';

export class TodoRepository extends DefaultCrudRepository<
  Todo,
  typeof Todo.prototype.id
> {
  // EDITED HERE
  public readonly todos: HasManyRepositoryFactory<
    Todo,
    typeof Todo.prototype.id
  >;
  public readonly parentId: BelongsToAccessor<Todo, typeof Todo.prototype.id>;

  constructor(@inject('datasources.db') dataSource: juggler.DataSource) {
    super(Todo, dataSource);
    // EDITED HERE
    this.parentId = this.createBelongsToAccessorFor(
      'parentId',
      Getter.fromValue(this),
    );
    this.todos = this.createHasManyRepositoryFactoryFor(
      'todos',
      Getter.fromValue(this),
    );
  }
}

And added the following two endpoints to the controller:

@post('/todos/{id}/todo')
async createTodo(
  @param.path.number('id') todoId: typeof Todo.prototype.id,
  @requestBody() todoData: Todo,
): Promise<Todo> {
  return await this.todoRepo.todos(todoId).create(todoData);
}

@get('/todos/{id}/parentId')
async getTodo(
  @param.path.number('id') todoId: typeof Todo.prototype.id,
): Promise<Todo> {
  return await this.todoRepo.parentId(todoId);
}

Then tried creating a parent Todo with only a title property [POST /todos]:

{
  "id": 5,
  "title": "parent"
}

Then tried adding a child Todo via POST /todos/{id}/todo:

{
  "id": 6,
  "title": "child",
  "parentId": 5
}

Then when I tried getting the parent Todo via GET /todos/6/parentId:

{
  "id": 5,
  "title": "parent"
}

However, if I try GET todos/5/, it doesn't include the child Todo (I assume this will be available when partial updates are supported), but if I try GET todos/6/ then I do get the parentId:

[result from GET /todos/]

{
  // ...
  {
    "id": 5,
    "title": "parent"
  },
  {
    "id": 6,
    "title": "child",
    "parentId": 5
  }
}

@b-admike thoughts? Did I miss anything?

@b-admike b-admike assigned nabdelgadir and unassigned b-admike Feb 19, 2019
@b-admike
Copy link
Contributor

@nabdelgadir That looks good to me. Thank you for verifying! We can now capture this scenario in a test case in https://github.com/strongloop/loopback-next/blob/master/packages/repository/src/__tests__/acceptance/has-many.relation.acceptance.ts.

@nabdelgadir
Copy link
Contributor

Going to close this spike as the error couldn't be reproduced and leave adding a test as part of the original issue #1571.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Relations Model relations (has many, etc.)
Projects
None yet
Development

No branches or pull requests

5 participants