0

Create a Read-only Replica

by
Published Dec 20, 2024

To create a read-only replica for a PostgreSQL or MySQL database cluster, send a POST request to `/v2/databases/$DATABASE_ID/replicas` specifying the name it should be given, the size of the node to be used, and the region where it will be located.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Create a Read-only Replica
7
 * To create a read-only replica for a PostgreSQL or MySQL database cluster, send a POST request to `/v2/databases/$DATABASE_ID/replicas` specifying the name it should be given, the size of the node to be used, and the region where it will be located.
8
 */
9
export async function main(
10
  auth: Digitalocean,
11
  database_cluster_uuid: string,
12
  body: {
13
    id?: string;
14
    name: string;
15
    region?: string;
16
    size?: string;
17
    status?: "creating" | "online" | "resizing" | "migrating" | "forking";
18
    tags?: string[];
19
    created_at?: string;
20
    private_network_uuid?: string;
21
    connection?: {} & {
22
      uri?: string;
23
      database?: string;
24
      host?: string;
25
      port?: number;
26
      user?: string;
27
      password?: string;
28
      ssl?: false | true;
29
    };
30
    private_connection?: {} & {
31
      uri?: string;
32
      database?: string;
33
      host?: string;
34
      port?: number;
35
      user?: string;
36
      password?: string;
37
      ssl?: false | true;
38
    };
39
    storage_size_mib?: number;
40
  },
41
) {
42
  const url = new URL(
43
    `https://api.digitalocean.com/v2/databases/${database_cluster_uuid}/replicas`,
44
  );
45

46
  const response = await fetch(url, {
47
    method: "POST",
48
    headers: {
49
      "Content-Type": "application/json",
50
      Authorization: "Bearer " + auth.token,
51
    },
52
    body: JSON.stringify(body),
53
  });
54
  if (!response.ok) {
55
    const text = await response.text();
56
    throw new Error(`${response.status} ${text}`);
57
  }
58
  return await response.json();
59
}
60