new DataStore()
- Source:
Represents the dataStore that can be interacted with. This can be used to get instances of DataStoreNamespace, which can be used to interact with the namespace API.
The store is a key-value store, where a namespace contains a list of keys, and a key corresponds to an arbitrary JSON-object. The dataStore is DHIS2-instance wide.
Note that a namespace cannot exist without at least one key-value pair, for this reason you need to call set() after create() to save a namespace with a key and a value.
Example
import { init } from 'd2';
init({baseUrl: 'https://play.dhis2.org/demo/api'})
.then((d2) => {
d2.dataStore.get('namespace').then(namespace => {
namespace.get('key').then(value => console.log(value))
});
});
Extends
Methods
(static) getDataStore() → {DataStore}
- Source:
Get a new instance of the dataStore object. This will function as a singleton, when a DataStore object has been created when requesting getDataStore again the original version will be returned.
Returns:
Object with the dataStore interaction properties
- Type
- DataStore
create(namespace) → {Promise.<DataStoreNamespace>}
- Source:
- Overrides:
Creates a namespace. Ensures that the namespace does not exists on the server. Note that for the namespace to be saved on the server, you need to call set.
Example
Creating a namespace
d2.dataStore.create('new namespace').then(namespace => {
namespace.set('new key', value);
});
Parameters:
Name | Type | Description |
---|---|---|
namespace |
string | The namespace to create. |
Returns:
An instance of the current store-Namespace-instance representing the namespace that can be interacted with, or an error if namespace exists.
- Type
- Promise.<DataStoreNamespace>
delete(namespace) → {Promise}
- Source:
- Inherited From:
Deletes a namespace
Parameters:
Name | Type | Description |
---|---|---|
namespace |
string | The namespace to delete. |
Returns:
the response body from the API.
- Type
- Promise
get(namespace, autoLoadopt) → {Promise.<DataStoreNamespace>}
- Source:
- Overrides:
Tries to get the given namespace from the server, and returns an instance of DataStore that may be used to interact with this namespace. See DataStore.
Example
Getting a namespace
d2.dataStore.get('namespace').then(namespace => {
namespace.set('new key', value);
});
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
namespace |
Namespace to get. |
|||
autoLoad |
<optional> |
true
|
If true, autoloads the keys of the namespace from the server. If false, an instance of the namespace is returned without any keys (no request is sent to the server). |
Returns:
An instance of a DataStore representing the namespace that can be interacted with, or an error if namespace exists.
- Type
- Promise.<DataStoreNamespace>
getAll() → {Promise}
- Source:
- Inherited From:
Retrieves a list of all namespaces on the server.
Returns:
An array of namespaces.
- Type
- Promise
has(namespace) → {Promise.<boolean>}
- Source:
- Inherited From:
Convenience method to check if a namespace exists on the server.
Parameters:
Name | Type | Description |
---|---|---|
namespace |
string | Namespace to check. |
Returns:
True if namespace exists, false otherwise.
- Type
- Promise.<boolean>