Storage is a simple light weight utility class for handling all types of browser based storage using a single instance.
- Cookies
- Local Storage
- Session Storage
- Indexed DB
- Use single class instance to manage all types of storage in browser
- Comes with easy and handy methods to manipulate your data in the most effcient way
- Reduces effort and improves code redablity and development experience
In order to use Storage simply import the Storage class from the package and create a new instance
import Storage from "storage"
//definition
new Storage(<storage-type>, <storage-config>);
//cookie
const cookie = new Storage(Storage.COOKIE);
//local storage
const localStorage = new Storage(Storage.LOCAL_STORAGE)
//session storage
const sessionStorage = new Storage(Storage.SESSION_STORAGE)
//indexed db
const idb = new Storage(Storage.INDEXED_DB, IDB_CONFIG)
Functions | Arguments | Description |
---|---|---|
get | identifier, options | returns found data based on identifier |
getAll | options | returns all the data |
add | identifier, value, options | add data to the chosen storage type |
delete | identifier, options | deletes data based on identifier |
deleteAll | options | deletes all the data for a chosen storage type |
Note
identifier
is required for all types of storage except given that storage type inINDEXED_DB
andid
is configured inIDB_CONFIG
Properties | Description |
---|---|
Storage.COOKIE | used to set the storage type to Cookie |
Storage.LOCAL_STORAGE | used to set the storage type to Local Storage |
Storage.SESSION_STORAGE | used to set the storage type to Session Storage |
Storage.INDEXED_DB | used to set the storage type to Indedex Db |
Storage.COOKIE_PRIORITY | used to set the priority of the cookie. High, Medium, Low |
cookieSecured | (boolean) used to set the cookies secure propety |
const cookie = new Storage(Storage.COOKIE);
//cookieOptions - optional
const cookieOptions = {
expires: 10, //(in minutes) default - Session
priority: Storage.COOKIE_PRIORITY.HIGH, // default - Storage.COOKIE_PRIORITY.MEDIUM
path: "/", // default - "/"
};
// setting cookies secured
cookie.cookieSecured = true;
cookie.add("cookie_id", "cookie_value", cookieOptions);
cookie.get("cookie_id"); // => value
cookie.getAll(); // => Object {id1 : value1, id2 : value2 ...}
const cookieOptions = {
path: "/path", // default - "/"
domain: "yourdomain.com", //default - undefined
};
cookie.delete("cookie_id", cookieOptions);
const cookieOptions = {
path: "/path", // default - "/"
domain: "yourdomain.com", //default - undefined
};
cookie.deleteAll(cookieOptions);
const localStorage = new Storage(Storage.LOCAL_STORAGE);
//storageOptions - optional
const storageOptions = {
expires: 10, //(in minutes) default - null
};
localStorage.add("id", data, storageOptions);
localStorage.get("id"); // => data
localStorage.getAll(); // => Object {id1 : data1, id2 : data2 ...}
localStorage.delete("id");
localStorage.deleteAll();
const sessionStorage = new Storage(Storage.SESSION_STORAGE);
sessionStorage.add("id", data);
sessionStorage.get("id"); // => data
sessionStorage.getAll(); // => Object {id1 : data1, id2 : data2 ...}
sessionStorage.delete("id");
sessionStorage.deleteAll();
const IDB_CONFIG = {
databaseName: "ixora-db",
version: 1,
stores: [{
name: "customers",
id: { keyPath: "id", autoIncrement: true },
indices: [
{ name: "name", keyPath: "name", options: { unique: false } },
{ name: "email", keyPath: "email", options: { unique: true } },
],
},],
};
const idb = new Storage(Storage.INDEXED_DB,IDB_CONFIG);
idb.add(null, data, {currentStore : "customers"}) => returns JS Promise
const IDB_CONFIG = {
databaseName: "ixora-db",
version: 1,
stores: [{name: "customers"}]
};
const idb = new Storage(Storage.INDEXED_DB,IDB_CONFIG);
idb.add(id, data, {currentStore : "customers"}) => returns JS Promise
//returns JS Promise
idb.get("id",{currentStore : "customers"}).then(value => ...).catch(err => ...)
//returns JS Promise
sessionStorage.getAll({currentStore : "customers"}).then(values => ...).catch(err => ...)
sessionStorage.delete("id", {currentStore : "customers"}) => returns JS Promise
sessionStorage.deleteAll({currentStore : "customers"}) => returns JS Promise