budibase/packages/server/src/api/controllers/row/utils.ts

60 lines
1.6 KiB
TypeScript

import { InternalTables } from "../../../db/utils"
import * as userController from "../user"
import { context } from "@budibase/backend-core"
import { Ctx, Row, UserCtx } from "@budibase/types"
import validateJs from "validate.js"
validateJs.extend(validateJs.validators.datetime, {
parse: function (value: string) {
return new Date(value).getTime()
},
// Input is a unix timestamp
format: function (value: string) {
return new Date(value).toISOString()
},
})
export async function findRow(ctx: UserCtx, tableId: string, rowId: string) {
const db = context.getAppDB()
let row: Row
// TODO remove special user case in future
if (tableId === InternalTables.USER_METADATA) {
ctx.params = {
id: rowId,
}
await userController.findMetadata(ctx)
row = ctx.body
} else {
row = await db.get(rowId)
}
if (row.tableId !== tableId) {
throw "Supplied tableId does not match the rows tableId"
}
return row
}
export function getTableId(ctx: Ctx): string {
// top priority, use the URL first
if (ctx.params?.sourceId) {
return ctx.params.sourceId
}
// now check for old way of specifying table ID
if (ctx.params?.tableId) {
return ctx.params.tableId
}
// check body for a table ID
if (ctx.request.body?.tableId) {
return ctx.request.body.tableId
}
// now check if a specific view name
if (ctx.params?.viewName) {
return ctx.params.viewName
}
throw new Error("Unable to find table ID in request")
}
export function isUserMetadataTable(tableId: string) {
return tableId === InternalTables.USER_METADATA
}