WebApps 2

Key-value Data Store

Data in a key-value data store is stored and retrieved using a key that uniquely identifies the record.

API

The data storage API is available in WebApps and RESTApps.

// retrieve an instance of the storage API
const storage = require('storage');
// get a key-value store
// if no store exists with the provided name, one will be created
// (note: the name must also be explicitly registered in the app's manifest.json)
const keyValueStore = storage.getKeyValueDataStore('myStore');

API variants [@since 5.2]

The data storage API is available in two variants. One is callback-based whereas the other is callback-free. Please note that both variants execute synchronously, which probably makes the callback-free approach easier to work with.

put(key, data) [@since 5.2]

Stores data associated with a key.
Returns current data stored on the key.

Argument

Description

key

A key to uniquely identify the record

data

JSON data to store

const config = {
   name: 'Foo',
   title: 'Bar'
};

try {
   keyValueStore.put('config', config);
} catch (e) {
   // [Error handling](https://developer.sitevision.se/docs/data-storage/error-handling)
}

If a property is set to null, the property will be removed

get(key) [@since 5.2]

Returns data associated with a key.

Argument

Description

key

A key to retrieve data from

 try {
   config = keyValueStore.get('config');
} catch (e) {
   // [Error handling](https://developer.sitevision.se/docs/data-storage/error-handling)
}

remove(key) [@since 5.2]

Removes data associated with a key.
Returns the removed data.

Argument

Description

key

A key to identify data to remove

 try {
   keyValueStore.remove('config');
} catch (e) {
   // [Error handling](https://developer.sitevision.se/docs/data-storage/error-handling)
}