diff --git a/packages/boot/src/__tests__/acceptance/crud-rest-builder.acceptance.ts b/packages/boot/src/__tests__/acceptance/crud-rest-builder.acceptance.ts index 00ee00e7d572..f9ed3b0b3bdb 100644 --- a/packages/boot/src/__tests__/acceptance/crud-rest-builder.acceptance.ts +++ b/packages/boot/src/__tests__/acceptance/crud-rest-builder.acceptance.ts @@ -6,14 +6,10 @@ import {ApplicationConfig} from '@loopback/core'; import {juggler, RepositoryMixin} from '@loopback/repository'; import {RestApplication} from '@loopback/rest'; -import { - CrudRestComponent, - defineCrudRepositoryClass, -} from '@loopback/rest-crud'; +import {CrudRestComponent} from '@loopback/rest-crud'; import {expect, givenHttpServerConfig, TestSandbox} from '@loopback/testlab'; import {resolve} from 'path'; import {BootMixin, ModelApiBooter} from '../..'; -import {Product} from '../fixtures/product.model'; describe('CRUD rest builder acceptance tests', () => { let app: BooterApp; @@ -57,42 +53,6 @@ module.exports = { ); }); - it('uses bound repository class if it exists', async () => { - await sandbox.copyFile( - resolve(__dirname, '../fixtures/product.model.js'), - 'models/product.model.js', - ); - - await sandbox.writeTextFile( - 'model-endpoints/product.rest-config.js', - ` -const {Product} = require('../models/product.model'); -module.exports = { - model: Product, - pattern: 'CrudRest', - dataSource: 'db', - basePath: '/products', -}; - `, - ); - - const ProductRepository = defineCrudRepositoryClass(Product); - app.repository(ProductRepository); - - const binding = app.getBinding('repositories.ProductRepository'); - expect(binding.key).to.eql('repositories.ProductRepository'); - - // Boot & start the application - await app.boot(); - await app.start(); - - expect(app.getBinding('repositories.ProductRepository')).to.eql(binding); - - expect(app.getBinding('controllers.ProductController').key).to.eql( - 'controllers.ProductController', - ); - }); - it('throws if there is no base path in the config', async () => { await sandbox.copyFile( resolve(__dirname, '../fixtures/product.model.js'), @@ -138,7 +98,7 @@ module.exports = { // Boot the application await expect(app.boot()).to.be.rejectedWith( - /CrudRestController requires an Entity, Models are not supported/, + /CrudRestController requires a model that extends 'Entity'./, ); }); diff --git a/packages/rest-crud/README.md b/packages/rest-crud/README.md index f05849a39777..769b1a9e21df 100644 --- a/packages/rest-crud/README.md +++ b/packages/rest-crud/README.md @@ -40,7 +40,8 @@ export class TryApplication extends BootMixin( ``` Create a new file for the configuration, e.g. -`src/model-endpoints/product.rest-config.ts`: +`src/model-endpoints/product.rest-config.ts` that defines the `model`, +`pattern`, `dataSource`, and `basePath` properties: ```ts import {ModelCrudRestApiConfig} from '@loopback/rest-crud'; diff --git a/packages/rest-crud/src/crud-rest-builder.plugin.ts b/packages/rest-crud/src/crud-rest-builder.plugin.ts index 79bd45eaaa2d..06bfb942491b 100644 --- a/packages/rest-crud/src/crud-rest-builder.plugin.ts +++ b/packages/rest-crud/src/crud-rest-builder.plugin.ts @@ -45,21 +45,20 @@ export class CrudRestApiBuilder implements ModelApiBuilder { if (!(modelClass.prototype instanceof Entity)) { throw new Error( - `CrudRestController requires an Entity, Models are not supported. (Model name: ${modelName})`, + `CrudRestController requires a model that extends 'Entity'. (Model name ${modelName} does not extend 'Entity')`, ); } const entityClass = modelClass as typeof Entity & {prototype: Entity}; let repoClassName = entityClass.name + 'Repository'; - try { - application.getBinding('repositories.' + repoClassName); + if (application.isBound('repositories.' + repoClassName)) { debug( 'Using repository class', repoClassName, ', as it is already bound to application', ); - } catch { + } else { // repository class does not exist const repositoryClass = setupCrudRepository(entityClass, config); application.repository(repositoryClass); @@ -79,19 +78,15 @@ export class CrudRestApiBuilder implements ModelApiBuilder { * Set up a CRUD Repository class for the given Entity class. * * @param entityClass - the Entity class the repository is built for - * @param modelConfig - configuration of the Entity class + * @param config - configuration of the Entity class */ function setupCrudRepository( entityClass: typeof Entity & {prototype: Entity}, - modelConfig: ModelCrudRestApiConfig, + config: ModelCrudRestApiConfig, ): Class> { const repositoryClass = defineCrudRepositoryClass(entityClass); - inject(`datasources.${modelConfig.dataSource}`)( - repositoryClass, - undefined, - 0, - ); + inject(`datasources.${config.dataSource}`)(repositoryClass, undefined, 99); return repositoryClass; } @@ -100,17 +95,17 @@ function setupCrudRepository( * Set up a CRUD Controller class for the given Entity class. * * @param entityClass - the Entity class the controller is built for - * @param modelConfig - configuration of the Entity class + * @param config - configuration of the Entity class */ function setupCrudRestController( entityClass: typeof Entity & {prototype: Entity}, - modelConfig: ModelCrudRestApiConfig, + config: ModelCrudRestApiConfig, ): ControllerClass { const controllerClass = defineCrudRestController( entityClass, // important - forward the entire config object to allow controller // factories to accept additional (custom) config options - modelConfig, + config, ); inject(`repositories.${entityClass.name}Repository`)(