API for asynchronous local storage
API for asynchronous session storage
Alias for a clear method of the synchronous local storage API
Alias for a get method of the synchronous local storage API
Alias for a has method of the synchronous local storage API
API for synchronous local storage
Alias for a namespace method of the synchronous local storage API
Alias for a remove method of the synchronous local storage API
API for synchronous session storage
Alias for a set method of the synchronous local storage API
Creates a new kv-storage API with the specified engine
if true, then the storage is implemented async interface
core/kv-storage
This module provides API to work with a persistent key-value storage using different runtime engines, like localStorage, indexedDb, SQLite, etc.
Supported engines
browser-localstorage
browser-indexeddb
node-localstorage
API
Synchronous and asynchronous API
The module has interfaces to work with storage in a synchronous and asynchronous way. In the case of asynchronous API, all defined methods have the same input parameters as the synchronous API but return promises. Notice, not each engine implements the synchronous API, so we recommend preferring to use asynchronous API in most cases.
API structure
Both API exposes methods to implement simple CRUD operations.
has
Returns true if a value by the specified key exists in the storage. Notice, the method can take a list of additional parameters provided to the used storage' engine.
get
Returns a value from the storage by the specified key.
The returning value automatically parses by using
Object.parse
from a string to equivalent JS value, i.e.,'1'
will be parsed to1
,'true'
totrue
,'2021-07-09T08:15:57.753Z'
toDate
, etc.Notice, the method can take a list of additional parameters provided to the used storage' engine.
set
Saves a value to the storage by the specified key.
The value to parse automatically serializes to a string by using
Object.trySerialize
, i.e., arrays and dictionaries will be serialized to JSON, etc.Notice, the method can take a list of additional parameters provided to the used storage' engine.
remove
Removes a value from the storage by the specified key. Notice, the method can take a list of additional parameters provided to the used storage' engine.
clear
Clears the storage by the specified filter and returns a list of removed keys. Notice, the method can take a list of additional parameters provided to the used storage' engine.
namespace
All values are stored within a local storage in the global namespace, i.e., you can override any value you have. If you want to isolate data from other data, you can specify the custom namespace. Mind, you still can override or remove these values from the storage by using global API.
Asynchronous API
If you need to work with an asynchronous local storage, you can use `asyncLocal. The API is pretty similar to local, but its methods return a promise instead of a raw result.
Session API
If you need to store data only during the active session, you can use
session
andasyncSession
API-s.Providing API as a strategy
You can pass your storage API as a strategy to another function or method.
Specifying engines to store
By default, in a browser, the module uses native
window.localStorage
andwindow.sessionStorage
API-s. Also, there is a storage based onIndexedDb
. Use thefactory
method to specify an engine to store.Specifying a custom engine
You can specify your engine by using the
factory
method.The wrapped engine need to have CRUD methods from the interface: