ARAVINDA VISHWANATHAPURA

CouchDB: Auto increment IDs with PHP

Sep 24, 2010
1 minute read.
couchdb php

Create a seed, which will be a CouchDB view.

Map function extracts numeric part of every doc of type "photo".

// Map function
function(doc){
    if(doc.type == "photo"){
        emit(null, parseInt(doc._id.replace(/\D/g,'')));
    }
}

reduce function finds the max value from those numeric values emitted from map function.

// Reduce function
function(key, values){
    var max = 0;
    for(i in values){
        if(max < values[i]){
            max = values[i];
        }
    }
    return max;
}

This view is saved as _design/helpers/photoid

Now write a function in PHP to increment the ID as required and to add the document.

<?php
function addDoc($url, $data, $seed, $idprefix, $incrementBy=1){
    // Get the current ID value
    $ch = curl_init("$url/_design/$seed");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = json_decode(curl_exec($ch), true);

    // Increment and add prefix as per given parameters
    $id = $idprefix . ($result['rows'][0]['value'] + $incrementBy);

    // Add document to couchdb
    $ch = curl_init("$url/$id");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array ("Content-Type: application/json"));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // Print result
    echo curl_exec($ch);
}

Example usage of above PHP function

addDoc("http://localhost:5984/photoblog",
       Array("type"=>"photo", "published"=>0, "place"=>"Karnataka, India"),
       "helpers/_view/photoid",
       "p");

As of now, PHP function involves two http calls for adding single document. Let me know if you find any ways to reduce it to one http call.

About Aravinda Vishwanathapura

Co-Founder & CTO at Kadalu Technologies, Creator of Sanka, Creator of Chitra, GlusterFS core team member, Maintainer of Kadalu Storage
Contact: Linkedin | Twitter | Facebook | Github | mail@aravindavk.in