-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(repository): add hasManyThrough factory and tests
Co-authored-by: derdeka https://github.com/derdeka Co-authored-by: Jam https://github.com/codejamninja
- Loading branch information
Showing
4 changed files
with
307 additions
and
5 deletions.
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 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 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 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 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