get table definition
This commit is contained in:
parent
717de68f4e
commit
9944fb931a
2 changed files with 40 additions and 5 deletions
34
src/helpers/dynamicQueryBuilder.ts
Normal file
34
src/helpers/dynamicQueryBuilder.ts
Normal 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,
|
||||
})),
|
||||
};
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@ export interface DynamicQueryStructure {
|
|||
select: string[] | "*";
|
||||
table: string;
|
||||
where?: Partial<ConditionStructure>;
|
||||
join?: Array<DynamicQueryStructure>;
|
||||
join?: Array<DynamicQueryStructure & { foreignColumn: string }>;
|
||||
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: {
|
||||
|
|
Loading…
Reference in a new issue