0

Compare branch with user 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
 * Compare branch with user schema.
8
 *
9
 */
10
export async function main(
11
	auth: Xata,
12
	db_branch_name: string,
13
	body: {
14
		schema: {
15
			tables: {
16
				id?: string
17
				name: string
18
				columns: {
19
					name: string
20
					type:
21
						| 'string'
22
						| 'object'
23
						| 'bool'
24
						| 'int'
25
						| 'float'
26
						| 'text'
27
						| 'email'
28
						| 'multiple'
29
						| 'link'
30
						| 'datetime'
31
						| 'vector'
32
						| 'file[]'
33
						| 'file'
34
						| 'json'
35
					link?: { table: string }
36
					vector?: { dimension: number }
37
					file?: { defaultPublicAccess?: false | true }
38
					'file[]'?: { defaultPublicAccess?: false | true }
39
					notNull?: false | true
40
					defaultValue?: string
41
					unique?: false | true
42
					columns?: {}[]
43
				}[]
44
				revLinks?: { table: string; column: string }[]
45
			}[]
46
			tablesOrder?: string[]
47
		}
48
		schemaOperations?:
49
			| { addTable: { table: string } }
50
			| { removeTable: { table: string } }
51
			| { renameTable: { oldName: string; newName: string } }
52
			| {
53
					addColumn: {
54
						table: string
55
						column: {
56
							name: string
57
							type:
58
								| 'string'
59
								| 'object'
60
								| 'bool'
61
								| 'int'
62
								| 'float'
63
								| 'text'
64
								| 'email'
65
								| 'multiple'
66
								| 'link'
67
								| 'datetime'
68
								| 'vector'
69
								| 'file[]'
70
								| 'file'
71
								| 'json'
72
							link?: { table: string }
73
							vector?: { dimension: number }
74
							file?: { defaultPublicAccess?: false | true }
75
							'file[]'?: { defaultPublicAccess?: false | true }
76
							notNull?: false | true
77
							defaultValue?: string
78
							unique?: false | true
79
							columns?: {}[]
80
						}
81
					}
82
			  }
83
			| { removeColumn: { table: string; column: string } }
84
			| { renameColumn: { table: string; oldName: string; newName: string } }[]
85
		branchOperations?:
86
			| { addTable: { table: string } }
87
			| { removeTable: { table: string } }
88
			| { renameTable: { oldName: string; newName: string } }
89
			| {
90
					addColumn: {
91
						table: string
92
						column: {
93
							name: string
94
							type:
95
								| 'string'
96
								| 'object'
97
								| 'bool'
98
								| 'int'
99
								| 'float'
100
								| 'text'
101
								| 'email'
102
								| 'multiple'
103
								| 'link'
104
								| 'datetime'
105
								| 'vector'
106
								| 'file[]'
107
								| 'file'
108
								| 'json'
109
							link?: { table: string }
110
							vector?: { dimension: number }
111
							file?: { defaultPublicAccess?: false | true }
112
							'file[]'?: { defaultPublicAccess?: false | true }
113
							notNull?: false | true
114
							defaultValue?: string
115
							unique?: false | true
116
							columns?: {}[]
117
						}
118
					}
119
			  }
120
			| { removeColumn: { table: string; column: string } }
121
			| { renameColumn: { table: string; oldName: string; newName: string } }[]
122
	}
123
) {
124
	const url = new URL(`${auth.workspaceUrl}/db/${db_branch_name}/schema/compare`)
125

126
	const response = await fetch(url, {
127
		method: 'POST',
128
		headers: {
129
			'Content-Type': 'application/json',
130
			Authorization: 'Bearer ' + auth.apiKey
131
		},
132
		body: JSON.stringify(body)
133
	})
134
	if (!response.ok) {
135
		const text = await response.text()
136
		throw new Error(`${response.status} ${text}`)
137
	}
138
	return await response.json()
139
}
140