Implement create

This commit is contained in:
Adria Navarro 2024-07-11 10:04:25 +02:00
parent 063eeeb6df
commit 7dbfcc398e
8 changed files with 74 additions and 11 deletions

View File

@ -25,12 +25,18 @@ export async function find(ctx: Ctx<void, RowActionsResponse>) {
}
}
export async function create(ctx: Ctx<CreateRowActionRequest, void>) {
export async function create(
ctx: Ctx<CreateRowActionRequest, RowActionsResponse>
) {
const table = await getTable(ctx)
// TODO
const created = await sdk.rowActions.create(table._id!, ctx.request.body)
ctx.status = 204
ctx.body = {
tableId: table._id!,
...created,
}
ctx.status = 201
}
export function update() {

View File

@ -1,4 +1,6 @@
import _ from "lodash"
import tk from "timekeeper"
import { CreateRowActionRequest, Table } from "@budibase/types"
import * as setup from "./utilities"
import { generator } from "@budibase/backend-core/tests"
@ -9,6 +11,7 @@ describe("/rowsActions", () => {
let table: Table
beforeAll(async () => {
tk.freeze(new Date())
await config.init()
table = await config.api.table.save(setup.structures.basicTable())
@ -62,14 +65,21 @@ describe("/rowsActions", () => {
describe("create", () => {
unauthorisedTests()
it("accepts creating new row actions", async () => {
it("accepts creating new row actions for", async () => {
const rowAction = createRowActionRequest()
const res = await config.api.rowAction.save(table._id!, rowAction, {
status: 204,
status: 201,
})
expect(res).toEqual({})
expect(res).toEqual({
_id: `${table._id}_row_actions`,
_rev: expect.stringMatching(/^1-\w+/),
actions: [{ name: rowAction.name }],
tableId: table._id,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
})
})
it("rejects with bad request when creating with no name", async () => {

View File

@ -348,3 +348,11 @@ export function isRelationshipColumn(
): column is RelationshipFieldMetadata {
return column.type === FieldType.LINK
}
/**
* Generates a new row actions ID.
* @returns The new row actions ID which the row actions doc can be stored under.
*/
export function generateRowActionsID(tableId: string) {
return `${tableId}${SEPARATOR}row_actions`
}

View File

@ -0,0 +1,30 @@
import { context } from "@budibase/backend-core"
import { generateRowActionsID } from "../../db/utils"
import { TableRowActions } from "@budibase/types"
export async function create(tableId: string, rowAction: { name: string }) {
const db = context.getAppDB()
const rowActionsId = generateRowActionsID(tableId)
let doc: TableRowActions
try {
doc = await db.get<TableRowActions>(rowActionsId)
} catch (e: any) {
if (e.status !== 404) {
throw e
}
doc = { _id: rowActionsId, actions: [] }
}
doc.actions.push(rowAction)
await db.put(doc)
return await get(tableId)
}
export async function get(tableId: string) {
const db = context.getAppDB()
const rowActionsId = generateRowActionsID(tableId)
return await db.get<TableRowActions>(rowActionsId)
}

View File

@ -10,6 +10,7 @@ import { default as users } from "./users"
import { default as plugins } from "./plugins"
import * as views from "./app/views"
import * as permissions from "./app/permissions"
import * as rowActions from "./app/rowActions"
const sdk = {
backups,
@ -24,6 +25,7 @@ const sdk = {
views,
permissions,
links,
rowActions,
}
// default export for TS

View File

@ -2,11 +2,9 @@ export interface CreateRowActionRequest {
name: string
}
interface RowAction {
name: string
}
export interface RowActionsResponse {
tableId: string
actions: RowAction[]
actions: {
name: string
}[]
}

View File

@ -16,3 +16,4 @@ export * from "./links"
export * from "./component"
export * from "./sqlite"
export * from "./snippet"
export * from "./rowActions"

View File

@ -0,0 +1,8 @@
import { Document } from "../document"
export interface TableRowActions extends Document {
_id: string
actions: {
name: string
}[]
}