Repository: supabase/supabase
File: apps/studio/data/table-editor/table-editor-query.ts (TypeScript)

Code before:

export type TableEditorVariables = TableEditorArgs & {
  projectRef?: string
  connectionString?: string | null
}

export async function getTableEditor(
  { projectRef, connectionString, id }: TableEditorVariables,
  signal?: AbortSignal
) {
  if (!id) {
    throw new Error('id is required')
  }

  const sql = getTableEditorSql(id)
  const { result } = await executeSql(
    {
      projectRef,
      connectionString,
      sql,
      queryKey: ['table-editor', id],
    },
    signal
  )

  return (result[0]?.entity ?? undefined) as Entity | undefined
}

export type TableEditorData = Awaited<ReturnType<typeof getTableEditor>>
export type TableEditorError = ExecuteSqlError

export const useTableEditorQuery = <TData = TableEditorData>(
  { projectRef, connectionString, id }: TableEditorVariables,
  { enabled = true, ...options }: UseQueryOptions<TableEditorData, TableEditorError, TData> = {}
) =>
  useQuery<TableEditorData, TableEditorError, TData>(
    tableEditorKeys.tableEditor(projectRef, id),
    ({ signal }) => getTableEditor({ projectRef, connectionString, id }, signal),
    {
      enabled:
        enabled && typeof projectRef !== 'undefined' && typeof id !== 'undefined' && !isNaN(id),
      refetchOnWindowFocus: false,
      refetchOnMount: false,
      staleTime: 5 * 60 * 1000, // 5 minutes
      ...options,
    }
  )

export function prefetchTableEditor(
  client: QueryClient,
  { projectRef, connectionString, id }: TableEditorVariables

Review comment (suggestion):
this is just to address a react query error - we shouldn't be saving `undefined` data into the react query state
<img width="463" height="140" alt="image" src="https://github.com/user-attachments/assets/2da12f21-6dac-4d90-a86b-dec6d660612c" />


Code after:

export type TableEditorVariables = TableEditorArgs & {
  projectRef?: string
  connectionString?: string | null
}

export async function getTableEditor(
  { projectRef, connectionString, id }: TableEditorVariables,
  signal?: AbortSignal
) {
  if (!id) {
    throw new Error('id is required')
  }

  const sql = getTableEditorSql(id)
  const { result } = await executeSql(
    {
      projectRef,
      connectionString,
      sql,
      queryKey: ['table-editor', id],
    },
    signal
  )

  return (result[0]?.entity ?? null) as Entity | undefined
}

export type TableEditorData = Awaited<ReturnType<typeof getTableEditor>>
export type TableEditorError = ExecuteSqlError

export const useTableEditorQuery = <TData = TableEditorData>(
  { projectRef, connectionString, id }: TableEditorVariables,
  { enabled = true, ...options }: UseQueryOptions<TableEditorData, TableEditorError, TData> = {}
) =>
  useQuery<TableEditorData, TableEditorError, TData>(
    tableEditorKeys.tableEditor(projectRef, id),
    ({ signal }) => getTableEditor({ projectRef, connectionString, id }, signal),
    {
      enabled:
        enabled && typeof projectRef !== 'undefined' && typeof id !== 'undefined' && !isNaN(id),
      refetchOnWindowFocus: false,
      refetchOnMount: false,
      staleTime: 5 * 60 * 1000, // 5 minutes
      ...options,
    }
  )

export function prefetchTableEditor(
  client: QueryClient,
  { projectRef, connectionString, id }: TableEditorVariables