State
You can store key-value state in Restate. Restate makes sure the state is consistent with the processing of the code execution.
This feature is only available for Virtual Objects and Workflows:
- For Virtual Objects, the state is isolated per Virtual Object and lives forever (across invocations for that object).
- For Workflows, you can think of it as if every workflow execution is a new object. So the state is isolated to a single workflow execution. The state can only be mutated by the
run
handler of the workflow. The other handlers can only read the state.
You can inspect and edit the K/V state stored in Restate via psql
and the CLI.
Have a look at the introspection docs for more information.
You can store any type of value that can be serialized as a Buffer with Buffer.from(JSON.stringify(yourObject))
and deserialized with JSON.parse(result.toString()) as T
.
Listing state keys
For a single Virtual Object, you can list all the state keys that have entries in the state store via:
const stateKeys = ctx.stateKeys();
Retrieving state
Use ctx.get
to retrieve the state for a key:
const myString = (await ctx.get<string>("my-string-key")) ?? "my-default";const myNumber = (await ctx.get<number>("my-number-key")) ?? 0;
The return value is null
if no value was stored.
Setting state
Use ctx.set
to set a new value for a key:
ctx.set("my-key", "my-new-value");
Clearing state
Use ctx.clear
to delete the value of a key:
ctx.clear("my-key");
Clearing all state
Delete all the state stored in Restate for a Virtual Object via:
ctx.clearAll();