error pass and select fix

This commit is contained in:
Julian Krauser 2024-12-16 13:56:22 +01:00
parent 7497787ae4
commit ac5a7e7427
2 changed files with 28 additions and 9 deletions

View file

@ -37,12 +37,20 @@ export async function executeQuery(req: Request, res: Response): Promise<any> {
let count = parseInt((req.query.count as string) ?? "25"); let count = parseInt((req.query.count as string) ?? "25");
const query = req.body.query; const query = req.body.query;
let [rows, total] = await DynamicQueryBuilder.buildQuery(query, offset, count).getManyAndCount(); try {
let [rows, total] = await DynamicQueryBuilder.buildQuery(query, offset, count).getManyAndCount();
res.json({ res.json({
rows: rows, rows: rows,
total: total, total: total,
offset: offset, offset: offset,
count: count, count: count,
}); });
} catch (error) {
res.status(500).send({
json: error.sql,
code: error.code,
msg: error.sqlMessage,
});
}
} }

View file

@ -67,11 +67,22 @@ export default abstract class DynamicQueryBuilder {
depth: number = 0 depth: number = 0
): void { ): void {
const alias = queryObject.table + "_" + depth; const alias = queryObject.table + "_" + depth;
let firstSelect = true;
let selects: Array<string> = [];
if (queryObject.select == "*") { if (queryObject.select == "*") {
query.addSelect(`${alias}.*`); let meta = this.getTableMeta(queryObject.table);
let relCols = meta.relations.map((r) => r.column);
selects = meta.columns.map((c) => c.column).filter((c) => !relCols.includes(c));
} else { } else {
for (const select of queryObject.select) { selects = queryObject.select;
}
for (const select of selects) {
if (firstSelect) {
query.select(`${alias}.${select}`);
firstSelect = false;
} else {
query.addSelect(`${alias}.${select}`); query.addSelect(`${alias}.${select}`);
} }
} }