mirror of
https://github.com/supabase/supabase.git
synced 2026-09-08 19:08:44 +08:00
## Context Just some clean up as I was going through stuff - `useExecuteSqlQuery` is deprecated and not used at all - As such `execute-sql-query` is technically irrelevant, the more relevant file is `execute-sql-mutation` - Hence opting to consolidate `execute-sql-query` into `execute-sql-mutation` - Also removing `ExecuteSqlError` since its just re-exporting the `ResponseError` type There's a lot of file changes but its essentially just updating the importing statements across the files
67 lines
2.0 KiB
TypeScript
67 lines
2.0 KiB
TypeScript
import { safeSql } from '@supabase/pg-meta/src/pg-format'
|
|
import { QueryClient, useQuery, UseQueryOptions } from '@tanstack/react-query'
|
|
import { z } from 'zod'
|
|
|
|
import { stripeSyncKeys } from './keys'
|
|
import { executeSql } from '@/data/sql/execute-sql-mutation'
|
|
import { ResponseError } from '@/types'
|
|
|
|
export type DbConnection = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
}
|
|
|
|
const StripeSyncStateSchema = z
|
|
.object({
|
|
started_at: z.string().nullable(),
|
|
closed_at: z.string().nullable(),
|
|
status: z.enum(['running', 'pending', 'complete', 'error']).nullable(),
|
|
})
|
|
.nullable()
|
|
|
|
export type StripeSyncState = z.infer<typeof StripeSyncStateSchema>
|
|
|
|
export type StripeSyncStateData = z.infer<typeof StripeSyncStateSchema>
|
|
export type StripeSyncStateError = ResponseError
|
|
|
|
export async function getStripeSyncState(
|
|
{ projectRef, connectionString }: DbConnection,
|
|
signal?: AbortSignal
|
|
) {
|
|
const { result } = await executeSql(
|
|
{
|
|
projectRef,
|
|
connectionString,
|
|
sql: safeSql`
|
|
SELECT started_at, closed_at, status FROM stripe.sync_runs WHERE status != 'pending' ORDER BY started_at DESC LIMIT 1;
|
|
`,
|
|
queryKey: stripeSyncKeys.syncState(projectRef),
|
|
},
|
|
signal
|
|
)
|
|
|
|
return result.length > 0 ? StripeSyncStateSchema.parse(result[0]) : null
|
|
}
|
|
|
|
export const useStripeSyncingState = <TData = StripeSyncStateData>(
|
|
{ projectRef, connectionString }: DbConnection,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: Omit<
|
|
UseQueryOptions<StripeSyncStateData, StripeSyncStateError, TData>,
|
|
'queryKey' | 'queryFn'
|
|
> = {}
|
|
) => {
|
|
return useQuery<StripeSyncStateData, StripeSyncStateError, TData>({
|
|
queryKey: stripeSyncKeys.syncState(projectRef),
|
|
queryFn: ({ signal }) => getStripeSyncState({ projectRef, connectionString }, signal),
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
})
|
|
}
|
|
|
|
export function invalidateStripeSyncStateQuery(client: QueryClient, projectRef: string) {
|
|
return client.invalidateQueries({ queryKey: stripeSyncKeys.syncState(projectRef) })
|
|
}
|