From 74a496a5e0dd7c134766052d7c0e562b0d19c42b Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Thu, 28 Nov 2024 17:53:22 +0100 Subject: [PATCH] query enhancement --- src/type/dynamicQueries.ts | 46 ++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/type/dynamicQueries.ts b/src/type/dynamicQueries.ts index 25abe12..629454f 100644 --- a/src/type/dynamicQueries.ts +++ b/src/type/dynamicQueries.ts @@ -6,22 +6,25 @@ export interface DynamicQueryStructure { orderBy?: { [key: string]: "ASC" | "DESC" }; } -export type ConditionStructure = +export type ConditionStructure = ( | { - type: WhereType; column: string; operation: WhereOperation; value: ConditionValue; } | { - type: WhereType; + invert?: boolean; condition: 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" | "_"; +export type WhereType = "OR" | "AND" | "_"; // _ represents initial where in (sub-)query export type WhereOperation = | "eq" // Equal @@ -45,22 +48,26 @@ const exampleQuery: DynamicQueryStructure = { table: "member", where: [ { - type: "_", + structureType: "condition", + concat: "_", column: "mail", operation: "endsWith", value: "@gmail.com", }, { - type: "AND", + structureType: "nested", + concat: "AND", condition: [ { - type: "_", + structureType: "condition", + concat: "_", column: "firstname", operation: "startsWith", value: "J", }, { - type: "OR", + structureType: "condition", + concat: "OR", column: "lastname", operation: "startsWith", value: "K", @@ -74,6 +81,27 @@ const exampleQuery: DynamicQueryStructure = { table: "communication", foreignColumn: "sendNewsletter", }, + { + select: "*", + table: "membership", + foreignColumn: "memberships", + join: [ + { + select: "*", + table: "membership_status", + foreignColumn: "status", + where: [ + { + structureType: "condition", + concat: "_", + column: "status", + operation: "eq", + value: "aktiv", + }, + ], + }, + ], + }, ], orderBy: { firstname: "ASC",