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');

put(key, data, callback)

Stores data associated with a key

Argument

Description

key

A key to uniquely identify the record

data

JSON data to store

callback

Called after execution. The first parameter of the callback will contain an error object if an error occurred, undefined otherwise. The second parameter will contain the current data stored on the key.

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

keyValueStore.put('config', config, (err, data) => {
   if (err) {
   // handle error
   }
   // data --> 
   {
      dsid: 'config', 
      name: 'Foo', 
      dstimestamp: 1554472667609, 
      title: 'Bar'
   }
});

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

get(key, callback)

Gets data associated with a key

Argument

Description

key

A key to retrieve data from

callback

Called after execution. The first parameter of the callback will contain an error object if an error occurred, undefined otherwise. The second parameter will contain the data associated with the key.

keyValueStore.get('config', (err, data) => {
   if (err) {
   // handle error
   }
   res.json(data);
});

remove(key, callback)

Removes data associated with a key

Argument

Description

key

A key to identify data to remove

callback

Called after execution. The first parameter of the callback will contain an error object if an error occurred, undefined otherwise. The second parameter will contain the removed data.

keyValueStore.remove('config', (err, data) => {
   if (err) {
   // handle error
   }
   // ...
});