-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(repository): add repo factory and tests for hasManyThrough #5606
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
...es/repository/src/__tests__/unit/repositories/has-many-through-repository-factory.unit.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/repository | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {createStubInstance, expect} from '@loopback/testlab'; | ||
| import { | ||
| DefaultCrudRepository, | ||
| Entity, | ||
| Getter, | ||
| juggler, | ||
| model, | ||
| property, | ||
| } from '../../..'; | ||
| import {createHasManyThroughRepositoryFactory} from '../../../relations/has-many/has-many-through-repository.factory'; | ||
| import {HasManyThroughResolvedDefinition} from '../../../relations/has-many/has-many-through.helpers'; | ||
|
|
||
| describe('createHasManyThroughRepositoryFactory', () => { | ||
| let categoryProductLinkRepo: CategoryProductLinkRepository; | ||
| let productRepo: ProductRepository; | ||
|
|
||
| beforeEach(() => { | ||
| givenStubbedProductRepo(); | ||
| givenStubbedCategoryProductLinkRepo(); | ||
| }); | ||
|
|
||
| it('should return a function that could create hasManyThrough repository', () => { | ||
| const relationMeta = resolvedMetadata as HasManyThroughResolvedDefinition; | ||
| const result = createHasManyThroughRepositoryFactory( | ||
| relationMeta, | ||
| Getter.fromValue(productRepo), | ||
| Getter.fromValue(categoryProductLinkRepo), | ||
| ); | ||
| expect(result).to.be.Function(); | ||
| }); | ||
|
|
||
| /*------------- HELPERS ---------------*/ | ||
|
|
||
| @model() | ||
| class Category extends Entity { | ||
| @property({id: true}) | ||
| id: number; | ||
|
|
||
| constructor(data: Partial<Category>) { | ||
| super(data); | ||
| } | ||
| } | ||
|
|
||
| @model() | ||
| class Product extends Entity { | ||
| @property({id: true}) | ||
| id: number; | ||
|
|
||
| constructor(data: Partial<Product>) { | ||
| super(data); | ||
| } | ||
| } | ||
|
|
||
| @model() | ||
| class CategoryProductLink extends Entity { | ||
| @property({id: true}) | ||
| id: number; | ||
| @property() | ||
| categoryId: number; | ||
| @property() | ||
| productId: number; | ||
|
|
||
| constructor(data: Partial<Product>) { | ||
| super(data); | ||
| } | ||
| } | ||
|
|
||
| class ProductRepository extends DefaultCrudRepository< | ||
| Product, | ||
| typeof Product.prototype.id | ||
| > { | ||
| constructor(dataSource: juggler.DataSource) { | ||
| super(Product, dataSource); | ||
| } | ||
| } | ||
|
|
||
| class CategoryProductLinkRepository extends DefaultCrudRepository< | ||
| CategoryProductLink, | ||
| typeof CategoryProductLink.prototype.id | ||
| > { | ||
| constructor(dataSource: juggler.DataSource) { | ||
| super(CategoryProductLink, dataSource); | ||
| } | ||
| } | ||
|
|
||
| const resolvedMetadata = { | ||
| name: 'products', | ||
| type: 'hasMany', | ||
| targetsMany: true, | ||
| source: Category, | ||
| keyFrom: 'id', | ||
| target: () => Product, | ||
| keyTo: 'id', | ||
| through: { | ||
| model: () => CategoryProductLink, | ||
| keyFrom: 'categoryId', | ||
| keyTo: 'productId', | ||
| }, | ||
| }; | ||
|
|
||
| function givenStubbedProductRepo() { | ||
| productRepo = createStubInstance(ProductRepository); | ||
| } | ||
|
|
||
| function givenStubbedCategoryProductLinkRepo() { | ||
| categoryProductLinkRepo = createStubInstance(CategoryProductLinkRepository); | ||
| } | ||
| }); |
93 changes: 93 additions & 0 deletions
93
packages/repository/src/relations/has-many/has-many-through-repository.factory.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/repository | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import { | ||
| DataObject, | ||
| Entity, | ||
| EntityCrudRepository, | ||
| Getter, | ||
| HasManyDefinition, | ||
| } from '../..'; | ||
| import { | ||
| createTargetConstraint, | ||
| createThroughConstraint, | ||
| createThroughFkConstraint, | ||
| resolveHasManyThroughMetadata, | ||
| } from './has-many-through.helpers'; | ||
| import { | ||
| DefaultHasManyThroughRepository, | ||
| HasManyThroughRepository, | ||
| } from './has-many-through.repository'; | ||
|
|
||
| /** | ||
| * a factory to generate hasManyThrough repository class. | ||
| * | ||
| * Warning: The hasManyThrough interface is experimental and is subject to change. | ||
| * If backwards-incompatible changes are made, a new major version may not be | ||
| * released. | ||
| */ | ||
|
|
||
| export type HasManyThroughRepositoryFactory< | ||
| TargetEntity extends Entity, | ||
| TargetID, | ||
| ThroughEntity extends Entity, | ||
| ForeignKeyType | ||
| > = ( | ||
| fkValue: ForeignKeyType, | ||
| ) => HasManyThroughRepository<TargetEntity, TargetID, ThroughEntity>; | ||
|
|
||
| export function createHasManyThroughRepositoryFactory< | ||
| Target extends Entity, | ||
| TargetID, | ||
| Through extends Entity, | ||
| ThroughID, | ||
| ForeignKeyType | ||
| >( | ||
| relationMetadata: HasManyDefinition, | ||
| targetRepositoryGetter: Getter<EntityCrudRepository<Target, TargetID>>, | ||
| throughRepositoryGetter: Getter<EntityCrudRepository<Through, ThroughID>>, | ||
| ): HasManyThroughRepositoryFactory<Target, TargetID, Through, ForeignKeyType> { | ||
| const meta = resolveHasManyThroughMetadata(relationMetadata); | ||
| const result = function (fkValue: ForeignKeyType) { | ||
| function getTargetContraint( | ||
| throughInstances: Through | Through[], | ||
| ): DataObject<Target> { | ||
| return createTargetConstraint<Target, Through>(meta, throughInstances); | ||
| } | ||
| function getThroughConstraint(): DataObject<Through> { | ||
| const constriant: DataObject<Through> = createThroughConstraint< | ||
| Through, | ||
| ForeignKeyType | ||
| >(meta, fkValue); | ||
| return constriant; | ||
| } | ||
|
|
||
| function getThroughFkConstraint( | ||
| targetInstance: Target, | ||
| ): DataObject<Through> { | ||
| const constriant: DataObject<Through> = createThroughFkConstraint< | ||
| Target, | ||
| Through | ||
| >(meta, targetInstance); | ||
| return constriant; | ||
| } | ||
|
|
||
| return new DefaultHasManyThroughRepository< | ||
| Target, | ||
| TargetID, | ||
| EntityCrudRepository<Target, TargetID>, | ||
| Through, | ||
| ThroughID, | ||
| EntityCrudRepository<Through, ThroughID> | ||
| >( | ||
| targetRepositoryGetter, | ||
| throughRepositoryGetter, | ||
| getTargetContraint, | ||
| getThroughConstraint, | ||
| getThroughFkConstraint, | ||
| ); | ||
| }; | ||
| return result; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need a new function? Have you considered to extend the existing
createHasManyRepositoryFactoryto support both "direct" and "through" variants? Maybe that's not a good idea, I just wanted to bring it forward for consideration.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point... I considered a bit. Since we only have one hasMany type, I think it might be more consistent if we only expose
createHasManyRepositoryFactoryas the entry for hasMany* relations, and letcreateHasManyRepositoryFactoryto decide which factory to create.Edit: I will leave it to the future improvements.