Skip to content

docs: booting in LB3 vs LB4#5178

Merged
hacksparrow merged 1 commit into
masterfrom
docs/lb3-vs-lb4-boot
Apr 27, 2020
Merged

docs: booting in LB3 vs LB4#5178
hacksparrow merged 1 commit into
masterfrom
docs/lb3-vs-lb4-boot

Conversation

@hacksparrow

Copy link
Copy Markdown
Contributor

Differences in LB3 and LB4 booting. Addresses #4838.

@hacksparrow
hacksparrow force-pushed the docs/lb3-vs-lb4-boot branch 2 times, most recently from 9bc3e7e to 3d518e2 Compare April 22, 2020 16:30
@hacksparrow
hacksparrow marked this pull request as ready for review April 22, 2020 16:32
Comment thread docs/site/LB3-vs-LB4-booting.md Outdated
### In LoopBack 4

In LoopBack 4, the booting tasks are shared between the
[RestApplication](./apidocs/apidocs.rest.restapplication.html) class

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What part of booting does RestApplication handle?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should mention it's possible to plug in new types of booters.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What part of booting does RestApplication handle?

The config part.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think RestApplication is handling the config part, isn't this handled by Application from @loopback/core? AFAIK, RestApplication is a thin wrapper around Application that's adding a single REST server instance and convenience methods for accessing that server.

Let's build a better understanding of what you are trying to describe, @hacksparrow, and how is that actually implemented.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So refer to Application of @loopback/core instead? We need to tell where the app configuration is applied as part of the booting process.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raymondfeng I have updated accordingly.

Comment thread docs/site/LB3-vs-LB4-booting.md Outdated

@bajtos bajtos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start!

Comment thread docs/site/LB3-vs-LB4-booting.md Outdated
Models are determined from application options or model files,
[mixins](https://loopback.io/doc/en/lb3/Defining-mixins.html) are applied,
and they are attached to their respective datasources. They are then attached
to the LoopBack Application object.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: In the first step, we define all models. In the second step, we attach the models to datasources and the application.

The list of models defined may be longer than the list of models attached:

  1. loopback-boot looks at server/model-config to see which files should be added to the app.
  2. Then it examines inheritance and mixin configurations, to determine dependencies (which models to add to the list)
  3. Models are sorted topologically based on inheritance, so that we define parent models before children.
  4. All models from the list above are defined via model registry.
  5. Only models specified in server/model-config are added to the app and attached to a datasource (unless dataSource flag is set to null).

loopback-boot also supports universal mode, where the entire loopback application is bundled for browser. In that case, the boot process is split in two parts:

  1. The first step finds out which models (and their parents, etc.) are needed by the app. It creates "instructions" for booting an app, these instructions are compiled into the browser bundle together with model source files (common/models/{model-name}.json).
  2. The second step is performed in the browser, it processes the instructions from the first step.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not worth getting into all those details for something that we are moving away from. Especially since the corresponding step in LB4 is an import.

Comment thread docs/site/LB3-vs-LB4-booting.md Outdated
### In LoopBack 4

In LoopBack 4, the booting tasks are shared between the
[RestApplication](./apidocs/apidocs.rest.restapplication.html) class

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think RestApplication is handling the config part, isn't this handled by Application from @loopback/core? AFAIK, RestApplication is a thin wrapper around Application that's adding a single REST server instance and convenience methods for accessing that server.

Let's build a better understanding of what you are trying to describe, @hacksparrow, and how is that actually implemented.

Comment thread docs/site/LB3-vs-LB4-booting.md Outdated

There is no `config.json` in LoopBack 4. The application options are passed in
the constructor of the
[RestApplication](./apidocs/apidocs.rest.restapplication.html) class.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using RestApplication is not mandatory, people can use the base Application class too. This will become more and more important as we add support for different API flavors (gRPC, event-stream consumers, etc.)

Comment thread docs/site/LB3-vs-LB4-booting.md Outdated
Datasources in LoopBack 3 are defined in a single `datasources.json` file. In
LoopBack 4, datasources are defined as TypeScript classes that come with an
associated JSON configuration file. They all reside in the `src/datasources`
directory.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that we are moving away from JSON config files for datasources, see #5000

Comment thread docs/site/LB3-vs-LB4-booting.md Outdated
associated JSON configuration file. They all reside in the `src/datasources`
directory.

`BootMixin` resolves the datasources, and attaches them to the application.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any documentation on how is the datasource booter working under the hood? For example, what files it looks for, how it decided which exported member of a datasource TS file it will add to the app, etc. If yes, then please add a link to it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have anything in-depth, AFAIK. The closest is https://loopback.io/doc/en/lb4/Booting-an-Application.html#controller-booter, added the link.

### 3. Definition of models

Although REST APIs are built around Models, they are not a part of the booting
process in LoopBack 4; Controllers and Repositories are.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please briefly explain why we are not booting models?

The main reason is that we need Models at compile time, therefore they must be imported directly.

process in LoopBack 4; Controllers and Repositories are.

Controllers and Repositories import models as regular TypeScript classes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please briefly explain why we are not booting models?

The main reason is that we need Models at compile time, because as you have correctly pointed out, controllers and repositories are importing them directly.

Comment thread docs/site/sidebars/lb4_sidebar.yml
@hacksparrow
hacksparrow force-pushed the docs/lb3-vs-lb4-boot branch 3 times, most recently from aa01187 to bde8541 Compare April 24, 2020 14:48
[loopback-boot](https://github.com/strongloop/loopback-boot),
which performs the following tasks:

### In LoopBack 3

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this paragraph

The booting process in LoopBack 3 is handled by
loopback-boot,
which performs the following tasks:

should be under ### In LoopBack 3, instead of before it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed.

@jannyHou jannyHou left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good content 👍 lgtm, a few nitpicks

and the
[@loopback/boot](./apidocs/apidocs.boot.html) package. Since your application
will extend `BootMixin` and `Application`, their separation will not be
apparent to you, and understanding of how they work together as one as not

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
apparent to you, and understanding of how they work together as one as not
apparent to you, and understanding of how they work together as one is not


There is no `config.json` in LoopBack 4. The application options are passed in
the constructor of the
[Application](./apidocs/apidocs.core.application.md) class.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'd be better to include a sample code, or the link https://loopback.io/doc/en/lb4/Application.html#configuring-your-application

@bajtos bajtos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @hacksparrow for the updates.

Please make sure all relative links are still working after you moved the file to docs/site/migration.

Also #5000 has been landed earlier today, please update the text accordingly.

{% include note.html content="We are moving away from JSON files for datasource
configuration. Refer to
[loopback-next#5000](https://github.com/strongloop/loopback-next/pull/5000)
for the progress." %}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR has been already landed, please update this section to reflect the current master.


`BootMixin` resolves the datasources, and attaches them to the application.
Read more about datasource booter
[here](./Booting-an-Application.md#controller-booter).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you update all links after moving the file to docs/site/migration? I don't think this link is valid now.

[here](./Booting-an-Application.md#controller-booter).

Read more about LoopBack 4 datasource [here](./DataSources.md).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for modularly adding functionality to the framework. Controllers, Repositories,
Datasources, Interceptors, etc., are added using booters, custom booters can
be written for adding new functionality to the framework." %}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refer to the
[booting documentation](./Booting-an-Application.md)." %}

### 1. Configuration of application settings

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this and the two below be fourth-level headings?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Thanks.

Read more about datasource booter
[here](./Booting-an-Application.md#controller-booter).

Read more about LoopBack 4 datasource [here](./DataSources.md).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: datasource -> datasources


If you want to mount an Express router in a LoopBack 4 application, you can use
the
[RestApplication.mountExpressRouter()](./apidocs/apidocs.rest.restapplication.mountexpressrouter.md)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hacksparrow
hacksparrow force-pushed the docs/lb3-vs-lb4-boot branch from 4d2b034 to 5b66456 Compare April 27, 2020 07:55

@bajtos bajtos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not convinced that the new links work (e.g. I don't see any file called migration-datasources.md in our repository). But as long as you have run the preview of the doc site and verified that all links work, the I am fine.

@hacksparrow

Copy link
Copy Markdown
Contributor Author

Thanks @bajtos, fixed them.

@hacksparrow

Copy link
Copy Markdown
Contributor Author

@nabdelgadir I have applied your feedback. Can you PTAL?

Differences in LB3 and LB4 booting.
@hacksparrow
hacksparrow force-pushed the docs/lb3-vs-lb4-boot branch from b35d76b to 92c7347 Compare April 27, 2020 15:24
@hacksparrow
hacksparrow merged commit f3bebe8 into master Apr 27, 2020
@hacksparrow
hacksparrow deleted the docs/lb3-vs-lb4-boot branch April 27, 2020 16:52
@hacksparrow hacksparrow mentioned this pull request Apr 27, 2020
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants