Source

query/types.ts

import { Condition } from "./Condition";
import { OrderBySelector } from "./selectors";
import { Model } from "@decaf-ts/decorator-validation";
import { Constructor } from "@decaf-ts/decoration";
import { OrderDirection } from "../repository/constants";

/**
 * @description
 * Options for configuring query building behavior.
 *
 * @summary
 * The `QueryOptions` type defines flags that determine whether certain clauses
 * (limit, offset, order by) are permitted, as well as whether violations
 * should throw an error during query construction.
 *
 * @memberOf module:query
 */
export type QueryOptions = {
  allowLimit?: boolean;
  allowOffset?: boolean;
  allowOrderBy?: boolean;
  throws?: boolean;
};

export type OrderLimitOffsetExtract = {
  orderBy?: OrderBySelector<any>[];
  limit?: number;
  offset?: number;
};

export type DirectionLimitOffset = {
  direction?: OrderDirection;
  order?: OrderBySelector<any>[];
  limit?: number;
  offset?: number;
  bookmark?: string | number;
};

export type PreparedStatement<M extends Model> = {
  class: Constructor<M>;
  method: string;
  args: any[];
  params: DirectionLimitOffset;
};

/**
 * @description
 * Structured query object representing parsed query clauses.
 *
 * @summary
 * The `QueryAssist` interface defines the standard structure returned
 * by query builders. It includes actions such as find, optional clauses
 * like select, groupBy, and orderBy, and pagination controls (limit, offset).
 *
 * @template T The entity or record type that conditions may apply to.
 *
 * @interface QueryAssist
 * @memberOf module:query
 */
/**
 * @description
 * Supported query action types for method-based queries.
 *
 * @summary
 * The `QueryAction` type defines the possible actions that can be
 * performed by a query built from a method name.
 *
 * @memberOf module:query
 */
export type QueryAction =
  | "find"
  | "page"
  | "count"
  | "sum"
  | "avg"
  | "min"
  | "max"
  | "distinct"
  | "group";

export interface QueryAssist {
  action: QueryAction;
  select: undefined | string[];
  selector?: string;
  where?: Condition<any>;
  groupBy?: string[];
  orderBy?: OrderBySelector<any>[];
  limit: number | undefined;
  offset: number | undefined;
}

/**
 * @description
 * Enumeration of supported query clauses for building method-based queries.
 *
 * @summary
 * The `QueryClause` enum defines string literals that represent
 * different segments of a query (e.g., `findBy`, `Select`, `And`, `Or`).
 *
 * @enum QueryClause
 * @memberOf module:query
 */
export enum QueryClause {
  FIND_BY = "findBy",
  PAGE_BY = "pageBy",
  COUNT_BY = "countBy",
  SUM_BY = "sumBy",
  AVG_BY = "avgBy",
  MIN_BY = "minBy",
  MAX_BY = "maxBy",
  DISTINCT_BY = "distinctBy",
  GROUP_BY_PREFIX = "groupBy",
  SELECT = "Select",
  AND = "And",
  OR = "Or",
  GROUP_BY = "GroupBy",
  ORDER_BY = "OrderBy",
  THEN = "Then",
  THEN_BY = "ThenBy",
}

/**
 * @description
 * Function signature for parsing operators in query building.
 *
 * @summary
 * The `OperatorParser` type represents a function that takes a field name
 * and arguments, then produces a `Condition` object that can be used in a query.
 *
 * @template T The type of the condition result.
 *
 * @param field {string} - The name of the field being parsed.
 * @param args {any[]} - Additional arguments for operator evaluation.
 *
 * @return {Condition<any>} A condition object representing the parsed operator.
 *
 * @memberOf module:query
 */
export type OperatorParser = (field: string, ...args: any) => Condition<any>;

/**
 * @description
 * Descriptor for fields and their associated operators in query parsing.
 *
 * @summary
 * The `FilterDescriptor` interface defines the structure used when parsing
 * method segments into filterable fields and associated operators.
 *
 * @interface FilterDescriptor
 * @memberOf module:query
 */
export interface FilterDescriptor {
  field: string;
  operator?: string;
}

export type ViewKey = string | string[];

export type ViewKind =
  | "view"
  | "groupBy"
  | "count"
  | "sum"
  | "max"
  | "min"
  | "distinct";

export interface ViewAuthOptions {
  expression?: string;
  field?: string;
  roles?: string[];
  mode?: "any" | "all";
}

export interface ViewOptions {
  name?: string;
  key?: ViewKey;
  value?: ViewKey | null;
  condition?: Condition<any> | string;
  auth?: string | ViewAuthOptions;
  compositions?: string[];
  directions?: OrderDirection[];
}

export interface ViewMetadata extends ViewOptions {
  kind: ViewKind;
  attribute: string;
}