Options
All
  • Public
  • Public/Protected
  • All
Menu

Class MemoryPersistence<T>

Abstract persistence component that stores data in memory.

This is the most basic persistence component that is only able to store data items of any type. Specific CRUD operations over the data items must be implemented in child classes by accessing this._items property and calling save method.

The component supports loading and saving items from another data source. That allows to use it as a base class for file and other types of persistence components that cache all data in memory.

References

  • *:logger:*:*:1.0 (optional) ILogger components to pass log messages

Example

class MyMemoryPersistence extends MemoryPersistence<MyData> {

    public getByName(correlationId: string, name: string, callback: (err, item) => void): void {
        let item = _.find(this._items, (d) => d.name == name);
        callback(null, item);
    });

    public set(correlatonId: string, item: MyData, callback: (err) => void): void {
        this._items = _.filter(this._items, (d) => d.name != name);
        this._items.push(item);
        this.save(correlationId, callback);
    }

}

let persistence = new MyMemoryPersistence();

persistence.set("123", { name: "ABC" }, (err) => {
    persistence.getByName("123", "ABC", (err, item) => {
        console.log(item);                   // Result: { name: "ABC" }
    });
});

Type parameters

  • T

Hierarchy

Implements

  • IReferenceable
  • IOpenable
  • ICleanable

Index

Constructors

constructor

Properties

Protected _items

_items: T[] = []

Protected _loader

_loader: ILoader<T>

Protected _logger

_logger: CompositeLogger = new CompositeLogger()

Protected _opened

_opened: boolean = false

Protected _saver

_saver: ISaver<T>

Methods

clear

  • clear(correlationId: string, callback?: function): void
  • Clears component state.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • Optional callback: function

      callback function that receives error or null no errors occured.

        • (err?: any): void
        • Parameters

          • Optional err: any

          Returns void

    Returns void

close

  • close(correlationId: string, callback?: function): void
  • Closes component and frees used resources.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • Optional callback: function

      callback function that receives error or null no errors occured.

        • (err: any): void
        • Parameters

          • err: any

          Returns void

    Returns void

isOpen

  • isOpen(): boolean

open

  • open(correlationId: string, callback?: function): void
  • Opens the component.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • Optional callback: function

      callback function that receives error or null no errors occured.

        • (err: any): void
        • Parameters

          • err: any

          Returns void

    Returns void

save

  • save(correlationId: string, callback?: function): void
  • Saves items to external data source using configured saver component.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • Optional callback: function

      (optional) callback function that receives error or null for success.

        • (err: any): void
        • Parameters

          • err: any

          Returns void

    Returns void

setReferences

  • setReferences(references: IReferences): void
  • Sets references to dependent components.

    Parameters

    • references: IReferences

      references to locate the component dependencies.

    Returns void

Generated using TypeDoc