0

Apply edit script.

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

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