1

I am trying to use hasMany relationship in loopback. I have problem while using it. I have an array, which is contains to ids from target model and I want to find all elements belongs to that array and I want to list them. How can I do it?

  @property({
    type: 'array',
    itemType: 'string',
  })
  exampleArray?: string[];

  @hasMany(() => MainComponent, {keyFrom: 'exampleArray', keyTo: 'id'})
  mainComponent: MainComponent[];
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

1 Answers1

1

Hello from the LoopBack team

I have an array, which is contains to ids from target model

LoopBack's hasMany relation uses a foreign key on the target model instance to establish the relation. For example, if a Category has many Product models, then Product model must have a property linking it back to the owning category - this is typically categoryId.

IIUC your domain model, you want the source model (Category) to store the list of target model ids (Product.id) that belong to this category. In LoopBack, we call such relation type referencesMany.

LoopBack 4 does not implement referencesMany relation yet, please join the discussion in GitHub issue loopback-next#2488 or at least upvote the issue to let us know about your interest.

Miroslav Bajtoš
  • 10,667
  • 1
  • 41
  • 99