0

Update table 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 table schema
8
 *
9
 */
10
export async function main(
11
	auth: Xata,
12
	db_branch_name: string,
13
	table_name: string,
14
	body: {
15
		columns: {
16
			name: string
17
			type:
18
				| 'string'
19
				| 'object'
20
				| 'bool'
21
				| 'int'
22
				| 'float'
23
				| 'text'
24
				| 'email'
25
				| 'multiple'
26
				| 'link'
27
				| 'datetime'
28
				| 'vector'
29
				| 'file[]'
30
				| 'file'
31
				| 'json'
32
			link?: { table: string }
33
			vector?: { dimension: number }
34
			file?: { defaultPublicAccess?: false | true }
35
			'file[]'?: { defaultPublicAccess?: false | true }
36
			notNull?: false | true
37
			defaultValue?: string
38
			unique?: false | true
39
			columns?: {}[]
40
		}[]
41
	}
42
) {
43
	const url = new URL(`${auth.workspaceUrl}/db/${db_branch_name}/tables/${table_name}/schema`)
44

45
	const response = await fetch(url, {
46
		method: 'PUT',
47
		headers: {
48
			'Content-Type': 'application/json',
49
			Authorization: 'Bearer ' + auth.apiKey
50
		},
51
		body: JSON.stringify(body)
52
	})
53
	if (!response.ok) {
54
		const text = await response.text()
55
		throw new Error(`${response.status} ${text}`)
56
	}
57
	return await response.json()
58
}
59