Page Contents

Home > @loopback/sequelize > SequelizeCrudRepository > createHasManyThroughRepositoryFactoryFor

SequelizeCrudRepository.createHasManyThroughRepositoryFactoryFor() method

Function to create a constrained hasManyThrough relation repository factory

Signature:

protected createHasManyThroughRepositoryFactoryFor<Target extends Entity, TargetID, Through extends Entity, ThroughID, ForeignKeyType>(relationName: string, targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetID>> | {
        [repoType: string]: Getter<EntityCrudRepository<Target, TargetID>>;
    }, throughRepositoryGetter: Getter<EntityCrudRepository<Through, ThroughID>>): HasManyThroughRepositoryFactory<Target, TargetID, Through, ForeignKeyType>;

Parameters

Parameter Type Description
relationName string Name of the relation defined on the source model
targetRepositoryGetter Getter<EntityCrudRepository<Target, TargetID>> | { [repoType: string]: Getter<EntityCrudRepository<Target, TargetID>>; }  
throughRepositoryGetter Getter<EntityCrudRepository<Through, ThroughID>>  

Returns:

HasManyThroughRepositoryFactory<Target, TargetID, Through, ForeignKeyType>

Example

class CustomerRepository extends SequelizeCrudRepository<
  Customer,
  typeof Customer.prototype.id,
  CustomerRelations
> {
  public readonly cartItems: HasManyRepositoryFactory<CartItem, typeof Customer.prototype.id>;

  constructor(
    protected db: SequelizeDataSource,
    cartItemRepository: EntityCrudRepository<CartItem, typeof, CartItem.prototype.id>,
    throughRepository: EntityCrudRepository<Through, typeof Through.prototype.id>,
  ) {
    super(Customer, db);
    this.cartItems = this.createHasManyThroughRepositoryFactoryFor(
      'cartItems',
      cartItemRepository,
    );
  }
}