Skip to main content
Version: Next (2.x)

createContextStore()

createContextStore<Result, Context>(key, options?): readonly [(ctx) => Promise<Result>, (ctx, loader) => void]

Creates a context store for managing asynchronous values within the context object. See https://baeta.io/docs/guides/context-store

Type Parameters

Type ParameterDefault type

Result

Context

unknown

Parameters

ParameterTypeDescription

key

symbol

A unique symbol to identify the stored value in the context

options?

ContextStoreOptions

Configuration options for the store

Returns

readonly [(ctx) => Promise<Result>, (ctx, loader) => void]

A tuple containing get and load functions for managing the stored value

Example

// Create a store for user data
const userStoreKey = Symbol("userStore");
const [getUser, setUserLoader] = createContextStore<User>(userStoreKey, {
lazy: true,
});

// Set the loader function after creating the context object
setUserLoader(ctx, async () => {
return fetchUser(userId);
});

// Later, retrieve the user in a resolver
const user = await getUser(ctx);