Skip to content

Commit fdf9133

Browse files
committed
fix(boot): warn only if attempts to call app.repository without RepositoryMixin
console warning is excessive / clutters the logs for users. For someone not using the RepositoryMixin, they can enable debug to help identify the issue.
1 parent e5f7aca commit fdf9133

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

packages/boot/src/booters/repository.booter.ts

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,8 @@ export class RepositoryBooter extends BaseArtifactBooter {
2929
public repositoryOptions: ArtifactOptions = {},
3030
) {
3131
super();
32-
33-
/**
34-
* Repository Booter requires the use of RepositoryMixin (so we have `app.repository`)
35-
* for binding a Repository Class. We check for it's presence and run
36-
* accordingly.
37-
*/
38-
// tslint:disable-next-line:no-any
39-
if (!this.app.repository) {
40-
console.warn(
41-
'app.repository() function is needed for RepositoryBooter. You can add ' +
42-
'it to your Application using RepositoryMixin from @loopback/repository.',
43-
);
44-
45-
/**
46-
* If RepositoryMixin is not used and a `.repository()` function is not
47-
* available, we change the methods to be empty so bootstrapper can
48-
* still run without any side-effects of loading this Booter.
49-
*/
50-
this.configure = async () => {};
51-
this.discover = async () => {};
52-
this.load = async () => {};
53-
} else {
54-
// Set Repository Booter Options if passed in via bootConfig
55-
this.options = Object.assign({}, RepositoryDefaults, repositoryOptions);
56-
}
32+
// Set Repository Booter Options if passed in via bootConfig
33+
this.options = Object.assign({}, RepositoryDefaults, repositoryOptions);
5734
}
5835

5936
/**
@@ -62,10 +39,24 @@ export class RepositoryBooter extends BaseArtifactBooter {
6239
*/
6340
async load() {
6441
await super.load();
65-
this.classes.forEach(cls => {
66-
// tslint:disable-next-line:no-any
67-
this.app.repository(cls);
68-
});
42+
/**
43+
* If Repository Classes were discovered, we need to make sure RepositoryMixin
44+
* was used (so we have `app.repository()`) to perform the binding of a
45+
* Repository Class.
46+
*/
47+
if (this.classes.length > 0) {
48+
if (!this.app.repository) {
49+
console.warn(
50+
'app.repository() function is needed for RepositoryBooter. You can add ' +
51+
'it to your Application using RepositoryMixin from @loopback/repository.',
52+
);
53+
} else {
54+
this.classes.forEach(cls => {
55+
// tslint:disable-next-line:no-any
56+
this.app.repository(cls);
57+
});
58+
}
59+
}
6960
}
7061
}
7162

packages/boot/test/unit/booters/repository.booter.unit.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,24 @@ describe('repository booter unit tests', () => {
2323

2424
beforeEach(() => sandbox.reset());
2525
beforeEach(getApp);
26-
before(createStub);
27-
after(restoreStub);
26+
beforeEach(createStub);
27+
afterEach(restoreStub);
2828

2929
it('gives a warning if called on an app without RepositoryMixin', async () => {
3030
const normalApp = new Application();
31-
// tslint:disable-next-line:no-unused-expression
32-
new RepositoryBooter(normalApp as AppWithRepository, SANDBOX_PATH);
31+
await sandbox.copyFile(
32+
resolve(__dirname, '../../fixtures/multiple.artifact.js'),
33+
);
34+
35+
const booterInst = new RepositoryBooter(
36+
normalApp as AppWithRepository,
37+
SANDBOX_PATH,
38+
);
39+
40+
// Load uses discovered property
41+
booterInst.discovered = [resolve(SANDBOX_PATH, 'multiple.artifact.js')];
42+
await booterInst.load();
43+
3344
sinon.assert.calledOnce(stub);
3445
sinon.assert.calledWith(
3546
stub,

0 commit comments

Comments
 (0)