Options
All
  • Public
  • Public/Protected
  • All
Menu

Class FilePersistence<T>

Abstract persistence component that stores data in flat files and caches them 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.

see

MemoryPersistence

see

JsonFilePersister

Configuration parameters

  • path: path to the file where data is stored

References

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

Example

class MyJsonFilePersistence extends FilePersistence<MyData> {
    public constructor(path?: string) {
        super(new JsonPersister(path));
    }

    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);
    }

}

Type parameters

  • T

Hierarchy

Implements

  • any
  • any
  • any
  • any
  • any

Index

Constructors

constructor

Properties

Protected _items

_items: T[] = []

Protected _loader

_loader: ILoader<T>

Protected _logger

_logger: CompositeLogger = new CompositeLogger()

Protected _maxPageSize

_maxPageSize: number = 100

Protected _opened

_opened: boolean = false

Protected _persister

_persister: JsonFilePersister<T>

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

configure

  • configure(config: ConfigParams): void
  • Configures component by passing configuration parameters.

    Parameters

    • config: ConfigParams

      configuration parameters to be set.

    Returns void

create

  • create(correlationId: string, item: T, callback?: function): void
  • Creates a data item.

    Parameters

    • correlationId: string
    • item: T

      an item to be created.

    • Optional callback: function

      (optional) callback function that receives created item or error.

        • (err: any, item: T): void
        • Parameters

          • err: any
          • item: T

          Returns void

    Returns void

Protected deleteByFilter

  • deleteByFilter(correlationId: string, filter: any, callback?: function): void
  • Deletes data items that match to a given filter.

    This method shall be called by a public deleteByFilter method from child class that receives FilterParams and converts them into a filter function.

    Parameters

    • correlationId: string

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

    • filter: any

      (optional) a filter function to filter items.

    • Optional callback: function

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

        • (err: any): void
        • Parameters

          • err: any

          Returns void

    Returns void

Protected getCountByFilter

  • getCountByFilter(correlationId: string, filter: any, callback: function): void
  • Gets a number of items retrieved by a given filter.

    This method shall be called by a public getCountByFilter method from child class that receives FilterParams and converts them into a filter function.

    Parameters

    • correlationId: string

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

    • filter: any

      (optional) a filter function to filter items

    • callback: function

      callback function that receives a data page or error.

        • (err: any, count: number): void
        • Parameters

          • err: any
          • count: number

          Returns void

    Returns void

Protected getListByFilter

  • getListByFilter(correlationId: string, filter: any, sort: any, select: any, callback: function): void
  • Gets a list of data items retrieved by a given filter and sorted according to sort parameters.

    This method shall be called by a public getListByFilter method from child class that receives FilterParams and converts them into a filter function.

    Parameters

    • correlationId: string

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

    • filter: any

      (optional) a filter function to filter items

    • sort: any

      (optional) sorting parameters

    • select: any

      (optional) projection parameters (not used yet)

    • callback: function

      callback function that receives a data list or error.

        • (err: any, items: T[]): void
        • Parameters

          • err: any
          • items: T[]

          Returns void

    Returns void

Protected getOneRandom

  • getOneRandom(correlationId: string, filter: any, callback: function): void
  • Gets a random item from items that match to a given filter.

    This method shall be called by a public getOneRandom method from child class that receives FilterParams and converts them into a filter function.

    Parameters

    • correlationId: string

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

    • filter: any

      (optional) a filter function to filter items.

    • callback: function

      callback function that receives a random item or error.

        • (err: any, item: T): void
        • Parameters

          • err: any
          • item: T

          Returns void

    Returns void

Protected getPageByFilter

  • getPageByFilter(correlationId: string, filter: any, paging: PagingParams, sort: any, select: any, callback: function): void
  • Gets a page of data items retrieved by a given filter and sorted according to sort parameters.

    This method shall be called by a public getPageByFilter method from child class that receives FilterParams and converts them into a filter function.

    Parameters

    • correlationId: string

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

    • filter: any

      (optional) a filter function to filter items

    • paging: PagingParams

      (optional) paging parameters

    • sort: any

      (optional) sorting parameters

    • select: any

      (optional) projection parameters (not used yet)

    • callback: function

      callback function that receives a data page or error.

        • (err: any, page: DataPage<T>): void
        • Parameters

          • err: any
          • page: DataPage<T>

          Returns void

    Returns void

isOpen

  • isOpen(): boolean
  • Checks if the component is opened.

    Returns boolean

    true if the component has been opened and false otherwise.

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