mirror of
https://github.com/supabase/supabase.git
synced 2026-10-07 10:25:06 +03:00
* feat: add `crypto-js`, `encryptString` with sample key * feat: include POSTGRES_PASSWORD in generated .env.test * feat: include POSTGRES_PASSWORD in turbo.json for studio * feat: read only query support * feat: configurable `POSTGRES_HOST`, `POSTGRES_DB`, `POSTGRES_PORT` * chore: rename POSTGRES_USER to clarify write permission * feat: configurable `PG_META_CRYPTO_KEY` * chore: add `PG_META_CRYPTO_KEY` to generateLocalEnv * feat: add 'postgres-meta' to linter dictionary * feat: restore read-only toggle in local MCP URL builder
54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { PG_META_URL } from 'lib/constants/index'
|
|
import { constructHeaders } from '../apiHelpers'
|
|
import { PgMetaDatabaseError, databaseErrorSchema, WrappedResult } from './types'
|
|
import { assertSelfHosted, encryptString, getConnectionString } from './util'
|
|
|
|
export type QueryOptions = {
|
|
query: string
|
|
readOnly?: boolean
|
|
headers?: HeadersInit
|
|
}
|
|
|
|
/**
|
|
* Executes a SQL query against the self-hosted Postgres instance via pg-meta service.
|
|
*
|
|
* _Only call this from server-side self-hosted code._
|
|
*/
|
|
export async function executeQuery<T = unknown>({
|
|
query,
|
|
readOnly = false,
|
|
headers,
|
|
}: QueryOptions): Promise<WrappedResult<T[]>> {
|
|
assertSelfHosted()
|
|
|
|
const connectionString = getConnectionString({ readOnly })
|
|
const connectionStringEncrypted = encryptString(connectionString)
|
|
|
|
const response = await fetch(`${PG_META_URL}/query`, {
|
|
method: 'POST',
|
|
headers: constructHeaders({
|
|
...headers,
|
|
'Content-Type': 'application/json',
|
|
'x-connection-encrypted': connectionStringEncrypted,
|
|
}),
|
|
body: JSON.stringify({ query }),
|
|
})
|
|
|
|
try {
|
|
const result = await response.json()
|
|
|
|
if (!response.ok) {
|
|
const { message, code, formattedError } = databaseErrorSchema.parse(result)
|
|
const error = new PgMetaDatabaseError(message, code, response.status, formattedError)
|
|
return { data: undefined, error }
|
|
}
|
|
|
|
return { data: result, error: undefined }
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
return { data: undefined, error }
|
|
}
|
|
throw error
|
|
}
|
|
}
|