Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension: CrudRestController #3582

Merged
merged 1 commit into from Aug 26, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to鈥
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

feat(rest-crud): initial implementation

Introduce a new package `@loopback/rest-crud` providing a controller
class implementing default CRUD operations for a given Entity.
  • Loading branch information...
bajtos committed Aug 20, 2019
commit 959e4eed383382487efedf5d8a36b8ec058f2a43
@@ -26,6 +26,7 @@ packages/repository/* @raymondfeng
packages/repository-tests/* @bajtos
packages/repository-json-schema/* @bajtos
packages/rest/* @bajtos @raymondfeng
packages/rest-crud/* @bajtos @hacksparrow
This conversation was marked as resolved by bajtos

This comment has been minimized.

Copy link
@bajtos

bajtos Aug 20, 2019

Author Member

Discussion point: should this new package live in packages/rest-crud or extensions/rest-crud?

This comment has been minimized.

Copy link
@raymondfeng

raymondfeng Aug 20, 2019

Member

My personal take: packages/rest-crud.

packages/service-proxy/* @raymondfeng
packages/testlab/* @bajtos
packages/tsdocs/* @raymondfeng
@@ -491,7 +491,6 @@ Please register the new package in the following files:

- Update [MONOREPO.md](./MONOREPO.md) - insert a new table row to describe the
new package, please keep the rows sorted by package name.
- Update
- Update [Reserved-binding-keys.md](./Reserved-binding-keys.md) - add a link to
the apidocs on Binding Keys if the new package has any.
- Update
@@ -37,6 +37,7 @@ The [loopback-next](https://github.com/strongloop/loopback-next) repository uses
| [repository](https://github.com/strongloop/loopback-next/tree/master/packages/repository) | @loopback/repository | Define and implement a common set of interfaces for interacting with databases |
| [repository-tests](https://github.com/strongloop/loopback-next/tree/master/packages/repository-tests) | @loopback/repository-tests | A shared test suite to verify `@loopback/repository` functionality with a given compatible connector |
| [rest](https://github.com/strongloop/loopback-next/tree/master/packages/rest) | @loopback/rest | Expose controllers as REST endpoints and route REST API requests to controller methods |
| [rest-crud](https://github.com/strongloop/loopback-next/tree/master/packages/rest-crud) | @loopback/rest-crud | REST API controller implementing default CRUD semantics |
| [service-proxy](https://github.com/strongloop/loopback-next/tree/master/packages/service-proxy) | @loopback/service-proxy | A common set of interfaces for interacting with service oriented backends such as REST APIs, SOAP Web Services, and gRPC microservices |
| [testlab](https://github.com/strongloop/loopback-next/tree/master/packages/testlab) | @loopback/testlab | A collection of test utilities we use to write LoopBack tests |
| [tsdocs](https://github.com/strongloop/loopback-next/tree/master/packages/tsdocs) | @loopback/tsdocs | An internal package to generate api docs using Microsoft api-extractor and api-documenter |
@@ -0,0 +1 @@
package-lock=true
@@ -0,0 +1,25 @@
Copyright (c) IBM Corp. 2019. All Rights Reserved.
Node module: @loopback/rest-crud
This project is licensed under the MIT License, full text below.

--------

MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@@ -0,0 +1,65 @@
# @loopback/rest-crud

REST API controller implementing default CRUD semantics.

## Overview

This module allows applications to quickly expose a model via REST API without
having to implement a custom controller class.

## Installation

```sh
npm install --save @loopback/rest-crud
```

## Basic use

1. Define your model class, e.g. using `lb4 model` tool.

2. Create a Repository class, e.g. using `lb4 repository` tool.

This comment has been minimized.

Copy link
@raymondfeng

raymondfeng Aug 23, 2019

Member

Is it required? IIUC, you want to skip Repository class so that a model can be used to expose REST CRUD APIs with a datasource.

This comment has been minimized.

Copy link
@bajtos

bajtos Aug 26, 2019

Author Member

A custom repository class is not required, this is just to make the documentation easier to follow.

This new component is meant to be a low-level building block to support #2036, I am expecting that most people most people will use #2036 and don't leverage the current API directly.

For the initial docs, I choose a scenario that's easy to understand (because it's using existing concepts) and that's matching the scenario used for testing.

Depending on the outcome of the next tasks (especially the spike #2738), we may add more "meat" to this component and update or even rework the README along the way.


3. Create a base REST CRUD controller class for your model.

```ts
const CrudRestController = defineCrudRestController<
Product,
typeof Product.prototype.id,
'id'
>(Product, {basePath: '/products'});
```

4. Create a new subclass of the base controller class to configure repository
injection.

```ts
class ProductController extends CrudRestController {
constructor(@repository(ProductRepository) repo: ProductRepository) {

This comment has been minimized.

Copy link
@raymondfeng

raymondfeng Aug 23, 2019

Member

If we skip the repository, can we inject a datasource instead?

This comment has been minimized.

Copy link
@bajtos

bajtos Aug 26, 2019

Author Member

Yes, I believe injecting a datasource should be a viable option too. The actual implementation does not prescribe any IoC setup, the base controller class is expecting to receive a repository instance. It's up to the code extending the controller class to decide how to obtain the repository.

I believe the following solution should work as an alternative approach too:

class ProductController extends CrudRestController {
  constructor(
    @inject('datasources.db') dataSource: juggler.DataSource,
  ) {
    const repo = new DefaultCrudRepository<
      Product,
      typeof Product.prototype.id
    >(Product, db);

    super(repo);
  }
}

@raymondfeng Would you like me to open a new issue to add a test & documentation for this approach?

super(repo);
}
}
```

5. Register the controller with your application.

```ts
app.controller(ProductController);
```

## Contributions

- [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md)
- [Join the team](https://github.com/strongloop/loopback-next/issues/110)

## Tests

Run `npm test` from the root folder.

## Contributors

See
[all contributors](https://github.com/strongloop/loopback-next/graphs/contributors).

## License

MIT
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/crud-rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './dist';
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/crud-rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

module.exports = require('./dist');
@@ -0,0 +1,8 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/crud-rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

// DO NOT EDIT THIS FILE
// Add any additional (re)exports to src/index.ts instead.
export * from './src';

Some generated files are not rendered by default. Learn more.

@@ -0,0 +1,46 @@
{
"name": "@loopback/rest-crud",
"version": "0.0.1",
"description": "REST API controller implementing default CRUD semantics",
"engines": {
"node": ">=8.9"
},
"main": "index",
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "lb-tsc",
"clean": "lb-clean loopback-rest-crud*.tgz dist tsconfig.build.tsbuildinfo package",
"pretest": "npm run build",
"test": "lb-mocha \"dist/__tests__/**/*.js\"",
"verify": "npm pack && tar xf loopback-rest-crud*.tgz && tree package && npm run clean"
},
"author": "IBM Corp.",
"copyright.owner": "IBM Corp.",
"license": "MIT",
"devDependencies": {
"@loopback/build": "^1.7.1",
"@loopback/repository": "^1.12.0",
"@loopback/rest": "^1.17.0",
"@loopback/testlab": "^1.7.4",
"@types/node": "^10.14.15"
},
"peerDependencies": {
"@loopback/repository": "^1.12.0",
"@loopback/rest": "^1.17.0"
},
"files": [
"README.md",
"index.js",
"index.d.ts",
"dist",
"src",
"!*/__tests__"
],
"repository": {
"type": "git",
"url": "https://github.com/strongloop/loopback-next.git",
"directory": "packages/rest-crud"
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can鈥檛 perform that action at this time.