UNPKG

2.82 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. and LoopBack contributors 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.ModelDefaults = exports.ModelBooter = void 0;
8const tslib_1 = require("tslib");
9const core_1 = require("@loopback/core");
10const repository_1 = require("@loopback/repository");
11const debug_1 = tslib_1.__importDefault(require("debug"));
12const keys_1 = require("../keys");
13const types_1 = require("../types");
14const base_artifact_booter_1 = require("./base-artifact.booter");
15const debug = (0, debug_1.default)('loopback:boot:model-booter');
16/**
17 * A class that extends BaseArtifactBooter to boot the 'Model' artifact type.
18 *
19 * Supported phases: configure, discover, load
20 *
21 * @param app - Application instance
22 * @param projectRoot - Root of User Project relative to which all paths are resolved
23 * @param bootConfig - Model Artifact Options Object
24 */
25let ModelBooter = class ModelBooter extends base_artifact_booter_1.BaseArtifactBooter {
26 constructor(app, projectRoot, modelConfig = {}) {
27 super(projectRoot,
28 // Set Model Booter Options if passed in via bootConfig
29 Object.assign({}, exports.ModelDefaults, modelConfig));
30 this.app = app;
31 this.modelConfig = modelConfig;
32 }
33 /**
34 * Uses super method to get a list of Artifact classes. Boot each file by
35 * creating a DataSourceConstructor and binding it to the application class.
36 */
37 async load() {
38 await super.load();
39 for (const cls of this.classes) {
40 if (!isModelClass(cls)) {
41 debug('Skipping class %s - no @model is found', cls.name);
42 continue;
43 }
44 debug('Bind class: %s', cls.name);
45 // We are binding the model class itself
46 const binding = this.app.model(cls);
47 debug('Binding created for model class %s: %j', cls.name, binding);
48 }
49 }
50};
51exports.ModelBooter = ModelBooter;
52exports.ModelBooter = ModelBooter = tslib_1.__decorate([
53 (0, types_1.booter)('models'),
54 tslib_1.__param(0, (0, core_1.inject)(core_1.CoreBindings.APPLICATION_INSTANCE)),
55 tslib_1.__param(1, (0, core_1.inject)(keys_1.BootBindings.PROJECT_ROOT)),
56 tslib_1.__param(2, (0, core_1.config)()),
57 tslib_1.__metadata("design:paramtypes", [Object, String, Object])
58], ModelBooter);
59/**
60 * Default ArtifactOptions for DataSourceBooter.
61 */
62exports.ModelDefaults = {
63 dirs: ['models'],
64 extensions: ['.model.js'],
65 nested: true,
66};
67function isModelClass(cls) {
68 return repository_1.ModelMetadataHelper.getModelMetadata(cls) != null;
69}
70//# sourceMappingURL=model.booter.js.map
\No newline at end of file