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) { export async function fetchView(ctx: any) {
const tableId = utils.getTableId(ctx) const tableId = utils.getTableId(ctx)
ctx.body = await quotas.addQuery(() => sdk.rows.fetchView(tableId, ctx), { const viewName = decodeURIComponent(ctx.params.viewName)
datasourceId: tableId,
}) 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) { 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 rows = ctx.body as Row[]
let schema: TableSchema = view && view.meta && view.meta.schema 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) const table: Table = await sdk.tables.getTable(tableId)
if (!schema) { if (!schema) {
schema = table.schema schema = table.schema

View File

@ -14,6 +14,12 @@ export interface SearchParams {
sortType?: string sortType?: string
} }
export interface ViewParams {
calculation: string
group: string
field: string
}
function pickApi(tableId: any) { function pickApi(tableId: any) {
if (isExternalTable(tableId)) { if (isExternalTable(tableId)) {
return external return external
@ -33,6 +39,10 @@ export async function fetch(tableId: string) {
return pickApi(tableId).fetch(tableId) return pickApi(tableId).fetch(tableId)
} }
export async function fetchView(tableId: string, ctx: Ctx) { export async function fetchView(
return pickApi(tableId).fetchView(ctx) 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 // there are no views in external datasources, shouldn't ever be called
// for now just fetch // for now just fetch
const split = ctx.params.viewName.split("all_") const split = viewName.split("all_")
const tableId = split[1] ? split[1] : split[0] const tableId = split[1] ? split[1] : split[0]
return fetch(tableId) return fetch(tableId)
} }

View File

@ -146,17 +146,17 @@ async function getRawTableData(db: Database, tableId: string) {
return rows as Row[] return rows as Row[]
} }
export async function fetchView(ctx: Ctx) { export async function fetchView(
const viewName = decodeURIComponent(ctx.params.viewName) viewName: string,
options: { calculation: string; group: string; field: string }
) {
// if this is a table view being looked for just transfer to that // if this is a table view being looked for just transfer to that
if (viewName.startsWith(DocumentType.TABLE)) { if (viewName.startsWith(DocumentType.TABLE)) {
ctx.params.tableId = viewName
return fetch(viewName) return fetch(viewName)
} }
const db = context.getAppDB() const db = context.getAppDB()
const { calculation, group, field } = ctx.query const { calculation, group, field } = options
const viewInfo = await getView(db, viewName) const viewInfo = await getView(db, viewName)
let response let response
if (env.SELF_HOSTED) { if (env.SELF_HOSTED) {