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

Retrieval of related model for a HasMany relation #1372

Closed
2 tasks done
b-admike opened this issue May 29, 2018 · 6 comments
Closed
2 tasks done

Retrieval of related model for a HasMany relation #1372

b-admike opened this issue May 29, 2018 · 6 comments
Assignees

Comments

@b-admike
Copy link
Contributor

b-admike commented May 29, 2018

Description

Follow up for #1342. Given Customer and Order models, and a customer that has orders already, I would like to define a HasMany relation and

  • find all the orders for said customer in a controller method
  • create an order for said customer in a controller method

Acceptance Criteria

  • Infer relation metadata from the hasMany decorator to construct constrained target repository instance on relational property
  • Create a calculated property on the source repository (CustomerRepository in this case) which returns a constrained target repository (for e.g. customerRepo.<decorated_property_name or relation_name>.update(...)) using the relation metadata and supplied constraint in the controller method

Code snippet for UX:

export class CustomerController {
  constructor(
    @repository(CustomerRepository) protected customerRepo: CustomerRepository,
    @repository(OrderRepository) protected orderRepo: OrderRepository
  ) {}

  async function getCustomerOrders(customerId: number): Promise<Order[]> {
    return await customerRepo.orders({id: customerId}).find();
  }

 async function createCustomerOrders(customerId: number, orderData: Partial<Order>): Promise<Order> {
    return await customerRepo.orders({id: customerId}).create(orderData);
  }
  }
export class CustomerRepository {
  // We should be able to inject a factory of OrderRepository by name or instance
  // and infer `Order` from the `OrderRepository`
  @hasMany('orderRepository');  // The argument can be an object to pass in more info
  public readonly orders: (key: Partial<Customer>) => OrderRepository;
  }

See Reporting Issues for more tips on writing good issues

@b-admike b-admike added the DP3 label May 29, 2018
@b-admike b-admike self-assigned this May 29, 2018
@dhmlau
Copy link
Member

dhmlau commented May 29, 2018

It corresponds to the first 2 points from #1032 (comment):

  1. HasMany with a single operation find, covering both repository/persistence and REST API in a controller method.
  2. Add create method to repository/persistence layer and expose it in REST API as a new controller method.

@dhmlau dhmlau added this to the June Milestone milestone May 29, 2018
@raymondfeng
Copy link
Contributor

raymondfeng commented May 29, 2018

Here is what I have in mind:

export class CustomerRepository {
  // We should be able to inject a factory of OrderRepository by name or instance
  // and infer `Order` from the `OrderRepository`
  @hasMany('orderRepository');  // The argument can be an object to pass in more info
  public readonly orders: (key: Partial<Customer>) => OrderRepository;
  }
async function getCustomerOrders(customerId: number): Promise<Order[]> {
    return await customerRepo.orders({id: customerId}).find();
}

@jannyHou
Copy link
Contributor

@b-admike a question for the code snippet:

export class CustomerController {
  constructor(
    @repository(CustomerRepository) protected customerRepo: CustomerRepository,
    @repository(OrderRepository) protected orderRepo: OrderRepository
  ) {}

  async function getCustomerOrders(customerId: number): Promise<Order[]> {
    return await customerRepo.orders.find(customerId, orderRepo);
  }

 async function createCustomerOrders(customerId: number, orderData: Partial<Order>): Promise<Order> {
    return await customerRepo.orders.create(customerId, orderData, orderRepo);
  }
  }

Does it mean we automatically get the related property customerRepo.orders by:

  • add relation in model def of Customer and Order
  • instintiate customerRepo and orderRepo in the controller class's constructor
    ?

@b-admike
Copy link
Contributor Author

Does it mean we automatically get the related property customerRepo.orders by:
add relation in model def of Customer and Order
instintiate customerRepo and orderRepo in the controller class's constructor?

@jannyHou yeah that's correct.

@bajtos
Copy link
Member

bajtos commented Jun 5, 2018

See also #1353

@b-admike
Copy link
Contributor Author

b-admike commented Jun 28, 2018

Closing as #1438 has landed.

@bajtos bajtos added the p1 label Aug 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants