import { JsonQLExpr } from "@mwater/jsonql" import { CleanExprOptions } from "./ExprCleaner" import { ValidateOptions } from "./ExprValidator" import { PromiseExprEvaluatorContext } from "./PromiseExprEvaluator" import Schema from "./Schema" import { AggrStatus, EnumValue, Expr, ExtensionExpr, FieldExpr, LiteralType, Variable } from "./types" /** Global lookup of extensions */ const exprExtensions: { [id: string]: ExprExtension } = {} /** Extension to the expression language. Referenced by ExtentionExprs */ export interface ExprExtension { cleanExpr(expr: T, options: CleanExprOptions, schema: Schema): Expr getExprAggrStatus(expr: T, schema: Schema): AggrStatus | null validateExpr(expr: T, options: ValidateOptions, schema: Schema): string | null /** Return array of { id: , name: } */ getExprEnumValues(expr: T, schema: Schema): EnumValue[] | null /** Gets the id table of an expression of type id */ getExprIdTable(expr: Expr, schema: Schema): string | null /** Gets the type of an expression */ getExprType(expr: Expr, schema: Schema): LiteralType | null /** Summarizes expression as text */ summarizeExpr(expr: Expr, locale: string | undefined, schema: Schema): string /** Get a list of fields that are referenced in a an expression * Useful to know which fields and joins are used. Includes joins as fields */ getReferencedFields(expr: Expr, schema: Schema): FieldExpr[] /** Compile to JsonQL */ compileExpr( expr: Expr, tableAlias: string, schema: Schema, ): JsonQLExpr | null /** Evaluate an expression given the context */ evaluate( expr: Expr, context: PromiseExprEvaluatorContext, schema: Schema | undefined, locale: string | undefined, ): Promise /** Evaluate an expression synchronously */ evaluateSync( expr: Expr, schema: Schema | undefined, locale: string | undefined, ): any } /** Register an extension to expressions. * @param id referenced in type { type: "extension", extension: , ... } */ export function registerExprExtension(id: string, extension: ExprExtension) { exprExtensions[id] = extension } export function getExprExtension(id: string): ExprExtension { const extension = exprExtensions[id] if (!extension) { throw new Error(`Extension ${id} not found`) } return extension }