// SPDX-License-Identifier: AGPL-3.0
import * as _ from "lodash";
-import { Resource } from "./common-resource-service";
-export default class FilterBuilder<T extends Resource = Resource> {
-
- static create<T extends Resource = Resource>(resourcePrefix = "") {
- return new FilterBuilder<T>(resourcePrefix);
- }
-
- constructor(
- private resourcePrefix = "",
- private filters = "") { }
+export function joinFilters(filters0?: string, filters1?: string) {
+ return [filters0, filters1].filter(s => s).join(",");
+}
- public addEqual(field: keyof T, value?: string) {
- return this.addCondition(field, "=", value);
- }
+export class FilterBuilder {
+ constructor(private filters = "") { }
- public addLike(field: keyof T, value?: string) {
- return this.addCondition(field, "like", value, "", "%");
+ public addEqual(field: string, value?: string, resourcePrefix?: string) {
+ return this.addCondition(field, "=", value, "", "", resourcePrefix );
}
- public addILike(field: keyof T, value?: string) {
- return this.addCondition(field, "ilike", value, "", "%");
+ public addLike(field: string, value?: string, resourcePrefix?: string) {
+ return this.addCondition(field, "like", value, "%", "%", resourcePrefix);
}
- public addIsA(field: keyof T, value?: string | string[]) {
- return this.addCondition(field, "is_a", value);
+ public addILike(field: string, value?: string, resourcePrefix?: string) {
+ return this.addCondition(field, "ilike", value, "%", "%", resourcePrefix);
}
- public addIn(field: keyof T, value?: string | string[]) {
- return this.addCondition(field, "in", value);
+ public addIsA(field: string, value?: string | string[], resourcePrefix?: string) {
+ return this.addCondition(field, "is_a", value, "", "", resourcePrefix);
}
- public concat<O extends Resource>(filterBuilder: FilterBuilder<O>) {
- return new FilterBuilder(this.resourcePrefix, this.filters + this.getSeparator() + filterBuilder.getFilters());
+ public addIn(field: string, value?: string | string[], resourcePrefix?: string) {
+ return this.addCondition(field, "in", value, "", "", resourcePrefix);
}
public getFilters() {
return this.filters;
}
- public serialize() {
- return "[" + this.filters + "]";
- }
-
- private addCondition(field: keyof T, cond: string, value?: string | string[], prefix: string = "", postfix: string = "") {
+ private addCondition(field: string, cond: string, value?: string | string[], prefix: string = "", postfix: string = "", resourcePrefix?: string) {
if (value) {
value = typeof value === "string"
? `"${prefix}${value}${postfix}"`
: `["${value.join(`","`)}"]`;
- const resourcePrefix = this.resourcePrefix
- ? _.snakeCase(this.resourcePrefix) + "."
+ const resPrefix = resourcePrefix
+ ? _.snakeCase(resourcePrefix) + "."
: "";
- this.filters += `${this.getSeparator()}["${resourcePrefix}${_.snakeCase(field.toString())}","${cond}",${value}]`;
+ this.filters += `${this.filters ? "," : ""}["${resPrefix}${_.snakeCase(field)}","${cond}",${value}]`;
}
return this;
}
-
- private getSeparator () {
- return this.filters ? "," : "";
- }
}