More types

This commit is contained in:
Adria Navarro 2023-07-26 13:53:35 +02:00
parent f0f68f10d8
commit af933bd158
5 changed files with 33 additions and 22 deletions

View File

@ -8,6 +8,8 @@ import {
Datasource, Datasource,
IncludeRelationship, IncludeRelationship,
Operation, Operation,
PatchRowRequest,
PatchRowResponse,
Row, Row,
Table, Table,
UserCtx, UserCtx,
@ -55,14 +57,12 @@ export async function handleRequest(
) )
} }
export async function patch(ctx: UserCtx) { export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
const inputs = ctx.request.body
const tableId = ctx.params.tableId const tableId = ctx.params.tableId
const id = inputs._id const { id, ...rowData } = ctx.request.body
// don't save the ID to db
delete inputs._id
const validateResult = await utils.validate({ const validateResult = await utils.validate({
row: inputs, row: rowData,
tableId, tableId,
}) })
if (!validateResult.valid) { if (!validateResult.valid) {
@ -70,7 +70,7 @@ export async function patch(ctx: UserCtx) {
} }
const response = await handleRequest(Operation.UPDATE, tableId, { const response = await handleRequest(Operation.UPDATE, tableId, {
id: breakRowIdField(id), id: breakRowIdField(id),
row: inputs, row: rowData,
}) })
const row = await getRow(tableId, id, { relationships: true }) const row = await getRow(tableId, id, { relationships: true })
const table = await sdk.tables.getTable(tableId) const table = await sdk.tables.getTable(tableId)

View File

@ -15,10 +15,17 @@ import * as utils from "./utils"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import { context, db as dbCore } from "@budibase/backend-core" import { context, db as dbCore } from "@budibase/backend-core"
import { finaliseRow, updateRelatedFormula } from "./staticFormula" import { finaliseRow, updateRelatedFormula } from "./staticFormula"
import { UserCtx, LinkDocumentValue, Row, Table } from "@budibase/types" import {
UserCtx,
LinkDocumentValue,
Row,
Table,
PatchRowRequest,
PatchRowResponse,
} from "@budibase/types"
import sdk from "../../../sdk" import sdk from "../../../sdk"
export async function patch(ctx: UserCtx) { export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
const inputs = ctx.request.body const inputs = ctx.request.body
const tableId = inputs.tableId const tableId = inputs.tableId
const isUserTable = tableId === InternalTables.USER_METADATA const isUserTable = tableId === InternalTables.USER_METADATA
@ -27,7 +34,7 @@ export async function patch(ctx: UserCtx) {
let dbTable = await sdk.tables.getTable(tableId) let dbTable = await sdk.tables.getTable(tableId)
oldRow = await outputProcessing( oldRow = await outputProcessing(
dbTable, dbTable,
await utils.findRow(ctx, tableId, inputs._id) await utils.findRow(ctx, tableId, inputs._id!)
) )
} catch (err) { } catch (err) {
if (isUserTable) { if (isUserTable) {
@ -74,7 +81,7 @@ export async function patch(ctx: UserCtx) {
if (isUserTable) { if (isUserTable) {
// the row has been updated, need to put it into the ctx // the row has been updated, need to put it into the ctx
ctx.request.body = row ctx.request.body = row as any
await userController.updateMetadata(ctx) await userController.updateMetadata(ctx)
return { row: ctx.body as Row, table } return { row: ctx.body as Row, table }
} }

View File

@ -1,6 +1,5 @@
import { generateUserMetadataID, generateUserFlagID } from "../../db/utils" import { generateUserFlagID } from "../../db/utils"
import { InternalTables } from "../../db/utils" import { InternalTables } from "../../db/utils"
import { getGlobalUsers } from "../../utilities/global"
import { getFullUser } from "../../utilities/users" import { getFullUser } from "../../utilities/users"
import { context } from "@budibase/backend-core" import { context } from "@budibase/backend-core"
import { Ctx, UserCtx } from "@budibase/types" import { Ctx, UserCtx } from "@budibase/types"

View File

@ -16,6 +16,7 @@ import {
FieldType, FieldType,
SortType, SortType,
SortOrder, SortOrder,
PatchRowRequest,
} from "@budibase/types" } from "@budibase/types"
import { import {
expectAnyInternalColsAttributes, expectAnyInternalColsAttributes,
@ -400,9 +401,9 @@ describe("/rows", () => {
const queryUsage = await getQueryUsage() const queryUsage = await getQueryUsage()
const res = await config.api.row.patch(table._id!, { const res = await config.api.row.patch(table._id!, {
_id: existing._id, _id: existing._id!,
_rev: existing._rev, _rev: existing._rev!,
tableId: table._id, tableId: table._id!,
name: "Updated Name", name: "Updated Name",
}) })
@ -428,9 +429,9 @@ describe("/rows", () => {
await config.api.row.patch( await config.api.row.patch(
table._id!, table._id!,
{ {
_id: existing._id, _id: existing._id!,
_rev: existing._rev, _rev: existing._rev!,
tableId: table._id, tableId: table._id!,
name: 1, name: 1,
}, },
{ expectStatus: 400 } { expectStatus: 400 }
@ -451,7 +452,7 @@ describe("/rows", () => {
const [row] = searchResponse.body.rows as Row[] const [row] = searchResponse.body.rows as Row[]
const res = await config.api.row.patch(table._id!, { const res = await config.api.row.patch(table._id!, {
...row, ...(row as PatchRowRequest),
name: "Updated Name", name: "Updated Name",
description: "Updated Description", description: "Updated Description",
}) })
@ -478,7 +479,7 @@ describe("/rows", () => {
const [row] = searchResponse.body.rows as Row[] const [row] = searchResponse.body.rows as Row[]
const res = await config.api.row.patch(table._id!, { const res = await config.api.row.patch(table._id!, {
...row, ...(row as PatchRowRequest),
name: "Updated Name", name: "Updated Name",
description: "Updated Description", description: "Updated Description",
}) })

View File

@ -1,6 +1,10 @@
import { Row } from "../../../documents" import { Row } from "../../../documents"
export interface PatchRowRequest extends Row {} export interface PatchRowRequest extends Row {
_id: string
_rev: string
tableId: string
}
export interface PatchRowResponse extends Row {} export interface PatchRowResponse extends Row {}