loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.95k stars 1.07k forks source link

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

Closed virkt25 closed 3 years ago

virkt25 commented 6 years ago

Acceptance Criteria

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

raymondfeng commented 6 years ago

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

virkt25 commented 6 years ago

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.

marioestradarosa commented 6 years ago

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 :-)

dhmlau commented 6 years ago

@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?

marioestradarosa commented 6 years ago

@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.

virkt25 commented 6 years ago

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

dhmlau commented 6 years ago

got it. Thanks!

dhmlau commented 5 years ago

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
        }
    }
}
yanamg7 commented 5 years ago

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?

stale[bot] commented 4 years ago

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.

bajtos commented 4 years ago

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.

stale[bot] commented 3 years ago

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] commented 3 years ago

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.