FYI - Combining Data From Collections

This post demonstrates real examples of combining data from various Collections 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 )

FFDL

This test application includes the following ffdl definition.

# Object Types
CREATE OBJECTTYPE Person (firstName STRING, lastName STRING, gender STRING, mother REFERENCE /Persons, father REFERENCE /Persons, siblings GRABBAG /Persons, picture BYTEARRAY)
CREATE OBJECTTYPE Episode (title STRING, description STRING, season NUMERIC, episode NUMERIC, originalAir DATE)
CREATE OBJECTTYPE Debut (person REFERENCE /Persons, episode REFERENCE /Episodes)
# Collection Resources
CREATE COLLECTION /Persons OBJECTTYPE Person
CREATE COLLECTION /Episodes OBJECTTYPE Episode
CREATE COLLECTION /Debuts OBJECTTYPE Debut
# Extension Resources
CREATE EXTENSION /combineData AS javascript:require('scripts/extensions.js').combineData();

Server Extension

This test will show you how to retrieve specific object data using a Server Extension. We first get the Person with firstName "Bart" and then return only the firstName of the person object and the title of the debut episode they appeared in.

// The Client Code ff.getObjFromExtension("/ff/ext/combineData?firstName=Bart", function(resp) {
    //handle response
});

// The Server Exension Code
exports.combineData = function() {
    var firstName = ff.getExtensionRequestData().httpParameters['firstName'];
    var person = ff.getObjFromUri("/Persons/(firstName eq '" + firstName + "')")
    var r = ff.response();
    var debut = ff.getObjFromUri(person.ffUrl + "/BackReferences.Debuts");
    var episode = ff.getReferredObject("episode", debut)
    r.result = {firstName:person.firstName,debutEpisodeTitle:episode.title};
    r.responseCode="200";
    r.statusMessage = "returned custom object data";
    r.mimeType = "application/json";
}


The following Models are 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;
}

function Episode() {
    this.title = null;
    this.description = null;
    this.season = null;
    this.episode = null;
    this.originalAir = null;
}

function Debut() {
    this.person = new Person();
    this.episode = new Episode();
}