Home
> Uncategorized > #NoSQL Implement persistent key/value storage in #JavaScript
#NoSQL Implement persistent key/value storage in #JavaScript
nosql.apixml.net
Implement persistent Key/Value storage using client side java script only
Installation
http://nosql.apixml.net/nosql.js
Save data
var id = nosql.guid(); nosql.put(id,"hello world").then(function (data) { // This stores "Hello world" });
Load data
nosql.get(id).then(function(data){ alert(data); });
Each key in the store must be a GUID (i.e. 9ED4B528-7F5F-3074-BBBF-947C6133ED13), this is to prevent overlaps between your keys and other user’s keys. Data can be any string, typically this would be json, but you can store any UTF8 string (not binary data).
This is backed by a REST API, which you make a GET, POST, or DELETE request to http://nosql.apixml.net/%5BGUID%5D to retrieve, update/insert and delete data respectively.
Categories: Uncategorized
Here’s a C# client code, for getting and storing any object remotely.
private static readonly WebClient Wc = new WebClient();
private static readonly JavaScriptSerializer JsSerializer = new JavaScriptSerializer();
public static void Put(T o, Guid id)
{
var strJson = JsSerializer.Serialize(o);
Wc.UploadString(“http://nosql.apixml.net/” + id, strJson);
}
public static T Get(Guid id)
{
var strJson = Wc.DownloadString(“http://nosql.apixml.net/” + id);
return JsSerializer.Deserialize(strJson);
}
LikeLike
A handy bit of code to create deterministic GUIDs (i.e. guids that are consistent, given a provided string) is here
public static Guid ToGuid(string src)
{
byte[] stringbytes = Encoding.UTF8.GetBytes(src);
byte[] hashedBytes = new System.Security.Cryptography
.SHA1CryptoServiceProvider()
.ComputeHash(stringbytes);
Array.Resize(ref hashedBytes, 16);
return new Guid(hashedBytes);
}
This allows you store based on a key/value as string/object rather than guid/object
LikeLike