github.com/mmatczuk/gohan@v0.0.0-20170206152520-30e45d9bdb69/examples/relation_sample/README.md (about)

     1  Gohan DB relation example
     2  --------------------------
     3  
     4  This is an example for how to use gohan DB relation.
     5  
     6  In this example, we have three requirements.
     7  
     8  (Req1) We should have Customer resource, Device resource, and DeviceType resource.
     9  (Req2) A customer has multiple devices.
    10  (Req3) A device has a reference to a DeviceType.
    11  
    12  You can use "parent" property for (Req2)
    13  
    14  ``` YAML
    15  - description: Devices
    16    id: device
    17    parent: customer
    18    plural: devices
    19    schema:
    20  ```
    21  
    22  Gohan will add customer_id property for device schema automatically, and API Server & WebUI can understand
    23  this relationship.
    24  
    25  We can use "relation" and "relation_property" for (Req3).
    26  You should specify schema_id for relation property.
    27  
    28  
    29  ``` YAML
    30  
    31        device_type_id:
    32          description: Device Type
    33          permission:
    34          - create
    35          - update
    36          relation: device_type
    37          relation_property: device_type
    38          title: Device Type
    39          type: string
    40  
    41  ```
    42  
    43  You can get related objects in List and Show API using
    44  relation_property. relation_property value will be used as a key for related object in the
    45  response.
    46  
    47  This is sample json output.
    48  
    49  ``` json
    50  {
    51    "devices": [
    52      {
    53        "customer_id": "3eb06bb3-e0fa-48c6-8edc-4f93d636334f",
    54        "description": "Test",
    55        "device_type": {
    56          "description": "type_a",
    57          "id": "ca85ae8e-5496-47ed-b797-0456ef7c36f8",
    58          "name": "type_a",
    59          "tenant_id": "fc394f2ab2df4114bde39905f800dc57"
    60        },
    61        "device_type_id": "ca85ae8e-5496-47ed-b797-0456ef7c36f8",
    62        "id": "ceb760d9-ebc7-4ea2-8f71-dbc1b20f870a",
    63        "mac_address": "xxxx",
    64        "name": "Test",
    65        "tenant_id": "fc394f2ab2df4114bde39905f800dc57"
    66      }
    67    ]
    68  }
    69  
    70  ```
    71  
    72  This is sample screenshot.
    73  
    74  ![WebUI screenshot](screenshot.png "screenshot")