v0.8

BrowserStorage

class BrowserStorage → file: core/lib/BrowserStorage.js

The library facilitates manipulation of browser localStorage and sessionStorage, allowing developers to easily store and retrieve data on the client-side. It simplifies managing both temporary and persistent storage needs in web applications.

Instance

Instance created with new BrowserStorage(browserStorageOpts).

Properties

Property Description Type Default
browserStorageOpts storage options (see below) object

browserStorageOpts

{
  storageType: 'local'|'session'  // default is local what means localStorage
}
More info at https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API.

Methods

The methods are identical to Cookie methods.

put(name, value) :void

Set local or session storage. The input value can be of any type and it's saved as string.
    ARGUMENTS:
  • name :string - cookie name
  • value :any - cookie value

putObject(name, value) :void

Set local or session storage. The input value is object and it's saved as string.
    ARGUMENTS:
  • name :string - cookie name
  • value :object - cookie value

get(name) :string

Get a storage value (string) by specific name. Returned value is string.
    ARGUMENTS:
  • name :string - cookie name

getObject(name) :object

Get a storage value by specific name. Returned value is object.
    ARGUMENTS:
  • name :string - cookie name

getObjectAll() :object

Get all storage values in array format: [{key1: val1}, {key2: val2},...] . The values (val1, val2, ...) are strings.

remove(name) :void

Remove storage by specific name.
    ARGUMENTS:
  • name :string - cookie name

removeAll() :void

Remove all storage values.

exists(name) :boolean

Check if specific browser storage exists.
    ARGUMENTS:
  • name :string - cookie name



Example

const { Controller, corelib } = require('@mikosoft/dodo');

class MyCtrl extends Controller {
  __init(trx) {
    const browserStorageOpts = {
      storageType: 'local'
    };
    this.bs = new corelib.BrowserStorage(browserStorageOpts);
  }

  test(name, value) {
    this.bs.put(name, value);
    this.bs.putObject('someObj', {x:22,y:'str'});
    console.log(this.bs.getAll());
    console.log(this.bs.get(name));
    console.log(this.bs.getObject('someObj'));
    this.bs.remove(name);
    this.bs.removeAll();
    console.log(this.bs.exists(name));
  }

}

Stackblitz Examples