mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 01:45:10 +03:00
* policy generation * add ai * refactor * table create performance * policy list * style * simplify * refactor * flag * tracking * track * ts * fixes * connection string * re-add rls and realtime toggle * restore old logic * base path * badge * false rls * copy * add permissions button * Refactor based on comments * Fix TS * More nudge * Update tests * Fix test * Fixx * Fix * Address feedback * Address issues * Improve experiment telemetry for generate policies A/B test (#41172) * Address code rabbit catch --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com> Co-authored-by: Sean Oliver <882952+seanoliver@users.noreply.github.com>
74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
import pgMeta from '@supabase/pg-meta'
|
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { executeSql } from 'data/sql/execute-sql-query'
|
|
import type { ResponseError, UseCustomMutationOptions } from 'types'
|
|
import { databasePoliciesKeys } from './keys'
|
|
|
|
export type CreatePolicyBody = {
|
|
name: string
|
|
table: string
|
|
schema?: string
|
|
definition?: string
|
|
check?: string
|
|
action?: 'PERMISSIVE' | 'RESTRICTIVE'
|
|
command?: 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE' | 'ALL'
|
|
roles?: string[]
|
|
}
|
|
|
|
export type DatabasePolicyCreateVariables = {
|
|
projectRef: string
|
|
connectionString?: string | null
|
|
payload: CreatePolicyBody
|
|
}
|
|
|
|
export async function createDatabasePolicy({
|
|
projectRef,
|
|
connectionString,
|
|
payload,
|
|
}: DatabasePolicyCreateVariables) {
|
|
let headers = new Headers()
|
|
if (connectionString) headers.set('x-connection-encrypted', connectionString)
|
|
|
|
const { sql } = pgMeta.policies.create(payload)
|
|
const { result } = await executeSql({
|
|
projectRef,
|
|
connectionString,
|
|
sql,
|
|
queryKey: ['policy', 'create'],
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
type DatabasePolicyCreateData = Awaited<ReturnType<typeof createDatabasePolicy>>
|
|
|
|
export const useDatabasePolicyCreateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<DatabasePolicyCreateData, ResponseError, DatabasePolicyCreateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DatabasePolicyCreateData, ResponseError, DatabasePolicyCreateVariables>({
|
|
mutationFn: (vars) => createDatabasePolicy(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: databasePoliciesKeys.list(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to create database policy: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|