Initial appMetadata sdk usage

This commit is contained in:
Adria Navarro 2024-10-16 17:06:15 +02:00
parent dac963a2ff
commit 8b9bb784c4
4 changed files with 23 additions and 11 deletions

View File

@ -208,9 +208,8 @@ export async function fetchAppDefinition(
export async function fetchAppPackage( export async function fetchAppPackage(
ctx: UserCtx<void, FetchAppPackageResponse> ctx: UserCtx<void, FetchAppPackageResponse>
) { ) {
const db = context.getAppDB()
const appId = context.getAppId() const appId = context.getAppId()
let application = await db.get<App>(DocumentType.APP_METADATA) const application = await sdk.appMetadata.get()
const layouts = await getLayouts() const layouts = await getLayouts()
let screens = await getScreens() let screens = await getScreens()
const license = await licensing.cache.getCachedLicense() const license = await licensing.cache.getCachedLicense()
@ -315,7 +314,7 @@ async function performAppCreate(ctx: UserCtx<CreateAppRequest, App>) {
// If we used a template or imported an app there will be an existing doc. // If we used a template or imported an app there will be an existing doc.
// Fetch and migrate some metadata from the existing app. // Fetch and migrate some metadata from the existing app.
try { try {
const existing: App = await db.get(DocumentType.APP_METADATA) const existing = await sdk.appMetadata.get()
const keys: (keyof App)[] = [ const keys: (keyof App)[] = [
"_rev", "_rev",
"navigation", "navigation",
@ -489,8 +488,7 @@ export async function update(
export async function updateClient(ctx: UserCtx) { export async function updateClient(ctx: UserCtx) {
// Get current app version // Get current app version
const db = context.getAppDB() const application = await sdk.appMetadata.get()
const application = await db.get<App>(DocumentType.APP_METADATA)
const currentVersion = application.version const currentVersion = application.version
let manifest let manifest
@ -518,8 +516,7 @@ export async function updateClient(ctx: UserCtx) {
export async function revertClient(ctx: UserCtx) { export async function revertClient(ctx: UserCtx) {
// Check app can be reverted // Check app can be reverted
const db = context.getAppDB() const application = await sdk.appMetadata.get()
const application = await db.get<App>(DocumentType.APP_METADATA)
if (!application.revertableVersion) { if (!application.revertableVersion) {
ctx.throw(400, "There is no version to revert to") ctx.throw(400, "There is no version to revert to")
} }
@ -577,7 +574,7 @@ async function destroyApp(ctx: UserCtx) {
const db = dbCore.getDB(devAppId) const db = dbCore.getDB(devAppId)
// standard app deletion flow // standard app deletion flow
const app = await db.get<App>(DocumentType.APP_METADATA) const app = await sdk.appMetadata.get()
const result = await db.destroy() const result = await db.destroy()
await quotas.removeApp() await quotas.removeApp()
await events.app.deleted(app) await events.app.deleted(app)
@ -728,7 +725,7 @@ export async function updateAppPackage(
) { ) {
return context.doInAppContext(appId, async () => { return context.doInAppContext(appId, async () => {
const db = context.getAppDB() const db = context.getAppDB()
const application = await db.get<App>(DocumentType.APP_METADATA) const application = await sdk.appMetadata.get()
const newAppPackage: App = { ...application, ...appPackage } const newAppPackage: App = { ...application, ...appPackage }
if (appPackage._rev !== application._rev) { if (appPackage._rev !== application._rev) {
@ -754,7 +751,7 @@ export async function setRevertableVersion(
return return
} }
const db = context.getAppDB() const db = context.getAppDB()
const app = await db.get<App>(DocumentType.APP_METADATA) const app = await sdk.appMetadata.get()
app.revertableVersion = ctx.request.body.revertableVersion app.revertableVersion = ctx.request.body.revertableVersion
await db.put(app) await db.put(app)
@ -763,7 +760,7 @@ export async function setRevertableVersion(
async function migrateAppNavigation() { async function migrateAppNavigation() {
const db = context.getAppDB() const db = context.getAppDB()
const existing: App = await db.get(DocumentType.APP_METADATA) const existing = await sdk.appMetadata.get()
const layouts: Layout[] = await getLayouts() const layouts: Layout[] = await getLayouts()
const screens: Screen[] = await getScreens() const screens: Screen[] = await getScreens()

View File

@ -0,0 +1,5 @@
import * as metadata from "./metadata"
export default {
...metadata,
}

View File

@ -0,0 +1,8 @@
import { context, DocumentType } from "@budibase/backend-core"
import { App } from "@budibase/types"
export async function get() {
const db = context.getAppDB()
const application = await db.get<App>(DocumentType.APP_METADATA)
return application
}

View File

@ -11,6 +11,7 @@ import { default as plugins } from "./plugins"
import * as views from "./app/views" import * as views from "./app/views"
import * as permissions from "./app/permissions" import * as permissions from "./app/permissions"
import * as rowActions from "./app/rowActions" import * as rowActions from "./app/rowActions"
import { default as appMetadata } from "./app/appMetadata"
const sdk = { const sdk = {
backups, backups,
@ -26,6 +27,7 @@ const sdk = {
permissions, permissions,
links, links,
rowActions, rowActions,
appMetadata,
} }
// default export for TS // default export for TS