Created
March 28, 2015 02:04
-
-
Save rolaveric/a2b04376f96412b1d0e8 to your computer and use it in GitHub Desktop.
Example of a relationship definition for JSON Schema design
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| For this schema, each object's relationships are defined as an object under | |
| a "links" property. | |
| Each property of that object is the name of the relationship, and the value | |
| is a single object with the "type" and "id" of the related object. | |
| For "to-many" relationships, the value can be an array of the same objects. | |
| */ | |
| var data = { | |
| "articles": [{ | |
| "id": "article1", | |
| "links": { | |
| "author": {"id": "person1", "type": "people"} | |
| } | |
| }], | |
| "people": [{ | |
| "id": "person1", | |
| "phoneNumber": "1234" | |
| }] | |
| }; | |
| function getLink(data, object, linkName) { | |
| var link = object.links[linkName]; | |
| if (link && data[link.type]) { | |
| return data[link.type].find(function(e) {return e.id === link.type;}); | |
| } | |
| } | |
| getLink(data, data.articles[0], 'author').phoneNumber; // "1234" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want a link to an awesome JSON blog post...