0

Add a New Database

by
Published Dec 20, 2024

To add a new database to an existing cluster, send a POST request to `/v2/databases/$DATABASE_ID/dbs`. Note: Database management is not supported for Redis clusters. The response will be a JSON object with a key called `db`. The value of this will be an object that contains the standard attributes associated with a database.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Add a New Database
7
 * To add a new database to an existing cluster, send a POST request to
8
`/v2/databases/$DATABASE_ID/dbs`.
9

10
Note: Database management is not supported for Redis clusters.
11

12
The response will be a JSON object with a key called `db`. The value of this will be
13
an object that contains the standard attributes associated with a database.
14

15
 */
16
export async function main(
17
  auth: Digitalocean,
18
  database_cluster_uuid: string,
19
  body: { name: string },
20
) {
21
  const url = new URL(
22
    `https://api.digitalocean.com/v2/databases/${database_cluster_uuid}/dbs`,
23
  );
24

25
  const response = await fetch(url, {
26
    method: "POST",
27
    headers: {
28
      "Content-Type": "application/json",
29
      Authorization: "Bearer " + auth.token,
30
    },
31
    body: JSON.stringify(body),
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39