LZStorage
LZStorage combines different storage types (local, session, and cookie) and provides a cross browser wrapper to store and retrieve data in an optimal manner. LZStorage implements Pieroxy's LZ compression algorithm to compress data while saving it in local storage.
Installation
npm install --save lzstorage
How does it work?
Common storage can be used in following ways:
- Using script tag
<script src="lzstorage.js"></script><script>const lzstorage = new LZStorage();lzstorage.get(...);</script>
- Using module bundler
;const lzstorage = ;lzstorage;
const lzstorage = ;const lzstorage = ;lzstorage;
Enable compression
lzstorage does not enable compression by default. To enable compression you need to create an instance of LZStorage as follows:
const lzswc = compression: true;
Enable debugging
To enable debug mode you need to pass debug: true
as a parameter to LZStorage constructor.
const lzswdebug = debug: true;
Storage methods:
set
To set data in HTML5 storage. By default the values are stored in localStorage
.
lzstorage;
To set key in session storage (set isSession
flag to true
by passing it as a third prameter)
lzstorage;
If storage is unavailable, the method automatically fallbacks to cookies as alternative storage
get
To get value from storage
lzstorage;
Get method checks all the available storages to get the data. The data is automatically parsed if it is a valid JSON string.
getAll
To get data from all stores. The values are returned in a form of array of objects with information such as type of storage used.
lzstorage; // --> Returns [{ value: <data value>, storage: '<type of storage>' }, { ... }, { ... }];
update
To update the value of an existing key. It is similar to set
except that it gives you more control over how you want to update the data. We have following examples that explain how update
is different from set
.
lzstorage;
Example 1: Updating object properties
Using set
:
lzstorage;// Updating the age to 27lzstorage; // You have re-write the entire object
Using update
:
lzstorage;// Updating profile by passing only the agelzstorage; // --> Value now becomes { name: "Joanne", age: 27 }
You can also use syntax below:
// 1. Arrow function with implicit returnlzstorage;// 2. Arrow function with data passed as valuelzstorage;
Example 2: Updating arrays
Using set
:
lzstorage;// Insert a new value to arraylzstorage; // <-- Inserts string 'hello'. But you need to pass the entire array in this case
Using update
:
lzstorage;// Insert a new value to arraylzstorage;
Example 3: Updating multiple stores at once
There are chances that your data exists in multiple stores having the same key. Update allows you to modify them all at once
Using set
:
lzstorage;lzstorage;// Add age = 27 to both profiles currently stored in different storeslzstorage;lzstorage;
Using update
:
lzstorage;lzstorage;// Add age = 27 to both profiles currently stored in different storeslzstorage; // <-- This updates all stores at once
More complex example where we want to update different values:
Consider same example as above. This time we are inserting different ages for Joanne and Adam.
Using set
:
lzstorage;lzstorage;// Add age = 27 to both profiles currently stored in different storeslzstorage;lzstorage;
Using update
:
lzstorage;lzstorage;// Add age = 27 to both profiles currently stored in different storeslzstorage;
Update is useful when data sets are large and you wisth to update only a small chunk.
setCookie
To create a cookie
lzstorage;
getCookie
To get cookie from cookie store
lzstorage;
removeCookie
To delete a cookie from cookie store
lzstorage;
resetCookie
To reset an existing cookie with new data. It is similar to setCookie
except that it deletes existing cookie first.
lzstorage;
"Reset" allows you to change expiry ,path and domain of existing cookie.
Summing up
Common storage provides with a robust and cross browser way to manage HTML5 storage. However, it doesn't not overcome certain limitations like storage size and availability. If HTML5 storage is not available, there is a fallback of cookie storage, but we know that cookie storage is limited in terms of size.
To overcome size limitation we are working on a version 4 which introduces lossless compression. To checkout the progress you can switch to feature/v4 branch.