//native
export type DynSelect_catalog = string
export type DynSelect_schema = string
// Dropdown of the metastore's catalogs.
export async function catalog(auth: RT.Databricks) {
const base = auth.workspace_url.replace(/\/$/, "")
const response = await fetch(`${base}/api/2.1/unity-catalog/catalogs`, {
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { catalogs } = (await response.json()) as {
catalogs?: { name: string }[]
}
return (catalogs ?? []).map((c) => ({ value: c.name, label: c.name }))
}
// Dropdown of the schemas in the chosen catalog (recomputes when catalog changes).
export async function schema(auth: RT.Databricks, catalog: DynSelect_catalog) {
const base = auth.workspace_url.replace(/\/$/, "")
const url = new URL(`${base}/api/2.1/unity-catalog/schemas`)
url.searchParams.append("catalog_name", catalog)
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { schemas } = (await response.json()) as {
schemas?: { name: string }[]
}
return (schemas ?? []).map((s) => ({ value: s.name, label: s.name }))
}
/**
* List Tables
* List the tables within a Unity Catalog schema. Paginate with page_token.
*/
export async function main(
auth: RT.Databricks,
catalog: DynSelect_catalog,
schema: DynSelect_schema,
max_results: number | undefined,
page_token: string | undefined
) {
const base = auth.workspace_url.replace(/\/$/, "")
const url = new URL(`${base}/api/2.1/unity-catalog/tables`)
url.searchParams.append("catalog_name", catalog)
url.searchParams.append("schema_name", schema)
if (max_results !== undefined)
url.searchParams.append("max_results", String(max_results))
if (page_token !== undefined && page_token !== "")
url.searchParams.append("page_token", page_token)
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 5 days ago