Options
All
  • Public
  • Public/Protected
  • All
Menu

Class IdentifiableFilePersistence<T, K>

Abstract persistence component that stores data in flat files and implements a number of CRUD operations over data items with unique ids. The data items must implement IIdentifiable interface.

In basic scenarios child classes shall only override getPageByFilter, getListByFilter or deleteByFilter operations with specific filter function. All other operations can be used out of the box.

In complex scenarios child classes can implement additional operations by accessing cached items via this._items property and calling save method on updates.

see

JsonFilePersister

see

MemoryPersistence

Configuration parameters

  • path: path to the file where data is stored
  • options:
    • max_page_size: Maximum number of items returned in a single page (default: 100)

References

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

Examples

class MyFilePersistence extends IdentifiableFilePersistence<MyData, string> {
    public constructor(path?: string) {
        super(new JsonPersister(path));
    }

    private composeFilter(filter: FilterParams): any {
        filter = filter || new FilterParams();
        let name = filter.getAsNullableString("name");
        return (item) => {
            if (name != null && item.name != name)
                return false;
            return true;
        };
    }

    public getPageByFilter(correlationId: string, filter: FilterParams, paging: PagingParams,
            callback: (err: any, page: DataPage<MyData>) => void): void {
        super.getPageByFilter(correlationId, this.composeFilter(filter), paging, null, null, callback);
    }

}

let persistence = new MyFilePersistence("./data/data.json");

persistence.create("123", { id: "1", name: "ABC" }, (err, item) => {
    persistence.getPageByFilter(
        "123",
        FilterParams.fromTuples("name", "ABC"),
        null,
        (err, page) => {
            console.log(page.data);          // Result: { id: "1", name: "ABC" }

            persistence.deleteById("123", "1", (err, item) => {
                ...
            });
        }
    )
});

Type parameters

  • T: IIdentifiable<K>

  • K

Hierarchy

Implements

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

create

  • create(correlationId: string, item: T, callback?: function): 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

deleteById

  • deleteById(correlationId: string, id: K, callback?: function): void

deleteByIds

  • deleteByIds(correlationId: string, ids: K[], callback?: function): void
  • Deletes multiple data items by their unique ids.

    Parameters

    • correlationId: string

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

    • ids: K[]

      ids of data items to be deleted.

    • Optional callback: function

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

        • (err: any): void
        • Parameters

          • err: any

          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

getListByIds

  • getListByIds(correlationId: string, ids: K[], callback: function): void
  • Gets a list of data items retrieved by given unique ids.

    Parameters

    • correlationId: string

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

    • ids: K[]

      ids of data items to be retrieved

    • 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

getOneById

  • getOneById(correlationId: string, id: K, callback: function): void
  • Gets a data item by its unique id.

    Parameters

    • correlationId: string

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

    • id: K

      an id of data item to be retrieved.

    • callback: function

      callback function that receives data item or error.

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

          • err: any
          • item: 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

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

set

  • set(correlationId: string, item: T, callback?: function): void
  • Sets a data item. If the data item exists it updates it, otherwise it create a new data item.

    Parameters

    • correlationId: string
    • item: T

      a item to be set.

    • Optional callback: function

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

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

          • err: any
          • item: T

          Returns void

    Returns void

setReferences

  • setReferences(references: IReferences): void

update

  • update(correlationId: string, item: T, callback?: function): void

updatePartially

  • updatePartially(correlationId: string, id: K, data: AnyValueMap, callback?: function): void

Generated using TypeDoc