Skip to content

Latest commit

 

History

History
179 lines (139 loc) · 3.52 KB

examples.adoc

File metadata and controls

179 lines (139 loc) · 3.52 KB

Examples

JSON Schema

The first step with LDS is to define your JSON Schemas. Here is a simple Person → Address example. Start by creating a SCHEMA_FOLDER directory.

Person object

SCHEMA_FOLDER/person.json

{
  "$ref": "#/definitions/person",
  "definitions": {
    "person": { (1)
      "type": "object",
      "properties": {
    "name": { (2)
          "type": "string"
        },
        "addresses": { (3)
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "_link_property_addresses": { (4)
          "type": "object",
          "properties": {
            "address": { (5)
              "type": "null"
            }
          }
        }
      }
    }
  },
  "$schema": "http://json-schema.org/draft-04/schema#"
}
  1. person is an object

  2. name is of primitive string type

  3. addresses is an array of links to type Address

  4. _linked_property_addresses is an instruction to LDS to maintain references to the object `Address`object

  5. Link property addresses to address-object

💡

The _linked_property is in terms of JSON Schema a property, just like any other property. However, the LDS interprets this as an instruction to maintain a linked relationship between two resources. The multiplicity is defined by the type of the outgoing link. In this case, an array (aka one-to-many). The suffix of addresses in _linked_property_addresses binds the property addresses`to the referred property `address in which instruct LDS to link this array to the address-object.

Address object

SCHEMA_FOLDER/address.json

{
  "$ref": "#/definitions/address",
  "definitions": {
    "address": { (1)
      "type": "object",
      "properties": {
        "street": { (2)
          "type": "string"
        },
        "city": { (3)
          "type": "string"
        }
      }
    }
  },
  "$schema": "http://json-schema.org/draft-04/schema#"
}
  1. address is an object

  2. street is a property

  3. city is a property

Start LDS

The docker image lds-memory:latest is just for demo purposes. You may also try out this example with e.g. lds-postgres:latest, lds-neo4j:latest or any other provider.

Run

docker run -it -v SCHEMA_FOLDER:/schemas -p 9090:9090 lds-memory:latest

Create, Overwrite, Read and Delete

Create Person

curl -X PUT -d {"name": "John Doe"} -i http://localhost:9090/ns/person/1

Get Person

curl -i http://localhost:9090/ns/person/1

should respond:

{
    "name": "John Doe"
}

Create Address

curl -X PUT -d {"street": "Doe Street 1", "city": "Cool City"} -i http://localhost:9090/ns/address/1

Connect Address to Person

curl -X PUT -i http://localhost:9090/ns/person/1/addresses/address/1

Get Person

curl -i http://localhost:9090/ns/person/1

should respond:

{
    "name": "John Doe",
    "addresses": [
        "/address/1"
    ]
}

Get Address

curl -i http://localhost:9090/ns/address/1

should respond:

{
    "street": "Doe Street 1",
    "city": "Cool City"
}