o100ja/rug
Rug is a PHP client library for CouchDB. Early-stage/experimental: the README notes it’s currently just an approach idea and shouldn’t be used until unit tests are completed.
Installation Add the package via Composer (if still functional):
composer require o100ja/rug
Note: Due to the last release being from 2013, verify compatibility with your PHP/CouchDB versions.
Basic Setup
Initialize the client in a Laravel service provider (e.g., AppServiceProvider):
use Rug\Client;
public function register()
{
$this->app->singleton('couchdb', function ($app) {
return new Client('http://localhost:5984');
});
}
First Use Case Connect to a CouchDB database and fetch a document:
$couch = app('couchdb');
$db = $couch->db('mydb');
$doc = $db->get('doc_id');
src/Rug/Client.php and src/Rug/Database.php for API structure.Database Operations
$db->save($document); // Upsert
$db->delete('doc_id');
$db->bulk($documents); // Array of docs
Querying
$result = $db->view('design_doc', 'view_name', ['key' => 'value']);
$docs = $db->find(['selector' => ['field' => 'value']]); // Mango Query (if supported)
Authentication
new Client('http://localhost:5984', 'user', 'pass');
Event Handling
$db->changes(function ($change) {
// Handle change
});
.env:
COUCHDB_URL=http://localhost:5984
COUCHDB_USER=user
COUCHDB_PASS=pass
Then inject via:
new Client(config('couchdb.url'), config('couchdb.user'), config('couchdb.pass'));
try {
$doc = $db->get('doc_id');
} catch (\Rug\Exception $e) {
Log::error($e->getMessage());
}
Deprecation Risk
Outdated Features
No Laravel-Specific Features
Authentication Issues
COUCHDB_COOKIE environment variable or raw HTTP headers.No Type Safety
$doc = $db->get('doc_id');
if (!isset($doc['_id'])) throw new \RuntimeException('Invalid document');
$client = new Client('http://localhost:5984', null, null, [
'debug' => true,
]);
$response = $db->get('doc_id', ['raw' => true]);
$client->getHttpClient()->getEmitter()->attach(
new \GuzzleHttp\Middleware::tap(function ($request) {
Log::debug('CouchDB Request:', ['url' => (string) $request->getUri()]);
})
);
Custom HTTP Client Override the default client (e.g., for retries or timeouts):
$client = new Client('http://localhost:5984', null, null, [
'http_client' => new \GuzzleHttp\Client(['timeout' => 30]),
]);
Event Dispatching Extend the client to dispatch Laravel events:
$db->save($doc);
event(new CouchDBDocumentSaved($doc));
Query Builder Create a fluent interface for complex queries:
$query = (new CouchDBQuery($db))
->where('type', 'user')
->limit(10);
$results = $query->get();
Caching Layer Cache frequent queries using Laravel’s cache:
$cacheKey = "couchdb:view:{$designDoc}:{$viewName}:".md5($options);
return Cache::remember($cacheKey, 60, function () use ($db, $designDoc, $viewName, $options) {
return $db->view($designDoc, $viewName, $options);
});
How can I help you explore Laravel packages today?