1 | # @loopback/boot
|
2 |
|
3 | A convention based project Bootstrapper and Booters for LoopBack Applications
|
4 |
|
5 | ## Overview
|
6 |
|
7 | A Booter is a Class that can be bound to an Application and is called to perform
|
8 | a task before the Application is started. A Booter may have multiple phases to
|
9 | complete its task. The task for a convention based Booter is to discover and
|
10 | bind Artifacts (Controllers, Repositories, Models, etc.).
|
11 |
|
12 | An example task of a Booter may be to discover and bind all artifacts of a given
|
13 | type.
|
14 |
|
15 | A Bootstrapper is needed to manage the Booters and execute them. This is
|
16 | provided in this package. For ease of use, everything needed is packages using a
|
17 | BootMixin. This Mixin will add convenience methods such as `boot` and `booter`,
|
18 | as well as properties needed for Bootstrapper such as `projectRoot`. The Mixin
|
19 | also 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
|
31 | import {Application} from '@loopback/core';
|
32 | import {BootMixin, Booter, Binding} from '@loopback/boot';
|
33 | class BootApp extends BootMixin(Application) {}
|
34 |
|
35 | const app = new BootApp();
|
36 | app.projectRoot = __dirname;
|
37 |
|
38 | await app.boot();
|
39 | await app.start();
|
40 | ```
|
41 |
|
42 | ### BootOptions
|
43 |
|
44 | List 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
|
65 | future without warning**
|
66 |
|
67 | To use `BootExecOptions` you must directly call `bootstrapper.boot()` and pass
|
68 | in `BootExecOptions`. `app.boot()` provided by `BootMixin` does not take any
|
69 | parameters.
|
70 |
|
71 | ```ts
|
72 | const bootstrapper: Bootstrapper = await this.get(
|
73 | BootBindings.BOOTSTRAPPER_KEY,
|
74 | );
|
75 | const execOptions: BootExecOptions = {
|
76 | booters: [MyBooter1, MyBooter2],
|
77 | filter: {
|
78 | phases: ['configure', 'discover'],
|
79 | },
|
80 | };
|
81 |
|
82 | const ctx = bootstrapper.boot(execOptions);
|
83 | ```
|
84 |
|
85 | You 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 |
|
99 | Discovers and binds Controller Classes using `app.controller()`.
|
100 |
|
101 | #### Options
|
102 |
|
103 | The options for this can be passed via `BootOptions` when calling
|
104 | `app.boot(options: BootOptions)`.
|
105 |
|
106 | The options for this are passed in a `controllers` object on `BootOptions`.
|
107 |
|
108 | Available 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 |
|
121 | Discovers and binds Repository Classes using `app.repository()` (Application
|
122 | must use `RepositoryMixin` from `@loopback/repository`).
|
123 |
|
124 | #### Options
|
125 |
|
126 | The options for this can be passed via `BootOptions` when calling
|
127 | `app.boot(options: BootOptions)`.
|
128 |
|
129 | The options for this are passed in a `repositories` object on `BootOptions`.
|
130 |
|
131 | Available 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 |
|
144 | Discovers and binds DataSource Classes using `app.dataSource()` (Application
|
145 | must use `RepositoryMixin` from `@loopback/repository`).
|
146 |
|
147 | #### Options
|
148 |
|
149 | The options for this can be passed via `BootOptions` when calling
|
150 | `app.boot(options: BootOptions)`.
|
151 |
|
152 | The options for this are passed in a `datasources` object on `BootOptions`.
|
153 |
|
154 | Available 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 |
|
167 | Discovers and binds Service providers using `app.serviceProvider()` (Application
|
168 | must use `ServiceMixin` from `@loopback/service-proxy`).
|
169 |
|
170 | **IMPORTANT:** For a class to be recognized by `ServiceBooter` as a service
|
171 | provider, its name must end with `Provider` suffix and its prototype must have a
|
172 | `value()` method.
|
173 |
|
174 | #### Options
|
175 |
|
176 | The options for this can be passed via `BootOptions` when calling
|
177 | `app.boot(options: BootOptions)`.
|
178 |
|
179 | The options for this are passed in a `services` object on `BootOptions`.
|
180 |
|
181 | Available 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 |
|
197 | Run `npm test` from the root folder.
|
198 |
|
199 | ## Contributors
|
200 |
|
201 | See
|
202 | [all contributors](https://github.com/loopbackio/loopback-next/graphs/contributors).
|
203 |
|
204 | ## License
|
205 |
|
206 | MIT
|