From c016550f51f2f0db74527b96b1ad06d056496448 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 5 Mar 2020 10:03:55 -0800 Subject: [PATCH] feat(rest-crud): use functions from @loopback/repository to define repository classes --- packages/rest-crud/README.md | 14 +++-- .../default-model-crud-rest.acceptance.ts | 5 +- .../unit/define-crud-repository-class.unit.ts | 22 -------- .../rest-crud/src/crud-rest.api-builder.ts | 5 +- packages/rest-crud/src/index.ts | 4 +- packages/rest-crud/src/repository-builder.ts | 55 ------------------- 6 files changed, 17 insertions(+), 88 deletions(-) delete mode 100644 packages/rest-crud/src/__tests__/unit/define-crud-repository-class.unit.ts delete mode 100644 packages/rest-crud/src/repository-builder.ts diff --git a/packages/rest-crud/README.md b/packages/rest-crud/README.md index 77b1d4c3ec34..c0aed151ca53 100644 --- a/packages/rest-crud/README.md +++ b/packages/rest-crud/README.md @@ -64,7 +64,7 @@ class defined without the need for a repository or controller class file. If you would like more flexibility, e.g. if you would only like to define a default `CrudRest` controller or repository, you can use the two helper methods -(`defineCrudRestController` and `defineCrudRepositoryClass`) exposed from +(`defineCrudRestController` and `defineEntityCrudRepositoryClass`) exposed from `@loopback/rest-crud`. These functions will help you create controllers and respositories using code. @@ -100,20 +100,22 @@ endpoints of an existing model with a respository. ### Creating a CRUD repository -Use the `defineCrudRepositoryClass` method to create named repositories (based -on the Model) for your app. +Use the `defineEntityCrudRepositoryClass` method to create named repositories +(based on the Model) for your app. Usage example: ```ts -const ProductRepository = defineCrudRepositoryClass(Product); +import {defineEntityCrudRepositoryClass} from '@loopback/repository'; + +const ProductRepository = defineEntityCrudRepositoryClass(Product); this.repository(ProductRepository); inject('datasources.db')(ProductRepository, undefined, 0); ``` ### Integrated example -Here is an example of an app which uses `defineCrudRepositoryClass` and +Here is an example of an app which uses `defineEntityCrudRepositoryClass` and `defineCrudRestController` to fulfill its repository and controller requirements. @@ -128,7 +130,7 @@ export class TryApplication extends BootMixin( async boot(): Promise { await super.boot(); - const ProductRepository = defineCrudRepositoryClass(Product); + const ProductRepository = defineEntityCrudRepositoryClass(Product); const repoBinding = this.repository(ProductRepository); inject('datasources.db')(ProductRepository, undefined, 0); diff --git a/packages/rest-crud/src/__tests__/acceptance/default-model-crud-rest.acceptance.ts b/packages/rest-crud/src/__tests__/acceptance/default-model-crud-rest.acceptance.ts index c10b4f92bbbb..418c55e8362b 100644 --- a/packages/rest-crud/src/__tests__/acceptance/default-model-crud-rest.acceptance.ts +++ b/packages/rest-crud/src/__tests__/acceptance/default-model-crud-rest.acceptance.ts @@ -4,6 +4,7 @@ // License text available at https://opensource.org/licenses/MIT import { + defineEntityCrudRepositoryClass, Entity, EntityCrudRepository, juggler, @@ -18,7 +19,7 @@ import { givenHttpServerConfig, toJSON, } from '@loopback/testlab'; -import {defineCrudRepositoryClass, defineCrudRestController} from '../..'; +import {defineCrudRestController} from '../..'; // In this test scenario, we create a product with a required & an optional // property and use the default model settings (strict mode, forceId). @@ -295,7 +296,7 @@ describe('CrudRestController for a simple Product model', () => { async function setupTestScenario() { const db = new juggler.DataSource({connector: 'memory'}); - const ProductRepository = defineCrudRepositoryClass(Product); + const ProductRepository = defineEntityCrudRepositoryClass(Product); repo = new ProductRepository(db); diff --git a/packages/rest-crud/src/__tests__/unit/define-crud-repository-class.unit.ts b/packages/rest-crud/src/__tests__/unit/define-crud-repository-class.unit.ts deleted file mode 100644 index c8b471f7e7d6..000000000000 --- a/packages/rest-crud/src/__tests__/unit/define-crud-repository-class.unit.ts +++ /dev/null @@ -1,22 +0,0 @@ -// 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'); - }); -}); diff --git a/packages/rest-crud/src/crud-rest.api-builder.ts b/packages/rest-crud/src/crud-rest.api-builder.ts index c16625cf7e25..370537870d2b 100644 --- a/packages/rest-crud/src/crud-rest.api-builder.ts +++ b/packages/rest-crud/src/crud-rest.api-builder.ts @@ -18,12 +18,13 @@ import { import { ApplicationWithRepositories, Class, + defineEntityCrudRepositoryClass, Entity, EntityCrudRepository, } from '@loopback/repository'; import {Model} from '@loopback/rest'; import debugFactory from 'debug'; -import {defineCrudRepositoryClass, defineCrudRestController} from '.'; +import {defineCrudRestController} from '.'; const debug = debugFactory('loopback:boot:crud-rest'); @@ -86,7 +87,7 @@ function setupCrudRepository( entityClass: typeof Entity & {prototype: Entity}, config: ModelCrudRestApiConfig, ): Class> { - const repositoryClass = defineCrudRepositoryClass(entityClass); + const repositoryClass = defineEntityCrudRepositoryClass(entityClass); injectFirstConstructorArg( repositoryClass, diff --git a/packages/rest-crud/src/index.ts b/packages/rest-crud/src/index.ts index 33130144140d..482acd247ac8 100644 --- a/packages/rest-crud/src/index.ts +++ b/packages/rest-crud/src/index.ts @@ -3,7 +3,9 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +// Reexport `defineEntityCrudRepositoryClass` from `@loopback/repository` as +// `defineCrudRepositoryClass` for backward-compatibility +export {defineEntityCrudRepositoryClass as defineCrudRepositoryClass} from '@loopback/repository'; export * from './crud-rest.api-builder'; export * from './crud-rest.component'; export * from './crud-rest.controller'; -export * from './repository-builder'; diff --git a/packages/rest-crud/src/repository-builder.ts b/packages/rest-crud/src/repository-builder.ts deleted file mode 100644 index f3ea30213c76..000000000000 --- a/packages/rest-crud/src/repository-builder.ts +++ /dev/null @@ -1,55 +0,0 @@ -// 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 assert from 'assert'; - -/** - * Create (define) a repository class for the given model. - * - * @example - * - * ```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 { - 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; -}