-
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.
Add `defineCrudRepositoryClass` - a helper to create named repository classes
- Loading branch information
Hage Yaapa
committed
Oct 31, 2019
1 parent
aef7b88
commit bc42658
Showing
5 changed files
with
152 additions
and
15 deletions.
There are no files selected for viewing
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
22 changes: 22 additions & 0 deletions
22
packages/rest-crud/src/__tests__/unit/define-crud-repository-class.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,22 @@ | ||
// Copyright IBM Corp. 2019. All Rights Reserved. | ||
// Node module: @loopback/rest-crud | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {Entity, model, property} from '@loopback/repository'; | ||
import {expect} from '@loopback/testlab'; | ||
import {defineCrudRepositoryClass} from '../..'; | ||
|
||
describe('defineCrudRepositoryClass', () => { | ||
it('should generate repository based on Model name', async () => { | ||
@model() | ||
class Product extends Entity { | ||
@property({id: true}) | ||
id: number; | ||
} | ||
|
||
const ProductRepository = defineCrudRepositoryClass(Product); | ||
|
||
expect(ProductRepository.name).to.equal('ProductRepository'); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright IBM Corp. 2019. All Rights Reserved. | ||
// Node module: @loopback/rest-crud | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import { | ||
DefaultCrudRepository, | ||
Entity, | ||
EntityCrudRepository, | ||
juggler, | ||
} from '@loopback/repository'; | ||
import * as assert from 'assert'; | ||
|
||
/** | ||
* Create (define) a repository class for the given model. | ||
* | ||
* Example usage: | ||
* | ||
* ```ts | ||
* const ProductRepository = defineCrudRepositoryClass(Product); | ||
* ``` | ||
* | ||
* @param modelCtor A model class, e.g. `Product`. | ||
*/ | ||
export function defineCrudRepositoryClass< | ||
T extends Entity, | ||
IdType, | ||
Relations extends object = {} | ||
>( | ||
entityClass: typeof Entity & {prototype: T}, | ||
): RepositoryClass<T, IdType, Relations> { | ||
const repoName = entityClass.name + 'Repository'; | ||
const defineNamedRepo = new Function( | ||
'EntityCtor', | ||
'BaseRepository', | ||
`return class ${repoName} extends BaseRepository { | ||
constructor(dataSource) { | ||
super(EntityCtor, dataSource); | ||
} | ||
};`, | ||
); | ||
|
||
// TODO(bajtos) make DefaultCrudRepository configurable (?) | ||
const repo = defineNamedRepo(entityClass, DefaultCrudRepository); | ||
assert.equal(repo.name, repoName); | ||
return repo; | ||
} | ||
|
||
export interface RepositoryClass< | ||
T extends Entity, | ||
IdType, | ||
Relations extends object | ||
> { | ||
new (ds: juggler.DataSource): EntityCrudRepository<T, IdType, Relations>; | ||
} |