From 1bd8bdf84c285e72dd57733a71b1b99146db3f47 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 17 Jul 2023 15:29:41 +0200 Subject: [PATCH] Clean ctx from fetchview --- .../server/src/api/controllers/row/index.ts | 18 +++++++++++++++--- .../server/src/api/controllers/view/index.ts | 5 ++++- packages/server/src/sdk/app/rows/search.ts | 14 ++++++++++++-- .../server/src/sdk/app/rows/search/external.ts | 4 ++-- .../server/src/sdk/app/rows/search/internal.ts | 10 +++++----- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/packages/server/src/api/controllers/row/index.ts b/packages/server/src/api/controllers/row/index.ts index a05c640c33..ed441f7784 100644 --- a/packages/server/src/api/controllers/row/index.ts +++ b/packages/server/src/api/controllers/row/index.ts @@ -65,9 +65,21 @@ export const save = async (ctx: any) => { } export async function fetchView(ctx: any) { const tableId = utils.getTableId(ctx) - ctx.body = await quotas.addQuery(() => sdk.rows.fetchView(tableId, ctx), { - datasourceId: tableId, - }) + const viewName = decodeURIComponent(ctx.params.viewName) + + const { calculation, group, field } = ctx.query + + ctx.body = await quotas.addQuery( + () => + sdk.rows.fetchView(tableId, viewName, { + calculation, + group, + field, + }), + { + datasourceId: tableId, + } + ) } export async function fetch(ctx: any) { diff --git a/packages/server/src/api/controllers/view/index.ts b/packages/server/src/api/controllers/view/index.ts index 83fbe7a17b..a087634292 100644 --- a/packages/server/src/api/controllers/view/index.ts +++ b/packages/server/src/api/controllers/view/index.ts @@ -162,7 +162,10 @@ export async function exportView(ctx: Ctx) { let rows = ctx.body as Row[] let schema: TableSchema = view && view.meta && view.meta.schema - const tableId = ctx.params.tableId || view.meta.tableId + const tableId = + ctx.params.tableId || + view?.meta?.tableId || + (viewName.startsWith(DocumentType.TABLE) && viewName) const table: Table = await sdk.tables.getTable(tableId) if (!schema) { schema = table.schema diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index 56dea32025..f8f50df2e6 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -14,6 +14,12 @@ export interface SearchParams { sortType?: string } +export interface ViewParams { + calculation: string + group: string + field: string +} + function pickApi(tableId: any) { if (isExternalTable(tableId)) { return external @@ -33,6 +39,10 @@ export async function fetch(tableId: string) { return pickApi(tableId).fetch(tableId) } -export async function fetchView(tableId: string, ctx: Ctx) { - return pickApi(tableId).fetchView(ctx) +export async function fetchView( + tableId: string, + viewName: string, + params: ViewParams +) { + return pickApi(tableId).fetchView(viewName, params) } diff --git a/packages/server/src/sdk/app/rows/search/external.ts b/packages/server/src/sdk/app/rows/search/external.ts index abed06eb31..e6d910b1c9 100644 --- a/packages/server/src/sdk/app/rows/search/external.ts +++ b/packages/server/src/sdk/app/rows/search/external.ts @@ -167,10 +167,10 @@ export async function fetch(tableId: string) { }) } -export async function fetchView(ctx: Ctx) { +export async function fetchView(viewName: string) { // there are no views in external datasources, shouldn't ever be called // for now just fetch - const split = ctx.params.viewName.split("all_") + const split = viewName.split("all_") const tableId = split[1] ? split[1] : split[0] return fetch(tableId) } diff --git a/packages/server/src/sdk/app/rows/search/internal.ts b/packages/server/src/sdk/app/rows/search/internal.ts index d3d15deb53..7caa5083c9 100644 --- a/packages/server/src/sdk/app/rows/search/internal.ts +++ b/packages/server/src/sdk/app/rows/search/internal.ts @@ -146,17 +146,17 @@ async function getRawTableData(db: Database, tableId: string) { return rows as Row[] } -export async function fetchView(ctx: Ctx) { - const viewName = decodeURIComponent(ctx.params.viewName) - +export async function fetchView( + viewName: string, + options: { calculation: string; group: string; field: string } +) { // if this is a table view being looked for just transfer to that if (viewName.startsWith(DocumentType.TABLE)) { - ctx.params.tableId = viewName return fetch(viewName) } const db = context.getAppDB() - const { calculation, group, field } = ctx.query + const { calculation, group, field } = options const viewInfo = await getView(db, viewName) let response if (env.SELF_HOSTED) {