Skip to content

Function: initStorage()

ts
function initStorage<T>(
   ctx: Context, 
   name: string, 
   scope: "user" | "org" | "skill"): {
  name: string;
  path: string;
  get: <K>(key: K) => Promise<T[K]>;
  getAll: () => Promise<T>;
  set: <K>(key: K, val: T[K]) => Promise<void>;
  setAll: (val: T) => Promise<void>;
  update: <K>(key: K, callback: (data: T[K]) => Promise<T[K]>) => Promise<void>;
  updateAll: (callback: (data: T) => Promise<T>) => Promise<void>;
  remove: <K>(key: K) => Promise<void>;
  removeAll: () => Promise<void>;
};

Defined in: src/storage/index.ts:227

Initialize a storage instance for a given context, name, and scope.

Type Parameters

Type ParameterDescription
T extends Record<string, any>Shape of the data to store.

Parameters

ParameterTypeDescription
ctxContextContext for the storage instance.
namestringName for the storage file.
scope"user" | "org" | "skill"Storage scope: 'user', 'org', or 'skill'.

Returns

A storage object with get, set, update, and remove methods.

name

ts
name: string;

path

ts
path: string = storePath;

get()

ts
get: <K>(key: K) => Promise<T[K]>;

Get a value from storage.

Type Parameters

Type Parameter
K extends string

Parameters

ParameterTypeDescription
keyKKey to retrieve.

Returns

Promise<T[K]>

The stored value.

Example

ts
const roast = await store.get('roast');

getAll()

ts
getAll: () => Promise<T>;

Get all values from storage.

Returns

Promise<T>

All stored data.

Example

ts
const allPrefs = await store.getAll();

set()

ts
set: <K>(key: K, val: T[K]) => Promise<void>;

Set a value in storage.

Type Parameters

Type Parameter
K extends string

Parameters

ParameterTypeDescription
keyKKey to write.
valT[K]Value to store.

Returns

Promise<void>

Resolves when the write completes.

Example

ts
await store.set('roast', 'espresso');

setAll()

ts
setAll: (val: T) => Promise<void>;

Set all values in storage at once.

Parameters

ParameterTypeDescription
valTComplete data object to store.

Returns

Promise<void>

Resolves when the write completes.

Example

ts
await store.setAll({roast: 'dark', shots: 3});

update()

ts
update: <K>(key: K, callback: (data: T[K]) => Promise<T[K]>) => Promise<void>;

Update a value in storage using a callback.

Type Parameters

Type Parameter
K extends string

Parameters

ParameterTypeDescription
keyKKey to update.
callback(data: T[K]) => Promise<T[K]>Receive the current value, return the new value.

Returns

Promise<void>

Resolves when the update completes.

Example

ts
await store.update('shots', async (prev) => prev + 1);

updateAll()

ts
updateAll: (callback: (data: T) => Promise<T>) => Promise<void>;

Update all values in storage using a callback.

Parameters

ParameterTypeDescription
callback(data: T) => Promise<T>Receive all current data, return the new data.

Returns

Promise<void>

Resolves when the update completes.

Example

ts
await store.updateAll(async (data) => ({...data, shots: 4}));

remove()

ts
remove: <K>(key: K) => Promise<void>;

Remove a key and its value from storage.

Type Parameters

Type Parameter
K extends string

Parameters

ParameterTypeDescription
keyKKey to remove.

Returns

Promise<void>

Example

ts
await store.remove('roast');

removeAll()

ts
removeAll: () => Promise<void>;

Remove all data from storage.

Returns

Promise<void>

Example

ts
await store.removeAll();

Example

ts
interface PizzaPrefs { topping: string; crustSize: number }
const store = initStorage<PizzaPrefs>(ctx, 'pizzaPrefs', 'user');
await store.set('topping', 'pineapple');
const topping = await store.get('topping'); // 'pineapple'

Matterway Assistant SDK Documentation