I am making several queries to a database (here influxdb) and mapping them to an interface. This works so far also everything. What bothers me is that I need a new function for each interface. I would like to have my code optimized so that I only need one function for each interface. Unfortunately my knowledge is not yet sufficient for this. I am looking forward to your opinions about the challenge.
import { InfluxDB } from '@influxdata/influxdb-client';
import { Observable, from } from 'rxjs';
import { map, toArray } from 'rxjs/operators';
import { url, token, org } from './env';
export class QueryService {
static fluxQuery1 = `from(bucket: "bucket0")
|> range(start: xxx, stop: yyy)
|> filter(fn: (r) => r["_measurement"] == "measure")
|> filter(fn: (r) => r["_field"] == "value")
|> last()`;
static fluxQuery2 = `from(bucket: "bucket0")
|> range(start: zzz, stop: wwww)
|> filter(fn: (r) => r["_measurement"] == "measure2")
|> last()`;
static queryApi = new InfluxDB({ url, token }).getQueryApi(org);
constructor() {
}
public queryInflux1(): Observable<Inteface1[]> {
return from(QueryService.queryApi.rows(QueryService.fluxQuery1)).pipe(
map(({ values, tableMeta }): Inteface1 => {
const returnvalue = tableMeta.toObject(values);
return {
name: returnvalue.string,
element: returnvalue.string,
typ: returnvalue.string
};
}),
toArray() // Collect into array of rows with rxjs operator
);
}
public queryInflux2(): Observable<Inteface2[]> {
return from(QueryService.queryApi.rows(QueryService.fluxQuery2)).pipe(
map(({ values, tableMeta }): Inteface2=> {
const returnvalue = tableMeta.toObject(values);
return {
isvalid: returnvalue.boolean,
value: returnvalue.number
};
}),
toArray()
);
}
}
export interface Interface2{
name: string;
element: string;
typ: string;
}
export interface Interface2{
isvalid: boolean;
value: number;
}