UNPKG

10.3 kBMarkdownView Raw
1# @loopback/boot
2
3A convention based project Bootstrapper and Booters for LoopBack Applications
4
5## Overview
6
7A Booter is a Class that can be bound to an Application and is called to perform
8a task before the Application is started. A Booter may have multiple phases to
9complete its task. The task for a convention based Booter is to discover and
10bind Artifacts (Controllers, Repositories, Models, etc.).
11
12An example task of a Booter may be to discover and bind all artifacts of a given
13type.
14
15A Bootstrapper is needed to manage the Booters and execute them. This is
16provided in this package. For ease of use, everything needed is packages using a
17BootMixin. This Mixin will add convenience methods such as `boot` and `booter`,
18as well as properties needed for Bootstrapper such as `projectRoot`. The Mixin
19also adds the `BootComponent` to your `Application` which binds the
20`Bootstrapper` and default `Booters` made available by this package.
21
22## Installation
23
24```shell
25$ npm i @loopback/boot
26```
27
28## Basic Use
29
30```ts
31import {Application} from '@loopback/core';
32import {BootMixin, Booter, Binding} from '@loopback/boot';
33class BootApp extends BootMixin(Application) {}
34
35const app = new BootApp();
36app.projectRoot = __dirname;
37
38await app.boot();
39await app.start();
40```
41
42### BootOptions
43
44List of Options available on BootOptions Object.
45
46| Option | Type | Description |
47| -------------- | ----------------- | ----------------------------------- |
48| `controllers` | `ArtifactOptions` | ControllerBooter convention options |
49| `repositories` | `ArtifactOptions` | RepositoryBooter convention options |
50| `datasources` | `ArtifactOptions` | DataSourceBooter convention options |
51| `services` | `ArtifactOptions` | ServiceBooter convention options |
52
53### ArtifactOptions
54
55| Options | Type | Description |
56| ------------ | -------------------- | ------------------------------------------------------------------------------------------------------------ |
57| `dirs` | `string \| string[]` | Paths relative to projectRoot to look in for Artifact |
58| `extensions` | `string \| string[]` | File extensions to match for Artifact |
59| `nested` | `boolean` | Look in nested directories in `dirs` for Artifact |
60| `glob` | `string` | A `glob` pattern string. This takes precedence over above 3 options (which are used to make a glob pattern). |
61
62### BootExecOptions
63
64**Experimental support. May be removed or changed in a non-compatible way in
65future without warning**
66
67To use `BootExecOptions` you must directly call `bootstrapper.boot()` and pass
68in `BootExecOptions`. `app.boot()` provided by `BootMixin` does not take any
69parameters.
70
71```ts
72const bootstrapper: Bootstrapper = await this.get(
73 BootBindings.BOOTSTRAPPER_KEY,
74);
75const execOptions: BootExecOptions = {
76 booters: [MyBooter1, MyBooter2],
77 filter: {
78 phases: ['configure', 'discover'],
79 },
80};
81
82const ctx = bootstrapper.boot(execOptions);
83```
84
85You can pass in the `BootExecOptions` object with the following properties:
86
87| Property | Type | Description |
88| ---------------- | ----------------------- | ------------------------------------------------ |
89| `booters` | `Constructor<Booter>[]` | Array of Booters to bind before running `boot()` |
90| `filter.booters` | `string[]` | Names of Booter classes that should be run |
91| `filter.phases` | `string[]` | Names of Booter phases to run |
92
93## Available Booters
94
95### ControllerBooter
96
97#### Description
98
99Discovers and binds Controller Classes using `app.controller()`.
100
101#### Options
102
103The options for this can be passed via `BootOptions` when calling
104`app.boot(options: BootOptions)`.
105
106The options for this are passed in a `controllers` object on `BootOptions`.
107
108Available options on the `controllers` object on `BootOptions` are as follows:
109
110| Options | Type | Default | Description |
111| ------------ | -------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------- |
112| `dirs` | `string \| string[]` | `['controllers']` | Paths relative to projectRoot to look in for Controller artifacts |
113| `extensions` | `string \| string[]` | `['.controller.js']` | File extensions to match for Controller artifacts |
114| `nested` | `boolean` | `true` | Look in nested directories in `dirs` for Controller artifacts |
115| `glob` | `string` | | A `glob` pattern string. This takes precendence over above 3 options (which are used to make a glob pattern). |
116
117### RepositoryBooter
118
119#### Description
120
121Discovers and binds Repository Classes using `app.repository()` (Application
122must use `RepositoryMixin` from `@loopback/repository`).
123
124#### Options
125
126The options for this can be passed via `BootOptions` when calling
127`app.boot(options: BootOptions)`.
128
129The options for this are passed in a `repositories` object on `BootOptions`.
130
131Available options on the `repositories` object on `BootOptions` are as follows:
132
133| Options | Type | Default | Description |
134| ------------ | -------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------ |
135| `dirs` | `string \| string[]` | `['repositories']` | Paths relative to projectRoot to look in for Repository artifacts |
136| `extensions` | `string \| string[]` | `['.repository.js']` | File extensions to match for Repository artifacts |
137| `nested` | `boolean` | `true` | Look in nested directories in `dirs` for Repository artifacts |
138| `glob` | `string` | | A `glob` pattern string. This takes precedence over above 3 options (which are used to make a glob pattern). |
139
140### DataSourceBooter
141
142#### Description
143
144Discovers and binds DataSource Classes using `app.dataSource()` (Application
145must use `RepositoryMixin` from `@loopback/repository`).
146
147#### Options
148
149The options for this can be passed via `BootOptions` when calling
150`app.boot(options: BootOptions)`.
151
152The options for this are passed in a `datasources` object on `BootOptions`.
153
154Available options on the `datasources` object on `BootOptions` are as follows:
155
156| Options | Type | Default | Description |
157| ------------ | -------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------ |
158| `dirs` | `string \| string[]` | `['datasources']` | Paths relative to projectRoot to look in for DataSource artifacts |
159| `extensions` | `string \| string[]` | `['.datasource.js']` | File extensions to match for DataSource artifacts |
160| `nested` | `boolean` | `true` | Look in nested directories in `dirs` for DataSource artifacts |
161| `glob` | `string` | | A `glob` pattern string. This takes precedence over above 3 options (which are used to make a glob pattern). |
162
163### ServiceBooter
164
165#### Description
166
167Discovers and binds Service providers using `app.serviceProvider()` (Application
168must use `ServiceMixin` from `@loopback/service-proxy`).
169
170**IMPORTANT:** For a class to be recognized by `ServiceBooter` as a service
171provider, its name must end with `Provider` suffix and its prototype must have a
172`value()` method.
173
174#### Options
175
176The options for this can be passed via `BootOptions` when calling
177`app.boot(options: BootOptions)`.
178
179The options for this are passed in a `services` object on `BootOptions`.
180
181Available options on the `services` object on `BootOptions` are as follows:
182
183| Options | Type | Default | Description |
184| ------------ | -------------------- | ----------------- | ------------------------------------------------------------------------------------------------------------ |
185| `dirs` | `string \| string[]` | `['services']` | Paths relative to projectRoot to look in for Service artifacts |
186| `extensions` | `string \| string[]` | `['.service.js']` | File extensions to match for Service artifacts |
187| `nested` | `boolean` | `true` | Look in nested directories in `dirs` for Service artifacts |
188| `glob` | `string` | | A `glob` pattern string. This takes precedence over above 3 options (which are used to make a glob pattern). |
189
190## Contributions
191
192- [Guidelines](https://github.com/loopbackio/loopback-next/blob/master/docs/CONTRIBUTING.md)
193- [Join the team](https://github.com/loopbackio/loopback-next/issues/110)
194
195## Tests
196
197Run `npm test` from the root folder.
198
199## Contributors
200
201See
202[all contributors](https://github.com/loopbackio/loopback-next/graphs/contributors).
203
204## License
205
206MIT