1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as _ from "lodash";
6 import { Resource } from "./common-resource-service";
8 export default class FilterBuilder<T extends Resource = Resource> {
11 static create<T extends Resource = Resource>() {
12 return new FilterBuilder<T>();
15 private addCondition(field: keyof T, cond: string, value?: string | string[], prefix: string = "", postfix: string = "") {
17 value = typeof value === "string"
18 ? `"${prefix}${value}${postfix}"`
19 : `["${value.join(`","`)}"]`;
21 this.filters += `["${_.snakeCase(field.toString())}","${cond}",${value}]`;
26 public addEqual(field: keyof T, value?: string) {
27 return this.addCondition(field, "=", value);
30 public addLike(field: keyof T, value?: string) {
31 return this.addCondition(field, "like", value, "", "%");
34 public addILike(field: keyof T, value?: string) {
35 return this.addCondition(field, "ilike", value, "", "%");
38 public addIsA(field: keyof T, value?: string | string[]) {
39 return this.addCondition(field, "is_a", value);
43 return "[" + this.filters + "]";