CouchDB datasource plugin for CakePHP
CouchDB datasource is a way to facilitate the communication from CakePHP application to CouchDB database., (*1)
DataSources are the link between models and the source of data that models represent., (*2)
CouchDB is an open source document-oriented database written mostly in the Erlang programming language., (*3)
Version
Written for CakePHP 2.x, (*4)
Copyright
Copyright (c) 2011 Maury M. Marques, (*5)
Installation
You can install this plugin using Composer, GIT Submodule, GIT Clone or Manually, (*6)
[Using Composer], (*7)
Add the plugin to your project's composer.json
- something like this:, (*8)
{
"require": {
"maurymmarques/couchdb-datasource-plugin": "dev-master"
},
"extra": {
"installer-paths": {
"app/Plugin/CouchDB": ["maurymmarques/couchdb-datasource-plugin"]
}
}
}
Then just run composer install
, (*9)
Because this plugin has the type cakephp-plugin
set in it's own composer.json
, composer knows to install it inside your /Plugin
directory, rather than in the usual vendors file., (*10)
[GIT Submodule], (*11)
In your app directory (app/Plugin
) type:, (*12)
git submodule add git://github.com/maurymmarques/couchdb-datasource.git Plugin/CouchDB
git submodule init
git submodule update
[GIT Clone], (*13)
In your plugin directory (app/Plugin
or plugins
) type:, (*14)
git clone https://github.com/maurymmarques/couchdb-datasource.git CouchDB
[Manual], (*15)
- Download the CouchDB archive.
- Unzip that download.
- Rename the resulting folder to
CouchDB
- Then copy this folder into
app/Plugin/
or plugins
Configuration
Bootstrap the plugin in app/Config/bootstrap.php:, (*16)
CakePlugin::load('CouchDB');
Connection in app/Config/database.php:, (*17)
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'CouchDB.CouchDBSource',
'persistent' => false,
'host' => 'localhost',
'port' => '5984',
'login' => 'root',
'password' => 'root',
'database' => null,
'prefix' => ''
);
}
Usage
The datasource works basically like CakePHP, (*18)
Creating a model
class Post extends AppModel {
public $schema = array(
'title' => array(
'type' => 'string',
'null' => true,
'key' => 'primary',
'length' => 32
)
);
}
You can set another CouchDB database name in your model using the attribute Model::useTable
, (*19)
public $useTable = 'posts';
Saving a document
$data = array('title' => 'My new title');
$this->Post->save($data);
// Id
$this->Post->id;
// Revision
$this->Post->rev;
Search for a document
$conditions = array('Post.id' => $this->Post->id);
$result = $this->Post->find('first', compact('conditions'));
Search for a document by specific revision
$conditions = array('Post.id' => $this->Post->id, 'Post.rev' => $this->Post->rev);
$result = $this->Post->find('first', compact('conditions'));
Change a document (changing the last revision)
$data = array('title' => 'My new title');
$this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420';
$this->Post->save($data);
Change a document to a particular revision
$data = array('title' => 'My new title');
$this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420';
$this->Post->rev = '26-5cd5713759905feeee9b384edc4cfb61';
$this->Post->save($data);
Deleting a document
$this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420';
$this->Post->delete($data);
REST
You can use the methods: curlGet, curlPost, curlPut, curlDelete, (*20)
$post = array(
'source' => 'post',
'target' => 'post-replicate',
'countinuos' => true
);
$return = $this->Post->curlPost('_replicate', $post, true, false);