Files
supabase/apps/studio/data/vault/vault-secret-decrypted-value-query.ts
Andrew Valleteau eb7efdef7f chore(studio): move Query to pgMeta add tests (#34232)
* chore(studio): move Query to pgMeta add tests

- Move the Query builder from studio to pgMeta
- Add e2e tests over the generated sql to ensure syntax and runtime
  result over pg database
- fix bug with orde by for table with undefined column

* chore: fix query import path

* chore: set ES target for lint

* chore: add github action for pg-meta test package

* chore: add tsconfig to sparse checkout
2025-03-20 19:04:58 +00:00

60 lines
1.7 KiB
TypeScript

import { UseQueryOptions, useQuery } from '@tanstack/react-query'
import { Query } from '@supabase/pg-meta/src/query'
import { executeSql } from '../sql/execute-sql-query'
import { vaultSecretsKeys } from './keys'
export const vaultSecretDecryptedValueQuery = (id: string) => {
const sql = new Query()
.from('decrypted_secrets', 'vault')
.select('decrypted_secret')
.match({ id })
.toSql()
return sql
}
export type VaultSecretsDecryptedValueVariables = {
projectRef?: string
connectionString?: string
id: string
}
export const getDecryptedValue = async (
{ projectRef, connectionString, id }: VaultSecretsDecryptedValueVariables,
signal?: AbortSignal
) => {
const sql = vaultSecretDecryptedValueQuery(id)
const { result } = await executeSql(
{
projectRef,
connectionString,
sql,
queryKey: vaultSecretsKeys.getDecryptedValue(projectRef, id),
},
signal
)
return result
}
export type VaultSecretsDecryptedValueData = string
export type VaultSecretsDecryptedValueError = unknown
export const useVaultSecretDecryptedValueQuery = <TData = VaultSecretsDecryptedValueData>(
{ projectRef, connectionString, id }: VaultSecretsDecryptedValueVariables,
{
enabled = true,
...options
}: UseQueryOptions<VaultSecretsDecryptedValueData, VaultSecretsDecryptedValueError, TData> = {}
) =>
useQuery<VaultSecretsDecryptedValueData, VaultSecretsDecryptedValueError, TData>(
vaultSecretsKeys.getDecryptedValue(projectRef, id),
({ signal }) => getDecryptedValue({ projectRef, connectionString, id }, signal),
{
select(data) {
return (data[0] as any).decrypted_secret
},
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)