get table definition

This commit is contained in:
Julian Krauser 2024-11-27 15:01:31 +01:00
parent 717de68f4e
commit 9944fb931a
2 changed files with 40 additions and 5 deletions

View file

@ -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,
})),
};
}
}

View file

@ -2,7 +2,7 @@ export interface DynamicQueryStructure {
select: string[] | "*"; select: string[] | "*";
table: string; table: string;
where?: Partial<ConditionStructure>; where?: Partial<ConditionStructure>;
join?: Array<DynamicQueryStructure>; join?: Array<DynamicQueryStructure & { foreignColumn: string }>;
orderBy?: { [key: string]: "ASC" | "DESC" }; orderBy?: { [key: string]: "ASC" | "DESC" };
} }
@ -40,17 +40,18 @@ const exampleQuery: DynamicQueryStructure = {
table: "member", table: "member",
where: { where: {
AND: { AND: {
mail: { eq: 1 }, mail: { endsWith: "@gmail.com" },
OR: { OR: {
firstname: { eq: "hi" }, firstname: { startsWith: "J" },
lastname: { eq: "ho" }, lastname: { endsWith: "K" },
}, },
}, },
}, },
join: [ join: [
{ {
select: "*", select: "*",
table: "adress", table: "communication",
foreignColumn: "sendNewsletter",
}, },
], ],
orderBy: { orderBy: {