From 717de68f4e45bf728162040ecceb24a70d7a4ff2 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Tue, 26 Nov 2024 15:00:21 +0100 Subject: [PATCH] dynamic Query typing --- src/type/dynamicQueries.ts | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/type/dynamicQueries.ts diff --git a/src/type/dynamicQueries.ts b/src/type/dynamicQueries.ts new file mode 100644 index 0000000..70d4158 --- /dev/null +++ b/src/type/dynamicQueries.ts @@ -0,0 +1,60 @@ +export interface DynamicQueryStructure { + select: string[] | "*"; + table: string; + where?: Partial; + join?: Array; + orderBy?: { [key: string]: "ASC" | "DESC" }; +} + +export type ConditionStructure = { + [opt in WhereOptions]: Partial | { [column: string]: Partial }; +}; + +export type ConditionOperation = { + [op in WhereOperation]: FieldType | Array | { start: FieldType; end: FieldType }; +}; + +export type FieldType = number | string | Date | boolean; + +export type WhereOptions = "OR" | "AND"; + +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 + +const exampleQuery: DynamicQueryStructure = { + select: ["firstname", "lastname"], + table: "member", + where: { + AND: { + mail: { eq: 1 }, + OR: { + firstname: { eq: "hi" }, + lastname: { eq: "ho" }, + }, + }, + }, + join: [ + { + select: "*", + table: "adress", + }, + ], + orderBy: { + firstname: "ASC", + lastname: "ASC", + }, +};