0

Create Database

by
Published Apr 8, 2025

Create Database with identifier name

Script xata Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Xata = {
3
	apiKey: string
4
}
5
/**
6
 * Create Database
7
 * Create Database with identifier name
8
 */
9
export async function main(
10
	auth: Xata,
11
	workspace_id: string,
12
	db_name: string,
13
	body: {
14
		branchName?: string
15
		region: string
16
		postgresEnabled?: false | true
17
		ui?: { color?: string }
18
		metadata?: {
19
			repository?: string
20
			branch?: string
21
			stage?: string
22
			labels?: string[]
23
		}
24
	}
25
) {
26
	const url = new URL(`https://api.xata.io/workspaces/${workspace_id}/dbs/${db_name}`)
27

28
	const response = await fetch(url, {
29
		method: 'PUT',
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