1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as _ from "lodash";
7 export class FilterBuilder {
8 static create(resourcePrefix = "") {
9 return new FilterBuilder(resourcePrefix);
13 private resourcePrefix = "",
14 private filters = "") { }
16 public addEqual(field: string, value?: string) {
17 return this.addCondition(field, "=", value);
20 public addLike(field: string, value?: string) {
21 return this.addCondition(field, "like", value, "%", "%");
24 public addILike(field: string, value?: string) {
25 return this.addCondition(field, "ilike", value, "%", "%");
28 public addIsA(field: string, value?: string | string[]) {
29 return this.addCondition(field, "is_a", value);
32 public addIn(field: string, value?: string | string[]) {
33 return this.addCondition(field, "in", value);
36 public concat(filterBuilder: FilterBuilder) {
37 return new FilterBuilder(this.resourcePrefix, this.filters + (this.filters && filterBuilder.filters ? "," : "") + filterBuilder.getFilters());
45 return "[" + this.filters + "]";
48 private addCondition(field: string, cond: string, value?: string | string[], prefix: string = "", postfix: string = "") {
50 value = typeof value === "string"
51 ? `"${prefix}${value}${postfix}"`
52 : `["${value.join(`","`)}"]`;
54 const resourcePrefix = this.resourcePrefix
55 ? _.snakeCase(this.resourcePrefix) + "."
58 this.filters += `${this.filters ? "," : ""}["${resourcePrefix}${_.snakeCase(field.toString())}","${cond}",${value}]`;