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, callback)
Stores data associated with a key
Argument | Description |
---|---|
| A key to uniquely identify the record |
| JSON data to store |
| Called after execution. The first parameter of the callback will contain an error object if an error occurred, |
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 |
---|---|
| A key to retrieve data from |
| Called after execution. The first parameter of the callback will contain an error object if an error occurred, |
keyValueStore.get('config', (err, data) => {
if (err) {
// handle error
}
res.json(data);
});
remove(key, callback)
Removes data associated with a key
Argument | Description |
---|---|
| A key to identify data to remove |
| Called after execution. The first parameter of the callback will contain an error object if an error occurred, |
keyValueStore.remove('config', (err, data) => {
if (err) {
// handle error
}
// ...
});