Skip to content

Commit

Permalink
feat(rest-crud): use functions from @loopback/repository to define re…
Browse files Browse the repository at this point in the history
…pository classes
  • Loading branch information
raymondfeng committed Mar 9, 2020
1 parent be642ab commit c016550
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 88 deletions.
14 changes: 8 additions & 6 deletions packages/rest-crud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class defined without the need for a repository or controller class file.

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
(`defineCrudRestController` and `defineEntityCrudRepositoryClass`) exposed from
`@loopback/rest-crud`. These functions will help you create controllers and
respositories using code.

Expand Down Expand Up @@ -100,20 +100,22 @@ endpoints of an existing model with a respository.

### Creating a CRUD repository

Use the `defineCrudRepositoryClass` method to create named repositories (based
on the Model) for your app.
Use the `defineEntityCrudRepositoryClass` method to create named repositories
(based on the Model) for your app.

Usage example:

```ts
const ProductRepository = defineCrudRepositoryClass(Product);
import {defineEntityCrudRepositoryClass} from '@loopback/repository';

const ProductRepository = defineEntityCrudRepositoryClass(Product);
this.repository(ProductRepository);
inject('datasources.db')(ProductRepository, undefined, 0);
```

### Integrated example

Here is an example of an app which uses `defineCrudRepositoryClass` and
Here is an example of an app which uses `defineEntityCrudRepositoryClass` and
`defineCrudRestController` to fulfill its repository and controller
requirements.

Expand All @@ -128,7 +130,7 @@ export class TryApplication extends BootMixin(
async boot(): Promise<void> {
await super.boot();

const ProductRepository = defineCrudRepositoryClass(Product);
const ProductRepository = defineEntityCrudRepositoryClass(Product);
const repoBinding = this.repository(ProductRepository);

inject('datasources.db')(ProductRepository, undefined, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {
defineEntityCrudRepositoryClass,
Entity,
EntityCrudRepository,
juggler,
Expand All @@ -18,7 +19,7 @@ import {
givenHttpServerConfig,
toJSON,
} from '@loopback/testlab';
import {defineCrudRepositoryClass, defineCrudRestController} from '../..';
import {defineCrudRestController} from '../..';

// In this test scenario, we create a product with a required & an optional
// property and use the default model settings (strict mode, forceId).
Expand Down Expand Up @@ -295,7 +296,7 @@ describe('CrudRestController for a simple Product model', () => {
async function setupTestScenario() {
const db = new juggler.DataSource({connector: 'memory'});

const ProductRepository = defineCrudRepositoryClass(Product);
const ProductRepository = defineEntityCrudRepositoryClass(Product);

repo = new ProductRepository(db);

Expand Down

This file was deleted.

5 changes: 3 additions & 2 deletions packages/rest-crud/src/crud-rest.api-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import {
import {
ApplicationWithRepositories,
Class,
defineEntityCrudRepositoryClass,
Entity,
EntityCrudRepository,
} from '@loopback/repository';
import {Model} from '@loopback/rest';
import debugFactory from 'debug';
import {defineCrudRepositoryClass, defineCrudRestController} from '.';
import {defineCrudRestController} from '.';

const debug = debugFactory('loopback:boot:crud-rest');

Expand Down Expand Up @@ -86,7 +87,7 @@ function setupCrudRepository(
entityClass: typeof Entity & {prototype: Entity},
config: ModelCrudRestApiConfig,
): Class<EntityCrudRepository<Entity, unknown>> {
const repositoryClass = defineCrudRepositoryClass(entityClass);
const repositoryClass = defineEntityCrudRepositoryClass(entityClass);

injectFirstConstructorArg(
repositoryClass,
Expand Down
4 changes: 3 additions & 1 deletion packages/rest-crud/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

// Reexport `defineEntityCrudRepositoryClass` from `@loopback/repository` as
// `defineCrudRepositoryClass` for backward-compatibility
export {defineEntityCrudRepositoryClass as defineCrudRepositoryClass} from '@loopback/repository';
export * from './crud-rest.api-builder';
export * from './crud-rest.component';
export * from './crud-rest.controller';
export * from './repository-builder';
55 changes: 0 additions & 55 deletions packages/rest-crud/src/repository-builder.ts

This file was deleted.

0 comments on commit c016550

Please sign in to comment.