//
// SPDX-License-Identifier: AGPL-3.0
-export enum FilterField {
- UUID = "uuid",
- OWNER_UUID = "owner_uuid"
+import * as _ from "lodash";
+
+export function joinFilters(filters0?: string, filters1?: string) {
+ return [filters0, filters1].filter(s => s).join(",");
}
-export default class FilterBuilder {
- private filters = "";
+export class FilterBuilder {
+ constructor(private filters = "") { }
- private addCondition(field: FilterField, cond: string, value?: string, prefix: string = "", postfix: string = "") {
- if (value) {
- this.filters += `["${field}","${cond}","${prefix}${value}${postfix}"]`;
- }
- return this;
+ public addEqual(field: string, value?: string, resourcePrefix?: string) {
+ return this.addCondition(field, "=", value, "", "", resourcePrefix );
}
- public addEqual(field: FilterField, value?: string) {
- return this.addCondition(field, "=", value);
+ public addLike(field: string, value?: string, resourcePrefix?: string) {
+ return this.addCondition(field, "like", value, "%", "%", resourcePrefix);
}
- public addLike(field: FilterField, value?: string) {
- return this.addCondition(field, "like", value, "", "%");
+ public addILike(field: string, value?: string, resourcePrefix?: string) {
+ return this.addCondition(field, "ilike", value, "%", "%", resourcePrefix);
}
- public addILike(field: FilterField, value?: string) {
- return this.addCondition(field, "ilike", value, "", "%");
+ public addIsA(field: string, value?: string | string[], resourcePrefix?: string) {
+ return this.addCondition(field, "is_a", value, "", "", resourcePrefix);
}
- public get() {
- return "[" + this.filters + "]";
+ public addIn(field: string, value?: string | string[], resourcePrefix?: string) {
+ return this.addCondition(field, "in", value, "", "", resourcePrefix);
+ }
+
+ public getFilters() {
+ return this.filters;
+ }
+
+ 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 resPrefix = resourcePrefix
+ ? _.snakeCase(resourcePrefix) + "."
+ : "";
+
+ this.filters += `${this.filters ? "," : ""}["${resPrefix}${_.snakeCase(field)}","${cond}",${value}]`;
+ }
+ return this;
}
}