0

Create new column

by
Published Apr 8, 2025

Adds a new column to the table. The body of the request should contain the column definition.

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

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