code cleanup and version 1.0.0-alpha.1 release

This commit is contained in:
2024-08-30 10:29:16 -04:00
parent a8999f81c7
commit fc543977fb
32 changed files with 9614 additions and 14217 deletions
+24
View File
@@ -0,0 +1,24 @@
# Data
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 8.2.4.
## Code scaffolding
Run `ng generate component component-name --project data` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project data`.
> Note: Don't forget to add `--project data` or else it will be added to the default project in your `angular.json` file.
## Build
Run `ng build data` to build the project. The build artifacts will be stored in the `dist/` directory.
## Publishing
After building your library with `ng build data`, go to the dist folder `cd dist/data` and run `npm publish`.
## Running unit tests
Run `ng test data` to execute the unit tests via [Karma](https://karma-runner.github.io).
## Further help
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).
+32
View File
@@ -0,0 +1,32 @@
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, '../../../coverage/poweredsoft/data'),
reports: ['html', 'lcovonly', 'text-summary'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
restartOnFileChange: true
});
};
+7
View File
@@ -0,0 +1,7 @@
{
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../dist/data",
"lib": {
"entryFile": "src/public-api.ts"
}
}
+8
View File
@@ -0,0 +1,8 @@
{
"name": "@openharbor/data",
"version": "1.0.0-alpha.1",
"repository": "https://git.openharbor.io/Open-Harbor/ts-data",
"peerDependencies": {
"rxjs": "^6.5.3"
}
}
+284
View File
@@ -0,0 +1,284 @@
import { Observable, of, Observer, BehaviorSubject, throwError, Subject } from 'rxjs';
import { IDataSource } from './IDataSource';
import { IQueryExecutionResult, IQueryExecutionGroupResult, IFilter, ISort, IAggregate, IGroup, IQueryCriteria } from './models';
import { finalize, catchError, map } from 'rxjs/operators';
import { IDataSourceOptions, IResolveCommandModelEvent } from '../public-api';
import { IDataSourceErrorMessage } from './IDataSourceErrorMessage';
import { IDataSourceValidationError } from './IDataSourceValidationError';
import { IDataSourceError } from './IDataSourceError';
import { IDataSourceNotifyMessage } from './IDataSourceNotifyMessage';
import { IDataSourceCommandStarted } from './IDataSourceCommandStarted';
export class DataSource<TModel> implements IDataSource<TModel>
{
data: IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel> = null;
protected _dataSubject: BehaviorSubject<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>> = new BehaviorSubject(null);
protected _loadingSubject: BehaviorSubject<boolean> = new BehaviorSubject(false);
protected _validationSubject: Subject<IDataSourceValidationError> = new Subject();
protected _commandStartedSubject: Subject<IDataSourceCommandStarted> = new Subject();
protected _notifyMessageSubject: Subject<IDataSourceNotifyMessage> = new Subject();
protected _data$: Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>;
protected _loading$: Observable<boolean>;
protected _validationError$: Observable<IDataSourceValidationError>;
protected _notifyMessage$: Observable<IDataSourceNotifyMessage>;
protected _commandStarted$: Observable<IDataSourceCommandStarted>;
protected _criteria: IQueryCriteria = {
page: null,
pageSize: null,
filters: [],
aggregates: [],
groups: [],
sorts: []
};
get data$() {
if (!this._data$)
this._data$ = this._dataSubject.asObservable();
return this._data$;
}
get loading$() {
if (!this._loading$)
this._loading$ = this._loadingSubject.asObservable();
return this._loading$;
}
get validationError$() {
if (!this._validationError$)
this._validationError$ = this._validationSubject.asObservable();
return this._validationError$;
}
get commandStarted$() {
if (!this._commandStarted$)
this._commandStarted$ = this._commandStartedSubject.asObservable();
return this._commandStarted$;
}
get notifyMessage$() {
if (!this._notifyMessage$)
this._notifyMessage$ = this._notifyMessageSubject.asObservable();
return this._notifyMessage$;
}
constructor(public options: IDataSourceOptions<TModel>) {
this._initCriteria();
}
clear() {
this.data = null;
this._dataSubject.next(null);
}
updateData(value: IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>) {
this.data = value;
this._dataSubject.next(this.data);
}
replaceDataWithArray(items: TModel[]) {
this.data = {
totalRecords: items.length,
numberOfPages: null,
groups: null,
aggregates: null,
data: items
};
this._dataSubject.next(this.data);
}
replaceDataWithSingle(item: TModel) {
this.data = {
totalRecords: 1,
numberOfPages: null,
groups: null,
aggregates: null,
data: [item]
};
this._dataSubject.next(this.data);
}
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;
this._criteria.sorts = copy.sorts || this._criteria.sorts;
}
resolveIdField<TKeyType extends any>(model: TModel): TKeyType {
if (this.options.idField)
return model[this.options.idField];
if (this.options.resolveIdField)
return this.options.resolveIdField(model);
throw new Error("Must specify an id field or supply a method to resolve the id field.");
}
resolveCommandModelByName<T extends any>(event: IResolveCommandModelEvent<TModel>) : Observable<T> {
if (!this.options.transport.commands.hasOwnProperty(event.command))
return throwError(<IDataSourceErrorMessage>{
type: 'message',
message: `command with name ${event.command} not found`
});
const commandOptions = this.options.transport.commands[event.command];
if (commandOptions.resolveCommandModel)
return commandOptions.resolveCommandModel(event);
const noResolveMethod: any = event.model || {};
return of<T>(noResolveMethod as T);
}
executeCommandByName<TCommand, TResult>(name: string, command: TCommand) : Observable<TResult> {
if (!this.options.transport.commands.hasOwnProperty(name))
return throwError(`command with name ${name} not found`);
this._commandStartedSubject.next({
name: name, command: command
});
return this.options.transport.commands[name].adapter.handle(command).pipe(
map(t => {
this._notifyMessageSubject.next({
type: 'success',
message: 'COMMAND_EXECUTED_SUCCESSFULLY',
messageParams: {
command: name
}
});
return t;
}),
catchError((err: IDataSourceError) => {
if (err.type == 'message')
this._notifyMessageSubject.next({
type: 'error',
message: (err as IDataSourceErrorMessage).message
});
else if(err.type == 'validation')
this._validationSubject.next(err as IDataSourceValidationError);
return throwError(err);
})
);
}
private _query() : Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>> {
return new Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>((o: Observer<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>) => {
this._loadingSubject.next(true);
this.options.transport.query.adapter.handle(this._criteria)
.pipe(
finalize(() => {
o.complete();
this._loadingSubject.next(false);
})
)
.subscribe(
result => {
this.data = result;
o.next(result);
this._dataSubject.next(this.data);
this._notifyMessageSubject.next({
message: 'NEW_DATA_READ_SUCCESSFULLY',
type: 'info'
});
},
err => {
o.error(err);
this._notifyMessageSubject.next({
message: 'UNEXPECTED_ERROR_OCCURRED',
type: 'error'
});
}
);
});
}
query<TQuery extends IQueryCriteria>(query: TQuery) {
this._criteria.page = query.page === undefined ? this._criteria.page:query.page;
this._criteria.pageSize = query.pageSize === undefined ? this._criteria.pageSize: query.pageSize;
this._criteria.filters = query.filters || this._criteria.filters;
this._criteria.groups = query.groups || this._criteria.groups;
this._criteria.aggregates = query.aggregates || this._criteria.aggregates;
this._criteria.sorts = query.sorts || this._criteria.sorts;
return this.refresh();
}
executeQuery<TQuery extends IQueryCriteria>(query: TQuery): Observable<IQueryExecutionGroupResult<TModel>>{
return this.options.transport.query.adapter.handle(query);
}
refresh() {
return this._query()
.subscribe();
}
get sorts() {
return this._criteria.sorts;
}
set sorts(value: ISort[]) {
this._criteria.sorts = value;
this.refresh();
}
get filters() {
return this._criteria.filters;
}
set filters(value: IFilter[]) {
this._criteria.filters = value;
this.refresh();
}
get groups() {
return this._criteria.groups;
}
set groups(value: IGroup[]) {
this._criteria.groups = value;
this.refresh();
}
get aggregates() {
return this._criteria.aggregates;
}
set aggregates(value: IAggregate[]) {
this._criteria.aggregates = value;
this.refresh();
}
get pageSize() {
return this._criteria.pageSize;
}
set pageSize(value: number) {
this._criteria.pageSize = value;
this.refresh();
}
get page() {
return this._criteria.page;
}
set page(value: number) {
this._criteria.page = value;
this.refresh();
}
}
@@ -0,0 +1,4 @@
import { IQueryCriteria, IQueryExecutionResult, IQueryExecutionGroupResult } from './models';
import { IQueryAdapter } from "./IQueryAdapter";
export interface IAdvanceQueryAdapter<TQuery extends IQueryCriteria, TResult> extends IQueryAdapter<TQuery, IQueryExecutionResult<TResult> & IQueryExecutionGroupResult<TResult>> {
}
+4
View File
@@ -0,0 +1,4 @@
import { Observable } from 'rxjs';
export interface ICommandAdapter<TCommand, TResult> {
handle(command: TCommand): Observable<TResult>;
}
+35
View File
@@ -0,0 +1,35 @@
import { Observable } from "rxjs";
import { ISort, IFilter, IGroup, IAggregate, IQueryExecutionResult, IQueryExecutionGroupResult, IQueryCriteria } from "./models";
import { IResolveCommandModelEvent } from "./IResolveCommandModelEvent";
import { IDataSourceValidationError } from './IDataSourceValidationError';
import { IDataSourceNotifyMessage } from './IDataSourceNotifyMessage';
import { IDataSourceCommandStarted } from "./IDataSourceCommandStarted";
export interface IDataSource<TModel>
{
resolveCommandModelByName<T extends any>(event: IResolveCommandModelEvent<TModel>) : Observable<T>;
executeCommandByName<TCommand, TResult>(name: string, command: TCommand) : Observable<TResult>;
query<TQuery extends IQueryCriteria>(query: TQuery);
executeQuery<TQuery extends IQueryCriteria>(query: TQuery): Observable<IQueryExecutionGroupResult<TModel> & IQueryExecutionGroupResult<TModel>>;
refresh();
resolveIdField<TKeyType extends any>(model: TModel) : TKeyType;
clear();
updateData(value: IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>);
replaceDataWithArray(items: TModel[]);
replaceDataWithSingle(item: TModel);
data$: Observable<IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>;
loading$: Observable<boolean>;
validationError$: Observable<IDataSourceValidationError>;
notifyMessage$: Observable<IDataSourceNotifyMessage>;
commandStarted$: Observable<IDataSourceCommandStarted>;
data: IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>;
sorts: ISort[];
filters: IFilter[];
groups: IGroup[];
aggregates: IAggregate[];
pageSize: number;
page: number;
}
@@ -0,0 +1,7 @@
import { Observable } from "rxjs";
import { ICommandAdapter } from "./ICommandAdapter";
import { IResolveCommandModelEvent } from "./IResolveCommandModelEvent";
export interface IDataSourceCommandAdapterOptions<TModel> {
adapter: ICommandAdapter<any, any>;
resolveCommandModel?: (event: IResolveCommandModelEvent<TModel>) => Observable<any>;
}
@@ -0,0 +1,5 @@
export interface IDataSourceCommandStarted {
name: string;
command: any;
}
@@ -0,0 +1,4 @@
export interface IDataSourceError {
type: 'message' | 'validation';
}
@@ -0,0 +1,7 @@
import { IDataSourceError } from './IDataSourceError';
export interface IDataSourceErrorMessage extends IDataSourceError {
message: string;
}
@@ -0,0 +1,5 @@
export interface IDataSourceNotifyMessage {
type: 'warning' | 'info' | 'success' | 'error';
message: string;
messageParams?: any;
}
@@ -0,0 +1,18 @@
import { IDataSourceQueryAdapterOptions } from "./IDataSourceQueryAdapterOptions";
import { IDataSourceCommandAdapterOptions } from "./IDataSourceCommandAdapterOptions";
import { IQueryCriteria } from "./models";
export interface IDataSourceOptions<TModel> {
transport: IDataSourceTransportOptions<TModel>;
idField?: string;
resolveIdField?: (model: TModel) => any;
defaultCriteria: IQueryCriteria;
}
export interface IDataSourceTransportOptions<TModel> {
query: IDataSourceQueryAdapterOptions<TModel>;
commands: {
[name: string]: IDataSourceCommandAdapterOptions<TModel>;
};
}
@@ -0,0 +1,5 @@
import { IAdvanceQueryAdapter } from "./IAdvanceQueryAdapter";
export interface IDataSourceQueryAdapterOptions<TModel> {
adapter: IAdvanceQueryAdapter<any, TModel>;
}
@@ -0,0 +1,6 @@
import { IDataSourceError } from './IDataSourceError';
export interface IDataSourceValidationError extends IDataSourceError
{
errors: { [field: string]: string[]; }
}
+4
View File
@@ -0,0 +1,4 @@
import { Observable } from 'rxjs';
export interface IQueryAdapter<TQuery, TResult> {
handle(query: TQuery): Observable<TResult>;
}
@@ -0,0 +1,5 @@
export interface IResolveCommandModelEvent<TModel> {
command: string;
model: TModel;
params?: any;
}
+95
View File
@@ -0,0 +1,95 @@
export interface IAggregate {
path: string;
type: string;
}
export interface ISort {
path: string;
ascending?: boolean;
}
export interface ISimpleFilter extends IFilter {
path: string;
value: any;
}
export interface IQueryResult<TModel> {
aggregates?: IAggregateResult[];
data?: TModel[];
}
export interface IQueryExecutionResultPaging {
totalRecords: number;
numberOfPages?: number;
}
export interface IQueryExecutionResult<TRecord> extends IQueryResult<TRecord>, IQueryExecutionResultPaging {
}
export interface IQueryExecutionGroupResult<TModel> extends IQueryExecutionResult<TModel> {
groups: IGroupQueryResult<TModel>[];
}
export interface IQueryCriteria {
page?: number;
pageSize?: number;
sorts?: ISort[];
groups?: IGroup[];
aggregates?: IAggregate[];
filters?: IFilter[];
}
export interface IGroupQueryResult<TModel> extends IQueryResult<TModel> {
groupPath: string;
groupValue: any;
hasSubGroups: boolean;
subGroups?: IGroupQueryResult<TModel>[];
}
export interface IGroup {
path: string;
ascending?: boolean;
}
export interface IFilter {
type: string;
and?: boolean;
}
export interface IAggregateResult {
path: string;
type: string;
value: any;
}
export interface ICompositeFilter extends IFilter {
filters: IFilter[];
}
export const enum AggregateType {
COUNT = 'Count',
SUM = 'Sum',
AVG = 'Avg',
LONGCOUNT = 'LongCount',
MIN = 'Min',
MAX = 'Max',
FIRST = 'First',
FIRSTORDEFAULT = 'FirstOrDefault',
LAST = 'Last',
LASTORDEFAULT = 'LastOrDefault'
}
export const enum FilterType {
EQUAL = 'Equal',
CONTAINS = 'Contains',
STARTSWITH = 'StartsWith',
ENDSWITH = 'EndsWith',
COMPOSITE = 'Composite',
NOTEQUAL = 'NotEqual',
GREATERTHAN = 'GreaterThan',
LESSTHANOREQUAL = 'LessThanOrEqual',
GREATERTHANOREQUAL = 'GreaterThanOrEqual',
LESSTHAN ='LessThan',
IN = 'In',
NOTIN = 'NotIn'
}
+19
View File
@@ -0,0 +1,19 @@
/*
* Public API Surface of data
*/
export * from './lib/models';
export * from './lib/DataSource';
export * from './lib/IAdvanceQueryAdapter';
export * from './lib/ICommandAdapter';
export * from './lib/IDataSource';
export * from './lib/IDataSourceOptions';
export * from './lib/IDataSourceQueryAdapterOptions';
export * from './lib/IQueryAdapter';
export * from './lib/IResolveCommandModelEvent';
export * from './lib/IDataSourceCommandAdapterOptions';
export * from './lib/IDataSourceValidationError';
export * from './lib/IDataSourceError';
export * from './lib/IDataSourceErrorMessage';
export * from './lib/IDataSourceNotifyMessage';
+26
View File
@@ -0,0 +1,26 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../../out-tsc/lib",
"target": "es2015",
"declaration": true,
"inlineSources": true,
"types": [],
"lib": [
"dom",
"es2018"
]
},
"angularCompilerOptions": {
"annotateForClosureCompiler": true,
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"enableResourceInlining": true
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
+17
View File
@@ -0,0 +1,17 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../../out-tsc/spec",
"types": [
"jasmine",
"node"
]
},
"files": [
"src/test.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
]
}
+17
View File
@@ -0,0 +1,17 @@
{
"extends": "../../tslint.json",
"rules": {
"directive-selector": [
true,
"attribute",
"lib",
"camelCase"
],
"component-selector": [
true,
"element",
"lib",
"kebab-case"
]
}
}