Move row.find to sdk
This commit is contained in:
parent
66a2b29ca2
commit
75609b2a9b
|
@ -57,9 +57,7 @@ export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
|
||||||
throw { validation: validateResult.errors }
|
throw { validation: validateResult.errors }
|
||||||
}
|
}
|
||||||
|
|
||||||
const beforeRow = await sdk.rows.external.getRow(tableId, _id, {
|
const beforeRow = await sdk.rows.external.find(tableId, _id)
|
||||||
relationships: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
const response = await handleRequest(Operation.UPDATE, tableId, {
|
const response = await handleRequest(Operation.UPDATE, tableId, {
|
||||||
id: breakRowIdField(_id),
|
id: breakRowIdField(_id),
|
||||||
|
@ -69,9 +67,7 @@ export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
|
||||||
// The id might have been changed, so the refetching would fail. Recalculating the id just in case
|
// The id might have been changed, so the refetching would fail. Recalculating the id just in case
|
||||||
const updatedId =
|
const updatedId =
|
||||||
generateIdForRow({ ...beforeRow, ...dataToUpdate }, table) || _id
|
generateIdForRow({ ...beforeRow, ...dataToUpdate }, table) || _id
|
||||||
const row = await sdk.rows.external.getRow(tableId, updatedId, {
|
const row = await sdk.rows.external.find(tableId, updatedId)
|
||||||
relationships: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [enrichedRow, oldRow] = await Promise.all([
|
const [enrichedRow, oldRow] = await Promise.all([
|
||||||
outputProcessing(table, row, {
|
outputProcessing(table, row, {
|
||||||
|
@ -92,25 +88,6 @@ export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function find(ctx: UserCtx): Promise<Row> {
|
|
||||||
const id = ctx.params.rowId
|
|
||||||
const tableId = utils.getTableId(ctx)
|
|
||||||
const row = await sdk.rows.external.getRow(tableId, id, {
|
|
||||||
relationships: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!row) {
|
|
||||||
ctx.throw(404)
|
|
||||||
}
|
|
||||||
|
|
||||||
const table = await sdk.tables.getTable(tableId)
|
|
||||||
// Preserving links, as the outputProcessing does not support external rows yet and we don't need it in this use case
|
|
||||||
return await outputProcessing(table, row, {
|
|
||||||
squash: true,
|
|
||||||
preserveLinks: true,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function destroy(ctx: UserCtx) {
|
export async function destroy(ctx: UserCtx) {
|
||||||
const tableId = utils.getTableId(ctx)
|
const tableId = utils.getTableId(ctx)
|
||||||
const _id = ctx.request.body._id
|
const _id = ctx.request.body._id
|
||||||
|
|
|
@ -117,7 +117,9 @@ export async function fetch(ctx: any) {
|
||||||
|
|
||||||
export async function find(ctx: UserCtx<void, GetRowResponse>) {
|
export async function find(ctx: UserCtx<void, GetRowResponse>) {
|
||||||
const tableId = utils.getTableId(ctx)
|
const tableId = utils.getTableId(ctx)
|
||||||
ctx.body = await pickApi(tableId).find(ctx)
|
const rowId = ctx.params.rowId
|
||||||
|
|
||||||
|
ctx.body = await sdk.rows.find(tableId, rowId)
|
||||||
}
|
}
|
||||||
|
|
||||||
function isDeleteRows(input: any): input is DeleteRows {
|
function isDeleteRows(input: any): input is DeleteRows {
|
||||||
|
@ -278,7 +280,8 @@ export async function downloadAttachment(ctx: UserCtx) {
|
||||||
const { columnName } = ctx.params
|
const { columnName } = ctx.params
|
||||||
|
|
||||||
const tableId = utils.getTableId(ctx)
|
const tableId = utils.getTableId(ctx)
|
||||||
const row = await pickApi(tableId).find(ctx)
|
const rowId = ctx.params.rowId
|
||||||
|
const row = await sdk.rows.find(tableId, rowId)
|
||||||
|
|
||||||
const table = await sdk.tables.getTable(tableId)
|
const table = await sdk.tables.getTable(tableId)
|
||||||
const columnSchema = table.schema[columnName]
|
const columnSchema = table.schema[columnName]
|
||||||
|
|
|
@ -96,15 +96,6 @@ export async function patch(ctx: UserCtx<PatchRowRequest, PatchRowResponse>) {
|
||||||
return { ...result, oldRow }
|
return { ...result, oldRow }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function find(ctx: UserCtx): Promise<Row> {
|
|
||||||
const tableId = utils.getTableId(ctx),
|
|
||||||
rowId = ctx.params.rowId
|
|
||||||
const table = await sdk.tables.getTable(tableId)
|
|
||||||
let row = await utils.findRow(tableId, rowId)
|
|
||||||
row = await outputProcessing(table, row)
|
|
||||||
return row
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function destroy(ctx: UserCtx) {
|
export async function destroy(ctx: UserCtx) {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const tableId = utils.getTableId(ctx)
|
const tableId = utils.getTableId(ctx)
|
||||||
|
|
|
@ -9,7 +9,7 @@ import {
|
||||||
import cloneDeep from "lodash/fp/cloneDeep"
|
import cloneDeep from "lodash/fp/cloneDeep"
|
||||||
import isEqual from "lodash/fp/isEqual"
|
import isEqual from "lodash/fp/isEqual"
|
||||||
|
|
||||||
export async function getRow(
|
async function getRow(
|
||||||
tableId: string,
|
tableId: string,
|
||||||
rowId: string,
|
rowId: string,
|
||||||
opts?: { relationships?: boolean }
|
opts?: { relationships?: boolean }
|
||||||
|
@ -53,7 +53,7 @@ export async function save(
|
||||||
|
|
||||||
const rowId = response.row._id
|
const rowId = response.row._id
|
||||||
if (rowId) {
|
if (rowId) {
|
||||||
const row = await sdk.rows.external.getRow(tableId, rowId, {
|
const row = await getRow(tableId, rowId, {
|
||||||
relationships: true,
|
relationships: true,
|
||||||
})
|
})
|
||||||
return {
|
return {
|
||||||
|
@ -67,3 +67,16 @@ export async function save(
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function find(tableId: string, rowId: string): Promise<Row> {
|
||||||
|
const row = await getRow(tableId, rowId, {
|
||||||
|
relationships: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
const table = await sdk.tables.getTable(tableId)
|
||||||
|
// Preserving links, as the outputProcessing does not support external rows yet and we don't need it in this use case
|
||||||
|
return await outputProcessing(table, row, {
|
||||||
|
squash: true,
|
||||||
|
preserveLinks: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
import { db } from "@budibase/backend-core"
|
import { context, db } from "@budibase/backend-core"
|
||||||
import { Row } from "@budibase/types"
|
import { Row } from "@budibase/types"
|
||||||
import sdk from "../../../sdk"
|
import sdk from "../../../sdk"
|
||||||
import cloneDeep from "lodash/fp/cloneDeep"
|
import cloneDeep from "lodash/fp/cloneDeep"
|
||||||
import { finaliseRow } from "../../../api/controllers/row/staticFormula"
|
import { finaliseRow } from "../../../api/controllers/row/staticFormula"
|
||||||
import { inputProcessing } from "../../../utilities/rowProcessor"
|
import {
|
||||||
|
inputProcessing,
|
||||||
|
outputProcessing,
|
||||||
|
} from "../../../utilities/rowProcessor"
|
||||||
import * as linkRows from "../../../db/linkedRows"
|
import * as linkRows from "../../../db/linkedRows"
|
||||||
|
import { InternalTables } from "../../../db/utils"
|
||||||
|
import { getFullUser } from "../../../utilities/users"
|
||||||
|
|
||||||
export async function save(
|
export async function save(
|
||||||
tableId: string,
|
tableId: string,
|
||||||
|
@ -47,3 +52,26 @@ export async function save(
|
||||||
updateFormula: true,
|
updateFormula: true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function find(tableId: string, rowId: string): Promise<Row> {
|
||||||
|
const table = await sdk.tables.getTable(tableId)
|
||||||
|
let row = await findRow(tableId, rowId)
|
||||||
|
|
||||||
|
row = await outputProcessing(table, row)
|
||||||
|
return row
|
||||||
|
}
|
||||||
|
|
||||||
|
async function findRow(tableId: string, rowId: string) {
|
||||||
|
const db = context.getAppDB()
|
||||||
|
let row: Row
|
||||||
|
// TODO remove special user case in future
|
||||||
|
if (tableId === InternalTables.USER_METADATA) {
|
||||||
|
row = await getFullUser(rowId)
|
||||||
|
} else {
|
||||||
|
row = await db.get(rowId)
|
||||||
|
}
|
||||||
|
if (row.tableId !== tableId) {
|
||||||
|
throw "Supplied tableId does not match the rows tableId"
|
||||||
|
}
|
||||||
|
return row
|
||||||
|
}
|
||||||
|
|
|
@ -34,3 +34,7 @@ export async function save(
|
||||||
) {
|
) {
|
||||||
return pickApi(tableId).save(tableId, row, userId)
|
return pickApi(tableId).save(tableId, row, userId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function find(tableId: string, rowId: string) {
|
||||||
|
return pickApi(tableId).find(tableId, rowId)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue