UNPKG

ts-db-helper

Version:
163 lines (162 loc) 5.82 kB
import { IDbLogger } from '../interfaces/db-logger'; import { DbHelperModuleConfig } from '../db-helper-module-config'; import { DbQuery } from '../models/db-query.model'; import { Observable } from 'rxjs/Observable'; import { QueryResult } from '../interfaces/query-result.interface'; import 'rxjs/add/operator/share'; /** * @private * @class QueryManager * * @description * This class is a singleton manging query * This manager has not to be exposed, it is used to handle queries * with the connector. It stack it during connector is not ready and * release it when connector ca query * * @author Olivier Margarit * @since 0.1 */ export declare class QueryManager { /** * @static * @private * @property {QueryManager} instance private reference to the model manager instance */ private static instance; private loggerValue; /** * @private * @property {Array<PendingDbQuery>} pendingDbQueries stack of queries to fire when the * connector is ready */ private pendingDbQueries; /** * @private * @property {Array<PendingDbQuery>} pendingBatchDbQueries stack of queries to fire in a single transaction */ private pendingBatchDbQueries; /** * @private * @property {boolean} isReady flag that should pas to true only when * query can be sent */ private isReady; /** * @private * @property {boolean} isInitializationFailed flag to reject all queries * in case of connector activation failure */ private isInitializationFailed; /** * @private * @property {QueryConnector | undefined} queryConnector query connector that handle queries * to real db */ private queryConnector; /** * @private * @property {ModelMigration | undefined} modelMigration that handle migration on version change */ private modelMigration; /** * @private * @property {any} batchLock batch locker that can fire transaction. This locker prevent batch to collide. * @since 0.2 */ private batchLock; /** * @private * @property {Subject<any> | undefined} batchSubject an Observable where multiple batch transaction owner an subscribe * @since 0.2 */ private batchSubject; private supportRowidValue; readonly logger: IDbLogger; readonly supportRowid: boolean; /** * @static * @public * @method init is a part of the private API, config is submitted to * pass connector, model migration and other config. see {@link NgDbHelperModuleConfig} * for more informations * * @param {NgDbHelperModuleConfig} config the module configuration with connector instance and model migration * * @return {QueryManager} the initialized instance */ static init(config: DbHelperModuleConfig): QueryManager; /** * @static * @public * @method getInstance to get the unique QueryManager instance * * @return {QueryManager} the instance */ static getInstance(): QueryManager; /** * @private * @constructor private constructor to preserve from other instance creation */ private constructor(); /** * @private * @method onQueryConnectorReady is called when query connector is ready to * manage model migration then dequeuing each pending queries */ private onQueryConnectorReady(); /** * @private * @method onInitializationFailure is called on initilization failure to cancel * all started queries and nexts * * @param {Error} err the error return by the initialization failure */ private onInitializationFailure(err); /** * @private * @method dequeuePendingRequest is called to dequeue pending request */ private dequeuePendingRequest(); /** * @public * @method startBatch start transaction for multiple queries * @param {any} locker the locker that can fire the transaction if none is set */ startBatch(locker: any): void; /** * @public * @method query private API to start queries, it check if queries can be executed * If initialization failed, error is sent back. If connector is not ready, query will * be stack until connector ready signal * * @param {DbQuery} dbQuery query information with params * * @return {Observable<QueryResult<any>>} in case of success, {@link QueryResult<any>} is passed * in case of failure, {@link QueryError} is passed */ query(dbQuery: DbQuery): Observable<QueryResult<any>>; /** * @public * @method queryBatch should only be called from {@link QueryBatch} function. * * @param locker the locker passed to startBatch that allow queries releasing * * @return {Observable<QueryResult<any>>} in case of success, {@link QueryResult<any>} is passed * in case of failure, {@link QueryError} is passed * * @since 0.2 */ execBatch(locker: any): Observable<QueryResult<any>>; /** * @private * @method executeQuery should be called only when connector is ready * * @param {DbQuery} dbQuery query information with params * @param {Observer} observer observer to manage query callback * * @return {Observable<QueryResult<any>>} in case of success, {@link QueryResult<any>} is passed * in case of failure, {@link QueryError} is passed */ private executeQuery(dbQuery, observer); }