From 9944fb931a6361ce4af90243194a97274d6c6394 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Wed, 27 Nov 2024 15:01:31 +0100 Subject: [PATCH] get table definition --- src/helpers/dynamicQueryBuilder.ts | 34 ++++++++++++++++++++++++++++++ src/type/dynamicQueries.ts | 11 +++++----- 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/helpers/dynamicQueryBuilder.ts diff --git a/src/helpers/dynamicQueryBuilder.ts b/src/helpers/dynamicQueryBuilder.ts new file mode 100644 index 0000000..ebd9e7a --- /dev/null +++ b/src/helpers/dynamicQueryBuilder.ts @@ -0,0 +1,34 @@ +import { dataSource } from "../data-source"; +import { DynamicQueryStructure } from "../type/dynamicQueries"; + +export default abstract class DynamicQueryBuilder { + public static buildQuery(query: DynamicQueryStructure) {} + + // use switch... for compare functions + // use NotBrackets/Brackets for nested conditions + // use joins by requesting table schema and setting correct column + + public static getTableMeta(tableName: string) { + let { name, columns, relations } = dataSource.getMetadata(tableName); + + const uniqueColumns = columns.map((c) => ({ column: c.propertyName, type: c.type })); + + return { + tableName: name, + columns: [ + ...uniqueColumns, + ...relations + .filter((r) => !uniqueColumns.some((c) => r.propertyName == c.column)) + .map((r) => ({ + column: r.propertyName, + type: r.inverseEntityMetadata?.columns.find((col) => col.propertyName === r.inverseSidePropertyPath)?.type, + })), + ], + relations: relations.map((r) => ({ + column: r.propertyName, + relationType: r.relationType, + referencedTableName: r.inverseEntityMetadata?.tableName, + })), + }; + } +} diff --git a/src/type/dynamicQueries.ts b/src/type/dynamicQueries.ts index 70d4158..a8c78a9 100644 --- a/src/type/dynamicQueries.ts +++ b/src/type/dynamicQueries.ts @@ -2,7 +2,7 @@ export interface DynamicQueryStructure { select: string[] | "*"; table: string; where?: Partial; - join?: Array; + join?: Array; orderBy?: { [key: string]: "ASC" | "DESC" }; } @@ -40,17 +40,18 @@ const exampleQuery: DynamicQueryStructure = { table: "member", where: { AND: { - mail: { eq: 1 }, + mail: { endsWith: "@gmail.com" }, OR: { - firstname: { eq: "hi" }, - lastname: { eq: "ho" }, + firstname: { startsWith: "J" }, + lastname: { endsWith: "K" }, }, }, }, join: [ { select: "*", - table: "adress", + table: "communication", + foreignColumn: "sendNewsletter", }, ], orderBy: {