From c498c646512c730ab1ca35b71450b3ba447e6ab0 Mon Sep 17 00:00:00 2001 From: Hage Yaapa Date: Fri, 4 Oct 2019 18:03:23 +0530 Subject: [PATCH] feat: add defineCrudRepositoryClass Add `defineCrudRepositoryClass` - a helper to create named repository classes --- .../default-model-crud-rest.acceptance.ts | 11 +++-- .../unit/define-crud-repository-class.unit.ts | 22 ++++++++++ packages/rest-crud/src/index.ts | 1 + packages/rest-crud/src/repository-builder.ts | 44 +++++++++++++++++++ 4 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 packages/rest-crud/src/__tests__/unit/define-crud-repository-class.unit.ts create mode 100644 packages/rest-crud/src/repository-builder.ts 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 3167ff0a746b..585dd00217c9 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,7 +4,6 @@ // License text available at https://opensource.org/licenses/MIT import { - DefaultCrudRepository, Entity, EntityCrudRepository, juggler, @@ -19,7 +18,7 @@ import { givenHttpServerConfig, toJSON, } from '@loopback/testlab'; -import {defineCrudRestController} from '../..'; +import {defineCrudRepositoryClass, 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). @@ -283,10 +282,10 @@ describe('CrudRestController for a simple Product model', () => { async function setupTestScenario() { const db = new juggler.DataSource({connector: 'memory'}); - repo = new DefaultCrudRepository( - Product, - db, - ); + + const ProductRepository = defineCrudRepositoryClass(Product); + + repo = new ProductRepository(db); const CrudRestController = defineCrudRestController< Product, 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 new file mode 100644 index 000000000000..c8d0477b5c21 --- /dev/null +++ b/packages/rest-crud/src/__tests__/unit/define-crud-repository-class.unit.ts @@ -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('defineCrudRestController', () => { + 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/index.ts b/packages/rest-crud/src/index.ts index a099ac3a456c..4a6f68f068e8 100644 --- a/packages/rest-crud/src/index.ts +++ b/packages/rest-crud/src/index.ts @@ -4,3 +4,4 @@ // License text available at https://opensource.org/licenses/MIT 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 new file mode 100644 index 000000000000..70aa651ebbb8 --- /dev/null +++ b/packages/rest-crud/src/repository-builder.ts @@ -0,0 +1,44 @@ +// 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'; + +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; +}