Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@
# - Standby owner(s): n/a
/packages/model-api-builder @nabdelgadir
/packages/rest-crud @nabdelgadir
/examples/rest-crud @nabdelgadir

#
# Cloud Native tooling
Expand Down
4 changes: 4 additions & 0 deletions docs/site/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ LoopBack 4 comes with the following example projects:
- **[loopback4-example-shopping](https://github.com/strongloop/loopback4-example-shopping)**:
An online e-commerce demo to validate/test the LoopBack 4 framework readiness.

- **[rest-crud](https://github.com/strongloop/loopback-next/tree/master/examples/rest-crud)**:
An example showing how to use [`CrudRestComponent`] to define default
repository and controller classes for a model without creating those classes.

You can download the example projects using our CLI tool `lb4`:

```sh
Expand Down
1 change: 1 addition & 0 deletions docs/site/MONOREPO.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The [loopback-next](https://github.com/strongloop/loopback-next) repository uses
| [example-lb3-application](https://github.com/strongloop/loopback-next/tree/master/examples/lb3-application) | @loopback/example-lb3-application | An example LoopBack 3 application mounted in a LoopBack 4 project. |
| [example-log-extension](https://github.com/strongloop/loopback-next/tree/master/examples/log-extension) | @loopback/example-log-extension | An example showing how to write a complex log extension for LoopBack 4 |
| [example-metrics-prometheus](https://github.com/strongloop/loopback-next/tree/master/examples/metrics-prometheus) | @loopback/example-metrics-prometheus | An example to illustrate metrics provided by @loopback/extension-metrics and Prometheus. |
| [example-rest-crud](https://github.com/strongloop/loopback-next/tree/master/examples/rest-crud) | @loopback/example-rest-crud | An example showing how to use @loopback/rest-crud to define default repository and controller classes |
| [example-rpc-server](https://github.com/strongloop/loopback-next/tree/master/examples/rpc-server) | @loopback/example-rpc-server | An example RPC server and application to demonstrate the creation of your own custom server |
| [example-soap-calculator](https://github.com/strongloop/loopback-next/tree/master/examples/soap-calculator) | @loopback/example-soap-calculator | A tutorial demonstrating integration with a SOAP webservice |
| [example-todo-list](https://github.com/strongloop/loopback-next/tree/master/examples/todo-list) | @loopback/example-todo-list | Continuation of the todo example using relations in LoopBack 4 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ Eager to continue learning about LoopBack 4? Check out our
[Examples](../../Examples.md) and [Tutorials](../../Tutorials.md) sections to
find examples for creating your own custom components, sequences and more!

In fact, this example can be simplified to only defining the model and
datasource, while still behaving the same. Using
[`CrudRestComponent`](https://loopback.io/doc/en/lb4/apidocs.rest-crud.crudrestcomponent.html),
the repository and controller classes can be omitted, as seen in the
[rest-crud example](https://github.com/strongloop/loopback-next/tree/master/examples/rest-crud).

### Navigation

Previous step: [Add a controller](todo-tutorial-controller.md)
5 changes: 5 additions & 0 deletions examples/rest-crud/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
npm-debug.log
/dist
# Cache used by TypeScript's incremental build
*.tsbuildinfo
8 changes: 8 additions & 0 deletions examples/rest-crud/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright IBM Corp. 2020. All Rights Reserved.
// Node module: @loopback/example-rest-crud
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

module.exports = {
extends: ['@loopback/eslint-config'],
};
2 changes: 2 additions & 0 deletions examples/rest-crud/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Exclude *.tsbuildinfo - cache for tsc incremental builds
*.tsbuildinfo
1 change: 1 addition & 0 deletions examples/rest-crud/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=true
2 changes: 2 additions & 0 deletions examples/rest-crud/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
*.json
6 changes: 6 additions & 0 deletions examples/rest-crud/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"bracketSpacing": false,
"singleQuote": true,
"printWidth": 80,
"trailingComma": "all"
}
20 changes: 20 additions & 0 deletions examples/rest-crud/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"editor.rulers": [80],
"editor.tabCompletion": "on",
"editor.tabSize": 2,
"editor.trimAutoWhitespace": true,
"editor.formatOnSave": true,

"files.exclude": {
"**/.DS_Store": true,
"**/.git": true,
"**/.hg": true,
"**/.svn": true,
"**/CVS": true,
"dist": true,
},
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,

"typescript.tsdk": "./node_modules/typescript/lib"
}
29 changes: 29 additions & 0 deletions examples/rest-crud/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Watch and Compile Project",
"type": "shell",
"command": "npm",
"args": ["--silent", "run", "build:watch"],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$tsc-watch"
},
{
"label": "Build, Test and Lint",
"type": "shell",
"command": "npm",
"args": ["--silent", "run", "test:dev"],
"group": {
"kind": "test",
"isDefault": true
},
"problemMatcher": ["$tsc", "$eslint-stylish", "$eslint-compact"]
}
]
}
Empty file.
28 changes: 28 additions & 0 deletions examples/rest-crud/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Check out https://hub.docker.com/_/node to select a new base image
FROM node:10-slim

# Set to a non-root built-in user `node`
USER node

# Create app directory (with user `node`)
RUN mkdir -p /home/node/app

WORKDIR /home/node/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY --chown=node package*.json ./

RUN npm install

# Bundle app source code
COPY --chown=node . .

RUN npm run build

# Bind to all network interfaces so that it can be mapped to the host OS
ENV HOST=0.0.0.0 PORT=3000

EXPOSE ${PORT}
CMD [ "node", "." ]
25 changes: 25 additions & 0 deletions examples/rest-crud/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) IBM Corp. 2020. All Rights Reserved.
Node module: @loopback/example-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.
74 changes: 74 additions & 0 deletions examples/rest-crud/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# @loopback/example-rest-crud

This is a basic example demonstrating how to use the default CRUD REST
repository and controller with a single model class and datasource.

## Overview

The
[Todo example](https://github.com/strongloop/loopback-next/tree/master/examples/todo)
can be simplified to use the default repository and controller classes provided
by `@loopback/rest-crud`. With this package, there's no longer a need to define
a repository or controller class for a model.

Given a `Todo` model and `db` datasource, create
`src/model-endpoints/todo.rest-config.ts` and add the following configuration to
it:

```ts
import {ModelCrudRestApiConfig} from '@loopback/rest-crud';
import {Todo} from '../models';

module.exports = <ModelCrudRestApiConfig>{
model: Todo,
pattern: 'CrudRest',
dataSource: 'db',
basePath: '/todos',
};
```

The `pattern` `CrudRest` means it will use the default CRUD REST repository and
controller classes. The `dataSource` should correspond to the datasource you
want to use with this model. Finally, the `basePath` should refer to the base
path the controller will use.

Add the following import to `src/application.ts`:

```ts
import {CrudRestComponent} from '@loopback/rest-crud';
```

And the folowing component to the application constructor:

```ts
this.component(CrudRestComponent);
```

And there you go! Now the application should behave almost the same as the Todo
example (with the exception of the geocoder service).

You can run the application by running the following from the root directory:

```
$ npm start

Server is running at http://127.0.0.1:3000
```

## 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
13 changes: 13 additions & 0 deletions examples/rest-crud/data/db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"ids": {
"Todo": 5
},
"models": {
"Todo": {
"1": "{\"title\":\"Take over the galaxy\",\"desc\":\"MWAHAHAHAHAHAHAHAHAHAHAHAHAMWAHAHAHAHAHAHAHAHAHAHAHAHA\",\"id\":1}",
"2": "{\"title\":\"destroy alderaan\",\"desc\":\"Make sure there are no survivors left!\",\"id\":2}",
"3": "{\"title\":\"play space invaders\",\"desc\":\"Become the very best!\",\"id\":3}",
"4": "{\"title\":\"crush rebel scum\",\"desc\":\"Every.Last.One.\",\"id\":4}"
}
}
}
6 changes: 6 additions & 0 deletions examples/rest-crud/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2020. All Rights Reserved.
// Node module: @loopback/example-rest-crud
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './dist';
26 changes: 26 additions & 0 deletions examples/rest-crud/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright IBM Corp. 2020. All Rights Reserved.
// Node module: @loopback/example-rest-crud
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const application = require('./dist');

module.exports = application;

if (require.main === module) {
// Run the application
const config = {
rest: {
port: +process.env.PORT || 3000,
host: process.env.HOST || 'localhost',
openApiSpec: {
// useful when used with OpenAPI-to-GraphQL to locate your application
setServersFromRequest: true,
},
},
};
application.main(config).catch(err => {
console.error('Cannot start the application.', err);
process.exit(1);
});
}
8 changes: 8 additions & 0 deletions examples/rest-crud/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright IBM Corp. 2020. All Rights Reserved.
// Node module: @loopback/example-rest-crud
// 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';
Loading