for single and queries that are not dynamic queries.
This commit is contained in:
parent
2485bdb835
commit
d2677a896d
6
package-lock.json
generated
6
package-lock.json
generated
@ -1927,9 +1927,9 @@
|
||||
}
|
||||
},
|
||||
"@poweredsoft/data": {
|
||||
"version": "0.0.31",
|
||||
"resolved": "https://registry.npmjs.org/@poweredsoft/data/-/data-0.0.31.tgz",
|
||||
"integrity": "sha512-J7Vl9Zk0/K3s8+n1PRUAFTDM20hi/zB8SL5ahuJVvHhc6de4fv5f19DzObwcvQeIN4PNMYD/Fhro9i0lECKlMQ==",
|
||||
"version": "0.0.32",
|
||||
"resolved": "https://registry.npmjs.org/@poweredsoft/data/-/data-0.0.32.tgz",
|
||||
"integrity": "sha512-vBsjBjKeNvjj3R0QwhBa+eKDZR3rK5lCueYUXIMU5iVFmw1UxVM3tW/0dbWmsDYnXi+/nm53Uz2MlqX9xpCStQ==",
|
||||
"requires": {
|
||||
"tslib": "^1.9.0"
|
||||
}
|
||||
|
@ -23,7 +23,7 @@
|
||||
"@angular/platform-browser": "~8.2.4",
|
||||
"@angular/platform-browser-dynamic": "~8.2.4",
|
||||
"@angular/router": "~8.2.4",
|
||||
"@poweredsoft/data": "0.0.31",
|
||||
"@poweredsoft/data": "0.0.32",
|
||||
"apollo-angular": "^1.8.0",
|
||||
"apollo-angular-link-http": "^1.9.0",
|
||||
"apollo-cache-inmemory": "^1.6.0",
|
||||
|
107
projects/poweredsoft/ngx-data/src/lib/ListDataSourceBuilder.ts
Normal file
107
projects/poweredsoft/ngx-data/src/lib/ListDataSourceBuilder.ts
Normal file
@ -0,0 +1,107 @@
|
||||
import { HttpClient } from "@angular/common/http";
|
||||
import { IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult, IQueryExecutionResultPaging } from "@poweredsoft/data";
|
||||
import { Observable, of } from "rxjs";
|
||||
import { map, switchMap } from "rxjs/operators";
|
||||
import { BaseHttpDataSourceOptionsBuilder } from "./BaseHttpDataSourceOptionsBuilder";
|
||||
|
||||
export class ListDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
||||
extends BaseHttpDataSourceOptionsBuilder<TModel, TKey>
|
||||
{
|
||||
private _beforeRead: (query: IQueryCriteria) => Observable<TQuery>;
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
super(http);
|
||||
}
|
||||
|
||||
public beforeRead(beforeRead: (query: IQueryCriteria) => Observable<TQuery>) {
|
||||
this._beforeRead = beforeRead;
|
||||
return this;
|
||||
}
|
||||
|
||||
public queryUrlWithGet(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of(<TQuery>{}));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return this.http.get<TModel[]>(url, {
|
||||
params: this.convertToParams(finalQuery)
|
||||
}).pipe(
|
||||
map(result => {
|
||||
return <IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>
|
||||
{
|
||||
totalRecords: result.length,
|
||||
data: result
|
||||
};
|
||||
})
|
||||
)
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
protected convertToParams(finalQuery: TQuery)
|
||||
{
|
||||
return Object.keys(finalQuery).reduce((prev, key) => {
|
||||
prev[key] = finalQuery[key];
|
||||
return prev;
|
||||
}, {} as { [param: string]: string | string[]; });
|
||||
}
|
||||
|
||||
public queryUrl(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
const finalBeforeRead = this._beforeRead || ((_: IQueryCriteria) => of(<TQuery>{}));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return this.http.post<TModel[]>(url, finalQuery).pipe(
|
||||
map(result => {
|
||||
return <IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>
|
||||
{
|
||||
totalRecords: result.length,
|
||||
data: result
|
||||
};
|
||||
})
|
||||
)
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public queryHandler(queryHandler: (query: IQueryCriteria) => Observable<TModel[]>) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: TQuery) => {
|
||||
const finalBeforeRead = this._beforeRead || (t => of({}));
|
||||
return finalBeforeRead(query)
|
||||
.pipe(
|
||||
switchMap(finalQuery => {
|
||||
return queryHandler(finalQuery).pipe(
|
||||
map(result => {
|
||||
return <IQueryExecutionResult<TModel> & IQueryExecutionGroupResult<TModel>>{
|
||||
totalRecords: result.length,
|
||||
data: result
|
||||
};
|
||||
})
|
||||
)
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
import { HttpClient, HttpResponse } from "@angular/common/http";
|
||||
import { Query } from "@angular/core";
|
||||
import { HttpClient } from "@angular/common/http";
|
||||
import { IQueryCriteria, IQueryExecutionGroupResult, IQueryExecutionResult, IQueryExecutionResultPaging } from "@poweredsoft/data";
|
||||
import { Observable, of } from "rxjs";
|
||||
import { map, switchMap } from "rxjs/operators";
|
||||
@ -14,6 +13,11 @@ export class SingleDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
||||
super(http);
|
||||
}
|
||||
|
||||
public beforeRead(beforeRead: (query: IQueryCriteria) => Observable<TQuery>) {
|
||||
this._beforeRead = beforeRead;
|
||||
return this;
|
||||
}
|
||||
|
||||
public queryUrlWithGet(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
@ -50,7 +54,7 @@ export class SingleDataSourceOptionsBuilder<TQuery, TModel, TKey>
|
||||
}, {} as { [param: string]: string | string[]; });
|
||||
}
|
||||
|
||||
public queryPostUrl(url: string) {
|
||||
public queryUrl(url: string) {
|
||||
this._query = {
|
||||
adapter: {
|
||||
handle: (query: IQueryCriteria) => {
|
||||
|
@ -1,6 +1,8 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { HttpDataSourceOptionsBuilder } from "./HttpDataSourceBuilder";
|
||||
import { SingleDataSourceOptionsBuilder } from "./SingleObjectDataSourceBuilder";
|
||||
import { ListDataSourceOptionsBuilder } from "./ListDataSourceBuilder";
|
||||
|
||||
|
||||
@Injectable({
|
||||
@ -13,4 +15,12 @@ export class HttpDataSourceService {
|
||||
builder<TModel, TKey>() {
|
||||
return new HttpDataSourceOptionsBuilder<TModel, TKey>(this.http);
|
||||
}
|
||||
|
||||
singleBuilder<TQuery, TModel, TKey>() {
|
||||
return new SingleDataSourceOptionsBuilder<TQuery, TModel, TKey>(this.http);
|
||||
}
|
||||
|
||||
listBuilder<TQuery, TModel, TKey>() {
|
||||
return new ListDataSourceOptionsBuilder<TQuery, TModel, TKey>(this.http);
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,9 @@
|
||||
|
||||
<h4>Latest Data</h4>
|
||||
<hr>
|
||||
<pre>
|
||||
{{ latestData | json }}
|
||||
</pre>
|
||||
<p>Standard</p>
|
||||
<pre>{{ latestData | json }}</pre>
|
||||
<p>Single</p>
|
||||
<pre>{{ latestSingle | json }}</pre>
|
||||
<p>List</p>
|
||||
<pre>{{ latestList | json }}</pre>
|
@ -11,12 +11,18 @@ import { GraphQLDataSourceService, IGraphQLAdvanceQueryInput } from 'projects/po
|
||||
import { TestService, ITestModel, IValidationTestCommand } from './services/test.service';
|
||||
import { HttpDataSourceService} from '@poweredsoft/ngx-data';
|
||||
|
||||
export class IContact
|
||||
export interface IContact
|
||||
{
|
||||
id: number
|
||||
displayName: string
|
||||
}
|
||||
|
||||
export interface IPerson {
|
||||
id: number,
|
||||
firstName: string,
|
||||
lastName: string
|
||||
}
|
||||
|
||||
export interface ICreatePerson {
|
||||
firstName: string
|
||||
lastName: string
|
||||
@ -32,6 +38,14 @@ export interface IMyQuery extends IQueryCriteria{
|
||||
}
|
||||
}
|
||||
|
||||
export interface IOnePersonQuery {
|
||||
personId: number
|
||||
}
|
||||
|
||||
export interface IListPersonQuery {
|
||||
search?: string
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
templateUrl: './app.component.html',
|
||||
@ -42,7 +56,33 @@ export class AppComponent implements OnInit {
|
||||
dataSource: IDataSource<IContact>;
|
||||
latestData: any;
|
||||
|
||||
onePersonDs: IDataSource<IPerson>;
|
||||
listPersonDs: IDataSource<IPerson>;
|
||||
latestSingle: any;
|
||||
latestList: any;
|
||||
|
||||
constructor(private hdss: HttpDataSourceService) {
|
||||
|
||||
this.onePersonDs = hdss.singleBuilder<IOnePersonQuery, IPerson, number>()
|
||||
.keyResolver(t => t.id)
|
||||
.queryUrl('https://localhost:5001/api/query/onePerson')
|
||||
.beforeRead(_ => {
|
||||
return of({
|
||||
personId: 1
|
||||
})
|
||||
})
|
||||
.createDataSource();
|
||||
|
||||
this.listPersonDs = hdss.listBuilder<IListPersonQuery, IPerson, number>()
|
||||
.keyResolver(t => t.id)
|
||||
.queryUrl('https://localhost:5001/api/query/listPerson')
|
||||
.beforeRead(criteria => {
|
||||
return of({
|
||||
search: "Doe"
|
||||
})
|
||||
})
|
||||
.createDataSource();
|
||||
|
||||
const ds = hdss
|
||||
.builder<IContact, number>()
|
||||
.keyResolver(m => m.id)
|
||||
@ -80,10 +120,20 @@ export class AppComponent implements OnInit {
|
||||
if (message.type == 'error')
|
||||
alert(message.message);
|
||||
});
|
||||
|
||||
this.onePersonDs.data$.subscribe(onePerson => {
|
||||
this.latestSingle = onePerson;
|
||||
})
|
||||
|
||||
this.listPersonDs.data$.subscribe(t => {
|
||||
this.latestList = t;
|
||||
})
|
||||
}
|
||||
|
||||
refresh() {
|
||||
this.dataSource.refresh();
|
||||
this.onePersonDs.refresh();
|
||||
this.listPersonDs.refresh();
|
||||
}
|
||||
|
||||
echoCommand() {
|
||||
|
Loading…
Reference in New Issue
Block a user