0

Create Database branch

by
Published Apr 8, 2025
Script xata Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Xata = {
3
	apiKey: string
4
	workspaceUrl: string
5
}
6
/**
7
 * Create Database branch
8
 *
9
 */
10
export async function main(
11
	auth: Xata,
12
	db_branch_name: string,
13
	from: string | undefined,
14
	body: {
15
		from?: string
16
		metadata?: {
17
			repository?: string
18
			branch?: string
19
			stage?: string
20
			labels?: string[]
21
		}
22
	}
23
) {
24
	const url = new URL(`${auth.workspaceUrl}/db/${db_branch_name}`)
25
	for (const [k, v] of [['from', from]]) {
26
		if (v !== undefined && v !== '' && k !== undefined) {
27
			url.searchParams.append(k, v)
28
		}
29
	}
30
	const response = await fetch(url, {
31
		method: 'PUT',
32
		headers: {
33
			'Content-Type': 'application/json',
34
			Authorization: 'Bearer ' + auth.apiKey
35
		},
36
		body: JSON.stringify(body)
37
	})
38
	if (!response.ok) {
39
		const text = await response.text()
40
		throw new Error(`${response.status} ${text}`)
41
	}
42
	return await response.json()
43
}
44