2022-03-21 18:13:16 +01:00
|
|
|
import * as rowController from "../row"
|
2022-03-01 19:35:08 +01:00
|
|
|
import { addRev } from "./utils"
|
2022-11-28 19:14:10 +01:00
|
|
|
import { Row } from "@budibase/types"
|
2022-03-09 11:12:26 +01:00
|
|
|
import { convertBookmark } from "../../../utilities"
|
2022-02-22 18:38:27 +01:00
|
|
|
|
2022-02-18 18:44:08 +01:00
|
|
|
// makes sure that the user doesn't need to pass in the type, tableId or _id params for
|
|
|
|
// the call to be correct
|
2023-07-14 10:11:34 +02:00
|
|
|
export function fixRow(row: Row, params: any) {
|
2022-02-18 18:44:08 +01:00
|
|
|
if (!params || !row) {
|
|
|
|
return row
|
|
|
|
}
|
|
|
|
if (params.rowId) {
|
|
|
|
row._id = params.rowId
|
|
|
|
}
|
|
|
|
if (params.tableId) {
|
|
|
|
row.tableId = params.tableId
|
|
|
|
}
|
|
|
|
if (!row.type) {
|
|
|
|
row.type = "row"
|
|
|
|
}
|
|
|
|
return row
|
|
|
|
}
|
|
|
|
|
2022-03-01 19:35:08 +01:00
|
|
|
export async function search(ctx: any, next: any) {
|
2022-02-24 16:13:14 +01:00
|
|
|
let { sort, paginate, bookmark, limit, query } = ctx.request.body
|
|
|
|
// update the body to the correct format of the internal search
|
|
|
|
if (!sort) {
|
|
|
|
sort = {}
|
|
|
|
}
|
|
|
|
ctx.request.body = {
|
|
|
|
sort: sort.column,
|
|
|
|
sortType: sort.type,
|
|
|
|
sortOrder: sort.order,
|
2022-03-09 11:12:26 +01:00
|
|
|
bookmark: convertBookmark(bookmark),
|
2022-02-24 16:13:14 +01:00
|
|
|
paginate,
|
|
|
|
limit,
|
|
|
|
query,
|
|
|
|
}
|
2022-02-22 18:38:27 +01:00
|
|
|
await rowController.search(ctx)
|
2022-03-01 19:35:08 +01:00
|
|
|
await next()
|
2022-02-17 19:58:09 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 19:35:08 +01:00
|
|
|
export async function create(ctx: any, next: any) {
|
2022-02-18 18:44:08 +01:00
|
|
|
ctx.request.body = fixRow(ctx.request.body, ctx.params)
|
2022-02-23 19:31:32 +01:00
|
|
|
await rowController.save(ctx)
|
2022-03-01 19:35:08 +01:00
|
|
|
await next()
|
2022-02-17 19:58:09 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 19:35:08 +01:00
|
|
|
export async function read(ctx: any, next: any) {
|
2022-02-28 13:54:32 +01:00
|
|
|
await rowController.fetchEnrichedRow(ctx)
|
2022-03-01 19:35:08 +01:00
|
|
|
await next()
|
2022-02-23 19:31:32 +01:00
|
|
|
}
|
2022-02-17 19:58:09 +01:00
|
|
|
|
2022-03-01 19:35:08 +01:00
|
|
|
export async function update(ctx: any, next: any) {
|
2022-10-03 21:31:11 +02:00
|
|
|
const { tableId } = ctx.params
|
|
|
|
ctx.request.body = await addRev(fixRow(ctx.request.body, ctx.params), tableId)
|
2022-02-25 19:02:08 +01:00
|
|
|
await rowController.save(ctx)
|
2022-03-01 19:35:08 +01:00
|
|
|
await next()
|
2022-02-17 19:58:09 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 19:35:08 +01:00
|
|
|
export async function destroy(ctx: any, next: any) {
|
2022-10-03 21:31:11 +02:00
|
|
|
const { tableId } = ctx.params
|
2022-02-23 19:31:32 +01:00
|
|
|
// set the body as expected, with the _id and _rev fields
|
2022-10-03 21:31:11 +02:00
|
|
|
ctx.request.body = await addRev(
|
|
|
|
fixRow({ _id: ctx.params.rowId }, ctx.params),
|
|
|
|
tableId
|
|
|
|
)
|
2022-02-23 19:31:32 +01:00
|
|
|
await rowController.destroy(ctx)
|
|
|
|
// destroy controller doesn't currently return the row as the body, need to adjust this
|
|
|
|
// in the public API to be correct
|
2022-03-01 19:35:08 +01:00
|
|
|
ctx.body = ctx.row
|
|
|
|
await next()
|
2022-02-23 19:31:32 +01:00
|
|
|
}
|
2022-02-24 16:13:14 +01:00
|
|
|
|
|
|
|
export default {
|
|
|
|
create,
|
|
|
|
read,
|
|
|
|
update,
|
|
|
|
destroy,
|
|
|
|
search,
|
|
|
|
}
|