1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {config, CoreBindings, inject} from '@loopback/core';
|
7 | import {
|
8 | ApplicationWithRepositories,
|
9 | Class,
|
10 | juggler,
|
11 | } from '@loopback/repository';
|
12 | import {BootBindings} from '../keys';
|
13 | import {ArtifactOptions, booter} from '../types';
|
14 | import {BaseArtifactBooter} from './base-artifact.booter';
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | @booter('datasources')
|
27 | export class DataSourceBooter extends BaseArtifactBooter {
|
28 | constructor(
|
29 | @inject(CoreBindings.APPLICATION_INSTANCE)
|
30 | public app: ApplicationWithRepositories,
|
31 | @inject(BootBindings.PROJECT_ROOT) projectRoot: string,
|
32 | @config()
|
33 | public datasourceConfig: ArtifactOptions = {},
|
34 | ) {
|
35 | super(
|
36 | projectRoot,
|
37 |
|
38 | Object.assign({}, DataSourceDefaults, datasourceConfig),
|
39 | );
|
40 | }
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 | async load() {
|
47 | await super.load();
|
48 |
|
49 | |
50 |
|
51 |
|
52 |
|
53 |
|
54 | if (this.classes.length > 0) {
|
55 | if (!this.app.dataSource) {
|
56 | console.warn(
|
57 | 'app.dataSource() function is needed for DataSourceBooter. You can add ' +
|
58 | 'it to your Application using RepositoryMixin from @loopback/repository.',
|
59 | );
|
60 | } else {
|
61 | this.classes.forEach(cls => {
|
62 | this.app.dataSource(cls as Class<juggler.DataSource>);
|
63 | });
|
64 | }
|
65 | }
|
66 | }
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | export const DataSourceDefaults: ArtifactOptions = {
|
73 | dirs: ['datasources'],
|
74 | extensions: ['.datasource.js'],
|
75 | nested: true,
|
76 | };
|