Class PersistBase<EntityName>

Base class for file-based persistence with atomic writes.

Features:

  • Atomic file writes using writeFileAtomic
  • Auto-validation and cleanup of corrupted files
  • Async generator support for iteration
  • Retry logic for file deletion
const persist = new PersistBase("my-entity", "./data");
await persist.waitForInit(true);
await persist.writeValue("key1", { data: "value" });
const value = await persist.readValue("key1");

Type Parameters

  • EntityName extends string = string

Implements

Constructors

Properties

_directory: string

Computed directory path for entity storage

"[BASE_WAIT_FOR_INIT_SYMBOL]": () => Promise<void> & ISingleshotClearable
baseDir: string
entityName: EntityName

Methods

  • Computes file path for entity ID.

    Parameters

    Returns string

    Full file path to entity JSON file

  • Check if entity exists in storage.

    Parameters

    • entityId: EntityId

      Unique entity identifier

    Returns Promise<boolean>

    Promise resolving to true if exists, false otherwise

  • Async generator yielding all entity IDs. Sorted alphanumerically. Used internally by waitForInit for validation.

    Returns AsyncGenerator<EntityId>

    AsyncGenerator yielding entity IDs

    Error if reading fails

  • Read entity from persistence storage.

    Type Parameters

    • T extends IEntity = IEntity

    Parameters

    • entityId: EntityId

      Unique entity identifier

    Returns Promise<T>

    Promise resolving to entity data

    Error if entity not found or read fails

  • Initialize persistence directory and validate existing files. Uses singleshot to ensure one-time execution.

    Parameters

    • initial: boolean

      Whether this is the first initialization

    Returns Promise<void>

    Promise that resolves when initialization is complete

  • Write entity to storage with atomic file writes.

    Type Parameters

    • T extends IEntity = IEntity

    Parameters

    • entityId: EntityId

      Unique entity identifier

    • entity: T

      Entity data to persist

    Returns Promise<void>

    Promise that resolves when write is complete

    Error if write fails