Variable PersistBaseConst

PersistBase: new <EntityName extends string = string>(
    entityName: EntityName,
    baseDir?: string,
) => {
    _directory: string;
    _getFilePath(entityId: EntityId): string;
    "[asyncIterator]"(): AsyncIterableIterator<any>;
    "[BASE_WAIT_FOR_INIT_SYMBOL]": () => Promise<void> & functools_kit.ISingleshotClearable;
    baseDir: string;
    entityName: EntityName;
    filter<T extends IEntity = IEntity>(
        predicate: (value: T) => boolean,
    ): AsyncGenerator<T>;
    getCount(): Promise<number>;
    hasValue(entityId: EntityId): Promise<boolean>;
    keys(): AsyncGenerator<EntityId>;
    readValue<T extends IEntity = IEntity>(entityId: EntityId): Promise<T>;
    removeAll(): Promise<void>;
    removeValue(entityId: EntityId): Promise<void>;
    take<T extends IEntity = IEntity>(
        total: number,
        predicate?: (value: T) => boolean,
    ): AsyncGenerator<T>;
    values<T extends IEntity = IEntity>(): AsyncGenerator<T>;
    waitForInit(initial: boolean): Promise<void>;
    writeValue<T extends IEntity = IEntity>(
        entityId: EntityId,
        entity: T,
    ): Promise<void>;
}

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

Type declaration

    • new <EntityName extends string = string>(
          entityName: EntityName,
          baseDir?: string,
      ): {
          _directory: string;
          _getFilePath(entityId: EntityId): string;
          "[asyncIterator]"(): AsyncIterableIterator<any>;
          "[BASE_WAIT_FOR_INIT_SYMBOL]": () => Promise<void> & functools_kit.ISingleshotClearable;
          baseDir: string;
          entityName: EntityName;
          filter<T extends IEntity = IEntity>(
              predicate: (value: T) => boolean,
          ): AsyncGenerator<T>;
          getCount(): Promise<number>;
          hasValue(entityId: EntityId): Promise<boolean>;
          keys(): AsyncGenerator<EntityId>;
          readValue<T extends IEntity = IEntity>(entityId: EntityId): Promise<T>;
          removeAll(): Promise<void>;
          removeValue(entityId: EntityId): Promise<void>;
          take<T extends IEntity = IEntity>(
              total: number,
              predicate?: (value: T) => boolean,
          ): AsyncGenerator<T>;
          values<T extends IEntity = IEntity>(): AsyncGenerator<T>;
          waitForInit(initial: boolean): Promise<void>;
          writeValue<T extends IEntity = IEntity>(
              entityId: EntityId,
              entity: T,
          ): Promise<void>;
      }
    • Type Parameters

      • EntityName extends string = string

      Parameters

      Returns {
          _directory: string;
          _getFilePath(entityId: EntityId): string;
          "[asyncIterator]"(): AsyncIterableIterator<any>;
          "[BASE_WAIT_FOR_INIT_SYMBOL]": () => Promise<void> & functools_kit.ISingleshotClearable;
          baseDir: string;
          entityName: EntityName;
          filter<T extends IEntity = IEntity>(
              predicate: (value: T) => boolean,
          ): AsyncGenerator<T>;
          getCount(): Promise<number>;
          hasValue(entityId: EntityId): Promise<boolean>;
          keys(): AsyncGenerator<EntityId>;
          readValue<T extends IEntity = IEntity>(entityId: EntityId): Promise<T>;
          removeAll(): Promise<void>;
          removeValue(entityId: EntityId): Promise<void>;
          take<T extends IEntity = IEntity>(
              total: number,
              predicate?: (value: T) => boolean,
          ): AsyncGenerator<T>;
          values<T extends IEntity = IEntity>(): AsyncGenerator<T>;
          waitForInit(initial: boolean): Promise<void>;
          writeValue<T extends IEntity = IEntity>(
              entityId: EntityId,
              entity: T,
          ): Promise<void>;
      }

      • _directory: string

        Computed directory path for entity storage

      • _getFilePath:function
        • Computes file path for entity ID.

          Parameters

          Returns string

          Full file path to entity JSON file

      • [asyncIterator]:function
        • Async iterator implementation. Delegates to values() generator.

          Returns AsyncIterableIterator<any>

          AsyncIterableIterator yielding entities

      • [BASE_WAIT_FOR_INIT_SYMBOL]: () => Promise<void> & functools_kit.ISingleshotClearable
      • ReadonlybaseDir: string
      • ReadonlyentityName: EntityName
      • filter:function
        • Filters entities by predicate function.

          Type Parameters

          • T extends IEntity = IEntity

          Parameters

          • predicate: (value: T) => boolean

            Filter function

          Returns AsyncGenerator<T>

          AsyncGenerator yielding filtered entities

      • getCount:function
        • Returns count of persisted entities.

          Returns Promise<number>

          Promise resolving to number of .json files in directory

      • hasValue:function
      • keys:function
        • Async generator yielding all entity IDs. Sorted alphanumerically.

          Returns AsyncGenerator<EntityId>

          AsyncGenerator yielding entity IDs

          Error if reading fails

      • readValue:function
        • Type Parameters

          • T extends IEntity = IEntity

          Parameters

          Returns Promise<T>

      • removeAll:function
        • Removes all entities from storage.

          Returns Promise<void>

          Promise that resolves when all entities are deleted

          Error if deletion fails

      • removeValue:function
        • Removes entity from storage.

          Parameters

          • entityId: EntityId

            Entity identifier to remove

          Returns Promise<void>

          Promise that resolves when entity is deleted

          Error if entity not found or deletion fails

      • take:function
        • Takes first N entities, optionally filtered.

          Type Parameters

          • T extends IEntity = IEntity

          Parameters

          • total: number

            Maximum number of entities to yield

          • Optionalpredicate: (value: T) => boolean

            Optional filter function

          Returns AsyncGenerator<T>

          AsyncGenerator yielding up to total entities

      • values:function
        • Async generator yielding all entity values. Sorted alphanumerically by entity ID.

          Type Parameters

          • T extends IEntity = IEntity

          Returns AsyncGenerator<T>

          AsyncGenerator yielding entities

          Error if reading fails

      • waitForInit:function
        • Parameters

          • initial: boolean

          Returns Promise<void>

      • writeValue:function
        • Type Parameters

          • T extends IEntity = IEntity

          Parameters

          Returns Promise<void>

const persist = new PersistBase("my-entity", "./data");
await persist.waitForInit(true);
await persist.writeValue("key1", { data: "value" });
const value = await persist.readValue("key1");