diff --git a/trailbase-assets/js/admin/src/components/tables/RecordApiSettings.tsx b/trailbase-assets/js/admin/src/components/tables/RecordApiSettings.tsx index 9f5d5c43..5c19847a 100644 --- a/trailbase-assets/js/admin/src/components/tables/RecordApiSettings.tsx +++ b/trailbase-assets/js/admin/src/components/tables/RecordApiSettings.tsx @@ -228,21 +228,42 @@ function ConflictResolutionSrategyToString( } } +export function getRecordApis( + config: Config | undefined, + tableName: string, +): RecordApiConfig[] { + return (config?.recordApis ?? []).filter( + (api) => api.tableName === tableName, + ); +} + +export function hasRecordApis( + config: Config | undefined, + tableName: string, +): boolean { + for (const api of config?.recordApis ?? []) { + if (api.tableName === tableName) { + return true; + } + } + return false; +} + function findRecordApi( config: Config | undefined, tableName: string, ): RecordApiConfig | undefined { - if (!config) { - return undefined; - } + const apis = getRecordApis(config, tableName); - for (const api of config.recordApis) { - if (api.tableName == tableName) { - return api; - } + switch (apis.length) { + case 0: + return undefined; + case 1: + return apis[0]; + default: + console.warn("Multiple APIs not yet supported in UI, picking first."); + return apis[0]; } - - return undefined; } function StyledHoverCard(props: { children: JSXElement }) { @@ -298,7 +319,7 @@ export function RecordApiSettingsForm(props: { // FIXME: We don't currently handle the "multiple APIs for a single table" case. const currentApi = () => - findRecordApi(config.data!.config!, props.schema.name); + findRecordApi(config.data!.config, props.schema.name); const form = createForm(() => { const tableName = props.schema.name; diff --git a/trailbase-assets/js/admin/src/components/tables/SchemaDownload.tsx b/trailbase-assets/js/admin/src/components/tables/SchemaDownload.tsx index ae670fb9..5f83081a 100644 --- a/trailbase-assets/js/admin/src/components/tables/SchemaDownload.tsx +++ b/trailbase-assets/js/admin/src/components/tables/SchemaDownload.tsx @@ -1,9 +1,18 @@ -import { createSignal, createResource, Switch, Match, Show } from "solid-js"; +import { + createMemo, + createSignal, + createResource, + Switch, + Match, + Show, +} from "solid-js"; +import { createWritableMemo } from "@solid-primitives/memo"; import { adminFetch } from "@/lib/fetch"; import { TbDownload, TbColumns, TbColumnsOff } from "solid-icons/tb"; import { showSaveFileDialog } from "@/lib/utils"; import { iconButtonStyle } from "@/components/IconButton"; +import { RecordApiConfig } from "@proto/config"; import type { Table } from "@bindings/Table"; import type { TableIndex } from "@bindings/TableIndex"; import type { TableTrigger } from "@bindings/TableTrigger"; @@ -33,7 +42,7 @@ const modes = ["Insert", "Select", "Update"] as const; type Mode = (typeof modes)[number]; function SchemaDownloadButton(props: { - tableName: string; + apiName: string; mode: Mode; schema: object; }) { @@ -46,7 +55,7 @@ function SchemaDownloadButton(props: { // possible fallback: https://stackoverflow.com/a/67806663 showSaveFileDialog({ contents: JSON.stringify(props.schema, null, " "), - filename: `${props.tableName}_${props.mode.toLowerCase()}_schema.json`, + filename: `${props.apiName}_${props.mode.toLowerCase()}_schema.json`, }).catch(console.error); }} > @@ -55,17 +64,23 @@ function SchemaDownloadButton(props: { ); } -export function SchemaDialog(props: { tableName: string }) { +export function SchemaDialog(props: { + tableName: string; + apis: RecordApiConfig[]; +}) { const [mode, setMode] = createSignal("Select"); + const apiNames = createMemo(() => props.apis.map((api) => api.name)); + const [api, setApi] = createWritableMemo(() => apiNames()[0] ?? ""); + const args = () => ({ mode: mode(), - tableName: props.tableName, + apiName: api(), }); - const [schema] = createResource(args, async ({ mode, tableName }) => { - console.debug(`Fetching ${tableName}: ${mode}`); + const [schema] = createResource(args, async ({ mode, apiName }) => { + console.debug(`Fetching ${apiName}: ${mode}`); const response = await adminFetch( - `/table/${tableName}/schema.json?mode=${mode}`, + `/schema/${apiName}/schema.json?mode=${mode}`, ); return await response.json(); }); @@ -77,7 +92,8 @@ export function SchemaDialog(props: { tableName: string }) { - JSON Schema of "{props.tableName}" + + JSON Schemas of "{props.tableName}" @@ -89,12 +105,31 @@ export function SchemaDialog(props: { tableName: string }) {
+ +