0

Update Branch schema

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
 * Update Branch schema
8
 *
9
 */
10
export async function main(
11
	auth: Xata,
12
	db_branch_name: string,
13
	body: {
14
		parentID?: string
15
		operations:
16
			| { addTable: { table: string } }
17
			| { removeTable: { table: string } }
18
			| { renameTable: { oldName: string; newName: string } }
19
			| {
20
					addColumn: {
21
						table: string
22
						column: {
23
							name: string
24
							type:
25
								| 'string'
26
								| 'object'
27
								| 'bool'
28
								| 'int'
29
								| 'float'
30
								| 'text'
31
								| 'email'
32
								| 'multiple'
33
								| 'link'
34
								| 'datetime'
35
								| 'vector'
36
								| 'file[]'
37
								| 'file'
38
								| 'json'
39
							link?: { table: string }
40
							vector?: { dimension: number }
41
							file?: { defaultPublicAccess?: false | true }
42
							'file[]'?: { defaultPublicAccess?: false | true }
43
							notNull?: false | true
44
							defaultValue?: string
45
							unique?: false | true
46
							columns?: {}[]
47
						}
48
					}
49
			  }
50
			| { removeColumn: { table: string; column: string } }
51
			| { renameColumn: { table: string; oldName: string; newName: string } }[]
52
	}
53
) {
54
	const url = new URL(`${auth.workspaceUrl}/db/${db_branch_name}/schema/update`)
55

56
	const response = await fetch(url, {
57
		method: 'POST',
58
		headers: {
59
			'Content-Type': 'application/json',
60
			Authorization: 'Bearer ' + auth.apiKey
61
		},
62
		body: JSON.stringify(body)
63
	})
64
	if (!response.ok) {
65
		const text = await response.text()
66
		throw new Error(`${response.status} ${text}`)
67
	}
68
	return await response.json()
69
}
70