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://github.com/maurymmarques/couchdb-datasource.git Plugin/CouchDB
git submodule init
git submodule update
[GIT Clone]
In your plugin directory (app/Plugin
or plugins
) type:
git clone https://github.com/maurymmarques/couchdb-datasource.git CouchDB
[Manual]
- Download the CouchDB archive.
- Unzip that download.
- Rename the resulting folder to
CouchDB
- Then copy this folder into
app/Plugin/
orplugins
Bootstrap the plugin in app/Config/bootstrap.php:
CakePlugin::load('CouchDB');
Connection in app/Config/database.php:
class DATABASE_CONFIG {
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
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';
$data = array('title' => 'My new title');
$this->Post->save($data);
// Id
$this->Post->id;
// Revision
$this->Post->rev;
$conditions = array('Post.id' => $this->Post->id);
$result = $this->Post->find('first', compact('conditions'));
$conditions = array('Post.id' => $this->Post->id, 'Post.rev' => $this->Post->rev);
$result = $this->Post->find('first', compact('conditions'));
$data = array('title' => 'My new title');
$this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420';
$this->Post->save($data);
$data = array('title' => 'My new title');
$this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420';
$this->Post->rev = '26-5cd5713759905feeee9b384edc4cfb61';
$this->Post->save($data);
$this->Post->id = '8e64f1eadab2b3b32c94ef2scf3094420';
$this->Post->delete($data);
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);