Metadata API.

This commit is contained in:
mike12345567 2024-12-02 17:42:46 +00:00
parent f52bc41de0
commit c073500d50
10 changed files with 59 additions and 44 deletions

View File

@ -1,24 +1,35 @@
import { MetadataTypes } from "../../constants"
import { generateMetadataID } from "../../db/utils"
import { saveEntityMetadata, deleteEntityMetadata } from "../../utilities"
import { context } from "@budibase/backend-core"
import { BBContext } from "@budibase/types"
import {
UserCtx,
MetadataType,
GetMetadataTypesResponse,
SaveMetadataRequest,
SaveMetadataResponse,
DeleteMetadataResponse,
FindMetadataResponse,
} from "@budibase/types"
export async function getTypes(ctx: BBContext) {
export async function getTypes(ctx: UserCtx<void, GetMetadataTypesResponse>) {
ctx.body = {
types: MetadataTypes,
types: MetadataType,
}
}
export async function saveMetadata(ctx: BBContext) {
export async function saveMetadata(
ctx: UserCtx<SaveMetadataRequest, SaveMetadataResponse>
) {
const { type, entityId } = ctx.params
if (type === MetadataTypes.AUTOMATION_TEST_HISTORY) {
if (type === MetadataType.AUTOMATION_TEST_HISTORY) {
ctx.throw(400, "Cannot save automation history type")
}
ctx.body = await saveEntityMetadata(type, entityId, ctx.request.body)
}
export async function deleteMetadata(ctx: BBContext) {
export async function deleteMetadata(
ctx: UserCtx<void, DeleteMetadataResponse>
) {
const { type, entityId } = ctx.params
await deleteEntityMetadata(type, entityId)
ctx.body = {
@ -26,17 +37,9 @@ export async function deleteMetadata(ctx: BBContext) {
}
}
export async function getMetadata(ctx: BBContext) {
export async function getMetadata(ctx: UserCtx<void, FindMetadataResponse>) {
const { type, entityId } = ctx.params
const db = context.getAppDB()
const id = generateMetadataID(type, entityId)
try {
ctx.body = await db.get(id)
} catch (err: any) {
if (err.status === 404) {
ctx.body = {}
} else {
ctx.throw(err.status, err)
}
}
ctx.body = (await db.tryGet(id)) || {}
}

View File

@ -1,11 +1,11 @@
const { testAutomation } = require("./utilities/TestFunctions")
const setup = require("./utilities")
const { MetadataTypes } = require("../../../constants")
import { testAutomation } from "./utilities/TestFunctions"
import * as setup from "./utilities"
import { MetadataType, Automation } from "@budibase/types"
describe("/metadata", () => {
let request = setup.getRequest()
let config = setup.getConfig()
let automation
let automation: Automation
afterAll(setup.afterAll)
@ -15,8 +15,8 @@ describe("/metadata", () => {
})
async function createMetadata(
data,
type = MetadataTypes.AUTOMATION_TEST_INPUT
data: Record<string, string>,
type = MetadataType.AUTOMATION_TEST_INPUT
) {
const res = await request
.post(`/api/metadata/${type}/${automation._id}`)
@ -27,7 +27,7 @@ describe("/metadata", () => {
expect(res.body._rev).toBeDefined()
}
async function getMetadata(type) {
async function getMetadata(type: MetadataType) {
const res = await request
.get(`/api/metadata/${type}/${automation._id}`)
.set(config.defaultHeaders())
@ -39,14 +39,14 @@ describe("/metadata", () => {
describe("save", () => {
it("should be able to save some metadata", async () => {
await createMetadata({ test: "a" })
const testInput = await getMetadata(MetadataTypes.AUTOMATION_TEST_INPUT)
const testInput = await getMetadata(MetadataType.AUTOMATION_TEST_INPUT)
expect(testInput.test).toBe("a")
})
it("should save history metadata on automation run", async () => {
// this should have created some history
await testAutomation(config, automation)
const metadata = await getMetadata(MetadataTypes.AUTOMATION_TEST_HISTORY)
await testAutomation(config, automation, {})
const metadata = await getMetadata(MetadataType.AUTOMATION_TEST_HISTORY)
expect(metadata).toBeDefined()
expect(metadata.history.length).toBe(1)
expect(typeof metadata.history[0].occurredAt).toBe("number")
@ -57,13 +57,13 @@ describe("/metadata", () => {
it("should be able to delete some test inputs", async () => {
const res = await request
.delete(
`/api/metadata/${MetadataTypes.AUTOMATION_TEST_INPUT}/${automation._id}`
`/api/metadata/${MetadataType.AUTOMATION_TEST_INPUT}/${automation._id}`
)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.message).toBeDefined()
const metadata = await getMetadata(MetadataTypes.AUTOMATION_TEST_INPUT)
const metadata = await getMetadata(MetadataType.AUTOMATION_TEST_INPUT)
expect(metadata.test).toBeUndefined()
})
})

View File

@ -2,7 +2,6 @@ import { Thread, ThreadType } from "../threads"
import { definitions } from "./triggerInfo"
import { automationQueue } from "./bullboard"
import { updateEntityMetadata } from "../utilities"
import { MetadataTypes } from "../constants"
import { context, db as dbCore, utils } from "@budibase/backend-core"
import { getAutomationMetadataParams } from "../db/utils"
import { cloneDeep } from "lodash/fp"
@ -14,6 +13,7 @@ import {
AutomationStepDefinition,
AutomationTriggerDefinition,
AutomationTriggerStepId,
MetadataType,
} from "@budibase/types"
import { automationsEnabled } from "../features"
import { helpers, REBOOT_CRON } from "@budibase/shared-core"
@ -107,7 +107,7 @@ export async function updateTestHistory(
history: any
) {
return updateEntityMetadata(
MetadataTypes.AUTOMATION_TEST_HISTORY,
MetadataType.AUTOMATION_TEST_HISTORY,
automation._id,
(metadata: any) => {
if (metadata && Array.isArray(metadata.history)) {

View File

@ -124,11 +124,6 @@ export enum BaseQueryVerbs {
DELETE = "delete",
}
export enum MetadataTypes {
AUTOMATION_TEST_INPUT = "automationTestInput",
AUTOMATION_TEST_HISTORY = "automationTestHistory",
}
export enum InvalidColumns {
ID = "_id",
REV = "_rev",

View File

@ -3,10 +3,10 @@ import {
RequiredKeys,
Webhook,
WebhookActionType,
MetadataType,
} from "@budibase/types"
import { generateAutomationID, getAutomationParams } from "../../../db/utils"
import { deleteEntityMetadata } from "../../../utilities"
import { MetadataTypes } from "../../../constants"
import {
context,
events,
@ -161,7 +161,7 @@ export async function update(automation: Automation) {
if (oldAutoTrigger && oldAutoTrigger.id !== newAutoTrigger?.id) {
await events.automation.triggerUpdated(automation)
await deleteEntityMetadata(
MetadataTypes.AUTOMATION_TEST_INPUT,
MetadataType.AUTOMATION_TEST_INPUT,
automation._id!
)
}
@ -183,11 +183,8 @@ export async function remove(automationId: string, rev: string) {
})
// delete metadata first
await deleteEntityMetadata(MetadataTypes.AUTOMATION_TEST_INPUT, automationId)
await deleteEntityMetadata(
MetadataTypes.AUTOMATION_TEST_HISTORY,
automationId
)
await deleteEntityMetadata(MetadataType.AUTOMATION_TEST_INPUT, automationId)
await deleteEntityMetadata(MetadataType.AUTOMATION_TEST_HISTORY, automationId)
const result = await db.remove(automationId, rev)

View File

@ -88,7 +88,7 @@ export async function saveEntityMetadata(
type: string,
entityId: string,
metadata: Document
) {
): Promise<Document> {
return updateEntityMetadata(type, entityId, () => {
return metadata
})

View File

@ -11,3 +11,4 @@ export * from "./rowAction"
export * from "./automation"
export * from "./component"
export * from "./integration"
export * from "./metadata"

View File

@ -0,0 +1,14 @@
import { MetadataType, Document } from "../../../documents"
export interface GetMetadataTypesResponse {
types: typeof MetadataType
}
export interface SaveMetadataRequest extends Document {}
export interface SaveMetadataResponse extends Document {}
export interface DeleteMetadataResponse {
message: string
}
export interface FindMetadataResponse extends Document {}

View File

@ -19,3 +19,4 @@ export * from "./snippet"
export * from "./rowAction"
export * from "./theme"
export * from "./deployment"
export * from "./metadata"

View File

@ -0,0 +1,4 @@
export enum MetadataType {
AUTOMATION_TEST_INPUT = "automationTestInput",
AUTOMATION_TEST_HISTORY = "automationTestHistory",
}