Skip to content

Commit 6e6faad

Browse files
feat(cli): add test for multiple repositories
1 parent 26915e5 commit 6e6faad

File tree

7 files changed

+84
-22
lines changed

7 files changed

+84
-22
lines changed

docs/site/Repository-generator.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ permalink: /doc/en/lb4/Repository-generator.html
1010

1111
### Synopsis
1212

13-
Adds a new [Repository or Multiple Repositories](Repositories.md) class to a
14-
LoopBack application with one single command.
13+
Adds a new
14+
[Repository class (or multiple backed by the same datasource)](Repositories.md)
15+
to a LoopBack application with one single command.
1516

1617
```sh
1718
lb4 repository [options] [<name>]

docs/site/tables/lb4-artifact-commands.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<tr>
3333
<td><code>lb4 repository</code></td>
34-
<td>Add a new repository or multiple repositories to a LoopBack 4 application</td>
34+
<td>Add new repositories for selected model(s) to a LoopBack 4 application</td>
3535
<td><a href="Repository-generator.html">Repository generator</a></td>
3636
</tr>
3737

packages/cli/generators/repository/index.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
4343
* get the property name for the id field
4444
* @param {string} modelName
4545
*/
46-
async _getModelIdType(modelName) {
46+
async _getModelIdProperty(modelName) {
4747
let fileContent = '';
4848
let modelFile = path.join(
4949
this.artifactInfo.modelDir,
@@ -157,6 +157,9 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
157157
utils.modelsDir,
158158
);
159159

160+
// to be able to write multiple files to the index.ts
161+
this.artifactInfo.indexesToBeUpdated = [];
162+
160163
this.artifactInfo.defaultTemplate = REPOSITORY_CRUD_TEMPLATE;
161164

162165
this.option('model', {
@@ -354,7 +357,7 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
354357

355358
async promptModelId() {
356359
if (this.shouldExit()) return false;
357-
let idType;
360+
let idProperty;
358361

359362
debug(`Model ID property name from command line: ${this.options.id}`);
360363
debug(`Selected Models: ${this.artifactInfo.modelNameList}`);
@@ -379,19 +382,19 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
379382
if (this.options.id) {
380383
debug(`passing thru this.options.id with value : ${this.options.id}`);
381384

382-
idType = this.options.id;
385+
idProperty = this.options.id;
383386
/** make sure it is only used once, in case user selected more
384387
* than one model.
385388
*/
386389
delete this.options.id;
387390
} else {
388-
idType = await this._getModelIdType(item);
389-
if (idType === null) {
391+
idProperty = await this._getModelIdProperty(item);
392+
if (idProperty === null) {
390393
const answer = await this.prompt(prompts);
391-
idType = answer.propertyName;
394+
idProperty = answer.propertyName;
392395
}
393396
}
394-
this.artifactInfo.idType = idType;
397+
this.artifactInfo.idProperty = idProperty;
395398
// Generate this repository
396399
await this._scaffold();
397400
}
@@ -413,13 +416,15 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
413416
this.artifactInfo.className = utils.toClassName(
414417
this.artifactInfo.modelName,
415418
);
419+
416420
this.artifactInfo.outFile = utils.getRepositoryFileName(
417421
this.artifactInfo.modelName,
418422
);
419-
}
420423

421-
if (debug.enabled) {
422-
debug(`Artifact output filename set to: ${this.artifactInfo.outFile}`);
424+
this.artifactInfo.indexesToBeUpdated.push({
425+
dir: this.artifactInfo.outDir,
426+
file: this.artifactInfo.outFile,
427+
});
423428
}
424429

425430
const source = this.templatePath(
@@ -430,9 +435,6 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
430435
),
431436
);
432437

433-
if (debug.enabled) {
434-
debug(`Using template at: ${source}`);
435-
}
436438
const dest = this.destinationPath(
437439
path.join(this.artifactInfo.outDir, this.artifactInfo.outFile),
438440
);

packages/cli/generators/repository/templates/src/repositories/repository-crud-default-template.ts.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {inject} from '@loopback/core';
55

66
export class <%= className %>Repository extends <%= repositoryTypeClass %><
77
<%= modelName %>,
8-
typeof <%= modelName %>.prototype.<%= idType %>
8+
typeof <%= modelName %>.prototype.<%= idProperty %>
99
> {
1010
constructor(
1111
@inject('datasources.<%= dataSourceName %>') protected datasource: <%= dataSourceClassName %>,

packages/cli/generators/repository/templates/src/repositories/repository-kv-template.ts.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export class <%= className %>Repository extends <%= repositoryTypeClass %><
99
constructor(
1010
@inject('datasources.<%= dataSourceName %>') datasource: <%= dataSourceClassName %>,
1111
) {
12-
super(<%= modelName %>,datasource);
12+
super(<%= modelName %>, datasource);
1313
}
1414
}

packages/cli/lib/artifact-generator.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,19 @@ module.exports = class ArtifactGenerator extends BaseGenerator {
129129
// Index Update Disabled
130130
if (this.artifactInfo.disableIndexUpdate) return;
131131

132+
if (!this.artifactInfo.indexesToBeUpdated) {
133+
this.artifactInfo.indexesToBeUpdated = [];
134+
}
135+
132136
// No Array given for Index Update, Create default array
133137
if (
134-
!this.artifactInfo.indexesToBeUpdated &&
135138
this.artifactInfo.outDir &&
136-
this.artifactInfo.outFile
139+
this.artifactInfo.outFile &&
140+
this.artifactInfo.indexesToBeUpdated.length === 0
137141
) {
138142
this.artifactInfo.indexesToBeUpdated = [
139143
{dir: this.artifactInfo.outDir, file: this.artifactInfo.outFile},
140144
];
141-
} else {
142-
this.artifactInfo.indexesToBeUpdated = [];
143145
}
144146

145147
for (const idx of this.artifactInfo.indexesToBeUpdated) {

packages/cli/test/integration/generators/repository.integration.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,63 @@ describe('lb4 repository', () => {
2828

2929
// special cases regardless of the repository type
3030
describe('generate repositories on special conditions', () => {
31+
it('generates multipe crud repositories', async () => {
32+
const multiItemPrompt = {
33+
dataSourceClass: 'DbmemDatasource',
34+
modelNameList: ['MultiWord', 'Defaultmodel'],
35+
};
36+
37+
await testUtils
38+
.executeGenerator(generator)
39+
.inDir(
40+
SANDBOX_PATH,
41+
async () => await prepareGeneratorForRepository(SANDBOX_PATH),
42+
)
43+
.withPrompts(multiItemPrompt);
44+
45+
const expectedMultiWordFile = path.join(
46+
SANDBOX_PATH,
47+
REPOSITORY_APP_PATH,
48+
'multi-word.repository.ts',
49+
);
50+
const expectedDefaultModelFile = path.join(
51+
SANDBOX_PATH,
52+
REPOSITORY_APP_PATH,
53+
'defaultmodel.repository.ts',
54+
);
55+
56+
assert.file(expectedMultiWordFile);
57+
assert.file(expectedDefaultModelFile);
58+
59+
assert.fileContent(
60+
expectedMultiWordFile,
61+
/export class MultiWordRepository extends DefaultCrudRepository</,
62+
);
63+
assert.fileContent(
64+
expectedMultiWordFile,
65+
/typeof MultiWord.prototype.pk/,
66+
);
67+
68+
assert.fileContent(
69+
expectedDefaultModelFile,
70+
/export class DefaultmodelRepository extends DefaultCrudRepository\</,
71+
);
72+
assert.fileContent(
73+
expectedDefaultModelFile,
74+
/typeof Defaultmodel.prototype.id/,
75+
);
76+
77+
assert.file(INDEX_FILE);
78+
assert.fileContent(
79+
INDEX_FILE,
80+
/export \* from '.\/multi-word.repository';/,
81+
);
82+
assert.fileContent(
83+
INDEX_FILE,
84+
/export \* from '.\/defaultmodel.repository';/,
85+
);
86+
});
87+
3188
it('generates a multi-word crud repository', async () => {
3289
const multiItemPrompt = {
3390
dataSourceClass: 'DbmemDatasource',

0 commit comments

Comments
 (0)