Skip to content

Instantly share code, notes, and snippets.

@drewkerrigan
Last active February 6, 2018 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drewkerrigan/5ee14793295980f3b220 to your computer and use it in GitHub Desktop.
Save drewkerrigan/5ee14793295980f3b220 to your computer and use it in GitHub Desktop.
A few snippets describing how to use the riakpbc node.js library

Put

var riakObj = {mykey1: "myvalue1"};

var data    = {
    key: 'my_key',
    bucket: 'my_bucket', 
    type: 'my_bucket_type',
    content = { 
        'value': JSON.stringify(riakObj), 
        content_type: 'application/json' 
    },
    return_body: true
    // vclock = vclock // Use the vclock if you have it
};

client.put(data, function(e,d) {
    if (e) {
        callback(e, null);
    } else {
        if(d.key) {
            console.log(d.key);
        }
        console.log(d.vclock);
        
        callback(null, d);
    }
});

Get

var value = {};


client.get({ 'bucket': 'my_bucket', 'key': 'my_key', 'type': 'my_bucket_type' }, function(e,d)
{
    if (e) {
        callback(e, {});
    } else {
        try
        {
            value = d.content[0].value;
            value.vclock = d.vclock;
        }
        catch(ex)
        {
            value = {};
        }

        callback(null, value);
    }
});

Search

var options = {
    q: {mykey1: "*value*"},
    index: "my_search_index",
    rows: 100,
    start: 0
};

client.search(options, function(e, d) 
{
    if (e) {
        callback(e, {count: 0, values: []});
    } else {
        var keys = null;
        var num_found = 0;
        try
        {
            num_found = d.num_found;

            var keys = d.docs.map(function(doc){
                var keyField = doc.fields.filter(function(d){
                    return (d['key'] == '_yz_rk') ? true : false;
                });

                var key = keyField[0]['value'];
                return key;
            });
        }
        catch(ex)
        {
            console.log("oops!");
        }

        callback(null, {count: num_found, values: keys});
    }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment