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

Rename collection mapping field #550

Open
eduardoboucas opened this issue Apr 29, 2019 · 0 comments
Open

Rename collection mapping field #550

eduardoboucas opened this issue Apr 29, 2019 · 0 comments

Comments

@eduardoboucas
Copy link
Contributor

eduardoboucas commented Apr 29, 2019

When retrieving a composed view of document with multi-collection references, there needs to be information about what collection each of the documents is coming from. Currently, this is done using one field per Reference field, named as _ref[ReferenceFieldName] (e.g. authors leads to _refAuthors).

Example:

{
  "results": [
    {
      "_id": "560a5baf320039f1a1b68d4c",
      "_composed": {
        "crew": [
          "5ac16b70bd0d9b7724b24a41",
          "5ac16b70bd0d9b7724b24a42",
          "5ac16b70bd0d9b7724b24a43"
        ]
      },
      "_refCrew": {
        "5ac16b70bd0d9b7724b24a41": "writers",
        "5ac16b70bd0d9b7724b24a42": "directors",
        "5ac16b70bd0d9b7724b24a43": "producers"
      },
      "title": "Casablanca",
      "crew": [
        {
          "_id": "5ac16b70bd0d9b7724b24a41",
          "name": "Julius J. Epstein"
        },
        {
          "_id": "5ac16b70bd0d9b7724b24a42",
          "name": "Michael Curtiz"
        },
        {
          "_id": "5ac16b70bd0d9b7724b24a43",
          "name": "Hal B. Wallis"
        }
      ]
    }
  ]
}

In hindsight, the naming of this internal field was a poor design decision (my bad ✋). It forces clients to compute the name of the mapping field from the name of the Reference field, with the whole camel-case conversion to add more complexity to the mix.

Instead, a single field (e.g. _references) should hold all the collection mapping, in an object where keys are the Reference field names. The response above would be changed to:

{
  "results": [
    {
      "_id": "560a5baf320039f1a1b68d4c",
      "_composed": {
        "crew": [
          "5ac16b70bd0d9b7724b24a41",
          "5ac16b70bd0d9b7724b24a42",
          "5ac16b70bd0d9b7724b24a43"
        ]
      },
      "_references": {
        "crew": {
          "5ac16b70bd0d9b7724b24a41": "writers",
          "5ac16b70bd0d9b7724b24a42": "directors",
          "5ac16b70bd0d9b7724b24a43": "producers"
        }
      },
      "title": "Casablanca",
      "crew": [
        {
          "_id": "5ac16b70bd0d9b7724b24a41",
          "name": "Julius J. Epstein"
        },
        {
          "_id": "5ac16b70bd0d9b7724b24a42",
          "name": "Michael Curtiz"
        },
        {
          "_id": "5ac16b70bd0d9b7724b24a43",
          "name": "Hal B. Wallis"
        }
      ]
    }
  ]
}

This way, consumers don't need to compute any field names. Accessing the collection mapping for the crew fields is as simple as accessing the _references.crew object.

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

No branches or pull requests

1 participant