X
X

Login

Login if you already have an account

LOGIN

Register

Create a new account. join us!

REGISTER

Support

Need some help? Check out our forum.

FORUM

FYI – Datagraph with Objects and Collections


As a developer, I want to have complete control of my objects and collections on the backend.

There are so many terms being thrown around these days that it is often difficult to separate the truth from the marketing hype. So – at FatFractal, when we talk about our Datagraph capabilities, we mean a real graph, not marketing hype. To illustrate, let me show you a couple of simple examples.

You can see working code for everything below (here).
You can access the source code for the sample application (here).

Most of the time, you will define an object and map it to a identical collection.

Let’s start with couple of simple object definitions:

function Movie() {
this.title = null;
this.description = null;
this.year = null;
}
function Actor() {
this.firstName = null;
this.lastName = null;
}

Then, we add the following to our FFDL (what’s this?), to define these object types and collections on the backend as follows:

CREATE OBJECTTYPE Movie (title STRING, description STRING, year NUMERIC)
CREATE OBJECTTYPE Actor (firstName STRING, lastName STRING)
CREATE COLLECTION /Movies OBJECTTYPE Movie
CREATE COLLECTION /Actors OBJECTTYPE Actor

Note – that the two collections are typed to an object type (Movie and Actor), which means that attempting to store a Movie in the Actors collection or vice-a-versa will generate a validation error.

But, let’s say that you want to create a collection of favorites and allow it to hold different object types, while also still being able to distinguish them from each other. No problem, to do that, let’s add a new collection called Favs that is not typed:

CREATE COLLECTION /Favs OBJECTTYPE *

Examples:

First, let’s store an object in a collection typed to a Movie object type:

var m = new Movie();
m.title = "Argo";
m.description = "Argo is a 2012 historical drama thriller film directed by Ben Affleck.";
m.year = 2012;
ff.createObjAtUri(m, "/Movies");

Now – let’s try saving a Movie to the Favs collection…

ff.createObjAtUri(m, "/Favs");

Similarly, you can store an Actor in the Actors collection or in the Favs collection…

var a = new Actor();
a.firstName = "Ben";
a.lastName = "Affleck"
FatFractal.createObjAtUri(a, "/Actors");
FatFractal.createObjAtUri(a, "/Favs");

But, if you try to store a Movie in the Actors (or vice-a-versa) collection, you will get an error…

FatFractal.createObjAtUri(m, "/Actors");

Now – let’s see how you can retrieve objects of a particular class from a collection that holds more than one object type.

Get all the Movie objects from the Favs collection…

ff.getArrayFromUri("/ff/resources/Favs/(clazz eq 'Movie')", function(r) {
// handle success
}, function(code,message){
// handle error
});

Get all the Actor objects from the Favs collection…

ff.getArrayFromUri("/ff/resources/Favs/(clazz eq 'Actor')", function(r) {
// handle success
}, function(code, message){
// handle error
});

Get all the objects from the Favs collection…

ff.getArrayFromUri("/ff/resources/Favs/", function(r) {
// handle success
}, function(code, message){
// handle error
});

That’s it – we hope that you will agree that this is another example of FatFractal being the most flexible, robust and capable backend for your app – enjoy!

Kevin

Contact