A lightweight utility to manage browser storage (localStorage, sessionStorage, and cookies) with advanced features. Easily configure namespaces, key trimming, and data expiry.
npm install opendb-store> A lightweight utility to manage browser storage (localStorage, sessionStorage, and cookies) with advanced features. Easily configure namespaces, key trimming, and data expiry.
> local - Contain localStorage methods
> session - Contain sessionStorage methods
npm i opendb-store
`Import opendb-store in Your Project
`
import db from 'opendb-store'db.local.set('libname', 'OpenDB Store');
console.log('Welcome to: ', db.local.get('libname'));
`Local Storage
> LocalStorage is a web storage that allows websites to store data persistently on a user's browser.
`
db.local.methodname
`Session Storage
> SessionStorage is a web storage that stores data for the duration of a page session and is cleared when the browser or tab is closed.
`
db.session.methodname
`Browser Support
Supports modern browsers including Chrome, Firefox, Safari, and Edge.
Demonstrating LocalStorage and SessionStorage with an Example
`
opendb store
`Key Changes from the Old Approach
- Powerful Method: Enhanced functionality.
- ES6: Modern JavaScript features.
- Modular: Reusable and maintainable code.
- Namespacing: Organized and conflict-free.
For further details, see the old-approach documentation.
List of Local and Session Storage Methods
$3
Stores data in local or session storage with a key and value.
Example:
`javascriptdb.local.set('libname', 'OpenDB Store'); // Set simple value
db.local.set('object', { name: 'OpenDB Store', version: 'x.y.z' }); // Set Object Value
db.local.set('array', ['OpenDB Store', 'x.y.z']); // Set Array Value
db.local.set('expiringKey', 'It will expire', { expire: 1 * 1000 }); // Set simple value expire after 1 second
// for session storage
db.session.set('libname', 'OpenDB Store'); // Set simple value inside session storage
`$3
Retrieves data from local or session storage by key. Returns defaultValue if the key does not exist.`javascript// Get Simple Value
console.log('Welcome to: ', db.local.get('libname')); // OpenDB Store
// Get Object Value
console.log('Object: ', db.local.get('object'));
// Or
const { name, version } = db.local.get('object');
console.log(name, version);
// Get Array Value
console.log('Array: ', db.local.get('array'));
// Get defaultValue
db.local.get('missingKey', {}); // {} by default it will null
console.log('Before(2 sec) expiringKey', db.local.get('expiringKey'))
(async () => {
await new Promise((resolve) => setTimeout(resolve, 2 * 1000));
console.log('After(2 sec) expiringKey', db.local.get('expiringKey')); // will get default value
})();
`$3
check whether a specified key exists in local or session storage`javascript// Checks if a key exists in local storage.
console.log(db.local.has('libname')); // true
console.log(db.local.has('missingkey')); // false
`$3
remove a specific item from local or session storage`javascript// Removes a value from storage.
db.local.remove('libname');
`$3
Empty the entire storage.`javascript// Clears all data from local storage
db.local.clear();
`$3
Get the size of the value for a specific key.`javascript
const sizeInBytes = 2 1024 1024; // 2 MB
const largeString = "A".repeat(sizeInBytes); // Create a 2MB string
const largeObject = { data: largeString };db.local.set('largeObj', largeObject);
console.log(db.local.size('largeObj')); // 2097163 (raw bytes)
console.log(db.local.size('largeObj', { format: 'KB', unit: true })); // 2.00 MB
`$3
Get the remaining free space in local or session storage.`javascriptconsole.log(db.local.free()); // raw bytes
console.log(db.local.free({ format: 'mb', unit: true })); // e.g., "3.00 MB"
`$3
Get the total capacityof local or session storage.`javascriptconsole.log(db.local.capacity()); // raw bytes
console.log(db.local.capacity({ format: 'mb', unit: true })); // e.g., "5.00 MB"
``