0

Update table

by
Published Apr 8, 2025

Update table. Currently there is only one update operation supported: renaming the table by providing a new name. In the example below, we rename a table from “users” to “people”: ```json // PATCH /db/test:main/tables/users { "name": "people" } ```

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
8
 * Update table. Currently there is only one update operation supported: renaming the table by providing a new name.
9

10
In the example below, we rename a table from “users” to “people”:
11

12
```json
13
// PATCH /db/test:main/tables/users
14

15
{
16
  "name": "people"
17
}
18
```
19
 */
20
export async function main(
21
	auth: Xata,
22
	db_branch_name: string,
23
	table_name: string,
24
	body: { name: string }
25
) {
26
	const url = new URL(`${auth.workspaceUrl}/db/${db_branch_name}/tables/${table_name}`)
27

28
	const response = await fetch(url, {
29
		method: 'PATCH',
30
		headers: {
31
			'Content-Type': 'application/json',
32
			Authorization: 'Bearer ' + auth.apiKey
33
		},
34
		body: JSON.stringify(body)
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