Merge branch '16159-logout-request-with-token'
[arvados-workbench2.git] / src / services / api / filter-builder.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 export function joinFilters(...filters: string[]) {
6     return filters.filter(s => s).join(",");
7 }
8
9 export class FilterBuilder {
10     constructor(private filters = "") { }
11
12     public addEqual(field: string, value?: string | boolean | null, resourcePrefix?: string) {
13         return this.addCondition(field, "=", value, "", "", resourcePrefix);
14     }
15
16     public addDistinct(field: string, value?: string | boolean | null, resourcePrefix?: string) {
17         return this.addCondition(field, "!=", value, "", "", resourcePrefix);
18     }
19
20     public addLike(field: string, value?: string, resourcePrefix?: string) {
21         return this.addCondition(field, "like", value, "%", "%", resourcePrefix);
22     }
23
24     public addILike(field: string, value?: string, resourcePrefix?: string) {
25         return this.addCondition(field, "ilike", value, "%", "%", resourcePrefix);
26     }
27
28     public addContains(field: string, value?: string, resourcePrefix?: string) {
29         return this.addCondition(field, "contains", value, "", "", resourcePrefix);
30     }
31
32     public addIsA(field: string, value?: string | string[], resourcePrefix?: string) {
33         return this.addCondition(field, "is_a", value, "", "", resourcePrefix);
34     }
35
36     public addIn(field: string, value?: string | string[], resourcePrefix?: string) {
37         return this.addCondition(field, "in", value, "", "", resourcePrefix);
38     }
39
40     public addNotIn(field: string, value?: string | string[], resourcePrefix?: string) {
41         return this.addCondition(field, "not in", value, "", "", resourcePrefix);
42     }
43
44     public addGt(field: string, value?: string, resourcePrefix?: string) {
45         return this.addCondition(field, ">", value, "", "", resourcePrefix);
46     }
47
48     public addGte(field: string, value?: string, resourcePrefix?: string) {
49         return this.addCondition(field, ">=", value, "", "", resourcePrefix);
50     }
51
52     public addLt(field: string, value?: string, resourcePrefix?: string) {
53         return this.addCondition(field, "<", value, "", "", resourcePrefix);
54     }
55
56     public addLte(field: string, value?: string, resourcePrefix?: string) {
57         return this.addCondition(field, "<=", value, "", "", resourcePrefix);
58     }
59
60     public addExists(value?: string, resourcePrefix?: string) {
61         return this.addCondition("properties", "exists", value, "", "", resourcePrefix);
62     }
63     public addDoesNotExist(field: string, resourcePrefix?: string) {
64         return this.addCondition("properties." + field, "exists", false, "", "", resourcePrefix);
65     }
66
67     public addFullTextSearch(value: string) {
68         const terms = value.trim().split(/(\s+)/);
69         terms.forEach(term => {
70             if (term !== " ") {
71                 this.addCondition("any", "ilike", term, "%", "%");
72             }
73         });
74         return this;
75     }
76
77     public getFilters() {
78         return this.filters;
79     }
80
81     private addCondition(field: string, cond: string, value?: string | string[] | boolean | null, prefix: string = "", postfix: string = "", resourcePrefix?: string) {
82         if (value !== undefined) {
83             if (typeof value === "string") {
84                 value = `"${prefix}${value}${postfix}"`;
85             } else if (Array.isArray(value)) {
86                 value = `["${value.join(`","`)}"]`;
87             } else if (value !== null) {
88                 value = value ? "true" : "false";
89             }
90
91             const resPrefix = resourcePrefix
92                 ? resourcePrefix + "."
93                 : "";
94
95             this.filters += `${this.filters ? "," : ""}["${resPrefix}${field}","${cond}",${value}]`;
96         }
97         return this;
98     }
99 }