logo

AsyncStorage

The AsyncStorage class is an abstraction to implement any asynchronous 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 { AsyncStorage } from '@aracna/core';

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

(async () => {
  await 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 { AsyncStorage } from '@aracna/core';

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

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

  // will log { name: "John", surname: "Doe" }
  console.log(await 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 { AsyncStorage } from '@aracna/core';

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

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

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

  await 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 { AsyncStorage } from '@aracna/core';

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

(async () => {
  await storage.set('item1', { name: 'John', surname: 'Doe' });
  await storage.set('item2', { name: 'Paul', surname: 'Smith' });

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

  await 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 { AsyncStorage } from '@aracna/core';

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

(async () => {
  let target = {};

  await storage.set('item', { name: 'John', surname: 'Doe' });
  await 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 { AsyncStorage } from '@aracna/core';

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

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

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