Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions docs/site/Repository-generator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
lang: en
title: 'Repository generator'
keywords: LoopBack 4.0, LoopBack 4
sidebar: lb4_sidebar
permalink: /doc/en/lb4/Repository-generator.html
---

{% include content/generator-create-app.html lang=page.lang %}

### Synopsis

Adds a new
[Repository class (or multiple backed by the same datasource)](Repositories.md)
to a LoopBack application with one single command.

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

### Options

`--datasource` : _(Optional)_ name of a valid datasource already created in
src/datasources

`--model` : _(Optional)_ name of a valid model already created in src/models

`--id` : _(Optional)_ name of the property serving as **ID** in the selected
model. If you supply this value, the CLI will not try to infer this value from
the selected model file.

### Configuration file

This generator supports a config file with the following format, see the
Standard options below to see different ways you can supply this configuration
file.

```ts
{
"name": "repositoryNameToBeGenerated",
"datasource": "validDataSourceName",
"model": "validDModelName",
"id": "anOptionalNameForID"
}
```

### Notes

Service oriented datasources such as REST or SOAP are not considered valid in
this context and will not be presented to you in the selection list.

There should be at least one valid _(KeyValue or Persisted)_ data source and one
model already created in their respective directories.

{% include_relative includes/CLI-std-options.md %}

### Arguments

`<name>` - Optional argument specifyng the respository name to be generated. In
case you select multiple models, the first model will take this argument for its
repository file name.

### Interactive Prompts

The tool will prompt you for:

- **Please select the datasource.** _(name)_ If the name of the datasource had
been supplied from the command line, the prompt is skipped, otherwise it will
present you the list of available datasources to select one. It will use this
datasource to check what kind of repository it will generate.

- **Select the model(s) you want to generate a repository.** _(model)_ If the
name of the model had been supplied from the command line with `--model`
option and it is a valid model, then the prompt is skipped, otherwise it will
present the error `Error: No models found` in the console.

If no `--model` is supplied, then the it will present you with a valid list of
models from `src/models` directory and you will be able to select one or
multiple models. The tool will generate a repository for each of the selected
models.

**NOTE:** The tool will inspect each of the selected models and try to find
the name of the property serving as **ID** for the model.

- **Please enter the name of the ID property for _modelName_.** _(id)_ If the
CLI cannot find the corresponding ID property name for the model, it will
prompt you to enter a name here. If you don't specify any name, it will use
_id_ as the default one.

### Output

Once all the prompts have been answered, the CLI will do the following for each
of the selected models.

- Create a Repository class as follows:
`/src/repositories/${modelName}.repository.ts`
- Update `/src/repositories/index.ts` to export the newly created Repository
class.
4 changes: 4 additions & 0 deletions docs/site/sidebars/lb4_sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ children:
url: Model-generator.html
output: 'web, pdf'

- title: 'Repository generator'
url: Repository-generator.html
output: 'web, pdf'

- title: 'OpenAPI generator'
url: OpenAPI-generator.html
output: 'web, pdf'
Expand Down
6 changes: 6 additions & 0 deletions docs/site/tables/lb4-artifact-commands.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<td><a href="Model-generator.html">Model generator</a></td>
</tr>

<tr>
<td><code>lb4 repository</code></td>
<td>Add new repositories for selected model(s) to a LoopBack 4 application</td>
<td><a href="Repository-generator.html">Repository generator</a></td>
</tr>

<tr>
<td><code>lb4 openapi</code></td>
<td>Generate controllers and models from OpenAPI specs</td>
Expand Down
7 changes: 3 additions & 4 deletions packages/cli/generators/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
_setupGenerator() {
this.artifactInfo = {
type: 'model',
rootDir: 'src',
rootDir: utils.sourceRootDir,
};

this.artifactInfo.outDir = path.resolve(
this.artifactInfo.rootDir,
'models',
utils.modelsDir,
);

// Model Property Types
Expand Down Expand Up @@ -190,8 +190,7 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
debug('scaffolding');

// Data for templates
this.artifactInfo.fileName = utils.kebabCase(this.artifactInfo.name);
this.artifactInfo.outFile = `${this.artifactInfo.fileName}.model.ts`;
this.artifactInfo.outFile = utils.getModelFileName(this.artifactInfo.name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay for this simplification!


// Resolved Output Path
const tsPath = this.destinationPath(
Expand Down
Loading