CouchDB Datasource plugin for CakePHP
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

CouchDB datasource plugin for CakePHP

CouchDB datasource is a way to facilitate the communication from CakePHP application to CouchDB database.

DataSources are the link between models and the source of data that models represent.

CouchDB is an open source document-oriented database written mostly in the Erlang programming language.


Written for CakePHP 2.x


Copyright (c) 2011 Maury M. Marques


You can install this plugin using Composer, GIT Submodule, GIT Clone or Manually

[Using Composer]

Add the plugin to your project's composer.json - something like this:

  "require": {
    "maurymmarques/couchdb-datasource-plugin": "dev-master"
  "extra": {
    "installer-paths": {
      "app/Plugin/CouchDB": ["maurymmarques/couchdb-datasource-plugin"]

Then just run composer install

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.

[GIT Submodule]

In your app directory (app/Plugin) type:

git submodule add git:// Plugin/CouchDB
git submodule init
git submodule update

[GIT Clone]

In your plugin directory (app/Plugin or plugins) type:

git clone CouchDB


  • Download the CouchDB archive.
  • Unzip that download.
  • Rename the resulting folder to CouchDB
  • Then copy this folder into app/Plugin/ or plugins


Bootstrap the plugin in app/Config/bootstrap.php:


Connection in app/Config/database.php:


	public $default = array(
		'datasource'	=> 'CouchDB.CouchDBSource',
		'persistent'	=> false,
		'host'			=> 'localhost',
		'port'			=> '5984',
		'login'			=> 'root',
		'password'		=> 'root',
		'database'		=> null,
		'prefix'		=> ''



The datasource works basically like CakePHP

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

public $useTable = 'posts';

Saving a document

$data = array('title' => 'My new title');

// Id

// Revision

Search for a document

$conditions = array('' => $this->Post->id);
$result = $this->Post->find('first', compact('conditions'));

Search for a document by specific revision

$conditions = array('' => $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';

Change a document to a particular revision

$data = array('title' => 'My new title');
$this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420';
$this->Post->rev = '26-5cd5713759905feeee9b384edc4cfb61';

Deleting a document

$this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420';


You can use the methods: curlGet, curlPost, curlPut, curlDelete

$post = array(
	'source' => 'post',
	'target' => 'post-replicate',
	'countinuos' => true

$return = $this->Post->curlPost('_replicate', $post, true, false);