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 "../../models/resource";
8 export default class OrderBuilder<T extends Resource = Resource> {
10 static create<T extends Resource = Resource>(prefix?: string){
11 return new OrderBuilder<T>([], prefix);
15 private order: string[] = [],
16 private prefix = ""){}
18 private addRule (direction: string, attribute: keyof T) {
19 const prefix = this.prefix ? this.prefix + "." : "";
20 const order = [...this.order, `${prefix}${_.snakeCase(attribute.toString())} ${direction}`];
21 return new OrderBuilder<T>(order, prefix);
24 addAsc(attribute: keyof T) {
25 return this.addRule("asc", attribute);
28 addDesc(attribute: keyof T) {
29 return this.addRule("desc", attribute);
32 concat(orderBuilder: OrderBuilder){
33 return new OrderBuilder<T>(
34 this.order.concat(orderBuilder.getOrder()),
40 return this.order.slice();