Metadata API.
This commit is contained in:
parent
f52bc41de0
commit
c073500d50
|
@ -1,24 +1,35 @@
|
||||||
import { MetadataTypes } from "../../constants"
|
|
||||||
import { generateMetadataID } from "../../db/utils"
|
import { generateMetadataID } from "../../db/utils"
|
||||||
import { saveEntityMetadata, deleteEntityMetadata } from "../../utilities"
|
import { saveEntityMetadata, deleteEntityMetadata } from "../../utilities"
|
||||||
import { context } from "@budibase/backend-core"
|
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 = {
|
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
|
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.throw(400, "Cannot save automation history type")
|
||||||
}
|
}
|
||||||
ctx.body = await saveEntityMetadata(type, entityId, ctx.request.body)
|
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
|
const { type, entityId } = ctx.params
|
||||||
await deleteEntityMetadata(type, entityId)
|
await deleteEntityMetadata(type, entityId)
|
||||||
ctx.body = {
|
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 { type, entityId } = ctx.params
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const id = generateMetadataID(type, entityId)
|
const id = generateMetadataID(type, entityId)
|
||||||
try {
|
ctx.body = (await db.tryGet(id)) || {}
|
||||||
ctx.body = await db.get(id)
|
|
||||||
} catch (err: any) {
|
|
||||||
if (err.status === 404) {
|
|
||||||
ctx.body = {}
|
|
||||||
} else {
|
|
||||||
ctx.throw(err.status, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
const { testAutomation } = require("./utilities/TestFunctions")
|
import { testAutomation } from "./utilities/TestFunctions"
|
||||||
const setup = require("./utilities")
|
import * as setup from "./utilities"
|
||||||
const { MetadataTypes } = require("../../../constants")
|
import { MetadataType, Automation } from "@budibase/types"
|
||||||
|
|
||||||
describe("/metadata", () => {
|
describe("/metadata", () => {
|
||||||
let request = setup.getRequest()
|
let request = setup.getRequest()
|
||||||
let config = setup.getConfig()
|
let config = setup.getConfig()
|
||||||
let automation
|
let automation: Automation
|
||||||
|
|
||||||
afterAll(setup.afterAll)
|
afterAll(setup.afterAll)
|
||||||
|
|
||||||
|
@ -15,8 +15,8 @@ describe("/metadata", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
async function createMetadata(
|
async function createMetadata(
|
||||||
data,
|
data: Record<string, string>,
|
||||||
type = MetadataTypes.AUTOMATION_TEST_INPUT
|
type = MetadataType.AUTOMATION_TEST_INPUT
|
||||||
) {
|
) {
|
||||||
const res = await request
|
const res = await request
|
||||||
.post(`/api/metadata/${type}/${automation._id}`)
|
.post(`/api/metadata/${type}/${automation._id}`)
|
||||||
|
@ -27,7 +27,7 @@ describe("/metadata", () => {
|
||||||
expect(res.body._rev).toBeDefined()
|
expect(res.body._rev).toBeDefined()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getMetadata(type) {
|
async function getMetadata(type: MetadataType) {
|
||||||
const res = await request
|
const res = await request
|
||||||
.get(`/api/metadata/${type}/${automation._id}`)
|
.get(`/api/metadata/${type}/${automation._id}`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
|
@ -39,14 +39,14 @@ describe("/metadata", () => {
|
||||||
describe("save", () => {
|
describe("save", () => {
|
||||||
it("should be able to save some metadata", async () => {
|
it("should be able to save some metadata", async () => {
|
||||||
await createMetadata({ test: "a" })
|
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")
|
expect(testInput.test).toBe("a")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should save history metadata on automation run", async () => {
|
it("should save history metadata on automation run", async () => {
|
||||||
// this should have created some history
|
// this should have created some history
|
||||||
await testAutomation(config, automation)
|
await testAutomation(config, automation, {})
|
||||||
const metadata = await getMetadata(MetadataTypes.AUTOMATION_TEST_HISTORY)
|
const metadata = await getMetadata(MetadataType.AUTOMATION_TEST_HISTORY)
|
||||||
expect(metadata).toBeDefined()
|
expect(metadata).toBeDefined()
|
||||||
expect(metadata.history.length).toBe(1)
|
expect(metadata.history.length).toBe(1)
|
||||||
expect(typeof metadata.history[0].occurredAt).toBe("number")
|
expect(typeof metadata.history[0].occurredAt).toBe("number")
|
||||||
|
@ -57,13 +57,13 @@ describe("/metadata", () => {
|
||||||
it("should be able to delete some test inputs", async () => {
|
it("should be able to delete some test inputs", async () => {
|
||||||
const res = await request
|
const res = await request
|
||||||
.delete(
|
.delete(
|
||||||
`/api/metadata/${MetadataTypes.AUTOMATION_TEST_INPUT}/${automation._id}`
|
`/api/metadata/${MetadataType.AUTOMATION_TEST_INPUT}/${automation._id}`
|
||||||
)
|
)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.expect("Content-Type", /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
expect(res.body.message).toBeDefined()
|
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()
|
expect(metadata.test).toBeUndefined()
|
||||||
})
|
})
|
||||||
})
|
})
|
|
@ -2,7 +2,6 @@ import { Thread, ThreadType } from "../threads"
|
||||||
import { definitions } from "./triggerInfo"
|
import { definitions } from "./triggerInfo"
|
||||||
import { automationQueue } from "./bullboard"
|
import { automationQueue } from "./bullboard"
|
||||||
import { updateEntityMetadata } from "../utilities"
|
import { updateEntityMetadata } from "../utilities"
|
||||||
import { MetadataTypes } from "../constants"
|
|
||||||
import { context, db as dbCore, utils } from "@budibase/backend-core"
|
import { context, db as dbCore, utils } from "@budibase/backend-core"
|
||||||
import { getAutomationMetadataParams } from "../db/utils"
|
import { getAutomationMetadataParams } from "../db/utils"
|
||||||
import { cloneDeep } from "lodash/fp"
|
import { cloneDeep } from "lodash/fp"
|
||||||
|
@ -14,6 +13,7 @@ import {
|
||||||
AutomationStepDefinition,
|
AutomationStepDefinition,
|
||||||
AutomationTriggerDefinition,
|
AutomationTriggerDefinition,
|
||||||
AutomationTriggerStepId,
|
AutomationTriggerStepId,
|
||||||
|
MetadataType,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { automationsEnabled } from "../features"
|
import { automationsEnabled } from "../features"
|
||||||
import { helpers, REBOOT_CRON } from "@budibase/shared-core"
|
import { helpers, REBOOT_CRON } from "@budibase/shared-core"
|
||||||
|
@ -107,7 +107,7 @@ export async function updateTestHistory(
|
||||||
history: any
|
history: any
|
||||||
) {
|
) {
|
||||||
return updateEntityMetadata(
|
return updateEntityMetadata(
|
||||||
MetadataTypes.AUTOMATION_TEST_HISTORY,
|
MetadataType.AUTOMATION_TEST_HISTORY,
|
||||||
automation._id,
|
automation._id,
|
||||||
(metadata: any) => {
|
(metadata: any) => {
|
||||||
if (metadata && Array.isArray(metadata.history)) {
|
if (metadata && Array.isArray(metadata.history)) {
|
||||||
|
|
|
@ -124,11 +124,6 @@ export enum BaseQueryVerbs {
|
||||||
DELETE = "delete",
|
DELETE = "delete",
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum MetadataTypes {
|
|
||||||
AUTOMATION_TEST_INPUT = "automationTestInput",
|
|
||||||
AUTOMATION_TEST_HISTORY = "automationTestHistory",
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum InvalidColumns {
|
export enum InvalidColumns {
|
||||||
ID = "_id",
|
ID = "_id",
|
||||||
REV = "_rev",
|
REV = "_rev",
|
||||||
|
|
|
@ -3,10 +3,10 @@ import {
|
||||||
RequiredKeys,
|
RequiredKeys,
|
||||||
Webhook,
|
Webhook,
|
||||||
WebhookActionType,
|
WebhookActionType,
|
||||||
|
MetadataType,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { generateAutomationID, getAutomationParams } from "../../../db/utils"
|
import { generateAutomationID, getAutomationParams } from "../../../db/utils"
|
||||||
import { deleteEntityMetadata } from "../../../utilities"
|
import { deleteEntityMetadata } from "../../../utilities"
|
||||||
import { MetadataTypes } from "../../../constants"
|
|
||||||
import {
|
import {
|
||||||
context,
|
context,
|
||||||
events,
|
events,
|
||||||
|
@ -161,7 +161,7 @@ export async function update(automation: Automation) {
|
||||||
if (oldAutoTrigger && oldAutoTrigger.id !== newAutoTrigger?.id) {
|
if (oldAutoTrigger && oldAutoTrigger.id !== newAutoTrigger?.id) {
|
||||||
await events.automation.triggerUpdated(automation)
|
await events.automation.triggerUpdated(automation)
|
||||||
await deleteEntityMetadata(
|
await deleteEntityMetadata(
|
||||||
MetadataTypes.AUTOMATION_TEST_INPUT,
|
MetadataType.AUTOMATION_TEST_INPUT,
|
||||||
automation._id!
|
automation._id!
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -183,11 +183,8 @@ export async function remove(automationId: string, rev: string) {
|
||||||
})
|
})
|
||||||
|
|
||||||
// delete metadata first
|
// delete metadata first
|
||||||
await deleteEntityMetadata(MetadataTypes.AUTOMATION_TEST_INPUT, automationId)
|
await deleteEntityMetadata(MetadataType.AUTOMATION_TEST_INPUT, automationId)
|
||||||
await deleteEntityMetadata(
|
await deleteEntityMetadata(MetadataType.AUTOMATION_TEST_HISTORY, automationId)
|
||||||
MetadataTypes.AUTOMATION_TEST_HISTORY,
|
|
||||||
automationId
|
|
||||||
)
|
|
||||||
|
|
||||||
const result = await db.remove(automationId, rev)
|
const result = await db.remove(automationId, rev)
|
||||||
|
|
||||||
|
|
|
@ -88,7 +88,7 @@ export async function saveEntityMetadata(
|
||||||
type: string,
|
type: string,
|
||||||
entityId: string,
|
entityId: string,
|
||||||
metadata: Document
|
metadata: Document
|
||||||
) {
|
): Promise<Document> {
|
||||||
return updateEntityMetadata(type, entityId, () => {
|
return updateEntityMetadata(type, entityId, () => {
|
||||||
return metadata
|
return metadata
|
||||||
})
|
})
|
||||||
|
|
|
@ -11,3 +11,4 @@ export * from "./rowAction"
|
||||||
export * from "./automation"
|
export * from "./automation"
|
||||||
export * from "./component"
|
export * from "./component"
|
||||||
export * from "./integration"
|
export * from "./integration"
|
||||||
|
export * from "./metadata"
|
||||||
|
|
|
@ -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 {}
|
|
@ -19,3 +19,4 @@ export * from "./snippet"
|
||||||
export * from "./rowAction"
|
export * from "./rowAction"
|
||||||
export * from "./theme"
|
export * from "./theme"
|
||||||
export * from "./deployment"
|
export * from "./deployment"
|
||||||
|
export * from "./metadata"
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
export enum MetadataType {
|
||||||
|
AUTOMATION_TEST_INPUT = "automationTestInput",
|
||||||
|
AUTOMATION_TEST_HISTORY = "automationTestHistory",
|
||||||
|
}
|
Loading…
Reference in New Issue