Functions to get source type and usage endpoint for screens.

This commit is contained in:
mike12345567 2025-01-30 16:36:44 +00:00
parent b8e5512f30
commit 1b8a229e85
8 changed files with 49 additions and 1 deletions

View File

@ -83,11 +83,15 @@ export function isViewId(id: string): boolean {
/** /**
* Check if a given ID is that of a datasource or datasource plus. * Check if a given ID is that of a datasource or datasource plus.
*/ */
export const isDatasourceId = (id: string): boolean => { export function isDatasourceId(id: string): boolean {
// this covers both datasources and datasource plus // this covers both datasources and datasource plus
return !!id && id.startsWith(`${DocumentType.DATASOURCE}${SEPARATOR}`) return !!id && id.startsWith(`${DocumentType.DATASOURCE}${SEPARATOR}`)
} }
export function isQueryId(id: string): boolean {
return !!id && id.startsWith(`${DocumentType.QUERY}${SEPARATOR}`)
}
/** /**
* Gets parameters for retrieving workspaces. * Gets parameters for retrieving workspaces.
*/ */

View File

@ -16,8 +16,10 @@ import {
SaveScreenRequest, SaveScreenRequest,
SaveScreenResponse, SaveScreenResponse,
DeleteScreenResponse, DeleteScreenResponse,
UsageScreenResponse,
} from "@budibase/types" } from "@budibase/types"
import { builderSocket } from "../../websockets" import { builderSocket } from "../../websockets"
import sdk from "../../sdk"
export async function fetch(ctx: UserCtx<void, FetchScreenResponse>) { export async function fetch(ctx: UserCtx<void, FetchScreenResponse>) {
const db = context.getAppDB() const db = context.getAppDB()
@ -140,3 +142,8 @@ function findPlugins(component: ScreenProps, foundPlugins: string[]) {
} }
component._children.forEach(child => findPlugins(child, foundPlugins)) component._children.forEach(child => findPlugins(child, foundPlugins))
} }
export async function usage(ctx: UserCtx<void, UsageScreenResponse>) {
const sourceId = ctx.params.sourceId
const sourceType = sdk.common.getSourceType(sourceId)
}

View File

@ -19,5 +19,10 @@ router
authorized(permissions.BUILDER), authorized(permissions.BUILDER),
controller.destroy controller.destroy
) )
.post(
"/api/screens/usage/:sourceId",
authorized(permissions.BUILDER),
controller.usage
)
export default router export default router

View File

@ -0,0 +1 @@
export * from "./utils"

View File

@ -0,0 +1,15 @@
import { SourceType } from "@budibase/types"
import { docIds } from "@budibase/backend-core"
export function getSourceType(sourceId: string): SourceType {
if (docIds.isTableId(sourceId)) {
return SourceType.TABLE
} else if (docIds.isViewId(sourceId)) {
return SourceType.VIEW
} else if (docIds.isDatasourceId(sourceId)) {
return SourceType.DATASOURCE
} else if (docIds.isQueryId(sourceId)) {
return SourceType.QUERY
}
throw new Error("Unknown source type - cannot find document type")
}

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 * as common from "./app/common"
const sdk = { const sdk = {
backups, backups,
@ -26,6 +27,7 @@ const sdk = {
permissions, permissions,
links, links,
rowActions, rowActions,
common,
} }
// default export for TS // default export for TS

View File

@ -15,3 +15,10 @@ export interface SaveScreenResponse extends Screen {}
export interface DeleteScreenResponse { export interface DeleteScreenResponse {
message: string message: string
} }
export interface UsageScreenResponse {
screens: {
url: string
_id: string
}[]
}

View File

@ -57,3 +57,10 @@ export interface RestConfig {
} }
dynamicVariables?: DynamicVariable[] dynamicVariables?: DynamicVariable[]
} }
export enum SourceType {
DATASOURCE = "datasource",
QUERY = "query",
TABLE = "table",
VIEW = "view",
}