Skip to content

Commit 5f54b96

Browse files
committed
revert(example-getting-started): sync code and docs
This reverts commit ae1a7f8.
1 parent ae1a7f8 commit 5f54b96

File tree

13 files changed

+58
-145
lines changed

13 files changed

+58
-145
lines changed

packages/example-getting-started/docs/controller.md

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ logic will live_!
1010

1111
### Create your controller
1212

13-
So, let's create a controller to handle our Todo routes. Inside the
14-
`src/controllers` directory create the following two files:
13+
So, let's create a controller to handle our Todo routes. Create the
14+
`src/controllers` directory and two files inside:
1515
- `index.ts` (export helper)
1616
- `todo.controller.ts`
1717

@@ -25,7 +25,7 @@ repository, which we'll use to perform our operations against the datasource.
2525
#### src/controllers/todo.controller.ts
2626
```ts
2727
import {repository} from '@loopback/repository';
28-
import {TodoRepository} from '../repositories';
28+
import {TodoRepository} from '../repositories/index';
2929

3030
export class TodoController {
3131
constructor(
@@ -51,10 +51,11 @@ Now that we have the repository wireup, let's create our first handler function.
5151

5252
#### src/controllers/todo.controller.ts
5353
```ts
54-
import {repository} from '@loopback/repository';
55-
import {TodoRepository} from '../repositories';
54+
import {post, param} from '@loopback/openapi-v3';
55+
import {HttpErrors} from '@loopback/rest';
5656
import {Todo} from '../models';
57-
import {HttpErrors, post, param, requestBody} from '@loopback/rest';
57+
import {repository} from '@loopback/repository';
58+
import {TodoRepository} from '../repositories/index';
5859

5960
export class TodoController {
6061
constructor(
@@ -93,36 +94,29 @@ verbs:
9394

9495
#### src/controllers/todo.controller.ts
9596
```ts
96-
import {repository} from '@loopback/repository';
97-
import {TodoRepository} from '../repositories';
97+
import {post, param, requestBody, get, put, patch, del} from '@loopback/openapi-v3';
98+
import {HttpErrors} from '@loopback/rest';
9899
import {Todo} from '../models';
99-
import {
100-
HttpErrors,
101-
post,
102-
param,
103-
requestBody,
104-
get,
105-
put,
106-
patch,
107-
del,
108-
} from '@loopback/rest';
100+
import {repository} from '@loopback/repository';
101+
import {TodoRepository} from '../repositories/index';
109102

110103
export class TodoController {
111104
constructor(
112105
@repository(TodoRepository.name) protected todoRepo: TodoRepository,
113106
) {}
114107

115108
@post('/todo')
116-
async createTodo(@requestBody() todo: Todo) {
109+
async createTodo(@requestBody()todo: Todo) {
117110
if (!todo.title) {
118111
return Promise.reject(new HttpErrors.BadRequest('title is required'));
119112
}
120113
return await this.todoRepo.create(todo);
121114
}
122115

123116
@get('/todo/{id}')
124-
async findTodoById(@param.path.number('id') id: number): Promise<Todo> {
125-
id = +id;
117+
async findTodoById(
118+
@param.path.number('id') id: number,
119+
@param.query.boolean('items') items?: boolean): Promise<Todo> {
126120
return await this.todoRepo.findById(id);
127121
}
128122

@@ -134,18 +128,14 @@ export class TodoController {
134128
@put('/todo/{id}')
135129
async replaceTodo(
136130
@param.path.number('id') id: number,
137-
@requestBody() todo: Todo,
138-
): Promise<boolean> {
139-
id = +id;
131+
@requestBody() todo: Todo): Promise<boolean> {
140132
return await this.todoRepo.replaceById(id, todo);
141133
}
142134

143135
@patch('/todo/{id}')
144136
async updateTodo(
145137
@param.path.number('id') id: number,
146-
@requestBody() todo: Todo,
147-
): Promise<boolean> {
148-
id = +id;
138+
@requestBody() todo: Todo): Promise<boolean> {
149139
return await this.todoRepo.updateById(id, todo);
150140
}
151141

@@ -160,7 +150,7 @@ Some additional things to note about this example:
160150
- Routes like `@get('/todo/{id}')` can be paired with the `@param.path`
161151
decorators to inject those values at request time into the handler function.
162152
- LoopBack's `@param` decorator also contains a namespace full of other
163-
"subdecorators" like `@param.path`, `@param.query`, and `@param.header` that
153+
"subdecorators" like `@param.path`, `@param.query`, and `@param.body` that
164154
allow specification of metadata for those parts of a REST request.
165155
- LoopBack's `@param.path` and `@param.query` also provide subdecorators for
166156
specifying the type of certain value primitives, such as

packages/example-getting-started/docs/datasource.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ of this tutorial, we'll be using the memory connector provided with the Juggler.
2626
}
2727
```
2828

29-
Inside the `src/datasources` directory create a new file called `db.datasource.ts`. This file will create
29+
Create another folder called `datasources` in the `src` directory, and inside
30+
that folder, create a new file called `db.datasource.ts`. This file will create
3031
a strongly-typed export of our datasource using the `DataSourceConstructor`,
3132
which we can consume in our application via injection.
3233

packages/example-getting-started/docs/juggler.md

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,24 @@ the `RepositoryMixin`:
2020
#### src/application.ts
2121
```ts
2222
import {ApplicationConfig} from '@loopback/core';
23-
import {RestApplication, RestServer} from '@loopback/rest';
24-
import {MySequence} from './sequence';
23+
import {RestApplication} from '@loopback/rest';
2524

2625
/* tslint:disable:no-unused-variable */
26+
// Do not remove!
27+
// Class and Repository imports required to infer types in consuming code!
2728
// Binding and Booter imports are required to infer types for BootMixin!
2829
import {BootMixin, Booter, Binding} from '@loopback/boot';
2930
import {
3031
Class,
3132
Repository,
3233
RepositoryMixin,
33-
juggler,
3434
} from '@loopback/repository';
3535
/* tslint:enable:no-unused-variable */
36-
37-
export class TodoListApplication extends BootMixin(
36+
export class TodoApplication extends BootMixin(
3837
RepositoryMixin(RestApplication),
3938
) {
4039
constructor(options?: ApplicationConfig) {
4140
super(options);
42-
43-
// Set up the custom sequence
44-
this.sequence(MySequence);
45-
4641
this.projectRoot = __dirname;
4742
// Customize @loopback/boot Booter Conventions here
4843
this.bootOptions = {
@@ -54,15 +49,6 @@ export class TodoListApplication extends BootMixin(
5449
},
5550
};
5651
}
57-
58-
async start() {
59-
await super.start();
60-
61-
const server = await this.getServer(RestServer);
62-
const port = await server.get('rest.port');
63-
console.log(`Server is running at http://127.0.0.1:${port}`);
64-
console.log(`Try http://127.0.0.1:${port}/ping`);
65-
}
6652
}
6753
```
6854

packages/example-getting-started/docs/model.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ For our Todo model to represent our Todo instances, it will need:
2626
- a description that details what the todo is all about
2727
- a boolean flag for whether or not we've completed the task
2828

29-
Inside the `src/models` folder, create two files:
29+
Create another folder in `src` called `models` and inside of that folder,
30+
create two files:
3031
- `index.ts`
3132
- `todo.model.ts`
3233

packages/example-getting-started/docs/putting-it-together.md

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,26 @@ other artifacts and inject them into our application for use.
2626
#### src/application.ts
2727
```ts
2828
import {ApplicationConfig} from '@loopback/core';
29-
import {RestApplication, RestServer} from '@loopback/rest';
30-
import {MySequence} from './sequence';
29+
import {RestApplication} from '@loopback/rest';
3130
import {db} from './datasources/db.datasource';
3231

3332
/* tslint:disable:no-unused-variable */
33+
// Do not remove!
34+
// Class and Repository imports required to infer types in consuming code!
3435
// Binding and Booter imports are required to infer types for BootMixin!
3536
import {BootMixin, Booter, Binding} from '@loopback/boot';
3637
import {
3738
Class,
3839
Repository,
3940
RepositoryMixin,
40-
juggler,
41-
DataSourceConstructor,
4241
} from '@loopback/repository';
4342
/* tslint:enable:no-unused-variable */
4443

45-
export class TodoListApplication extends BootMixin(
44+
export class TodoApplication extends BootMixin(
4645
RepositoryMixin(RestApplication),
4746
) {
4847
constructor(options?: ApplicationConfig) {
4948
super(options);
50-
51-
// Set up the custom sequence
52-
this.sequence(MySequence);
53-
5449
this.projectRoot = __dirname;
5550
// Customize @loopback/boot Booter Conventions here
5651
this.bootOptions = {
@@ -61,7 +56,6 @@ export class TodoListApplication extends BootMixin(
6156
nested: true,
6257
},
6358
};
64-
6559
this.setupDatasources();
6660
}
6761

@@ -74,17 +68,7 @@ export class TodoListApplication extends BootMixin(
7468
: db;
7569
this.bind('datasource').to(datasource);
7670
}
77-
78-
async start() {
79-
await super.start();
80-
81-
const server = await this.getServer(RestServer);
82-
const port = await server.get('rest.port');
83-
console.log(`Server is running at http://127.0.0.1:${port}`);
84-
console.log(`Try http://127.0.0.1:${port}/ping`);
85-
}
86-
}
87-
71+
}}
8872
```
8973

9074
### Try it out

packages/example-getting-started/docs/repository.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ layer.
88

99
### Create your repository
1010

11-
In the `src/repositories` directory create two files:
11+
Create another folder in `src` called `repositories` and inside of that folder,
12+
create two files:
1213
- `index.ts` (our export helper)
1314
- `todo.repository.ts`
1415

packages/example-getting-started/docs/scaffolding.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ To generate your application using the toolkit, run the `lb4 app` command
88
and fill out the on-screen prompts:
99
```
1010
$ lb4 app
11-
? Project name: todo-list
11+
? Project name: (todo-list)
1212
? Project description: A todo list API made with LoopBack 4.
1313
? Project root directory: (todo-list)
1414
? Application class name: (TodoListApplication)

packages/example-getting-started/src/application.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,39 @@
55

66
import {ApplicationConfig} from '@loopback/core';
77
import {RestApplication} from '@loopback/rest';
8-
import {MySequence} from './sequence';
98
import {db} from './datasources/db.datasource';
109

1110
/* tslint:disable:no-unused-variable */
11+
// Do not remove!
12+
// Class and Repository imports required to infer types in consuming code!
1213
// Binding and Booter imports are required to infer types for BootMixin!
1314
import {BootMixin, Booter, Binding} from '@loopback/boot';
1415
import {
1516
Class,
1617
Repository,
18+
DataSourceConstructor,
1719
RepositoryMixin,
1820
juggler,
19-
DataSourceConstructor,
2021
} from '@loopback/repository';
2122
/* tslint:enable:no-unused-variable */
2223

23-
export class TodoListApplication extends BootMixin(
24+
export class TodoApplication extends BootMixin(
2425
RepositoryMixin(RestApplication),
2526
) {
2627
constructor(options?: ApplicationConfig) {
2728
super(options);
28-
29-
// Set up the custom sequence
30-
this.sequence(MySequence);
31-
3229
this.projectRoot = __dirname;
33-
// Customize @loopback/boot Booter Conventions here
34-
this.bootOptions = {
35-
controllers: {
36-
// Customize ControllerBooter Conventions here
37-
dirs: ['controllers'],
38-
extensions: ['.controller.js'],
39-
nested: true,
40-
},
41-
};
42-
43-
this.setupDatasources();
30+
this.setupDataSources();
4431
}
4532

46-
setupDatasources() {
47-
// This will allow you to test your application without needing to
48-
// use the "real" datasource!
33+
// Helper functions (just to keep things organized)
34+
setupDataSources() {
35+
// TODO(bajtos) Automate datasource and repo registration via @loopback/boot
36+
// See https://github.com/strongloop/loopback-next/issues/441
4937
const datasource =
5038
this.options && this.options.datasource
5139
? new DataSourceConstructor(this.options.datasource)
5240
: db;
53-
this.bind('datasource').to(datasource);
41+
this.dataSource(datasource);
5442
}
5543
}

packages/example-getting-started/src/controllers/todo.controller.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import {repository} from '@loopback/repository';
2-
import {TodoRepository} from '../repositories';
3-
import {Todo} from '../models';
41
import {
5-
HttpErrors,
62
post,
73
param,
8-
requestBody,
94
get,
105
put,
116
patch,
127
del,
13-
} from '@loopback/rest';
8+
requestBody,
9+
} from '@loopback/openapi-v3';
10+
import {HttpErrors} from '@loopback/rest';
11+
import {Todo} from '../models';
12+
import {repository} from '@loopback/repository';
13+
import {TodoRepository} from '../repositories';
1414

1515
export class TodoController {
1616
// TODO(bajtos) Fix documentation (and argument names?) of @repository()
@@ -35,7 +35,6 @@ export class TodoController {
3535
@param.path.number('id') id: number,
3636
@param.query.boolean('items') items?: boolean,
3737
): Promise<Todo> {
38-
id = +id;
3938
return await this.todoRepo.findById(id);
4039
}
4140

packages/example-getting-started/src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6-
import {TodoListApplication} from './application';
7-
import {ApplicationConfig} from '@loopback/core';
6+
import {TodoApplication} from './application';
87
import {RestServer} from '@loopback/rest';
98

10-
export async function main(options?: ApplicationConfig) {
11-
const app = new TodoListApplication(options);
9+
export async function main() {
10+
const app = new TodoApplication();
1211
try {
1312
await app.boot();
1413
await app.start();

0 commit comments

Comments
 (0)