Merge branch 'master' into features/per-user-per-creator-realease2

This commit is contained in:
jvcalderon 2023-10-18 14:04:24 +02:00
commit 362ac27422
6 changed files with 44 additions and 47 deletions

View File

@ -280,17 +280,8 @@ function isEditableColumn(column: FieldSchema) {
return !(isExternalAutoColumn || isFormula)
}
export type ExternalRequestReturnType<T> = T extends Operation.READ
?
| Row[]
| {
row: Row
table: Table
}
: {
row: Row
table: Table
}
export type ExternalRequestReturnType<T extends Operation> =
T extends Operation.READ ? Row[] : { row: Row; table: Table }
export class ExternalRequest<T extends Operation> {
private readonly operation: T
@ -857,11 +848,12 @@ export class ExternalRequest<T extends Operation> {
}
const output = this.outputProcessing(response, table, relationships)
// if reading it'll just be an array of rows, return whole thing
const result = (
operation === Operation.READ && Array.isArray(response)
? output
: { row: output[0], table }
if (operation === Operation.READ) {
return (
Array.isArray(output) ? output : [output]
) as ExternalRequestReturnType<T>
return result
} else {
return { row: output[0], table } as ExternalRequestReturnType<T>
}
}
}

View File

@ -44,7 +44,7 @@ export async function handleRequest<T extends Operation>(
return [] as any
}
return new ExternalRequest(operation, tableId, opts?.datasource).run(
return new ExternalRequest<T>(operation, tableId, opts?.datasource).run(
opts || {}
)
}
@ -148,17 +148,17 @@ export async function find(ctx: UserCtx): Promise<Row> {
export async function destroy(ctx: UserCtx) {
const tableId = utils.getTableId(ctx)
const _id = ctx.request.body._id
const { row } = (await handleRequest(Operation.DELETE, tableId, {
const { row } = await handleRequest(Operation.DELETE, tableId, {
id: breakRowIdField(_id),
includeSqlRelationships: IncludeRelationship.EXCLUDE,
})) as { row: Row }
})
return { response: { ok: true, id: _id }, row }
}
export async function bulkDestroy(ctx: UserCtx) {
const { rows } = ctx.request.body
const tableId = utils.getTableId(ctx)
let promises: Promise<Row[] | { row: Row; table: Table }>[] = []
let promises: Promise<{ row: Row; table: Table }>[] = []
for (let row of rows) {
promises.push(
handleRequest(Operation.DELETE, tableId, {
@ -167,7 +167,7 @@ export async function bulkDestroy(ctx: UserCtx) {
})
)
}
const responses = (await Promise.all(promises)) as { row: Row }[]
const responses = await Promise.all(promises)
return { response: { ok: true }, rows: responses.map(resp => resp.row) }
}
@ -183,11 +183,11 @@ export async function fetchEnrichedRow(ctx: UserCtx) {
ctx.throw(400, "Datasource has not been configured for plus API.")
}
const tables = datasource.entities
const response = (await handleRequest(Operation.READ, tableId, {
const response = await handleRequest(Operation.READ, tableId, {
id,
datasource,
includeSqlRelationships: IncludeRelationship.INCLUDE,
})) as Row[]
})
const table: Table = tables[tableName]
const row = response[0]
// this seems like a lot of work, but basically we need to dig deeper for the enrich

View File

@ -7,11 +7,11 @@ export async function getRow(
rowId: string,
opts?: { relationships?: boolean }
) {
const response = (await handleRequest(Operation.READ, tableId, {
const response = await handleRequest(Operation.READ, tableId, {
id: breakRowIdField(rowId),
includeSqlRelationships: opts?.relationships
? IncludeRelationship.INCLUDE
: IncludeRelationship.EXCLUDE,
})) as Row[]
})
return response ? response[0] : response
}

View File

@ -1,4 +1,4 @@
import { SearchFilters, SearchParams } from "@budibase/types"
import { SearchFilters, SearchParams, Row } from "@budibase/types"
import { isExternalTable } from "../../../integrations/utils"
import * as internal from "./search/internal"
import * as external from "./search/external"
@ -45,7 +45,7 @@ export async function exportRows(
return pickApi(options.tableId).exportRows(options)
}
export async function fetch(tableId: string) {
export async function fetch(tableId: string): Promise<Row[]> {
return pickApi(tableId).fetch(tableId)
}
@ -53,6 +53,6 @@ export async function fetchView(
tableId: string,
viewName: string,
params: ViewParams
) {
): Promise<Row[]> {
return pickApi(tableId).fetchView(viewName, params)
}

View File

@ -55,15 +55,15 @@ export async function search(options: SearchParams) {
try {
const table = await sdk.tables.getTable(tableId)
options = searchInputMapping(table, options)
let rows = (await handleRequest(Operation.READ, tableId, {
let rows = await handleRequest(Operation.READ, tableId, {
filters: query,
sort,
paginate: paginateObj as PaginationJson,
includeSqlRelationships: IncludeRelationship.INCLUDE,
})) as Row[]
})
let hasNextPage = false
if (paginate && rows.length === limit) {
const nextRows = (await handleRequest(Operation.READ, tableId, {
const nextRows = await handleRequest(Operation.READ, tableId, {
filters: query,
sort,
paginate: {
@ -71,7 +71,7 @@ export async function search(options: SearchParams) {
page: bookmark! * limit + 1,
},
includeSqlRelationships: IncludeRelationship.INCLUDE,
})) as Row[]
})
hasNextPage = nextRows.length > 0
}
@ -172,12 +172,18 @@ export async function exportRows(
}
}
export async function fetch(tableId: string) {
const response = await handleRequest(Operation.READ, tableId, {
export async function fetch(tableId: string): Promise<Row[]> {
const response = await handleRequest<Operation.READ>(
Operation.READ,
tableId,
{
includeSqlRelationships: IncludeRelationship.INCLUDE,
})
}
)
const table = await sdk.tables.getTable(tableId)
return await outputProcessing(table, response, { preserveLinks: true })
return await outputProcessing<Row[]>(table, response, {
preserveLinks: true,
})
}
export async function fetchView(viewName: string) {

View File

@ -6,26 +6,26 @@ import {
import env from "../../../../environment"
import { fullSearch, paginatedSearch } from "./internalSearch"
import {
InternalTables,
getRowParams,
DocumentType,
getRowParams,
InternalTables,
} from "../../../../db/utils"
import { getGlobalUsersFromMetadata } from "../../../../utilities/global"
import { outputProcessing } from "../../../../utilities/rowProcessor"
import { Database, Row, Table, SearchParams } from "@budibase/types"
import { Database, Row, SearchParams, Table } from "@budibase/types"
import { cleanExportRows } from "../utils"
import {
Format,
csv,
Format,
json,
jsonWithSchema,
} from "../../../../api/controllers/view/exporters"
import * as inMemoryViews from "../../../../db/inMemoryView"
import {
migrateToInMemoryView,
migrateToDesignView,
getFromDesignDoc,
getFromMemoryDoc,
migrateToDesignView,
migrateToInMemoryView,
} from "../../../../api/controllers/view/utils"
import sdk from "../../../../sdk"
import { ExportRowsParams, ExportRowsResult } from "../search"
@ -139,13 +139,12 @@ export async function exportRows(
}
}
export async function fetch(tableId: string) {
export async function fetch(tableId: string): Promise<Row[]> {
const db = context.getAppDB()
const table = await sdk.tables.getTable(tableId)
const rows = await getRawTableData(db, tableId)
const result = await outputProcessing(table, rows)
return result
return await outputProcessing(table, rows)
}
async function getRawTableData(db: Database, tableId: string) {