Skip to content

Commit

Permalink
feat: add HasManyThroughFactory to Juggler bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin authored and agnes512 committed Jun 29, 2020
1 parent 413db45 commit c2d4352
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions packages/repository/src/repositories/legacy-juggler-bridge.ts
Expand Up @@ -29,9 +29,11 @@ import {
BelongsToDefinition,
createBelongsToAccessor,
createHasManyRepositoryFactory,
createHasManyThroughRepositoryFactory,
createHasOneRepositoryFactory,
HasManyDefinition,
HasManyRepositoryFactory,
HasManyThroughRepositoryFactory,
HasOneDefinition,
HasOneRepositoryFactory,
includeRelatedModels,
Expand Down Expand Up @@ -283,6 +285,62 @@ export class DefaultCrudRepository<
);
}

/**
* Function to create a constrained hasManyThrough relation repository factory
*
* @example
* ```ts
* class CustomerRepository extends DefaultCrudRepository<
* Customer,
* typeof Customer.prototype.id,
* CustomerRelations
* > {
* public readonly cartItems: HasManyRepositoryFactory<CartItem, typeof Customer.prototype.id>;
*
* constructor(
* protected db: juggler.DataSource,
* cartItemRepository: EntityCrudRepository<CartItem, typeof, CartItem.prototype.id>,
* throughRepository: EntityCrudRepository<Through, typeof Through.prototype.id>,
* ) {
* super(Customer, db);
* this.cartItems = this.createHasManyThroughRepositoryFactoryFor(
* 'cartItems',
* cartItemRepository,
* );
* }
* }
* ```
*
* @param relationName - Name of the relation defined on the source model
* @param targetRepo - Target repository instance
* @param throughRepo - Through repository instance
*/
protected createHasManyThroughRepositoryFactoryFor<
Target extends Entity,
TargetID,
Through extends Entity,
ThroughID,
ForeignKeyType
>(
relationName: string,
targetRepoGetter: Getter<EntityCrudRepository<Target, TargetID>>,
throughRepoGetter: Getter<EntityCrudRepository<Through, ThroughID>>,
): HasManyThroughRepositoryFactory<
Target,
TargetID,
Through,
ForeignKeyType
> {
const meta = this.entityClass.definition.relations[relationName];
return createHasManyThroughRepositoryFactory<
Target,
TargetID,
Through,
ThroughID,
ForeignKeyType
>(meta as HasManyDefinition, targetRepoGetter, throughRepoGetter);
}

/**
* @deprecated
* Function to create a belongs to accessor
Expand Down

0 comments on commit c2d4352

Please sign in to comment.