couchdb-datasource
couchdb-datasource copied to clipboard
CouchDB Datasource plugin for CakePHP
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.
Version
Written for CakePHP 2.x
Copyright
Copyright (c) 2011 Maury M. Marques
Installation
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
Configuration
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' => ''
);
}
Usage
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');
$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
$post = array(
'source' => 'post',
'target' => 'post-replicate',
'countinuos' => true
);
$return = $this->Post->curlPost('_replicate', $post, true, false);