Interface IPersistBase<Entity>

Persistence interface for custom adapters. Defines only the essential CRUD operations required for persistence. Custom adapters should implement this interface.

Architecture:

  • IPersistBase: Public API for custom adapters (5 methods: waitForInit, readValue, hasValue, writeValue, keys)
  • PersistBase: Default implementation with keys() method for validation and iteration
  • TPersistBaseCtor: Constructor type requiring IPersistBase
interface IPersistBase<Entity extends IEntity | null = IEntity> {
    hasValue(entityId: EntityId): Promise<boolean>;
    keys(): AsyncGenerator<EntityId>;
    readValue(entityId: EntityId): Promise<Entity>;
    waitForInit(initial: boolean): Promise<void>;
    writeValue(entityId: EntityId, entity: Entity): Promise<void>;
}

Type Parameters

  • Entity extends IEntity | null = IEntity

Implemented by

Methods

  • 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 for iteration and validation.

    Returns AsyncGenerator<EntityId>

    AsyncGenerator yielding entity IDs

    Error if reading fails

  • Read entity from persistence storage.

    Parameters

    • entityId: EntityId

      Unique entity identifier

    Returns Promise<Entity>

    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.

    Parameters

    • entityId: EntityId

      Unique entity identifier

    • entity: Entity

      Entity data to persist

    Returns Promise<void>

    Promise that resolves when write is complete

    Error if write fails