UNPKG

pip-services3-data-node

Version:
190 lines (189 loc) 8.99 kB
/** @module persistence */ import { IReferenceable } from 'pip-services3-commons-node'; import { IReferences } from 'pip-services3-commons-node'; import { IOpenable } from 'pip-services3-commons-node'; import { ICleanable } from 'pip-services3-commons-node'; import { PagingParams } from 'pip-services3-commons-node'; import { DataPage } from 'pip-services3-commons-node'; import { ConfigParams } from 'pip-services3-commons-node'; import { IConfigurable } from 'pip-services3-commons-node'; import { CompositeLogger } from 'pip-services3-components-node'; import { ILoader } from '../ILoader'; import { ISaver } from '../ISaver'; /** * 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 <code>this._items</code> 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. * * ### Configuration parameters ### * * - options: * - max_page_size: Maximum number of items returned in a single page (default: 100) * * ### References ### * * - <code>\*:logger:\*:\*:1.0</code> (optional) [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/log.ilogger.html 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" } * }); * }); */ export declare class MemoryPersistence<T> implements IConfigurable, IReferenceable, IOpenable, ICleanable { protected _logger: CompositeLogger; protected _items: T[]; protected _loader: ILoader<T>; protected _saver: ISaver<T>; protected _opened: boolean; protected _maxPageSize: number; /** * Creates a new instance of the persistence. * * @param loader (optional) a loader to load items from external datasource. * @param saver (optional) a saver to save items to external datasource. */ constructor(loader?: ILoader<T>, saver?: ISaver<T>); /** * Configures component by passing configuration parameters. * * @param config configuration parameters to be set. */ configure(config: ConfigParams): void; /** * Sets references to dependent components. * * @param references references to locate the component dependencies. */ setReferences(references: IReferences): void; /** * Checks if the component is opened. * * @returns true if the component has been opened and false otherwise. */ isOpen(): boolean; /** * Opens the component. * * @param correlationId (optional) transaction id to trace execution through call chain. * @param callback callback function that receives error or null no errors occured. */ open(correlationId: string, callback?: (err: any) => void): void; private load; /** * Closes component and frees used resources. * * @param correlationId (optional) transaction id to trace execution through call chain. * @param callback callback function that receives error or null no errors occured. */ close(correlationId: string, callback?: (err: any) => void): void; /** * Saves items to external data source using configured saver component. * * @param correlationId (optional) transaction id to trace execution through call chain. * @param callback (optional) callback function that receives error or null for success. */ save(correlationId: string, callback?: (err: any) => void): void; /** * Clears component state. * * @param correlationId (optional) transaction id to trace execution through call chain. * @param callback callback function that receives error or null no errors occured. */ clear(correlationId: string, callback?: (err?: any) => void): 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. * * @param correlationId (optional) transaction id to trace execution through call chain. * @param filter (optional) a filter function to filter items * @param paging (optional) paging parameters * @param sort (optional) sorting parameters * @param select (optional) projection parameters (not used yet) * @param callback callback function that receives a data page or error. */ protected getPageByFilter(correlationId: string, filter: any, paging: PagingParams, sort: any, select: any, callback: (err: any, page: DataPage<T>) => void): 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. * * @param correlationId (optional) transaction id to trace execution through call chain. * @param filter (optional) a filter function to filter items * @param callback callback function that receives a data page or error. */ protected getCountByFilter(correlationId: string, filter: any, callback: (err: any, count: number) => void): 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. * * @param correlationId (optional) transaction id to trace execution through call chain. * @param filter (optional) a filter function to filter items * @param paging (optional) paging parameters * @param sort (optional) sorting parameters * @param select (optional) projection parameters (not used yet) * @param callback callback function that receives a data list or error. */ protected getListByFilter(correlationId: string, filter: any, sort: any, select: any, callback: (err: any, items: T[]) => void): 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. * * @param correlationId (optional) transaction id to trace execution through call chain. * @param filter (optional) a filter function to filter items. * @param callback callback function that receives a random item or error. */ protected getOneRandom(correlationId: string, filter: any, callback: (err: any, item: T) => void): void; /** * Creates a data item. * * @param correlation_id (optional) transaction id to trace execution through call chain. * @param item an item to be created. * @param callback (optional) callback function that receives created item or error. */ create(correlationId: string, item: T, callback?: (err: any, item: T) => void): 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. * * @param correlationId (optional) transaction id to trace execution through call chain. * @param filter (optional) a filter function to filter items. * @param callback (optional) callback function that receives error or null for success. */ protected deleteByFilter(correlationId: string, filter: any, callback?: (err: any) => void): void; }