Skip to content

Commit

Permalink
fixup! feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Feb 13, 2020
1 parent 3246061 commit 0b75cca
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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'./,
);
});

Expand Down
3 changes: 2 additions & 1 deletion packages/rest-crud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
23 changes: 9 additions & 14 deletions packages/rest-crud/src/crud-rest-builder.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<EntityCrudRepository<Entity, unknown>> {
const repositoryClass = defineCrudRepositoryClass(entityClass);

inject(`datasources.${modelConfig.dataSource}`)(
repositoryClass,
undefined,
0,
);
inject(`datasources.${config.dataSource}`)(repositoryClass, undefined, 99);

return repositoryClass;
}
Expand All @@ -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`)(
Expand Down

0 comments on commit 0b75cca

Please sign in to comment.