Skip to content

Commit

Permalink
fixup! more feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Feb 17, 2020
1 parent abfb0d5 commit 5aef80d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ describe('CRUD rest builder acceptance tests', () => {
'models/product.model.js',
);

// when creating the config file in a real app, make sure to use
// module.exports = <ModelCrudRestApiConfig>{...}
// it's not used here because this is a .js file
await sandbox.writeTextFile(
'model-endpoints/product.rest-config.js',
`
Expand Down Expand Up @@ -67,6 +70,7 @@ module.exports = {
model: Product,
pattern: 'CrudRest',
dataSource: 'db',
// basePath not specified
};
`,
);
Expand All @@ -88,6 +92,7 @@ module.exports = {
`
const {NoEntity} = require('../models/no-entity.model');
module.exports = {
// this model extends Model, not Entity
model: NoEntity,
pattern: 'CrudRest',
dataSource: 'db',
Expand Down Expand Up @@ -119,10 +124,7 @@ module.exports = {
}

async function stopApp() {
try {
await app.stop();
} catch (err) {
// application is booting
}
if (app.state !== 'started') return;
await app.stop();
}
});
14 changes: 9 additions & 5 deletions packages/rest-crud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ npm install --save @loopback/rest-crud
## Basic use

`@loopback/rest-crud` can be used along with `@loopback/model-api-builder` to
easily create a repository class and a controller class for your model.
easily create a repository class and a controller class for your model. The
following use is a simple approach for this creation, however, you can look at
the "Advanced use" section instead for a more flexible approach.

For the examples in the following sections, we are assuming a model named
`Product` and a datasource named `db` have already been created.
Expand Down Expand Up @@ -49,7 +51,7 @@ import {Product} from '../models';

module.exports = <ModelCrudRestApiConfig>{
model: Product,
pattern: 'CrudRest',
pattern: 'CrudRest', // make sure to use this pattern
dataSource: 'db',
basePath: '/products',
};
Expand All @@ -60,9 +62,11 @@ class defined without the need for a repository or controller class file.

## Advanced use

`@loopback/rest-crud` also exposes two helper methods
(`defineCrudRestController` and `defineCrudRepositoryClass`) for creating
controllers and respositories using code.
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
`@loopback/rest-crud`. These functions will help you create controllers and
respositories using code.

For the examples in the following sections, we are also assuming a model named
`Product`, and a datasource named `db` have already been created.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {bind, ControllerClass, inject} from '@loopback/core';
import {
bind,
BindingSelector,
Constructor,
ControllerClass,
inject,
} from '@loopback/core';
import {
asModelApiBuilder,
ModelApiBuilder,
Expand Down Expand Up @@ -86,7 +92,10 @@ function setupCrudRepository(
): Class<EntityCrudRepository<Entity, unknown>> {
const repositoryClass = defineCrudRepositoryClass(entityClass);

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

return repositoryClass;
}
Expand All @@ -108,11 +117,28 @@ function setupCrudRestController(
config,
);

inject(`repositories.${entityClass.name}Repository`)(
injectFirstConstructorArg(
controllerClass,
undefined,
0,
`repositories.${entityClass.name}Repository`,
);

return controllerClass;
}

/**
* Inject given key into a given class constructor
*
* @param ctor - constructor for a class (e.g. a controller class)
* @param key - binding to use in order to resolve the value of the decorated
* constructor parameter or property
*/
function injectFirstConstructorArg<T>(
ctor: Constructor<T>,
key: BindingSelector,
) {
inject(key)(
ctor,
undefined /* constructor member */,
0 /* the first argument */,
);
}
2 changes: 1 addition & 1 deletion packages/rest-crud/src/crud-rest.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {Component, createBindingFromClass} from '@loopback/core';
import {CrudRestApiBuilder} from './crud-rest-builder.plugin';
import {CrudRestApiBuilder} from './crud-rest.api-builder';

export class CrudRestComponent implements Component {
bindings = [createBindingFromClass(CrudRestApiBuilder)];
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-crud/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './crud-rest-builder.plugin';
export * from './crud-rest.api-builder';
export * from './crud-rest.component';
export * from './crud-rest.controller';
export * from './repository-builder';

0 comments on commit 5aef80d

Please sign in to comment.