Chok’s Typescript Utilities
    Preparing search index...

    Class KvStore

    You can create multiple KvStores if you want, but most likely you will only need to use the default kvStore instance.

    Implements

    Index

    Constructors

    • Parameters

      • dbName: string
      • Optionaloptions: {
            dbVersion?: number;
            defaultExpiryMs?: number | Duration;
            gcIntervalMs?: number | Duration;
            storeName?: string;
        }

      Returns KvStore

    Properties

    dbName: string
    dbVersion: number
    defaultExpiryMs: number
    gcIntervalMs: number
    gcMsStorageKey: string

    Local storage key name for the last GC completed timestamp.

    storeName: string

    Accessors

    • get lastGcMs(): number

      Returns the ms timestamp for the last GC (garbage collection).

      Returns number

    • set lastGcMs(ms: number): void

      Set the ms timestamp for the last GC (garbage collection).

      Parameters

      • ms: number

      Returns void

    Methods

    • Returns all items as map of key to value, mainly used for debugging dumps. The type T is applied to all values, even though they might not be of type T (in the case when you store different data types in the same store).

      Type Parameters

      • T

      Returns Promise<Map<string, StoredObject<T>>>

    • Delete one or multiple keys.

      Parameters

      • key: string | string[]

      Returns Promise<void>

    • Generic way to iterate through all entries.

      Type Parameters

      • T

      Parameters

      • callback: (
            key: string,
            value: T,
            expiryMs: number,
            storedMs: number,
        ) => void | Promise<void>

      Returns Promise<void>

    • Perform garbage collection immediately without checking whether we are due for the next GC or not.

      Returns Promise<void>

    • Get a value by key, or undefined if it does not exist.

      Type Parameters

      • T

      Parameters

      • key: string

      Returns Promise<T | undefined>

    • Returns the number of items in the store. Note that getting the size requires iterating through the entire store because the items could expire at any time, and hence the size is a dynamic number.

      Returns Promise<number>