pjotor / b

Persistant KeyValue storage

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

/b

Persistant KeyValue storage


Some times you just need a quick and dirty data storage, something that just gives you a bucket to pour some data into.

This is that bucket.

Installing


Update the settings file with the values for your enviroment, in that file you'll also find the SQL for creating the database table.

Create/Upload/add/Edit and you'll have your own /b.

Saving


Saving is as simple as a GET request to the server. If an ID is supplied that post will be updated.

Remember that this method limits the amount of data transported to the max URL length, so try to keep it under 1855 characters in total.

Save (jQuery)

function saveData(key, data, id){
    var request = {
        key: key,
        data: JSON.stringify(data)
    };
    
    if(id)
        request.id = id;
    
    $.getJSON("/b", request)
    .fail(function(re){ 
        //Handle failures to save here
    })
    .done(function(re){
        //Handle successful save here
    })
}

If the save is successful you will get a JSON object back consisting of these keys:

  • type Response type (ie. "success")
  • info Action performed (ie. "inserted")
  • id The ID of the element effected

Save response example

{
    type: "success", 
    info: "inserted", 
    id: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr"
}

Loading


Loading is just as simple, just supply the key of the bucket and the ID of the data.

Load (jQuery)

function loadData(key, id){
    $.getJSON("/b",{
        key: key,
        id: id
    })
    .fail(function(re){ 
        //Handle load fail here
    })
    .done(function(re){
        //Handle successful loading here
        //Don't forgett to parse it if it's JSON
        //i.e. JSON.parse(re)
    })
}

Successfully loading data returns these keys:

  • type Response type (ie. "success")
  • info Action performed (ie. "loaded")
  • id The ID of the element effected
  • data The data saved (as a string)
  • created The time the ID was created

Load response example

{
    type: "success", 
    info: "loaded", 
    id: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr",
    data: '{"hello":"world"}', 
    created: "1970-01-01 00:00:00"
}

Errors


Not often but sometimes you go goof up, then the server will return an error. This happens when you don't send enough parameters, the wrong parameters or something else is wrong. This is what it can look like:

Error response example

{
    "type":"error",
    "info":"wrong parameters"
}  

Please note that all errors will be wrapped in a HTTP 500 response.

That's it. ♥

About

Persistant KeyValue storage

License:MIT License


Languages

Language:PHP 100.0%