Apps might need to share some data between runs. The SDK provides a State class that allows you to store, load, and do other operations with data.

State instance is inserted automatically to stream and scheduled apps' StatefulContext

⚠️⚠️⚠️ Task apps don't get a State inside StatelessContext as they aren't meant to store data between invokes.

ℹ️ State uses a dict-like database, so the data is stored as key:value pairs. ℹ️ key should be of string type, and value can have any of the following types: string, Buffer, number,any[].

The SDK by itself creates the unique key under the hood, so no need to add well id/company name/etc. to the keys.

Use case

A typical example of Cache usage is:

  1. Store some data during app invoke.
  2. Retrieve and use the data during app invoke.

ℹ️ To get the most out of Cache

  • Store as small amounts of data as possible.
  • Try to stay below 100kb.

Hierarchy

  • State

Methods

  • Clean value(s) from cache

    Example

    import { Corva } from '@corva/node-sdk';

    export const handler = new Corva().scheduled(async (event, { cache }) => {
    await cache.store({ key1: 'val1', key2: 'val2', key3: 'val3' });
    // cache: { key1: 'val1', key2: 'val2', key3: 'val3' }

    await cache.delete('key1');
    // cache: { key2: 'val2', key3: 'val3' }

    return { status: 'OK' };
    });

    Parameters

    • Rest ...keys: string[]

    Returns Promise<void>

  • Clean all values from cache

    Returns Promise<void>

  • Remove a few elements from cache

    Example

    import { Corva } from '@corva/node-sdk';

    export const handler = new Corva().scheduled(async (event, { cache }) => {
    await cache.store({ key1: 'value1', key2: 'value2', key3: 'value3' });
    // cache: { key1: 'value1', key2: 'value2', key3: 'value3' }

    await cache.deleteMany(['key1', 'key2']);
    // cache: { key3: 'value3' }

    return { status: 'OK' };
    });

    Parameters

    • keys: string[]

    Returns Promise<void>

  • Load cache value by provided key

    Example

    import { Corva } from '@corva/node-sdk';

    export const handler = new Corva().scheduled(async (event, { cache, logger }) => {
    await cache.store({ key: 'val' });

    const res = await cache.load('key');

    logger.debug(res); // val

    return { status: 'OK' };
    });

    Parameters

    • key: string

    Returns Promise<string>

  • Load all cache values

    Returns Promise<Record<string, string>>

  • Load multiple cache values by provided keys

    Example

    import { Corva } from '@corva/node-sdk';

    export const handler = new Corva().scheduled(async (event, { cache, logger }) => {
    await cache.store({ key1: 'val1', key2: 'val2', key3: 'val3' });

    const res = await cache.loadAll();

    logger.debug(res); // { key1: 'val1', key2: 'val2' }

    return { status: 'OK' };
    });

    Parameters

    • Rest ...keys: string[]

    Returns Promise<string[]>

  • Save multiple values

    ⚠️⚠️⚠️ Cache can store only string data. Cast your data to string before saving.

    ℹ️ By default, Cache sets an expiry to 60 days.

    Example

    import { Corva } from '@corva/node-sdk';

    export const handler = new Corva().scheduled(async (event, { cache, logger }) => {
    await cache.store({ key: 'val' });

    const res = await cache.load('key');

    logger.debug(res); // val

    return { status: 'OK' };
    });

    Parameters

    • key: string
    • value: ValueType
    • Optional expiry: number

    Returns Promise<void>

  • Parameters

    • input: Record<string, ValueType>
    • Optional expiry: number

    Returns Promise<void>