Mutate in middleware, don't update reference.
This commit is contained in:
parent
97e5fe37a1
commit
3b7a5c0392
|
@ -23,12 +23,7 @@ export default async (ctx: Ctx<Row>, next: Next) => {
|
|||
|
||||
// don't need to trim delete requests
|
||||
if (ctx?.method?.toLowerCase() !== "delete") {
|
||||
const { _viewId, ...trimmedView } = await trimViewFields(
|
||||
viewId,
|
||||
tableId,
|
||||
body
|
||||
)
|
||||
ctx.request.body = trimmedView
|
||||
await trimViewFields(ctx.request.body, viewId, tableId)
|
||||
}
|
||||
|
||||
ctx.params.sourceId = tableId
|
||||
|
@ -36,26 +31,29 @@ export default async (ctx: Ctx<Row>, next: Next) => {
|
|||
return next()
|
||||
}
|
||||
|
||||
// have to mutate the koa context, can't return
|
||||
export async function trimViewFields<T extends Row>(
|
||||
body: Row,
|
||||
viewId: string,
|
||||
tableId: string,
|
||||
data: T
|
||||
): Promise<T> {
|
||||
tableId: string
|
||||
): Promise<void> {
|
||||
const view = await sdk.views.get(viewId)
|
||||
if (!view?.schema || !Object.keys(view.schema).length) {
|
||||
return data
|
||||
return
|
||||
}
|
||||
|
||||
const table = await sdk.tables.getTable(tableId)
|
||||
const { schema } = sdk.views.enrichSchema(view!, table.schema)
|
||||
const result: Record<string, any> = {}
|
||||
for (const key of [
|
||||
const allowedKeys = [
|
||||
...Object.keys(schema),
|
||||
...db.CONSTANT_EXTERNAL_ROW_COLS,
|
||||
...db.CONSTANT_INTERNAL_ROW_COLS,
|
||||
]) {
|
||||
result[key] = data[key] !== null ? data[key] : undefined
|
||||
]
|
||||
// have to mutate the context, can't update reference
|
||||
const toBeRemoved = Object.keys(body).filter(
|
||||
key => !allowedKeys.includes(key)
|
||||
)
|
||||
for (let removeKey of toBeRemoved) {
|
||||
delete body[removeKey]
|
||||
}
|
||||
|
||||
return result as T
|
||||
}
|
||||
|
|
|
@ -1,9 +1,4 @@
|
|||
import {
|
||||
RenameColumn,
|
||||
TableSchema,
|
||||
View,
|
||||
ViewV2,
|
||||
} from "@budibase/types"
|
||||
import { RenameColumn, TableSchema, View, ViewV2 } from "@budibase/types"
|
||||
import { context, HTTPError } from "@budibase/backend-core"
|
||||
import { cloneDeep } from "lodash"
|
||||
|
||||
|
|
Loading…
Reference in New Issue