logo

SyncStorage

The SyncStorage class is an abstraction to implement any synchronous storage API in an uniform way.

Properties

  • name (string): The name of the storage instance.

Set an item

The set method sets an item in the storage. Optionally, you can specify the keys of the item that you want to set. If you don’t specify any key, the whole item will be set.

import { SyncStorage } from '@aracna/core';

const sm = new Map();
const storage = new SyncStorage(
  'storage',
  () => sm.clear(),
  (key) => sm.get(key),
  (key) => sm.has(key),
  (key) => sm.remove(key),
  (key, item) => sm.set(key, item)
);

storage.set('item', { name: 'John', surname: 'Doe' });

// will log { name: "John", surname: "Doe" }
console.log(sm.get('item'));

Get an item

The get method retrieves an item from the storage. Optionally, you can specify the keys of the item that you want to get. If you don’t specify any key, the whole item will be retrieved.

import { SyncStorage } from '@aracna/core';

const sm = new Map();
const storage = new SyncStorage(
  'storage',
  () => sm.clear(),
  (key) => sm.get(key),
  (key) => sm.has(key),
  (key) => sm.remove(key),
  (key, item) => sm.set(key, item)
);

storage.set('item', { name: 'John', surname: 'Doe' });

// will log { name: "John", surname: "Doe" }
console.log(storage.get('item'));

Remove an item

The remove method removes an item from the storage. Optionally, you can specify the keys of the item that you want to remove. If you don’t specify any key, the whole item will be removed.

import { SyncStorage } from '@aracna/core';

const sm = new Map();
const storage = new SyncStorage(
  'storage',
  () => sm.clear(),
  (key) => sm.get(key),
  (key) => sm.has(key),
  (key) => sm.remove(key),
  (key, item) => sm.set(key, item)
);

storage.set('item', { name: 'John', surname: 'Doe' });
storage.remove('item');

// will log false
console.log(sm.has('item'));

Clear all items

The clear method clears the storage, removing all the items.

import { SyncStorage } from '@aracna/core';

const sm = new Map();
const storage = new SyncStorage(
  'storage',
  () => sm.clear(),
  (key) => sm.get(key),
  (key) => sm.has(key),
  (key) => sm.remove(key),
  (key, item) => sm.set(key, item)
);

storage.set('item1', { name: 'John', surname: 'Doe' });
storage.set('item2', { name: 'Paul', surname: 'Smith' });
storage.clear();

// will log false, false
console.log(sm.has('item1'), sm.has('item2'));

Copy an item to a target object

The copy method copies an item from the storage to a target object. Optionally, you can specify the keys of the item that you want to copy. If you don’t specify any key, the whole item will be copied.

import { SyncStorage } from '@aracna/core';

const sm = new Map();
const storage = new SyncStorage(
  'storage',
  () => sm.clear(),
  (key) => sm.get(key),
  (key) => sm.has(key),
  (key) => sm.remove(key),
  (key, item) => sm.set(key, item)
);

const target = {};

storage.set('item', { name: 'John', surname: 'Doe' });
storage.copy('item', target);

// will log { name: "John", surname: "Doe" }
console.log(target);

Check if an item exists

The has method checks if an item exists in the storage. Optionally, you can specify the keys of the item that you want to check. If you don’t specify any key, the whole item will be checked.

import { SyncStorage } from '@aracna/core';

const sm = new Map();
const storage = new SyncStorage(
  'storage',
  () => sm.clear(),
  (key) => sm.get(key),
  (key) => sm.has(key),
  (key) => sm.remove(key),
  (key, item) => sm.set(key, item)
);

storage.set('item', { name: 'John', surname: 'Doe' });

// will log true
console.log(storage.has('item'));