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
|
// don't need to trim delete requests
|
||||||
if (ctx?.method?.toLowerCase() !== "delete") {
|
if (ctx?.method?.toLowerCase() !== "delete") {
|
||||||
const { _viewId, ...trimmedView } = await trimViewFields(
|
await trimViewFields(ctx.request.body, viewId, tableId)
|
||||||
viewId,
|
|
||||||
tableId,
|
|
||||||
body
|
|
||||||
)
|
|
||||||
ctx.request.body = trimmedView
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.params.sourceId = tableId
|
ctx.params.sourceId = tableId
|
||||||
|
@ -36,26 +31,29 @@ export default async (ctx: Ctx<Row>, next: Next) => {
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// have to mutate the koa context, can't return
|
||||||
export async function trimViewFields<T extends Row>(
|
export async function trimViewFields<T extends Row>(
|
||||||
|
body: Row,
|
||||||
viewId: string,
|
viewId: string,
|
||||||
tableId: string,
|
tableId: string
|
||||||
data: T
|
): Promise<void> {
|
||||||
): Promise<T> {
|
|
||||||
const view = await sdk.views.get(viewId)
|
const view = await sdk.views.get(viewId)
|
||||||
if (!view?.schema || !Object.keys(view.schema).length) {
|
if (!view?.schema || !Object.keys(view.schema).length) {
|
||||||
return data
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const table = await sdk.tables.getTable(tableId)
|
const table = await sdk.tables.getTable(tableId)
|
||||||
const { schema } = sdk.views.enrichSchema(view!, table.schema)
|
const { schema } = sdk.views.enrichSchema(view!, table.schema)
|
||||||
const result: Record<string, any> = {}
|
const allowedKeys = [
|
||||||
for (const key of [
|
|
||||||
...Object.keys(schema),
|
...Object.keys(schema),
|
||||||
...db.CONSTANT_EXTERNAL_ROW_COLS,
|
...db.CONSTANT_EXTERNAL_ROW_COLS,
|
||||||
...db.CONSTANT_INTERNAL_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 {
|
import { RenameColumn, TableSchema, View, ViewV2 } from "@budibase/types"
|
||||||
RenameColumn,
|
|
||||||
TableSchema,
|
|
||||||
View,
|
|
||||||
ViewV2,
|
|
||||||
} from "@budibase/types"
|
|
||||||
import { context, HTTPError } from "@budibase/backend-core"
|
import { context, HTTPError } from "@budibase/backend-core"
|
||||||
import { cloneDeep } from "lodash"
|
import { cloneDeep } from "lodash"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue