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(model): add support to nested keys for relations #127

Merged

Conversation

JoaoPedroAS51
Copy link
Collaborator

@JoaoPedroAS51 JoaoPedroAS51 commented Oct 10, 2020

As requested by @francoism90, this PR add support for nested keys e.g. relationships.tags

Usage

You can apply a model instance to a nested object by setting the key and the model in relations method.

If the backend responds with:

// response from API for /posts/1
{
  title: 'My title'
  body: 'Some text here',
  relationships: {
    tags: [
      {
        name: 'super'
      },
      {
        name: 'awesome'
      }
    ]
  }
}

We just need to set relationships.tags to Tag model:

/models/Post.js

class Post extends Model {
  relations () {
    return {
      // Apply Tag model to each object of `relationships.tags` array
      'relationships.tags': Tag
    }
  }
}

@francoism90
Copy link
Contributor

@JoaoPedroAS51 Many thanks 👍

@robsontenorio
Copy link
Owner

@JoaoPedroAS51 muito bom! Você poderia me mandar um DM @robsontenorio no Twitter?

@JoaoPedroAS51
Copy link
Collaborator Author

JoaoPedroAS51 commented Oct 11, 2020

@robsontenorio Vlw! :)
Eu não uso o Twitter. Pode ser Discord? Joao Pedro AS51#1284

@francoism90
Copy link
Contributor

@robsontenorio Sorry for asking, but is this going to be merged/planned to be supported? :)

@JoaoPedroAS51
Copy link
Collaborator Author

@francoism90 Yes, it will be merged soon. :)

@JoaoPedroAS51 JoaoPedroAS51 changed the base branch from master to dev October 22, 2020 01:05
@JoaoPedroAS51 JoaoPedroAS51 merged commit 095c1c3 into robsontenorio:dev Oct 22, 2020
@JoaoPedroAS51 JoaoPedroAS51 deleted the feat/instance-nested-objects branch October 22, 2020 01:20
JoaoPedroAS51 added a commit to JoaoPedroAS51/vue-api-query that referenced this pull request Oct 27, 2020
* docs: add nuxt docs

* docs: add introduction, installation, configuration, usage and nuxt example

* docs: add api docs

* docs: update configuration and usage

* ci: add nodejs ci

* ci: fix setup node version

* chore(eslint): add .eslintignore

* chore(scripts): add "lint" script

* chore(scripts): add "test-watch" script

* ci(docs): update deploy action

* ci: fix docs and publish

* ci: fix docs working directory

* docs: fix url

* docs: fix base url

* ci: fix docs action

* docs: fix pages position

* chore(script): add `--coverage` flag to `test` script

* ci(publish): add step to upload coverage to codecov

* chore(scripts): remove "prepare" script

* fix(model): add null check in `isValidId` (robsontenorio#115)

When an entity is being saved, nulls are considered as existing entities, so it tries to update and not insert.

* feat(model): handle 'data' wrapped responses for create and update (robsontenorio#109)

Laravel allows data wrapping for all responses, so create and update requests should support this if it is done.

* feat(model): add support to nested keys for relations (robsontenorio#127)

* feat(utils): add utilities `getProp` and `setProp`

* feat(model): add support to nested keys for relations

* test(model): test support of nested keys for relations

* test(utils): test utilities

* chore: update readme

* chore: bump node-fetch from 2.6.0 to 2.6.1 (robsontenorio#124)

Bumps [node-fetch](https://github.com/bitinn/node-fetch) from 2.6.0 to 2.6.1.
- [Release notes](https://github.com/bitinn/node-fetch/releases)
- [Changelog](https://github.com/node-fetch/node-fetch/blob/master/docs/CHANGELOG.md)
- [Commits](node-fetch/node-fetch@v2.6.0...v2.6.1)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* docs: update configuration and api options

* docs: update api docs

* docs(model options): update docs

* docs(query builder methods): update docs

* docs(model options): add `primaryKey` default value

* docs(model options): fix typo

* docs(example): rename file of nuxt example

* docs: remove usage section

* docs: add building query section

* docs: fix typo

* docs: add  "including relationships"

* docs: add helpers

* docs(operations): fix typo

* docs(api): update query builder methods

* docs: fix response of section "including relationships"

* docs: add "appending attributes"

* docs: add "selecting fields" and update other docs

* docs: add "paginating" and add missing colons.

* docs: add api reference to "selecting fields" section

* docs: update "paginating" section

* docs: add "applying parameters"

* docs: add "calling a custom resource"

* docs: add api reference of `params` and `custom`

* docs: add "composing without request"

* docs: remove unnecessary responses

* docs: update "calling a custom resource"

* docs: fix crud order

* docs: fix create request

* docs: update example of delete operation

* docs: add "mixing everything up"

* docs: fix append example

* docs: add "performing operations"

* docs: update `select` and `custom`

* docs: update model options

* docs: update configuration

* docs: add vue example

Co-authored-by: Guilherme Zanini <gui_rz@hotmail.com>
Co-authored-by: Francis Adu-Gyamfi <francis.adu-gyamfi@topgolf.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
JoaoPedroAS51 added a commit to JoaoPedroAS51/vue-api-query that referenced this pull request Oct 27, 2020
* docs: add nuxt docs

* docs: add introduction, installation, configuration, usage and nuxt example

* docs: add api docs

* docs: update configuration and usage

* ci: add nodejs ci

* ci: fix setup node version

* chore(eslint): add .eslintignore

* chore(scripts): add "lint" script

* chore(scripts): add "test-watch" script

* ci(docs): update deploy action

* ci: fix docs and publish

* ci: fix docs working directory

* docs: fix url

* docs: fix base url

* ci: fix docs action

* docs: fix pages position

* chore(script): add `--coverage` flag to `test` script

* ci(publish): add step to upload coverage to codecov

* chore(scripts): remove "prepare" script

* fix(model): add null check in `isValidId` (robsontenorio#115)

When an entity is being saved, nulls are considered as existing entities, so it tries to update and not insert.

* feat(model): handle 'data' wrapped responses for create and update (robsontenorio#109)

Laravel allows data wrapping for all responses, so create and update requests should support this if it is done.

* feat(model): add support to nested keys for relations (robsontenorio#127)

* feat(utils): add utilities `getProp` and `setProp`

* feat(model): add support to nested keys for relations

* test(model): test support of nested keys for relations

* test(utils): test utilities

* chore: update readme

* chore: bump node-fetch from 2.6.0 to 2.6.1 (robsontenorio#124)

Bumps [node-fetch](https://github.com/bitinn/node-fetch) from 2.6.0 to 2.6.1.
- [Release notes](https://github.com/bitinn/node-fetch/releases)
- [Changelog](https://github.com/node-fetch/node-fetch/blob/master/docs/CHANGELOG.md)
- [Commits](node-fetch/node-fetch@v2.6.0...v2.6.1)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* docs: update configuration and api options

* docs: update api docs

* docs(model options): update docs

* docs(query builder methods): update docs

* docs(model options): add `primaryKey` default value

* docs(model options): fix typo

* docs(example): rename file of nuxt example

* docs: remove usage section

* docs: add building query section

* chore: update package.json

* chore: update README.md

* docs: fix typo

* docs: add  "including relationships"

* docs: add helpers

* docs(operations): fix typo

* docs(api): update query builder methods

* docs: fix response of section "including relationships"

* docs: add "appending attributes"

* docs: add "selecting fields" and update other docs

* fix(utils): accept array of strings in param `propName` of `setProp`

* test(utils): add tests for `setProp`

* fix(utils): fix `getProp`

* fix(utils): accept array of strings in param `propName` of `getProp`

* test(utils): add tests for `getProp`

* refactor(utils): update jsdocs of `getProp` and `setProp`

* refactor(test): remove semicolon

* docs: add "paginating" and add missing colons.

* docs: add api reference to "selecting fields" section

* docs: update "paginating" section

* docs: add "applying parameters"

* docs: add "calling a custom resource"

* docs: add api reference of `params` and `custom`

* docs: add "composing without request"

* docs: remove unnecessary responses

* docs: update "calling a custom resource"

* docs: fix crud order

* docs: fix create request

* docs: update example of delete operation

* docs: add "mixing everything up"

* docs: fix append example

* docs: add "performing operations"

* docs: update `select` and `custom`

* docs: update model options

* docs: update configuration

* docs: add vue example

* docs: update "needless parent request"
JoaoPedroAS51 added a commit to JoaoPedroAS51/vue-api-query that referenced this pull request Oct 27, 2020
* docs: add nuxt docs

* docs: add introduction, installation, configuration, usage and nuxt example

* docs: add api docs

* docs: update configuration and usage

* ci: add nodejs ci

* ci: fix setup node version

* chore(eslint): add .eslintignore

* chore(scripts): add "lint" script

* chore(scripts): add "test-watch" script

* ci(docs): update deploy action

* ci: fix docs and publish

* ci: fix docs working directory

* docs: fix url

* docs: fix base url

* ci: fix docs action

* docs: fix pages position

* chore(script): add `--coverage` flag to `test` script

* ci(publish): add step to upload coverage to codecov

* chore(scripts): remove "prepare" script

* fix(model): add null check in `isValidId` (robsontenorio#115)

When an entity is being saved, nulls are considered as existing entities, so it tries to update and not insert.

* feat(model): handle 'data' wrapped responses for create and update (robsontenorio#109)

Laravel allows data wrapping for all responses, so create and update requests should support this if it is done.

* feat(model): add support to nested keys for relations (robsontenorio#127)

* feat(utils): add utilities `getProp` and `setProp`

* feat(model): add support to nested keys for relations

* test(model): test support of nested keys for relations

* test(utils): test utilities

* chore: update readme

* chore: bump node-fetch from 2.6.0 to 2.6.1 (robsontenorio#124)

Bumps [node-fetch](https://github.com/bitinn/node-fetch) from 2.6.0 to 2.6.1.
- [Release notes](https://github.com/bitinn/node-fetch/releases)
- [Changelog](https://github.com/node-fetch/node-fetch/blob/master/docs/CHANGELOG.md)
- [Commits](node-fetch/node-fetch@v2.6.0...v2.6.1)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* docs: update configuration and api options

* docs: update api docs

* docs(model options): update docs

* docs(query builder methods): update docs

* docs(model options): add `primaryKey` default value

* docs(model options): fix typo

* docs(example): rename file of nuxt example

* docs: remove usage section

* docs: add building query section

* chore: update package.json

* chore: update README.md

* docs: fix typo

* docs: add  "including relationships"

* docs: add helpers

* docs(operations): fix typo

* docs(api): update query builder methods

* docs: fix response of section "including relationships"

* docs: add "appending attributes"

* docs: add "selecting fields" and update other docs

* fix(utils): accept array of strings in param `propName` of `setProp`

* test(utils): add tests for `setProp`

* fix(utils): fix `getProp`

* fix(utils): accept array of strings in param `propName` of `getProp`

* test(utils): add tests for `getProp`

* refactor(utils): update jsdocs of `getProp` and `setProp`

* refactor(test): remove semicolon

* docs: add "paginating" and add missing colons.

* docs: add api reference to "selecting fields" section

* docs: update "paginating" section

* docs: add "applying parameters"

* docs: add "calling a custom resource"

* docs: add api reference of `params` and `custom`

* docs: add "composing without request"

* docs: remove unnecessary responses

* docs: update "calling a custom resource"

* docs: fix crud order

* docs: fix create request

* docs: update example of delete operation

* docs: add "mixing everything up"

* docs: fix append example

* docs: add "performing operations"

* docs: update `select` and `custom`

* docs: update model options

* docs: update configuration

* docs: add vue example

* docs: update "needless parent request"

* docs: remove helpers
@francoism90
Copy link
Contributor

@robsontenorio @JoaoPedroAS51 It seems this is missing in the docs? :)

@JoaoPedroAS51
Copy link
Collaborator Author

@francoism90 Thanks for report. I'll update the docs :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants