UNPKG

2.48 kBPlain TextView Raw
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
6import {config, CoreBindings, inject} from '@loopback/core';
7import {ApplicationWithRepositories} from '@loopback/repository';
8import {BootBindings} from '../keys';
9import {ArtifactOptions, booter} from '../types';
10import {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@booter('repositories')
24export class RepositoryBooter extends BaseArtifactBooter {
25 constructor(
26 @inject(CoreBindings.APPLICATION_INSTANCE)
27 public app: ApplicationWithRepositories,
28 @inject(BootBindings.PROJECT_ROOT) projectRoot: string,
29 @config()
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 */
68export const RepositoryDefaults: ArtifactOptions = {
69 dirs: ['repositories'],
70 extensions: ['.repository.js'],
71 nested: true,
72};