dev-master
9999999-dev https://github.com/MaxGaussDev/CCouchConnectA shell cURL wrapper for CouchDB on PHP
GNU GPL
The Requires
- php >=5.3.0
database php couchdb couch
A shell cURL wrapper for CouchDB on PHP
This is a simple PHP CouchDB cURL Wrapper, for basic CRUD commands in CouchDB., (*1)
Using composer:, (*2)
composer require maxgaussdev/ccouchconnect
To import the class all you need to do is include the CCouchConnect.php file and use the namespace:, (*3)
include('CCouchConnect.php'); use CCouch\Database\CCouchConnect as Database;
The constructor will support method overload, so basically there are three ways to create a Database Object:, (*4)
$db = new Database('database', 'server'); $db = new Database('database', 'server', 5984); $db = new Database('database', 'server', 5984, 'username', 'password');
By default the port will be set to the usual 5984 on CouchDB., (*5)
It is recommended that you use already existing database, but in case you need to create one with PHP, there is a createDatabase() method, that can be used in several ways:, (*6)
// declare a new database that does not exist on the server $db = new Database('database', 'server'); // we use createDatabase() method to create it if(!$db->createDatabase->error){ // success }else{ // fail } // this can also be set using empty constructor and usual setters $db = new Database(); $db->setDatabase('dbname'); $db->setServer('localhost'); if(!$db->createDatabase->error){ // success }else{ // fail }
If not set in advance, the port will be defined by the default 5984., (*7)
Public methods for database queries will return stdObjects as result, use print_r() for details if needed., (*8)
There are a couple of methods that will allow you to do the basic CRUD for objects. The return values by default will be php's stdObjects., (*9)
The Database info:, (*10)
$result = $db->dbInfo();
$document = array( "name" => "foo", "occupation" => "bar" ); $result = $db->addNew($document);
This will return an std object with createdAt and updatedAt parameters by default (using Datetime), like this:, (*11)
stdClass Object ( [_id] => efdf1c6dde49b78bf9834424131037f5 [_rev] => 1-4fb5af102470b0304292ec418e0cab09 [name] => foo [occupation] => bar [createdAt] => stdClass Object ( [date] => 2016-04-08 11:41:06.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) [updatedAt] => stdClass Object ( [date] => 2016-04-08 11:41:06.000000 [timezone_type] => 3 [timezone] => Europe/Berlin ) )
There are a couple of methods to retrieve a document from CouchDB., (*12)
// returns document object by _id $result = $db->findById('efdf1c6dde49b78bf9834424131037f5'); // returns an array of document objects filtered by array of keys and their values $result = $db->findBy(array("key" => "value")); // returns a document object filtered by array of keys and their values $result = $db->findOneBy(array("key" => "value")); // example with our object above: $result = $db->findOneBy(array("name" => "foo", "occupation" => "bar")); $result = $db->findAll(); // returns an array of all document objects
findBy() methods create a view in the database (by default the design document is _design/ccouch_views), with the md5 hash for the view name for each search input., (*13)
In case you don't want the search data to be cached in design document there are two following methods:, (*14)
$result = $db->findByNoCache(array("key" => "value")); $result = $db->findOneByNoCache(array("key" => "value"));
Both methods will return the data using temporary views., (*15)
In case you wish to search for documents with a keyword value in any non-specified parameter, there is a simple search method implemented:, (*16)
$result = $db->search('keyword');
Limit and offset can be used in the following way and are optional arguments:, (*17)
$result = $db->findAll($limit, $offset); $result = $db->findByNoCache(array("key" => "value"),$limit, $offset); $result = $db->findBy(array("key" => "value"),$limit, $offset);
Updating a document is simple, you just pass an object as an argument to save() method:, (*18)
// some document from the database $document = $db->findById('efdf1c6dde49b78bf9834424131037f5'); $document->name = "foo foo"; $result = $db->save($document);
Deleting the document:, (*19)
// some document from the database $document = $db->findById('efdf1c6dde49b78bf9834424131037f5'); $result = $db->delete($document);
Although it is not advised, you can also Purge the document. Typical CouchDb purge rules apply:, (*20)
// some document from the database $document = $db->findById('efdf1c6dde49b78bf9834424131037f5'); $result = $db->purge($document);
There are two bulk methods currently. Adding and Deleting multiple documents in a single request:, (*21)
// some documents $documentsArray = array($document1, $document2, $document3,...); // add multiple new documents $result = $db->saveBulk($documentsArray); // delete multiple documents $result = $db->deleteBulk($documentsArray);
Both methods will accept array of document objects as argument. Return result will be an array of objects with statuses., (*22)
// get document ids (findAll() withoud document details) $result = $db->listDocuments(); // get ids of changed documents $result = $db->listChanges(); // get list of all databases $result = $db->listDatabases(); // delete database $result = $db->deleteDatabase();
Here is the list of plans for future releases:, (*23)
A shell cURL wrapper for CouchDB on PHP
GNU GPL
database php couchdb couch