Files
supabase/apps/studio/data/database-queues/database-queues-create-mutation.ts
Joshen Lim d8a57c1c7e Add settings for queues: toggle expose through postgrest + permissions via table privileges (#30564)
* Add settings for queues: toggle expose through postgrest + permissions via table privileges

* Ensure appropriate grants are granted when toggling, and revoked when disabling

* Update to use queues_public schema

* Update queue schema to pgmq_public and add/remove from data api when enabling/disabling

* Fix query for retrieving toggle state

* Add schema invalidation

* Remove hard code

* Use QueuesSettings from Queues folder, remove from NewQueues

* Update SQL for toggling exposure + support RLS enabling

* Support toggling RLS for a queue

* Update admonition copy in queues for enabling/disable postgrest exposure

* Add custom RLS policy for queue

* Minor style fixes

* Fix

* Remove hard code

* Update RLS to add message regarding relevancy only if exposure to PostgREST is enabled

* Update message in exposing queues to postgREST

* Address feedback

* Address feedback

* Don't revoke postgres role stuff

* Remove hard code

* Update copy

* Update

* Address Oli's feedback, ensure that queues ALL have RLS enabled prior to allowing exposure to PostgREST

* Address remaining feedback

* Remove hardcode

* Update

* Address feedback
2024-11-27 12:10:33 +08:00

80 lines
2.3 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { executeSql } from 'data/sql/execute-sql-query'
import type { ResponseError } from 'types'
import { databaseQueuesKeys } from './keys'
import { tableKeys } from 'data/tables/keys'
export type DatabaseQueueCreateVariables = {
projectRef: string
connectionString?: string
name: string
type: 'basic' | 'partitioned' | 'unlogged'
enableRls: boolean
configuration?: {
partitionInterval?: number
retentionInterval?: number
}
}
export async function createDatabaseQueue({
projectRef,
connectionString,
name,
type,
enableRls,
configuration,
}: DatabaseQueueCreateVariables) {
const { partitionInterval, retentionInterval } = configuration ?? {}
const query =
type === 'partitioned'
? `select from pgmq.create_partitioned('${name}', '${partitionInterval}', '${retentionInterval}');`
: type === 'unlogged'
? `SELECT pgmq.create_unlogged('${name}');`
: `SELECT pgmq.create('${name}');`
const { result } = await executeSql({
projectRef,
connectionString,
sql: `${query} ${enableRls ? `alter table pgmq."q_${name}" enable row level security;` : ''}`.trim(),
queryKey: databaseQueuesKeys.create(),
})
return result
}
type DatabaseQueueCreateData = Awaited<ReturnType<typeof createDatabaseQueue>>
export const useDatabaseQueueCreateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<DatabaseQueueCreateData, ResponseError, DatabaseQueueCreateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<DatabaseQueueCreateData, ResponseError, DatabaseQueueCreateVariables>(
(vars) => createDatabaseQueue(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries(databaseQueuesKeys.list(projectRef))
queryClient.invalidateQueries(tableKeys.list(projectRef, 'pgmq'))
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to create database queue: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}