1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {config, Constructor, CoreBindings, inject} from '@loopback/core';
|
7 | import {
|
8 | ApplicationWithRepositories,
|
9 | ModelMetadataHelper,
|
10 | } from '@loopback/repository';
|
11 | import debugFactory from 'debug';
|
12 | import {BootBindings} from '../keys';
|
13 | import {ArtifactOptions, booter} from '../types';
|
14 | import {BaseArtifactBooter} from './base-artifact.booter';
|
15 |
|
16 | const debug = debugFactory('loopback:boot:model-booter');
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | @booter('models')
|
28 | export class ModelBooter extends BaseArtifactBooter {
|
29 | constructor(
|
30 | @inject(CoreBindings.APPLICATION_INSTANCE)
|
31 | public app: ApplicationWithRepositories,
|
32 | @inject(BootBindings.PROJECT_ROOT) projectRoot: string,
|
33 | @config()
|
34 | public modelConfig: ArtifactOptions = {},
|
35 | ) {
|
36 | super(
|
37 | projectRoot,
|
38 |
|
39 | Object.assign({}, ModelDefaults, modelConfig),
|
40 | );
|
41 | }
|
42 |
|
43 | |
44 |
|
45 |
|
46 |
|
47 | async load() {
|
48 | await super.load();
|
49 |
|
50 | for (const cls of this.classes) {
|
51 | if (!isModelClass(cls)) {
|
52 | debug('Skipping class %s - no @model is found', cls.name);
|
53 | continue;
|
54 | }
|
55 |
|
56 | debug('Bind class: %s', cls.name);
|
57 |
|
58 | const binding = this.app.model(cls);
|
59 | debug('Binding created for model class %s: %j', cls.name, binding);
|
60 | }
|
61 | }
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | export const ModelDefaults: ArtifactOptions = {
|
68 | dirs: ['models'],
|
69 | extensions: ['.model.js'],
|
70 | nested: true,
|
71 | };
|
72 |
|
73 | function isModelClass(cls: Constructor<unknown>) {
|
74 | return ModelMetadataHelper.getModelMetadata(cls) != null;
|
75 | }
|