The story Begins
The story Begins
Lately, I’ve decided to write my own JavaScript library for educational and experimental purposes. The idea came to me while I was creating a demo to a session that I will deliver in the near future. In the session, I wanted to show how to use different JavaScript storage types and didn’t wanted to change my code every time for every storage type.
So I’ve created a library to handle client-side data storages in simple and consistent way. The library is called story.js and its first bits are hosted in GitHub. Right now the library includes the following storage types:
- Web Storage
- IndexedDB
- Cookies
- In-memory
In the future expect more features. I’ve also convince my managers at Sela Group to help in this initiative and other Sela experts (Noam and Sebastian) agreed to contribute to the library. You can join us too – just let me know through the Blog’s contact form.
Here are simple code snippets that show how to use story.js with Web Storage:
/// <reference path="story.js" />
var storage = story.storage(story.StorageTypes.WEB_STORAGE);
storage.add("key", "value").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.get("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.contains("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.update("key", "value1").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.remove("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.contains("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.clear().then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
with in-memory:
/// <reference path="story.js" />
var storage = story.storage(story.StorageTypes.IN_MEMORY);
storage.add("key", "value").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.get("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.contains("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.update("key", "value1").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.remove("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.contains("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.clear().then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
with cookies:
/// <reference path="story.js" />
var storage = story.storage(story.StorageTypes.COOKIE);
storage.add("key", "value").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.get("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.contains("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.update("key", "value1").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.remove("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.contains("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.clear().then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
and with IndexedDB:
/// <reference path="story.js" />
var factory = story.storage(story.StorageTypes.INDEXEDDB);
factory.createStore({
name: "people",
keyPath: undefined,
autoInc: false,
names: ["name", "email"],
values: ["name", "email"],
unique: [false, true]
}).then(function (storage) {
console.log(storage);
storage.add("key", "value").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.get("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.contains("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.update("key", "value1").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.remove("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.contains("key").then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
storage.clear().then(function (data) {
console.log(data);
}, function (error) {
console.log(error);
});
}, function (error) {
console.log(error);
});
Pay attention that this is an experimental library and there might be bugs.
Expect more story versions in the future.
I’ll appreciate any feedback about this initiative.