A lightweight datastore wrapper
require(["store"], function(Store) {
// ...
});
<script src="src/client/store.js"></script>
Defaults are modified as follows:
Store.configure({
url: "http://localhost/datastore.php",
ttl: 36000
});
Note
configure()
alters the prototype object, hence modifications are propagated to instances on property level given that the respective instance properties have not been modified previously.
var objectStore = new Store({ namespace: 'object' });
var object = objectStore.create({
country: 'USA',
firstname: 'Stephen',
lastname: 'Colbert'
});
var object = objectStore.create();
object.set('country', 'USA');
object.set('firstname', 'Jon');
object.set('lastname', 'Stewart');
objectStore.list();
objectStore.get('8c0c1ff0-d0fe-38b7-376a-b0b1d53bd557');
object.update();
objectStore.update(object);
object.remove();
objectStore.remove(object);
<script src="src/client/store.js"></script>
<script>
(function(){
// local variables
var object, objectId, objectStore, objects;
// configure store defaults
Store.configure({
url: "http://localhost/store/examples/server.php",
ttl: 36000
});
// create new store
objectStore = new Store({ namespace: 'object' });
// create new store bound object
object = objectStore.create({
country: 'US',
firstname: 'Stephen',
lastname: 'Colbert'
});
// update on insert
object.update().done(function(object){
// wrap json object
object = objectStore.create(object);
// update properties
object.set('firstname', 'Jon');
object.set('lastname', 'Stewart');
// extract
objectId = object.get('id');
// update object
object.update().done(function(object){
// fetch object w/previously retrieved objectId
objectStore.get(objectId).done(function(object){
// wrap json object
object = objectStore.create(object);
// say hi
console.log(object.get('firstname') + " " + object.get('lastname'));
});
});
});
// collect update promises
objects = [];
// create some objects
Store.times(10, function(count){
// store update promises
objects.push(objectStore.create({
title: 'No.' + count,
abstract: 'Lorem Ipsum [...]',
text: 'Lorem Ipsum Si Amet They Say',
author: 'me',
lastmod: new Date().getTime()
}).update());
});
// when all objects have been updated
Store.when(objects).done(function(objects){
// fetch all objects
objects = objectStore.list();
// print when object have been retrieved
Store.when(objects).done(function(objects){
// log each
objects.forEach(function(object, index){
console.log(index, object);
});
});
});
// fetch object
object = objectStore.get(objectId);
// process fetched object
Store.when(object).done(function(object){
// execute on success
console.log("done", object);
}).fail(function(objects){
// execute on fail
console.log("fail", objects);
}).always(function(objects){
// always execute this block
console.log("always", objects);
});
})();
</script>
...
Collections stored on a per file basis.
Limitations
Built with prototyping in mind. Won't scale.
...
Supports embedding binary data base64 encoded.
Stores data using PHPs serialize()
function. Supports embedding binary data base64 encoded.
curl -h ...
Released under two licenses: new BSD, and MIT. You may pick the license that best suits your development needs.