export interface DynamicQueryStructure { id: string; select: string[] | "*"; table: string; where?: Array; join?: Array; orderBy?: Array; // only at top level } export type ConditionStructure = ( | { structureType: "condition"; column: string; operation: WhereOperation; value: ConditionValue; } | { structureType: "nested"; invert?: boolean; conditions: Array; } ) & { concat: WhereType; structureType: "condition" | "nested"; }; export type ConditionValue = FieldType | Array | { start: FieldType; end: FieldType }; export type FieldType = number | string | Date | boolean; export type WhereType = "OR" | "AND" | "_"; // _ represents initial where in (sub-)query export type WhereOperation = | "eq" // Equal | "neq" // Not equal | "lt" // Less than | "lte" // Less than or equal to | "gt" // Greater than | "gte" // Greater than or equal to | "in" // Included in an array | "notIn" // Not included in an array | "contains" // Contains | "notContains" // Does not contain | "null" // Is null | "notNull" // Is not null | "between" // Is between | "startsWith" // Starts with | "endsWith" // Ends with | "timespanEq"; // Date before x years (YYYY-01-01 YYYY-12-31) // TODO: age between | age equals | age greater | age smaller export type JoinStructure = { foreignColumn: string; type: "defined" } | { condition: string; type: "custom" }; export type OrderByStructure = { id: string; depth: number; table: string; column: string; order: OrderByType; }; export type OrderByType = "ASC" | "DESC"; export type QueryResult = { [key: string]: FieldType | QueryResult | Array; }; export const exampleQuery: DynamicQueryStructure = { id: "1234", select: ["firstname", "lastname"], table: "member", where: [ { structureType: "condition", concat: "_", column: "mail", operation: "endsWith", value: "@gmail.com", }, { structureType: "nested", concat: "AND", conditions: [ { structureType: "condition", concat: "_", column: "firstname", operation: "startsWith", value: "J", }, { structureType: "condition", concat: "OR", column: "lastname", operation: "startsWith", value: "K", }, ], }, ], join: [ { id: "5678", select: "*", table: "communication", foreignColumn: "sendNewsletter", type: "defined", }, { id: "91011", select: "*", table: "membership", foreignColumn: "memberships", type: "defined", join: [ { id: "121314", select: "*", table: "membership_status", foreignColumn: "status", type: "defined", where: [ { structureType: "condition", concat: "_", column: "status", operation: "eq", value: "aktiv", }, ], }, ], }, ], orderBy: [ { id: "1234", depth: 0, table: "member", column: "firstname", order: "ASC", }, { id: "1234", depth: 0, table: "member", column: "lastname", order: "ASC", }, ], };