UNPKG

2.92 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
3// Node module: @loopback/boot
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.DataSourceDefaults = exports.DataSourceBooter = void 0;
8const tslib_1 = require("tslib");
9const core_1 = require("@loopback/core");
10const keys_1 = require("../keys");
11const types_1 = require("../types");
12const base_artifact_booter_1 = require("./base-artifact.booter");
13/**
14 * A class that extends BaseArtifactBooter to boot the 'DataSource' artifact type.
15 * Discovered DataSources are bound using `app.dataSource()`.
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 - DataSource Artifact Options Object
22 */
23let DataSourceBooter = class DataSourceBooter extends base_artifact_booter_1.BaseArtifactBooter {
24 constructor(app, projectRoot, datasourceConfig = {}) {
25 super(projectRoot,
26 // Set DataSource Booter Options if passed in via bootConfig
27 Object.assign({}, exports.DataSourceDefaults, datasourceConfig));
28 this.app = app;
29 this.datasourceConfig = datasourceConfig;
30 }
31 /**
32 * Uses super method to get a list of Artifact classes. Boot each file by
33 * creating a DataSourceConstructor and binding it to the application class.
34 */
35 async load() {
36 await super.load();
37 /**
38 * If DataSource Classes were discovered, we need to make sure RepositoryMixin
39 * was used (so we have `app.dataSource()`) to perform the binding of a
40 * DataSource Class.
41 */
42 if (this.classes.length > 0) {
43 if (!this.app.dataSource) {
44 console.warn('app.dataSource() function is needed for DataSourceBooter. You can add ' +
45 'it to your Application using RepositoryMixin from @loopback/repository.');
46 }
47 else {
48 this.classes.forEach(cls => {
49 this.app.dataSource(cls);
50 });
51 }
52 }
53 }
54};
55DataSourceBooter = tslib_1.__decorate([
56 (0, types_1.booter)('datasources'),
57 tslib_1.__param(0, (0, core_1.inject)(core_1.CoreBindings.APPLICATION_INSTANCE)),
58 tslib_1.__param(1, (0, core_1.inject)(keys_1.BootBindings.PROJECT_ROOT)),
59 tslib_1.__param(2, (0, core_1.config)()),
60 tslib_1.__metadata("design:paramtypes", [Object, String, Object])
61], DataSourceBooter);
62exports.DataSourceBooter = DataSourceBooter;
63/**
64 * Default ArtifactOptions for DataSourceBooter.
65 */
66exports.DataSourceDefaults = {
67 dirs: ['datasources'],
68 extensions: ['.datasource.js'],
69 nested: true,
70};
71//# sourceMappingURL=datasource.booter.js.map
\No newline at end of file