-
Notifications
You must be signed in to change notification settings - Fork 28
Description
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.