This very first post will be dedicated to Facebook, as it is also the reason why this blog is started. I finally felt the need to document all my findings in a central location so that I do not need to rely on badly written wikis anymore. The facebook developer wiki is one such disaster.

There are quite a few tutorials out there, but nothing is clearer to me than something I write myself! If you are already a seasoned programmer I’m sure this snippet is enough to say it all:

//Create the table
$facebook->api_client->data_createObjectType("car");
$facebook->api_client->data_defineObjectProperty("car", "brand", 3);
$facebook->api_client->data_defineAssociation("user_to_row", 1, <code class="keyword">array("alias" => "user_id"), array("alias" => "row_id") );

//Add a row
$brand = "toyota";
$rowID = $facebook->api_client->data_createObject("car", <code class="keyword">array("brand" => "$brand");
$facebook->api_client->data_setAssociation("user_to_row", $userID, $rowID );

//Fetch the row value
$objects = $facebook->api_client->data_getAssociatedObjects("user_to_row", $userID, <code class="keyword">true);

// There is only a single row for this objectID
if( <code class="keyword">isset($objects) && <code class="keyword">isset($objects[0]) ) {
	// id2 holds the object id
	$objectID = $objects[0]["id2"];
}

$brand = $facebook->api_client->data_getObjectProperty( $rowID, "brand" );

echo "The car brand is $brand";

echo “The car brand is $brand”;
The database structure
To handle the exponential requirements of data storage for facebook apps with added privacy and security concerns, the developers have come up with a scalable, distributed solution which they call it the datastore. The datastore is accessed through the exposed datastore APIs which is still in beta as of this writing.

Tables versus Object Types
In a traditional RDBMS, the description of the storage location is a columnised table filled with values. In the facebook datastore, information is stored as objects. Each object type has properties much like tables have fields. One important difference from tables is that you do not define a primary key for the object type. Each object when created, will have its own _id which you will use for datastore associations.

Associations
An RDBMS relates rows between the tables using foreign keys. In the datastore, this relation is established using associations. You can see associations as merely a separate table of _ids relating one object type to the other (or table to table in RDBMS sense). Therefore using the example code above, you can create an association saying user with _id is associated with (or related to) object with _id.