Sana Assistant (online)
Table of Contents

Extension.Api.KeyValueStorage reference

Extension in Sana has access to additional key-value persistent data storage functionality via Api.KeyValueStorage property. This property holds an instance of KeyValueDataStorage type which serves as an entry point to save or retrieve data that the extension needs to store in Sana by key.

Class diagram

Methods

DeleteAllAsync

Deletes all data entities by entity type from persistent storage.

Api.KeyValueStorage.DeleteAllAsync("Entity1");

DeleteAsync

Deletes the data entity by key from persistent storage.

Api.KeyValueStorage.DeleteAsync("Entity1", "key1");

GetByKeyListAsync

Gets the data entities by keys from persistent storage.

var keys = new List<string> { "key1", "key2", "key3" };
var items = await Api.KeyValueStorage.GetByKeyListAsync("Entity1", keys);

GetCountAsync

Gets the total count of data entities by entity type from persistent storage.

var entitiesAmount = await Api.KeyValueStorage.GetCountAsync("Entity1");

GetListAsync

Gets the data entities by entity type and options from persistent storage.

var options = new KeyValueLoadOptions
{
    PageIndex = 0,
    PageSize = 10,
};
var items = await Api.KeyValueStorage.GetListAsync("Entity1", options);

SaveAsync

Saves the data items by entity type from persistent storage.

var items = new List<KeyValueData>
{
    new KeyValueData("key1", "value1"),
    new KeyValueData("key2", "value2"),
    new KeyValueData("key3", "value3"),
};
await Api.KeyValueStorage.SaveAsync("Entity1", items);

KeyValueData

Represents a key-value data entity that can be saved and retrieved from persistent data storage.

Class diagram

The KeyValueData class constructor initializes a new instance by accepting two parameters: a key and a value. The key parameter specifies the identifier for the data entry, while the value parameter represents the associated data.

var item = new KeyValueData("key1", "value1");

Parameters

Key

Specifies the identifier for the data entry that can be used to get data from persistent data storage.

var keys = new List<string> { "key1", "key2", "key3" };
var items = await Api.KeyValueStorage.GetByKeyListAsync("Entity1", keys);
var secondItem = items.SingleOrDefault(i => i.Key == "key2");

Value

Represents the data associated with a Key that can be retrieved from persistent data storage by key.

var keys = new List<string> { "key1", "key2", "key3" };
var items = await Api.KeyValueStorage.GetByKeyListAsync("Entity1", keys);
var secondItem = items.SingleOrDefault(i => i.Key == "key2");
var secondItemValue = secondItem.Value;

KeyValueLoadOptions

Represents the parameters used for loading data entities in a paginated format. This class specifies pagination options when retrieving data, allowing for efficient data loading and navigation through large datasets. This class is particularly used in the GetListAsync method to control the page index and the number of items returned per page.

Class diagram

Example:

var options = new KeyValueLoadOptions
{
    PageIndex = 0,
    PageSize = 10,
};

Parameters

PageIndex

Represents the index of the page to be retrieved. The page index is zero-based, meaning that the first page is indexed as 0, the second page as 1, and so on. The default value is 0.

var options = new KeyValueLoadOptions();
options.PageIndex = 2;

PageSize

The number of items to be included on each page. This defines the size of the page or the number of data entities returned per page. The default value is 100.

var options = new KeyValueLoadOptions();
options.PageSize = 15;

See also