0

Get the database schema

by
Published Apr 8, 2025

Retrieves the schema from the specified database. The `lsn` and `timestamp` values cannot be specified at the same time. If both are omitted, the database schema is retrieved from database's head.

Script neondb Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Neondb = {
3
	apiKey: string
4
}
5
/**
6
 * Get the database schema
7
 * Retrieves the schema from the specified database. The `lsn` and `timestamp` values cannot be specified at the same time. If both are omitted, the database schema is retrieved from database's head.
8
 */
9
export async function main(
10
	auth: Neondb,
11
	project_id: string,
12
	branch_id: string,
13
	db_name: string | undefined,
14
	lsn: string | undefined,
15
	timestamp: string | undefined
16
) {
17
	const url = new URL(
18
		`https://console.neon.tech/api/v2/projects/${project_id}/branches/${branch_id}/schema`
19
	)
20
	for (const [k, v] of [
21
		['db_name', db_name],
22
		['lsn', lsn],
23
		['timestamp', timestamp]
24
	]) {
25
		if (v !== undefined && v !== '' && k !== undefined) {
26
			url.searchParams.append(k, v)
27
		}
28
	}
29
	const response = await fetch(url, {
30
		method: 'GET',
31
		headers: {
32
			Authorization: 'Bearer ' + auth.apiKey
33
		},
34
		body: undefined
35
	})
36
	if (!response.ok) {
37
		const text = await response.text()
38
		throw new Error(`${response.status} ${text}`)
39
	}
40
	return await response.json()
41
}
42