This package has been deprecated

    Author message:

    this package has been deprecated, use @sifrr/storage instead.

    @aadityataparia/browser-storage

    0.1.0 • Public • Published

    BrowserStorage

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

    Build: CircleCI

    Size

    Type Size
    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;

    Install

    npm i @aadityataparia/browser-storage

    DownloadsWeekly Downloads

    6

    Version

    0.1.0

    License

    MIT

    Unpacked Size

    162 kB

    Total Files

    27

    Last publish

    Collaborators

    • aadityataparia