well advanced
This commit is contained in:
		
							parent
							
								
									f3c9dd0cbb
								
							
						
					
					
						commit
						bf08782a62
					
				@ -1,6 +1,6 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
  "name": "@poweredsoft/data",
 | 
					  "name": "@poweredsoft/data",
 | 
				
			||||||
  "version": "0.0.14",
 | 
					  "version": "0.0.18",
 | 
				
			||||||
  "peerDependencies": {
 | 
					  "peerDependencies": {
 | 
				
			||||||
    "rxjs": "^6.5.3"
 | 
					    "rxjs": "^6.5.3"
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
@ -1,22 +1,61 @@
 | 
				
			|||||||
import { Observable, of } from 'rxjs';
 | 
					import { Observable, of, Observer, BehaviorSubject } from 'rxjs';
 | 
				
			||||||
import { IDataSource } from './IDataSource';
 | 
					import { IDataSource } from './IDataSource';
 | 
				
			||||||
import { IQueryExecutionResult, IQueryExecutionGroupResult, IFilter, ISort, IAggregate, IGroup, IQueryCriteria } from './models';
 | 
					import { IQueryExecutionResult, IQueryExecutionGroupResult, IFilter, ISort, IAggregate, IGroup, IQueryCriteria } from './models';
 | 
				
			||||||
import { IDataSourceOptions } from './IDataSourceOptions';
 | 
					import { finalize } from 'rxjs/operators';
 | 
				
			||||||
import { IResolveCommandModelEvent } from './IResolveCommandModelEvent';
 | 
					import { IDataSourceOptions, IResolveCommandModelEvent } from '../public-api';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class DataSource<TModel> implements IDataSource<TModel> 
 | 
					export class DataSource<TModel> implements IDataSource<TModel> 
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					   
 | 
				
			||||||
    data: IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel> = null;
 | 
					    data: IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel> = null;
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    protected _page: number = 0;
 | 
					    protected _dataSubject: BehaviorSubject<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>> = new BehaviorSubject(null);
 | 
				
			||||||
    protected _pageSize: number = 0;
 | 
					    protected _loadingSubject: BehaviorSubject<boolean> = new BehaviorSubject(false);
 | 
				
			||||||
    protected _filters: IFilter[] = [];
 | 
					
 | 
				
			||||||
    protected _sorts: ISort[] = [];
 | 
					    protected _data$: Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>;
 | 
				
			||||||
    protected _aggregates: IAggregate[] = [];
 | 
					    protected _loading$: Observable<boolean>;
 | 
				
			||||||
    protected _groups: IGroup[] = [];
 | 
					
 | 
				
			||||||
 | 
					    // TODO: Validation error subject
 | 
				
			||||||
 | 
					    // TODO: Message error subject
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    protected _criteria: IQueryCriteria = {
 | 
				
			||||||
 | 
					        page: null,
 | 
				
			||||||
 | 
					        pageSize: null,
 | 
				
			||||||
 | 
					        filters: [],
 | 
				
			||||||
 | 
					        aggregates: [],
 | 
				
			||||||
 | 
					        groups: [],
 | 
				
			||||||
 | 
					        sorts: []
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get data$() {
 | 
				
			||||||
 | 
					        return this._data$;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get loading$() {
 | 
				
			||||||
 | 
					        return this._loading$;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    constructor(public options: IDataSourceOptions<TModel>) {
 | 
					    constructor(public options: IDataSourceOptions<TModel>) {
 | 
				
			||||||
 | 
					        this._initCriteria();
 | 
				
			||||||
 | 
					        this._initSubjectObservables();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    protected _initCriteria() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!this.options.defaultCriteria) 
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const copy: IQueryCriteria = JSON.parse(JSON.stringify(this.options.defaultCriteria));
 | 
				
			||||||
 | 
					        this._criteria.page = copy.page || this._criteria.page;
 | 
				
			||||||
 | 
					        this._criteria.pageSize = copy.pageSize || this._criteria.pageSize;
 | 
				
			||||||
 | 
					        this._criteria.filters = copy.filters || this._criteria.filters;
 | 
				
			||||||
 | 
					        this._criteria.groups = copy.groups || this._criteria.groups;
 | 
				
			||||||
 | 
					        this._criteria.aggregates = copy.aggregates || this._criteria.aggregates;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    protected _initSubjectObservables() {
 | 
				
			||||||
 | 
					        this._loading$ = this._loadingSubject.asObservable();
 | 
				
			||||||
 | 
					        this._data$ = this._dataSubject.asObservable();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    resolveCommandModelByName<T extends any>(event: IResolveCommandModelEvent<TModel>) : Observable<T> {
 | 
					    resolveCommandModelByName<T extends any>(event: IResolveCommandModelEvent<TModel>) : Observable<T> {
 | 
				
			||||||
@ -40,71 +79,84 @@ export class DataSource<TModel> implements IDataSource<TModel>
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    query<TQuery extends IQueryCriteria>(query: TQuery) : Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>> {
 | 
					    query<TQuery extends IQueryCriteria>(query: TQuery) : Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>> {
 | 
				
			||||||
        return this.options.transport.query.adapter.handle(query);
 | 
					        return Observable.create((o: Observer<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>) => {
 | 
				
			||||||
    }
 | 
					            this._loadingSubject.next(true);
 | 
				
			||||||
 | 
					            this.options.transport.query.adapter.handle(query)
 | 
				
			||||||
    public refresh() {
 | 
					                .pipe(
 | 
				
			||||||
        return this.query({
 | 
					                    finalize(() => {
 | 
				
			||||||
            sorts: this._sorts,
 | 
					                        o.complete();
 | 
				
			||||||
            filters: this._filters,
 | 
					                        this._loadingSubject.next(false);
 | 
				
			||||||
            groups: this._groups,
 | 
					                    })
 | 
				
			||||||
            aggregates: this._aggregates,
 | 
					                )
 | 
				
			||||||
            pageSize: this._pageSize,
 | 
					                .subscribe(
 | 
				
			||||||
            page: this._page
 | 
					                    result => {
 | 
				
			||||||
 | 
					                        this.data = result;
 | 
				
			||||||
 | 
					                        this._dataSubject.next(this.data);
 | 
				
			||||||
 | 
					                        o.next(result)
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
 | 
					                    err => o.error(err)
 | 
				
			||||||
 | 
					                );
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    refresh() {
 | 
				
			||||||
 | 
					        return this.query(this._criteria).subscribe(
 | 
				
			||||||
 | 
					            res => {},
 | 
				
			||||||
 | 
					            err => {}  
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    get sorts() {
 | 
					    get sorts() {
 | 
				
			||||||
        return this._sorts;
 | 
					        return this._criteria.sorts;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    set sorts(value: ISort[]) {
 | 
					    set sorts(value: ISort[]) {
 | 
				
			||||||
        this._sorts = value;
 | 
					        this._criteria.sorts = value;
 | 
				
			||||||
        this.refresh();
 | 
					        this.refresh();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    get filters() {
 | 
					    get filters() {
 | 
				
			||||||
        return this._filters;
 | 
					        return this._criteria.filters;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    set filters(value: IFilter[]) {
 | 
					    set filters(value: IFilter[]) {
 | 
				
			||||||
        this._filters = value;
 | 
					        this._criteria.filters = value;
 | 
				
			||||||
        this.refresh();
 | 
					        this.refresh();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    get groups() {
 | 
					    get groups() {
 | 
				
			||||||
        return this._groups;
 | 
					        return this._criteria.groups;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    set groups(value: IGroup[]) {
 | 
					    set groups(value: IGroup[]) {
 | 
				
			||||||
        this._groups = value;
 | 
					        this._criteria.groups = value;
 | 
				
			||||||
        this.refresh();
 | 
					        this.refresh();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    get aggregates() {
 | 
					    get aggregates() {
 | 
				
			||||||
        return this._aggregates;
 | 
					        return this._criteria.aggregates;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    set aggregates(value: IAggregate[]) {
 | 
					    set aggregates(value: IAggregate[]) {
 | 
				
			||||||
        this._aggregates = value;
 | 
					        this._criteria.aggregates = value;
 | 
				
			||||||
        this.refresh();
 | 
					        this.refresh();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    get pageSize() {
 | 
					    get pageSize() {
 | 
				
			||||||
        return this._pageSize;
 | 
					        return this._criteria.pageSize;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    set pageSize(value: number) {
 | 
					    set pageSize(value: number) {
 | 
				
			||||||
        this._pageSize = value;
 | 
					        this._criteria.pageSize = value;
 | 
				
			||||||
        this.refresh();
 | 
					        this.refresh();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    get page() {
 | 
					    get page() {
 | 
				
			||||||
        return this._page;
 | 
					        return this._criteria.page;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    set page(value: number) {
 | 
					    set page(value: number) {
 | 
				
			||||||
        this._page = value;
 | 
					        this._criteria.page = value;
 | 
				
			||||||
        this.refresh();
 | 
					        this.refresh();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -8,6 +8,9 @@ export interface IDataSource<TModel>
 | 
				
			|||||||
    executeCommandByName<TCommand, TResult>(name: string, command: TCommand) : Observable<TResult>;
 | 
					    executeCommandByName<TCommand, TResult>(name: string, command: TCommand) : Observable<TResult>;
 | 
				
			||||||
    query<TQuery extends IQueryCriteria>(query: TQuery) : Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>;
 | 
					    query<TQuery extends IQueryCriteria>(query: TQuery) : Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    data$: Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>;
 | 
				
			||||||
 | 
					    loading$: Observable<boolean>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    data: IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
 | 
					    data: IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sorts: ISort[];
 | 
					    sorts: ISort[];
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user