Skip to content

Commit 568307c

Browse files
fix(cli): fixed ds names that were hyphened
Now uses the functions created utils to refer properly to the src/datasources file name close #1791
1 parent 353b202 commit 568307c

File tree

3 files changed

+52
-43
lines changed

3 files changed

+52
-43
lines changed

packages/cli/generators/repository/index.js

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const ERROR_READING_FILE = 'Error reading file';
3131
const ERROR_NO_DATA_SOURCES_FOUND = 'No datasources found at';
3232
const ERROR_NO_MODELS_FOUND = 'No models found at';
3333
const ERROR_NO_MODEL_SELECTED = 'You did not select a valid model';
34-
const ERROR_NO_DIRECTORY = 'The directory was not found';
3534

3635
module.exports = class RepositoryGenerator extends ArtifactGenerator {
3736
// Note: arguments and options should be defined in the constructor.
@@ -66,8 +65,9 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
6665
if (!this.artifactInfo.dataSourceClass) {
6766
return;
6867
}
69-
let result = this._isConnectorOfType(
68+
let result = utils.isConnectorOfType(
7069
KEY_VALUE_CONNECTOR,
70+
this.artifactInfo.datasourcesDir,
7171
this.artifactInfo.dataSourceClass,
7272
);
7373
debug(`KeyValue Connector: ${result}`);
@@ -89,45 +89,6 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
8989
utils.toClassName(this.artifactInfo.dataSourceName) + 'DataSource';
9090
}
9191

92-
/**
93-
* load the connectors available and check if the basedModel matches any
94-
* connectorType supplied for the given connector name
95-
* @param {string} connectorType single or a comma separated string array
96-
*/
97-
_isConnectorOfType(connectorType, dataSourceClass) {
98-
debug(`calling isConnectorType ${connectorType}`);
99-
let jsonFileContent = '';
100-
let result = false;
101-
102-
if (!dataSourceClass) {
103-
return false;
104-
}
105-
let datasourceJSONFile = path.join(
106-
this.artifactInfo.datasourcesDir,
107-
dataSourceClass.replace('Datasource', '.datasource.json').toLowerCase(),
108-
);
109-
110-
try {
111-
jsonFileContent = this.fs.readJSON(datasourceJSONFile, {});
112-
} catch (err) {
113-
debug(`${ERROR_READING_FILE} ${datasourceJSONFile}: ${err.message}`);
114-
return this.exit(err);
115-
}
116-
117-
for (let connector of Object.values(connectors)) {
118-
const matchedConnector =
119-
jsonFileContent.connector === connector.name ||
120-
jsonFileContent.connector === `loopback-connector-${connector.name}`;
121-
122-
if (matchedConnector && connectorType.includes(connector.baseModel)) {
123-
result = true;
124-
break;
125-
}
126-
}
127-
128-
return result;
129-
}
130-
13192
_setupGenerator() {
13293
this.artifactInfo = {
13394
type: 'repository ',
@@ -238,9 +199,10 @@ module.exports = class RepositoryGenerator extends ArtifactGenerator {
238199
}
239200

240201
const availableDatasources = datasourcesList.filter(item => {
241-
debug(`data source unfiltered list: ${item}`);
242-
const result = this._isConnectorOfType(
202+
debug(`data source inspecting item: ${item}`);
203+
const result = utils.isConnectorOfType(
243204
VALID_CONNECTORS_FOR_REPOSITORY,
205+
this.artifactInfo.datasourcesDir,
244206
item,
245207
);
246208
return result;

packages/cli/test/fixtures/repository/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ exports.SANDBOX_FILES = [
3434
connector: 'memory',
3535
}),
3636
},
37+
{
38+
path: DATASOURCE_APP_PATH,
39+
file: 'my-ds.datasource.json',
40+
content: JSON.stringify({
41+
name: 'MyDS',
42+
connector: 'memory',
43+
}),
44+
},
3745
{
3846
path: DATASOURCE_APP_PATH,
3947
file: 'dbmem.datasource.ts',

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,45 @@ describe('lb4 repository', function() {
289289
);
290290
});
291291

292+
it('generates a crud repository from hyphened model file name', async () => {
293+
const basicPrompt = {
294+
dataSourceClass: 'MyDsDatasource',
295+
};
296+
await testUtils
297+
.executeGenerator(generator)
298+
.inDir(SANDBOX_PATH, () =>
299+
testUtils.givenLBProject(SANDBOX_PATH, {
300+
additionalFiles: SANDBOX_FILES,
301+
}),
302+
)
303+
.withPrompts(basicPrompt)
304+
.withArguments(' --model Defaultmodel');
305+
const expectedFile = path.join(
306+
SANDBOX_PATH,
307+
REPOSITORY_APP_PATH,
308+
'defaultmodel.repository.ts',
309+
);
310+
assert.file(expectedFile);
311+
assert.fileContent(
312+
expectedFile,
313+
/import {MyDSDataSource} from '..\/datasources';/,
314+
);
315+
assert.fileContent(
316+
expectedFile,
317+
/\@inject\('datasources.MyDS'\) protected datasource: MyDSDataSource,/,
318+
);
319+
assert.fileContent(
320+
expectedFile,
321+
/export class DefaultmodelRepository extends DefaultCrudRepository\</,
322+
);
323+
assert.fileContent(expectedFile, /typeof Defaultmodel.prototype.id/);
324+
assert.file(INDEX_FILE);
325+
assert.fileContent(
326+
INDEX_FILE,
327+
/export \* from '.\/defaultmodel.repository';/,
328+
);
329+
});
330+
292331
it('generates a crud repository from decorator defined model', async () => {
293332
await testUtils
294333
.executeGenerator(generator)

0 commit comments

Comments
 (0)