loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

[CLI] Add `--config` support for `lb4 model`

Open virkt25 opened this issue 8 years ago • 13 comments

Acceptance Criteria

Add support for --config for lb4 model as follows:

  • [ ] --config accepts an Object with a properties object that describes the properties for a model

    • [ ] If there are properties present, skip the prompts for properties and just generate the file.
  • [ ] This will allow us to test property generation logic (not the prompts) BY feeding in the properties via json and then checking the generated model. WRITE tests for testing the different types of properties and options available for properties to ensure we generate correct files.

virkt25 avatar Jul 11 '18 15:07 virkt25

I don't think there is need to introduce --json as we already have --config which takes a json file or stringified json.

raymondfeng avatar Jul 11 '18 15:07 raymondfeng

Oh, I might've confused the flags ... but it's basically adding support for the --config flag (I thought it was --json) ... by skipping over the prompts for properties if supplied by a user.

virkt25 avatar Jul 11 '18 15:07 virkt25

Adding a use case for @virkt25 proposal. We use models that have more than 20,30 columns. We create manually our models and for this scenario we would not be using an lb4 model wizard that asks each property step by step. But we would like an option that could generate a basic repository and a model class out of this definition file automatically. That could save us a lot of time.

lb4 model --config src/models/large.definition.ts

And then it would create the src/models/large.model.ts and src/repositories/large.repository.ts automatically, that could help :-)

marioestradarosa avatar Jul 12 '18 18:07 marioestradarosa

@marioestradarosa, It might be a stupid question... what would be the difference between large.definition.ts that user provides and large.model.ts that LB CLI generates?

dhmlau avatar Aug 16 '18 01:08 dhmlau

@dhmlau 😄 forgive me, but I think I am the one who made the mistake for the .ts extension. I meant .json extension and I meant having a large table defined in json format (LB3 ), we should provide a way to migrate to LB4 with single command

lb4 model --config-json="anydirectory/large.table.json

or

lb4 model --config-json="anydirectory/large.table.json --with-repository

and it would create the LB4 artifacts in src/models

By large, I meant a table with several columns. I believe for demos is fine to have the lb4 model to ask for properties etc., but in large applications that wouldn't work. So this feature makes sense for two scenarios:

  1. As part of a migration path from LB3 to LB4
  2. As an option for enterprise applications where we have the database designed already and in production.

Developers can create these json files compatible with juggler easily out of their production schema (reverse-engineer more or less)

I hope this clarifies what I meant and thank you for answering. Please let me know if my comment makes sense and is aligned with @virkt25 request.

marioestradarosa avatar Aug 16 '18 02:08 marioestradarosa

Makes perfect sense to me @marioestradarosa. @dhmlau The json can be considered LB3 equivalent of a ModelDefinition json file.

For --with-repository I'll be making a new issue later today to track how we want generators to be composed because calling model, repository, controller can be a bit annoying and I see it as a separate feature than support for --config

virkt25 avatar Aug 16 '18 13:08 virkt25

got it. Thanks!

dhmlau avatar Aug 16 '18 14:08 dhmlau

Looks like this is working. WIth the following model.json, I can use lb4 model --config model.config.json --yes to create the model. But it seems like the need to have --yes is not trivial.

{
    "name": "Customer",
    "base": "Entity",
    "properties": {
        "id": {
            "type": "number",
            "id": true,
            "required": false
        },
        "name": {
            "type": "string",
            "length": 200,
            "id": false,
            "required": true
        }
    }
}

dhmlau avatar Oct 26 '18 19:10 dhmlau

Hi,

I have a problem with generation model with inner attributes (property properties) for example:

{
 "id": {
      "type": "string",
      "annotations": [
        {
          "destinationClass": "className",
          "destinationField": "name",
          "prefix": "prefix"
        }
      ],
      "required": true,
      "id": true
    },
    "name": {
      "type": "string",
      "required": true
    }
}

The Model look like:

{
@property({
    type: 'string',
    annotations: [object Object],
    required: true,
    id: true,
  })
  id: string;

  @property({
    type: 'string',
    required: true,
  })
  name: string;
}

Is it possible to generate Model with correct property properties?

yanamg7 avatar Jan 10 '19 13:01 yanamg7

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

stale[bot] avatar Jan 09 '20 11:01 stale[bot]

IIUC, the problem is in the way how we are emitting property metadata fields.

@property({
  // ...
  annotations: [object Object],
})

In https://github.com/strongloop/loopback-next/pull/4180, we fixed the problem for model discovery. Unfortunately that fix was not generic enough to address other use cases.

I think we should improve the template to handle nested objects in all property metadata fields. You can check the model generator to see how it deals with this problem.

bajtos avatar Jan 10 '20 16:01 bajtos

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

stale[bot] avatar Dec 25 '20 16:12 stale[bot]

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

stale[bot] avatar Jul 14 '21 11:07 stale[bot]