Introduction - ace411/fauxton-client GitHub Wiki

fauxton-client wiki

The information in this wiki is meant to help understand how to use Couch DB's RESTful web service to utilize the database's features. Getting familiar with the CouchDB documentation is quite useful in this regard so, consider perusing it.

Prerequisites

  • curl
  • PHP 5.4 or greater

Installation

  • Add fauxton-client to the list of dependencies in your composer.json file so as to make it appear as shown below:
{
   "require": {
      "chemem/fauxton-client" : "dev-master"
   }
}
  • Proceed to install the package by typing composer install in your preferred command line interface.

Creating your first Database with fauxton-client

require dirname(__DIR__) . '/vendor/autoload.php';

use Chemem\Fauxton\DatabaseActions;

$db = new DatabaseActions;
$db::setReturnType(false); //returns a JSON-decoded array

$myDb = 'nba_info'; //name of the database you intend to create

//check if the database exists
if (!in_array($myDb, $db->showAllDatabases())) {
   $db->createDatabase($myDb); //create the database if it doesn't exist 
}

The code snippet above demonstrates how to set up a Couch database called "nba_info". For SQL users, the query shown above is the equivalent of writing something like this:

CREATE TABLE IF NOT EXISTS nba_info;

CouchDB structure

Couch's storage architecture is key-value pair driven and largely JSON-compatible. Therefore, most interactions with CouchDB are JSON-oriented.

Documents vs Tables

The traditional SQL setup is centered on storing data in relational table structures with atomic rows and columns. CouchDB is a NoSQL database built on the availability of revision-based documents which are, fundamentally, JSON implementations.

Sample CouchDB document structure

{
  "_id": "438b3cc86deac558b9ea76fd19000328",
  "_rev": "1-7c28723b01655b3eb4ca48a716c18a1e",
  "tv_show": "Elementary",
  "category": "crime",
  "network": "CBS"
}

The _id and _rev keys are of grave significance as they add specificity to CouchDB documents. The rest of the keys (analogous to the fields in SQL relational models) are arbitrary.

Sample SQL table

id tv_show category network
43 Elementary Crime CBS

There is no need for a _rev field in the SQL table equivalent of the JSON document.