Skip to content

Emit schema excluding certain model properties#3297

Merged
nabdelgadir merged 6 commits into
masterfrom
exclude-model-schema
Jul 10, 2019
Merged

Emit schema excluding certain model properties#3297
nabdelgadir merged 6 commits into
masterfrom
exclude-model-schema

Conversation

@nabdelgadir

@nabdelgadir nabdelgadir commented Jul 2, 2019

Copy link
Copy Markdown
Contributor

See #2653.

  • Add exclude option to emit model schema excluding certain model properties.
  • Update POST in example applications to exclude id.
    • Update their docs for consistency.
  • Update the CLI templates to also exclude id from POST request body.

Checklist

👉 Read and sign the CLA (Contributor License Agreement) 👈

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

@bajtos bajtos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great patch! Let's improve few details please.

Comment thread packages/repository-json-schema/src/__tests__/unit/build-schema.unit.ts Outdated
Comment thread packages/repository-json-schema/src/__tests__/unit/build-schema.unit.ts Outdated
Comment thread packages/repository-json-schema/src/build-schema.ts Outdated
@nabdelgadir nabdelgadir force-pushed the exclude-model-schema branch from d8befdd to 969c5fe Compare July 4, 2019 14:20
@nabdelgadir nabdelgadir requested a review from bajtos July 4, 2019 14:21
@nabdelgadir nabdelgadir force-pushed the exclude-model-schema branch 2 times, most recently from b1975d2 to 0ea2feb Compare July 5, 2019 18:24

@bajtos bajtos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM with a small comment.

@nabdelgadir nabdelgadir force-pushed the exclude-model-schema branch from 0ea2feb to db19455 Compare July 8, 2019 12:32

@jannyHou jannyHou left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@nabdelgadir The fix looks reasonable in general 👍 Please note a model's id name is not always id, therefore we cannot hardcode the property name. You can get the id name by calling method like https://github.com/strongloop/loopback-next/blob/008c8b53bf00c576028b91d3a791ed2d6eafab7f/packages/repository/src/model.ts#L277

async create(
@param.path.<%= sourceModelPrimaryKeyType %>('id') id: typeof <%= sourceModelClassName %>.prototype.<%= sourceModelPrimaryKey %>,
@requestBody() <%= targetModelRequestBody %>: <%= targetModelClassName %>,
@requestBody({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The id field is not always called id, I think we need to get a model's id name in the template.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good point. AFAICT, lb4 model does allow users to create a primary key property with a different name than id.

Example prompts & answers to use pk instead of id:

Model Product will be created in src/models/product.model.ts

Let's add a property to Product
Enter an empty property name when done

? Enter the property name: pk
? Property type: string
? Is pk the ID property? Yes
? Is it required?: No
? Default value [leave blank for none]:

I agree with @jannyHou that we should honor custom PK name, there should be a new test to verify. If this is too much work then I can live with adding support for custom PK names in a follow-up pull request.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If this is too much work then I can live with adding support for custom PK names in a follow-up pull request.

Yeah forgot to say, feel free to address it in another PR :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will do in a separate PR 👍

},
},
})
todoList: Omit<TodoList, 'id'>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we support include for todoList: Pick<TodoList, 'id'>?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm fine to do it in a separate PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not sure what would you use include for? A kind of a white-list? That would make sense 👍

Let's discuss include and Pick in a separate PR. I think it's best to wait until there is (user) demand for that feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants