motivation:

overview:

requirements/environement:

usage:

  1. Save the code below to a file on your server, eg kv.xml
  2. Edit the file to change all occurrences of ‘{your domain}’ to your domain
  3. Create a file called kv.env and define your table url and YQL storage addresses into it, as described in the YQL documentation
  4. For select queries, just pass the select address in the request.  For all other queries, set env={your domain}/kv.env in the request URL

code:

[sourcecode lang=“xml”]

// supplant fn (credit: http://www.crockford.com/javascript/remedial.html) if(!String.prototype.supplant){String.prototype.supplant=function(o){return this.replace(/{([^{}]*)}/g,function(a,b){var r=o[b];return typeof r===‘string’||typeof r===‘number’?r:a;});};}

// response response.object = function () { var queries = [], results = [];

queries[0] = “select * from yql.storage where name = ‘{select}’” .supplant( { ‘select’ : select } ); results[0] = y.xmlToJson( y.query(queries[0]).results );

if ( results[0].results.result.value ) { return results[0].results.result.value[key]; } }(); <!-- sample query: use ‘http://{your domain}/kv.xml’ as kv; insert into kv (key, val) values (‘foo’, ‘bar’) <!-- note: use env file to define table url, select, & update –> // http://www.JSON.org/json2.js w/ alert removed y.include(‘http://{your domain}/json2.js’);

// supplant fn (credit: http://www.crockford.com/javascript/remedial.html) if(!String.prototype.supplant){String.prototype.supplant=function(o){return this.replace(/{([^{}]*)}/g,function(a,b){var r=o[b];return typeof r===‘string’||typeof r===‘number’?r:a;});};}

// response response.object = function () { var queries = [], results = [];

queries[0] = “select * from yql.storage where name = ‘{select}’” .supplant( { ‘select’ : select } ); results[0] = y.xmlToJson( y.query(queries[0]).results );

results[0].results.result.value[key] = val;

queries[1] = “update yql.storage set value = ‘{value}’ where name = ‘{update}’” .supplant( { ‘value’ : JSON.stringify( results[0].results.result.value ), ‘update’ : update } ); results[1] = y.xmlToJson( y.query( queries[1] ).results );

return results[1].results }(); <!-- note: use env file to define table url, select, & update –> // http://www.JSON.org/json2.js w/ alert removed y.include(‘http://{your domain}/json2.js’);

// supplant fn (credit: http://www.crockford.com/javascript/remedial.html) if(!String.prototype.supplant){String.prototype.supplant=function(o){return this.replace(/{([^{}]*)}/g,function(a,b){var r=o[b];return typeof r===‘string’||typeof r===‘number’?r:a;});};}

// response response.object = function () { var queries = [], results = [];

queries[0] = “select * from yql.storage where name = ‘{select}’” .supplant( { ‘select’ : select } ); results[0] = y.xmlToJson( y.query(queries[0]).results );

if ( !results[0].results.result.value || !results[0].results.result.value[key] ) { return { error : ‘key not found’ } }

results[0].results.result.value[key] = val;

queries[1] = “update yql.storage set value=’{value}’ where name=’{update}’” .supplant( { ‘value’ : JSON.stringify( results[0].results.result.value ), ‘update’ : update } ); results[1] = y.xmlToJson( y.query( queries[1] ).results );

return results[1].results }(); <!-- sample query: use ‘http://{your domain}/kv.xml’ as kv; delete from kv where key=‘foo’ –> <!-- note: use env file to define table url, select, & update –> // http://www.JSON.org/json2.js w/ alert removed y.include(‘http://{your domain}/json2.js’);

// supplant fn (credit: http://www.crockford.com/javascript/remedial.html) if(!String.prototype.supplant){String.prototype.supplant=function(o){return this.replace(/{([^{}]*)}/g,function(a,b){var r=o[b];return typeof r===‘string’||typeof r===‘number’?r:a;});};}

// response response.object = function () { var queries = [], results = [];

queries[0] = “select * from yql.storage where name = ‘{select}’” .supplant( { ‘select’ : select } ); results[0] = y.xmlToJson( y.query(queries[0]).results );

if ( !results[0].results.result.value || !results[0].results.result.value[key] ) { return { error : ‘key not found’ } }

delete results[0].results.result.value[key];

queries[1] = “update yql.storage set value=’{value}’ where name=’{update}’” .supplant( { ‘value’ : JSON.stringify( results[0].results.result.value ), ‘update’ : update } ); results[1] = y.xmlToJson( y.query( queries[1] ).results );

return results[1].results }();

Please post back w/ suggestions/questions.  Here’s a duck for good luck:

Flickr picture of a mallard

Photo credit: foxypar4