Clean ctx from fetchview

This commit is contained in:
Adria Navarro 2023-07-17 15:29:41 +02:00
parent e7f1bcab9e
commit 1bd8bdf84c
5 changed files with 38 additions and 13 deletions

View File

@ -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) {

View File

@ -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

View File

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

View File

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

View File

@ -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) {