Basil.js
The missing Javascript smart persistence layer. Unified localstorage, cookie and session storage JavaScript API.
Philosophy
Basil aims to ease the frontend storage management for developers. It strives to be bulletproof and handle disabled cookies, full localStorage and other unwanted native storage exceptions..
When you try to store something, basil will automatically look through all the available storage mechanisms and find the best suited one to store your value. It also handles storage of complex javascript objects using json.
Basic Usage
basil = options; // basic methodsbasil; // store 'bar' value under 'foo' keybasil; // store 'xyz' value under 'abc' keybasil; // returns 'bar'basil; // returns ['abc', 'foo']basil; // returns { 'abc': ['local'], 'foo': ['local'] }basil; // remove 'foo' value // advanced methodsbasil; // boolean. Test if localStorage is availablebasil; // reset all stored values under namespace
Advanced Usage
Storages
basil = options; // force storage on the go through basil// set 'bar' value under 'foo' key in localStoragebasil; // set 'bar' value under 'foo' key.// try first to store it into cookies and if not possible into localStoragebasil; // set 'xyz' value under 'abc' key in memorybasil; // set value without JSON encodingbasil; // will save { "bar": "baz" } as string // retrieve keysbasil; // returns ['foo', 'abc']basil; // returns ['abc'] // retrieve keys mapbasil; // returns { 'foo': ['local', 'cookie'], 'abc': ['memory'] }basil; // returns { 'abc': ['memory'] }
Native storages
// Access native storages
// With basil API, but without namespace nor JSON parsing for values
// cookies has specific options
Basil.cookie.get(key);
Basil.cookie.set(key, value, {
'expireDays': days,
'domain': 'mydomain.com',
'secure': true,
'sameSite': 'strict'
});
// localStorage
Basil.localStorage.get(key);
Basil.localStorage.set(key, value);
// sessionStorage
Basil.sessionStorage.get(key);
Basil.sessionStorage.set(key, value);
Namespaces
basil = options; // store data under default namespacebasil; // store data under a given namespacebasil;basil; // retrieve databasil; // return 'world'basil; // return 42 // retrieves keysbasil; // returns ['hello']basil; // returns ['hello', 'abc'] // retrieves keys mapbasil; // returns { 'hello': ['local'] }basil; // returns { 'hello': ['local'], 'abc': ['memory'] } // remove data under a given namespacebasil;basil; // return 'world'basil; // return null // reset data under a given namespacebasil;
Configuration
Here is the whole options
object that you could give to Basil:
options = // Namespace. Namespace your Basil stored data // default: 'b45i1' namespace: 'foo' // storages. Specify all Basil supported storages and priority order // default: `['local', 'cookie', 'session', 'memory']` storages: 'cookie' 'local' // expireDays. Default number of days before cookies expiration // default: 365 expireDays: 31 // keyDelimiter. The value used delimt the namespace from the key name // default: '.' keyDelimiter: '.' ;
Compatibility
- Firefox 3.5+
- Internet Explorer 7 (requires json2.js)
- Internet Explorer 8+
- Chrome 4+
- Safari 4+
Plugins
List plugin
This plugin mimics Redis Lists methods and behaviors. Here are the (yet) supported methods.
basil = options;basil;basil;basil;basil;basil;basil;basil;basil;basil;basil;basil;
Set plugin
This plugin mimics Redis Sets methods and behaviors. Except sscan all the methods are implemented.
basil = options;basil;basil;basil;basil;basil;basil;basil;basil;basil;basil;basil;basil;basil;basil;
Build
To generate the production files, make sure you have already installed the
dependencies using npm install
and then just use:
npm run-script build
Tests
To launch the test suite, make sure you have already installed the dependencies
using npm install
.
Tests are launching in all your installed browsers. They're also launched on
Travis CI, in PhantomJS.
npm test
npm run-script test-plugins
License
MIT. See LICENSE.md