Skip to content

Commit

Permalink
fix(rest-crud): use given repository class if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Feb 7, 2020
1 parent 423e0fa commit bcc3000
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import {ApplicationConfig} from '@loopback/core';
import {juggler, RepositoryMixin} from '@loopback/repository';
import {RestApplication} from '@loopback/rest';
import {CrudRestComponent} from '@loopback/rest-crud';
import {
CrudRestComponent,
defineCrudRepositoryClass,
} 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 @@ -53,6 +57,42 @@ 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
21 changes: 16 additions & 5 deletions packages/rest-crud/src/crud-rest-builder.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,22 @@ export class CrudRestApiBuilder implements ModelApiBuilder {
}
const entityClass = modelClass as typeof Entity & {prototype: Entity};

// TODO Check if the repository class has been already defined.
// If yes, then skip creation of the default repository
const repositoryClass = setupCrudRepository(entityClass, config);
application.repository(repositoryClass);
debug('Registered repository class', repositoryClass.name);
let repoClassName = entityClass.name + 'Repository';

try {
application.getBinding('repositories.' + repoClassName);
debug(
'Using repository class',
repoClassName,
', as it is already bound to application',
);
} catch {
// repository class does not exist
const repositoryClass = setupCrudRepository(entityClass, config);
application.repository(repositoryClass);
repoClassName = repositoryClass.name;
debug('Registered repository class', repoClassName);
}

const controllerClass = setupCrudRestController(entityClass, config);
application.controller(controllerClass);
Expand Down

0 comments on commit bcc3000

Please sign in to comment.