-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Comments
When we will have a fix for it? |
I modified the @hasMany(() => Todo, {keyTo: 'parentId'})
todos?: Todo[];
@belongsTo(() => Todo)
parentId?: number; And the 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 {
"id": 5,
"title": "parent"
} Then tried adding a child {
"id": 6,
"title": "child",
"parentId": 5
} Then when I tried getting the parent {
"id": 5,
"title": "parent"
} However, if I try [result from {
// ...
{
"id": 5,
"title": "parent"
},
{
"id": 6,
"title": "child",
"parentId": 5
}
} @b-admike thoughts? Did I miss anything? |
@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. |
Going to close this spike as the error couldn't be reproduced and leave adding a test as part of the original issue #1571. |
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
Timebox it to 2 days
The text was updated successfully, but these errors were encountered: