Skip to main content

Local Storage Agent - JS/TS

Read and write value in local storage.

Logic TypeAvailable
Generic logic
Aggregator logic

Local Storage

Unlike a task's session storage, the local storage is implemented in Kubernetes' etcd database and can be accessed by different tasks. Data can stay persistent after executions for a limit amount of time.

The local storage is not meant as the primary way to exchange data across tasks, more like a debugging or testing tool.

Import and Usage

import {
LocalStorageAgent,
} from "@fstnetwork/loc-logic-sdk";

export async function run(ctx) {
const data = await LocalStorageAgent.get("data");
}

export async function handleError(ctx, error) {
// ... same
}

Class Reference

Type

  • LocalStorageAgent

Method: Get Local Value

async get(key: string): Promise<string | number | object | Uint8Array>
ParameterDescription
keyKey of local data

Returns a value from local storage (null if not exist).

The return type depends how it was written into the local storage. If the value is a single number (which is valid JSON) and stored using putJson, it would be returned as a number.

Methods: Write Local Value

async putJson(key: string, value: any, timeout?: number): Promise<boolean>
ParameterDescription
keyKey of local data
valueJavaScript JSON object
timeout?Data persistent limit (seconds; default 300, maximum 86400 = 1 day)

Write a JSON value into local storage. Overwrite if already exists.

warning

Runtime will throw a JSON parsing error if the object cannot be serialised properly to JSON.

You can try using JSON.parse(JSON.stringify(object) to transform an object with methods into a proper JSON object, although some fields may be different or discarded.

Method: Delete Session Value

async delete(key: string): Promise<void>
async remove(key: string): Promise<void>
ParameterDescription
keyKey of session data

Delete a value in local storage. Do nothing if not exist.

Examples

Write String Data Into Local Storage

await LocalStorageAgent.putString(
"data",
"so long, and thanks for all the fish",
86400, // persistent for 1 day
);

Write JSON Data Into Local Storage

await LocalStorageAgent.putJson(
"data",
{
// ...
},
86400, // persistent for 1 day
);

See: Write JSON Data Into Session Storage

Read String Data from Local Storage

const data = await LocalStorageAgent.get("data");

Set a default value if the local data does not exist (returns null):

const data = await LocalStorageAgent.get("data") || "default value";

Read Json Data from Local Storage

const data = await LocalStorageAgent.get("data");

// get values (undefined if not found)
const name = data?.name;
const age = data?.age;

See: Read Json Data from Session Storage