Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(repository): include navigation properties in JSON #3188

Merged
merged 1 commit into from
Jun 20, 2019

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Jun 20, 2019

Modify the code converting Model instances to JSON data to include navigational properties for the defined model relations.

See #3171 and #2633

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 👈

Modify the code converting Model instances to JSON data to include
navigational properties for the defined model relations.

Signed-off-by: Miroslav Bajtoš <[email protected]>
@bajtos bajtos added feature Repository Issues related to @loopback/repository package labels Jun 20, 2019
@bajtos bajtos added this to the June 2019 milestone milestone Jun 20, 2019
@bajtos bajtos requested review from raymondfeng, nabdelgadir and a team June 20, 2019 11:11
@bajtos bajtos self-assigned this Jun 20, 2019
Copy link
Contributor

@nabdelgadir nabdelgadir left a comment

Choose a reason for hiding this comment

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

👏

@nabdelgadir
Copy link
Contributor

From #3171:

it('includes TodoList in query result', async () => {
    const list = await givenTodoListInstance(todoListRepo);
    const todo = await givenTodoInstance(todoRepo, {todoListId: list.id});
    const filter = JSON.stringify({include: [{relation: 'todoList'}]});
    const response = await client.get('/todos').query({filter: filter});
    expect(response.body).to.have.length(1);
    expect(response.body[0]).to.deepEqual({
      ...toJSON(todo),
      todoList: toJSON(list),
});

This test along with the symmetrical includes Todo test were both failing because:

From @bajtos:

As for describing navigational properties in model definitions: originally, @hasMany was calling @property decorator under the hood. This had its own problems though, for example the navigational properties were then described in the request body of "create" and "update" operations, which is not desirable. That's why we moved away from the design where navigational properties are part of the model schema.

I think we need to find a way allowing Model's toJSON (and toObject) methods to understand that even in strict mode where no extra properties are allowed, the navigational properties should be still included in the result.


target: () => Product,
keyTo: 'categoryId',
});
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: IIRC, {...} as HasManyDefinition is a recommended style over <HasManyDefinition>{...}.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense. From what I remember, when using {...} as HasManyDefinition, TypeScript is not able to offer correct suggestions (auto-completion) for members of the object being created :(

@raymondfeng raymondfeng merged commit 008c8b5 into master Jun 20, 2019
@delete-merged-branch delete-merged-branch bot deleted the feat/include-navigational-props-in-json branch June 20, 2019 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Repository Issues related to @loopback/repository package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants