1 | // Copyright IBM Corp. and LoopBack contributors 2018,2019. All Rights Reserved.
|
2 | // Node module: @loopback/boot
|
3 | // This file is licensed under the MIT License.
|
4 | // License text available at https://opensource.org/licenses/MIT
|
5 |
|
6 | import {config, CoreBindings, inject} from '@loopback/core';
|
7 | import {ApplicationWithRepositories} from '@loopback/repository';
|
8 | import {BootBindings} from '../keys';
|
9 | import {ArtifactOptions, booter} from '../types';
|
10 | import {BaseArtifactBooter} from './base-artifact.booter';
|
11 |
|
12 | /**
|
13 | * A class that extends BaseArtifactBooter to boot the 'Repository' artifact type.
|
14 | * Discovered repositories are bound using `app.repository()` which must be added
|
15 | * to an Application using the `RepositoryMixin` from `@loopback/repository`.
|
16 | *
|
17 | * Supported phases: configure, discover, load
|
18 | *
|
19 | * @param app - Application instance
|
20 | * @param projectRoot - Root of User Project relative to which all paths are resolved
|
21 | * @param bootConfig - Repository Artifact Options Object
|
22 | */
|
23 | 'repositories')
( |
24 | export class RepositoryBooter extends BaseArtifactBooter {
|
25 | constructor(
|
26 | (CoreBindings.APPLICATION_INSTANCE)
|
27 | public app: ApplicationWithRepositories,
|
28 | string,
(BootBindings.PROJECT_ROOT) projectRoot: |
29 | ()
|
30 | public repositoryOptions: ArtifactOptions = {},
|
31 | ) {
|
32 | super(
|
33 | projectRoot,
|
34 | // Set Repository Booter Options if passed in via bootConfig
|
35 | Object.assign({}, RepositoryDefaults, repositoryOptions),
|
36 | );
|
37 | }
|
38 |
|
39 | /**
|
40 | * Uses super method to get a list of Artifact classes. Boot each class by
|
41 | * binding it to the application using `app.repository(repository);` if present.
|
42 | */
|
43 | async load() {
|
44 | await super.load();
|
45 | /**
|
46 | * If Repository Classes were discovered, we need to make sure RepositoryMixin
|
47 | * was used (so we have `app.repository()`) to perform the binding of a
|
48 | * Repository Class.
|
49 | */
|
50 | if (this.classes.length > 0) {
|
51 | if (!this.app.repository) {
|
52 | console.warn(
|
53 | 'app.repository() function is needed for RepositoryBooter. You can add ' +
|
54 | 'it to your Application using RepositoryMixin from @loopback/repository.',
|
55 | );
|
56 | } else {
|
57 | this.classes.forEach(cls => {
|
58 | this.app.repository(cls);
|
59 | });
|
60 | }
|
61 | }
|
62 | }
|
63 | }
|
64 |
|
65 | /**
|
66 | * Default ArtifactOptions for RepositoryBooter.
|
67 | */
|
68 | export const RepositoryDefaults: ArtifactOptions = {
|
69 | dirs: ['repositories'],
|
70 | extensions: ['.repository.js'],
|
71 | nested: true,
|
72 | };
|