@bizjournals/js-storage
v0.2.0
Published
ACBJ javascript storage classes
Keywords
Readme
Storage
This bundle has helper classes to interact with browser storage options. At present we
support localstorage and sessionstorage. These share a common abstract parent.
Storage references are critical to the implementation of our architecture. A reference is a named key that contains your data in the selected storage type.
Installing
Using npm:
npm install @bizjournals/vue-coreUsing yarn:
yarn add @bizjournals/vue-coreConfiguring your environment:
You will need to configure an NPM_TOKEN within your project to include these
private modules. The best idea is to use an existing project as an example.
For simple setups the .gitlab-ci.yml file within this project as a guide to accessing project variables via the .npmrc file
Example
import { StorageReference, LocalStorage, SessionStorage } from "@bizjournals/js-storage";
// This will setup a reference that is namespaced to bizj
// The full key in localstorage will be bizj.mykey
// This key will have an expiration partner that by default will be one hour
const something = new StorageReference('mykey', new LocalStorage());
something.saveAs({
my: 'data'
});
// local storage has a configurable API as well, and this can be used in storage
// reference objects too.
const local = new LocalStorage({
// by changing the namespace what was bizj by default will now be notbizj
// Example: notbizj.mykey
namespace: 'notbizj',
expires: 3600 // one hour
});
// session storage uses the same api as local storage
const somethingElse = new StorageReference('difkey', new SessionStorage({
namespace: 'woa'
}));
const somethingElsesData = somethingElse.obtain();Storage Reference
A storage reference is a keyed object that exists within either local or session storage. It may be prefixed with a namespace since of the respective storage container.
API
constructor(cacheKey : String, storageSystem : StorageAbstract)
obtain()
bust()
saveAs(data : Mixed)
Storage Abstract
The parent class for both local and session storage creates the API used by each of them.
API
constructor(options : Object : { namespace: String, expires: Number })
getItem(key: String)
hasItem(key : String)
setItem(key : String, value : Mixed, expires = true : Boolean)
Reference
getItem
Gets an item with a specified key. The namespace set in the construction of the object will
be pre-fixed to the beginning of the string, by default this is bizj.
hasItem
This checks to see if the item is available. If it is it'll cache it, so that if you need to get the item later you don't need to worry about additional reads.
setItem
Sets the item in storage and updates the item in cache. It will automatically prefix itself
with the namespace, by default bizj.
