Files
supabase/apps/studio/data/database-cron-jobs/database-cron-jobs-query.ts
Ivan VasilovandJoshen Lim fd3309c886 feat: UI for pg-cron (#29291)
* Add crons option in the database menu.

* Add react-hook-form to the studio package.json.

* Refactor the functionSelector to be used by other features.

* Add the bulk of the functionality for the cron UI. Some of the code is copy-pasted from functions feature, needs to be cleaned before merging.

* Tons of changes, the Create Cron sheet works now.

* Added some more functionality for the cronjob feature.

* Convert the Cron table to a listing with cards.

* Add click-to-copy in the Auth Hooks feature.

* Remove extra prop.

* Fix type errors.

* Fix some random issues. Fix the tests.

* Fix the tests.

* Add a style for disabled radio button item.

* Make the default SQL snippet. Handle the case pg_net is not installed.

* Fix the heading and save button when creating a new cron job.

* Change the name of the custom label in the schedule dropdown.

* Minor fixes.

* Rename all mentions of cronjobs to cron jobs.

* Always show the cron jobs link.

* Rename the link from crons to cron-jobs.

* Fix the disabled state for the stacked radio group.

* More minor fixes.

* More small fixes.

* Fix the tests.

* Minor UI tweaks

* More minor tweaks

* Add padding bottom

* Add feature flag for the cron ui.

* Fix the SQL function option.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-10-03 15:11:17 -02:30

57 lines
1.4 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { executeSql } from 'data/sql/execute-sql-query'
import { ResponseError } from 'types'
import { databaseCronJobsKeys } from './keys'
export type DatabaseCronJobsVariables = {
projectRef?: string
connectionString?: string
}
export type CronJob = {
jobid: number
schedule: string
command: string
nodename: string
nodeport: number
database: string
username: string
active: boolean
jobname: string
}
const cronJobSqlQuery = `select * from cron.job order by jobid;`
export async function getDatabaseCronJobs({
projectRef,
connectionString,
}: DatabaseCronJobsVariables) {
if (!projectRef) throw new Error('Project ref is required')
const { result } = await executeSql({
projectRef,
connectionString,
sql: cronJobSqlQuery,
})
return result
}
export type DatabaseCronJobData = CronJob[]
export type DatabaseCronJobError = ResponseError
export const useCronJobsQuery = <TData = DatabaseCronJobData>(
{ projectRef, connectionString }: DatabaseCronJobsVariables,
{
enabled = true,
...options
}: UseQueryOptions<DatabaseCronJobData, DatabaseCronJobError, TData> = {}
) =>
useQuery<DatabaseCronJobData, DatabaseCronJobError, TData>(
databaseCronJobsKeys.list(projectRef),
() => getDatabaseCronJobs({ projectRef, connectionString }),
{
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)