From 5aef80d5845f2ecbc5756726f47c05ce36841073 Mon Sep 17 00:00:00 2001 From: Nora Date: Mon, 17 Feb 2020 01:34:41 -0500 Subject: [PATCH] fixup! more feedback --- .../crud-rest-builder.acceptance.ts | 12 ++++--- packages/rest-crud/README.md | 14 +++++--- ...der.plugin.ts => crud-rest.api-builder.ts} | 36 ++++++++++++++++--- packages/rest-crud/src/crud-rest.component.ts | 2 +- packages/rest-crud/src/index.ts | 2 +- 5 files changed, 49 insertions(+), 17 deletions(-) rename packages/rest-crud/src/{crud-rest-builder.plugin.ts => crud-rest.api-builder.ts} (82%) 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 f9ed3b0b3bdb..9a559e299f20 100644 --- a/packages/boot/src/__tests__/acceptance/crud-rest-builder.acceptance.ts +++ b/packages/boot/src/__tests__/acceptance/crud-rest-builder.acceptance.ts @@ -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 = {...} + // it's not used here because this is a .js file await sandbox.writeTextFile( 'model-endpoints/product.rest-config.js', ` @@ -67,6 +70,7 @@ module.exports = { model: Product, pattern: 'CrudRest', dataSource: 'db', + // basePath not specified }; `, ); @@ -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', @@ -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(); } }); diff --git a/packages/rest-crud/README.md b/packages/rest-crud/README.md index 769b1a9e21df..3cd504d84247 100644 --- a/packages/rest-crud/README.md +++ b/packages/rest-crud/README.md @@ -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. @@ -49,7 +51,7 @@ import {Product} from '../models'; module.exports = { model: Product, - pattern: 'CrudRest', + pattern: 'CrudRest', // make sure to use this pattern dataSource: 'db', basePath: '/products', }; @@ -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. diff --git a/packages/rest-crud/src/crud-rest-builder.plugin.ts b/packages/rest-crud/src/crud-rest.api-builder.ts similarity index 82% rename from packages/rest-crud/src/crud-rest-builder.plugin.ts rename to packages/rest-crud/src/crud-rest.api-builder.ts index 06bfb942491b..9be42f6cf874 100644 --- a/packages/rest-crud/src/crud-rest-builder.plugin.ts +++ b/packages/rest-crud/src/crud-rest.api-builder.ts @@ -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, @@ -86,7 +92,10 @@ function setupCrudRepository( ): Class> { const repositoryClass = defineCrudRepositoryClass(entityClass); - inject(`datasources.${config.dataSource}`)(repositoryClass, undefined, 99); + injectFirstConstructorArg( + repositoryClass, + `datasources.${config.dataSource}`, + ); return repositoryClass; } @@ -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( + ctor: Constructor, + key: BindingSelector, +) { + inject(key)( + ctor, + undefined /* constructor member */, + 0 /* the first argument */, + ); +} diff --git a/packages/rest-crud/src/crud-rest.component.ts b/packages/rest-crud/src/crud-rest.component.ts index b3324359c87f..1fab5573cf00 100644 --- a/packages/rest-crud/src/crud-rest.component.ts +++ b/packages/rest-crud/src/crud-rest.component.ts @@ -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)]; diff --git a/packages/rest-crud/src/index.ts b/packages/rest-crud/src/index.ts index 5c1a27a9df36..33130144140d 100644 --- a/packages/rest-crud/src/index.ts +++ b/packages/rest-crud/src/index.ts @@ -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';