Socket
Book a DemoInstallSign in
Socket

@aadityataparia/browser-storage

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aadityataparia/browser-storage

Frontend key-value(JSON to JSON) storage library

0.1.0
latest
Source
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

BrowserStorage

Browser key-value(JSON) storage library with cow powers.

Build: CircleCI

Size

TypeSize
Normal (dist/browserstorage.js)Normal
Minified (dist/browserstorage.min.js)Minified
Minified + Gzipped (dist/browserstorage.min.js)Minified + Gzipped

Types of storages available (in default priority order)

  • IndexedDB
  • WebSQL
  • LocalStorage
  • Cookies

How to use

Directly in Browser using standalone distribution

Add any of /dist/browserstorage.js or /dist/browserstorage.min.js as script tag in your website.

Compatibility table for standalone distribution

  • chrome >= 55
  • safari >= 10.1
  • opera >= 42
  • firefox >= 53

Using npm

Do npm install browserstorage or add the package to your package.json.

API

BrowserStorage uses Promises API.

Initialization

  • Initialize a storage with a type
let storage = new BrowserStorage(type)

where type is one of indexeddb, websql, localstorage, cookies.

Note: If that type is not supported in the browser, then first supported storage will be selected based on priority order.

  • Initialize with options
// Options with default values
let options = {
  priority: ['indexeddb', 'websql', 'localstorage', 'cookies'], // Priority Array of type of storages to use
  name: 'BroswerStorage', // name of table (treat this as a variable name, i.e. no Spaces or special characters allowed)
  version: 1, // version number (integer / float / string), 1 is treated same as '1'
  desciption: 'Browser Storage', // description (text)
  size: 5 * 1024 * 1024 // Max db size in bytes only for websql (integer)
}
storage = new BrowserStorage(options)

Inserting key-value

// insert single key-value
let key = 'key';
let value = { value: 'value' };
storage.insert(key, value).then(() => {/* Do something here */});

// insert multiple key-values
let data = { a: 'b', c: { d: 'e' } }
storage.insert(data).then(() => {/* Do something here */});

Getting value

// select single key-value
storage.select('key').then((value) => console.log(value)); // > { key: { value: 'value' } }

// select multiple key-values
storage.select(['a', 'c']).then((value) => console.log(value)); // > { a: 'b', c: { d: 'e' } }

Deleting a key

// delete single key-value
storage.delete('key').then(() => {/* Do something here */});

// delete multiple key-values
storage.delete(['a', 'c']).then(() => {/* Do something here */});

Updating a key

// update single key-value
let newValue = { newValue: 'new' };
storage.update('key', newValue).then(() => {
  // checking if value is updated
  storage.select('key').then((v) => console.log(v)); // > { key: { newValue: 'new' } }
});

// update multiple key-values
storage.update({ a: 'bnew', c: { dnew: 'enew' } }).then(() => {
  // checking if value is updated
  storage.select(['a', 'c']).then((v) => console.log(v)); // > { a: 'bnew', c: { dnew: 'enew' } }
});

Upserting a key-value (Insert else update)

// upsert single key-value
let upValue = { upValue: 'up' };
storage.upsert('key', upValue).then(() => {
  // checking if value is updated
  storage.select('key').then((v) => console.log(v)); // > { key: { upValue: 'up' } }
});

// upsert multiple key-values
storage.upsert({ a: 'bup', c: { dup: 'eup' } }).then(() => {
  // checking if value is updated
  storage.select(['a', 'c']).then((v) => console.log(v)); // > { a: 'bup', c: { dup: 'eup' } }
});

Clear table

storage.clear().then(() => {
  // checking if value is deleted
  storage.select('key').then((v) => console.log(v)); // > {}
});

Get all data in table

storage.data().then((data) => console.log(data)); // > { key: { upValue: 'up' }, a: 'bup', c: { dup: 'eup' } }

Get all created storage instances

BrowserStorage.all;

Keywords

indexeddb

FAQs

Package last updated on 31 Aug 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.