Types
This commit is contained in:
parent
1d1ae341b1
commit
2c6725404f
|
@ -12,6 +12,7 @@ import {
|
||||||
SearchResponse,
|
SearchResponse,
|
||||||
SortOrder,
|
SortOrder,
|
||||||
SortType,
|
SortType,
|
||||||
|
UserCtx,
|
||||||
ViewV2,
|
ViewV2,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import * as utils from "./utils"
|
import * as utils from "./utils"
|
||||||
|
@ -29,7 +30,7 @@ function pickApi(tableId: any) {
|
||||||
return internal
|
return internal
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function patch(ctx: any): Promise<any> {
|
export async function patch(ctx: UserCtx): Promise<any> {
|
||||||
const appId = ctx.appId
|
const appId = ctx.appId
|
||||||
const tableId = utils.getTableId(ctx)
|
const tableId = utils.getTableId(ctx)
|
||||||
const body = ctx.request.body
|
const body = ctx.request.body
|
||||||
|
@ -53,7 +54,7 @@ export async function patch(ctx: any): Promise<any> {
|
||||||
ctx.message = `${table.name} updated successfully.`
|
ctx.message = `${table.name} updated successfully.`
|
||||||
ctx.body = row
|
ctx.body = row
|
||||||
gridSocket?.emitRowUpdate(ctx, row)
|
gridSocket?.emitRowUpdate(ctx, row)
|
||||||
} catch (err) {
|
} catch (err: any) {
|
||||||
ctx.throw(400, err)
|
ctx.throw(400, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,6 +79,7 @@ export const save = async (ctx: any) => {
|
||||||
ctx.body = row || squashed
|
ctx.body = row || squashed
|
||||||
gridSocket?.emitRowUpdate(ctx, row || squashed)
|
gridSocket?.emitRowUpdate(ctx, row || squashed)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchView(ctx: any) {
|
export async function fetchView(ctx: any) {
|
||||||
const tableId = utils.getTableId(ctx)
|
const tableId = utils.getTableId(ctx)
|
||||||
const viewName = decodeURIComponent(ctx.params.viewName)
|
const viewName = decodeURIComponent(ctx.params.viewName)
|
||||||
|
|
|
@ -147,8 +147,8 @@ export async function exportRows(
|
||||||
export async function fetch(tableId: string) {
|
export async function fetch(tableId: string) {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
|
|
||||||
let table = await sdk.tables.getTable(tableId)
|
const table = await sdk.tables.getTable(tableId)
|
||||||
let rows = await getRawTableData(db, tableId)
|
const rows = await getRawTableData(db, tableId)
|
||||||
const result = await outputProcessing(table, rows)
|
const result = await outputProcessing(table, rows)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
@ -171,7 +171,7 @@ async function getRawTableData(db: Database, tableId: string) {
|
||||||
export async function fetchView(
|
export async function fetchView(
|
||||||
viewName: string,
|
viewName: string,
|
||||||
options: { calculation: string; group: string; field: string }
|
options: { calculation: string; group: string; field: string }
|
||||||
) {
|
): Promise<Row[]> {
|
||||||
// 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)) {
|
||||||
return fetch(viewName)
|
return fetch(viewName)
|
||||||
|
@ -197,7 +197,7 @@ export async function fetchView(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
let rows
|
let rows: Row[] = []
|
||||||
if (!calculation) {
|
if (!calculation) {
|
||||||
response.rows = response.rows.map(row => row.doc)
|
response.rows = response.rows.map(row => row.doc)
|
||||||
let table: Table
|
let table: Table
|
||||||
|
|
|
@ -186,18 +186,21 @@ export function inputProcessing(
|
||||||
* @param {object} opts used to set some options for the output, such as disabling relationship squashing.
|
* @param {object} opts used to set some options for the output, such as disabling relationship squashing.
|
||||||
* @returns {object[]|object} the enriched rows will be returned.
|
* @returns {object[]|object} the enriched rows will be returned.
|
||||||
*/
|
*/
|
||||||
export async function outputProcessing(
|
export async function outputProcessing<T extends Row[] | Row>(
|
||||||
table: Table,
|
table: Table,
|
||||||
rows: Row[] | Row,
|
rows: T,
|
||||||
opts = { squash: true }
|
opts = { squash: true }
|
||||||
) {
|
): Promise<T> {
|
||||||
|
let safeRows: Row[]
|
||||||
let wasArray = true
|
let wasArray = true
|
||||||
if (!(rows instanceof Array)) {
|
if (!(rows instanceof Array)) {
|
||||||
rows = [rows]
|
safeRows = [rows]
|
||||||
wasArray = false
|
wasArray = false
|
||||||
|
} else {
|
||||||
|
safeRows = rows
|
||||||
}
|
}
|
||||||
// attach any linked row information
|
// attach any linked row information
|
||||||
let enriched = await linkRows.attachFullLinkedDocs(table, rows as Row[])
|
let enriched = await linkRows.attachFullLinkedDocs(table, safeRows)
|
||||||
|
|
||||||
// process formulas
|
// process formulas
|
||||||
enriched = processFormulas(table, enriched, { dynamic: true }) as Row[]
|
enriched = processFormulas(table, enriched, { dynamic: true }) as Row[]
|
||||||
|
@ -221,7 +224,7 @@ export async function outputProcessing(
|
||||||
enriched
|
enriched
|
||||||
)) as Row[]
|
)) as Row[]
|
||||||
}
|
}
|
||||||
return wasArray ? enriched : enriched[0]
|
return (wasArray ? enriched : enriched[0]) as T
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue