FYI - Working with your Datagraph

This post demonstrates real examples of working with FatFractal's datagraph.

You can see the data created by this app using the FatFractal DataBrowser ( here )

You can access the source code for the sample application ( here )

You can see the API metadata for this application ( here )

FFDL

This test application includes the following ffdl definition.

CREATE OBJECTTYPE Person (firstName STRING, lastName STRING, gender STRING, mother REFERENCE /Persons, father REFERENCE /Persons, siblings GRABBAG /Persons, picture BYTEARRAY)
CREATE COLLECTION /Persons OBJECTTYPE Person

References

This test will show you how to retrieve an object by reference. We first get the Person with firstName "Bart" and then get the Person referenced by the object member "father".

var bart, homer;
ff.getObjFromUri("/Persons/(firstName eq 'Bart')/father", function(response) {
    homer = response;
});
//or, if bart has already been loaded on the client
homer = bart.father;


Grab Bags

This test will show you how to retrieve a set of objects from a Grab Bag by reference. We want to get all of Bart's aunts on his mother's side. To do this, we will first get the Person with firstName "Bart", then get Marge from the "mother" member Reference, then get siblings from the Grab Bag referenced by the object member "siblings".

var grabbags;
ff.getArrayFromUri("/Persons/(firstName eq 'Bart')/mother/()/siblings/(gender eq 'Female')", function(response) {
    grabbags = response;
});


Back References

This test will show you how to retrieve objects using Back References. There are three tests here - the first gets all backreferences to "homer". The second gets all the Person objects from the Grab Back that reference homer by the "siblings" Reference member. The third gets all the Person objects that refer to homer by the "father" Reference member.

var homer, allBackRefs;
ff.grabBagGetAll(homer, "BackReferences", function(response) {
    allBackRefs = response;
});

var homer, allSiblingBackRefs;
ff.grabBagGetAll(homer, "BackReferences.Persons.siblings", function(response) {
    allSiblingBackRefs = response;
});

var homer, allFatherBackRefs;
ff.grabBagGetAll(homer, "BackReferences.Persons.father", function(response) {
    allFatherBackRefs = response;
});


Queries

This test will show you how to retrieve objects using various queries. There are five examples in this section. The first example will get all the Person objects that have a "father" Reference member (Note that the returned values are deduplicated). The second is basically the same, but uses the "mother" Reference member. The third example shows the use of a logical OR to get all of Bart's granparents. The fourth example is similar, but adds another locical OR to get all of Bart's granparents. The last shows a complex query to get all of Ling's cousins.

var fathers;
ff.getArrayFromUri("/Persons/()/father"), function(response) {
    fathers = response;
});

var mothers;
ff.getArrayFromUri("/Persons/()/mother"), function(response) {
    mothers = response;
});

var grandfathers;
ff.getArrayFromUri("/Persons/(firstName eq 'Bart')/father or mother/()/father"), function(response) {
    grandfathers = response;
});

var grandparents;
ff.getArrayFromUri("/Persons/(firstName eq 'Bart')/father or mother/()/mother or father"), function(response) {
    grandparents = response;
});

var cousins;
ff.getArrayFromUri("/Persons/(firstName eq 'Ling')/father or mother/()/siblings/()/BackReferences.Persons.mother or BackReferences.Persons.father"), function(response) {
    cousins = response;
});


Datagraph

This test will fetch the entire datagraph.

var datagraph;
ff.getArrayFromUri("/Persons/()?depthRef=3&depthGb=3", function(response) {
    datagraph = response;
});


The following Model is used with this post

function Person() {
    this.firstName = null;
    this.lastName = null;
    this.gender = null;
    this.mother = null;
    this.father = null;
    this.siblings = null;
    this.picture = null;
}