0

Add a New Connection Pool (PostgreSQL)

by
Published Dec 20, 2024

For PostgreSQL database clusters, connection pools can be used to allow a database to share its idle connections.

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 Connection Pool (PostgreSQL)
7
 * For PostgreSQL database clusters, connection pools can be used to allow a
8
database to share its idle connections.
9
 */
10
export async function main(
11
  auth: Digitalocean,
12
  database_cluster_uuid: string,
13
  body: {
14
    name: string;
15
    mode: string;
16
    size: number;
17
    db: string;
18
    user?: string;
19
    connection?: {
20
      uri?: string;
21
      database?: string;
22
      host?: string;
23
      port?: number;
24
      user?: string;
25
      password?: string;
26
      ssl?: false | true;
27
    } & {};
28
    private_connection?: {
29
      uri?: string;
30
      database?: string;
31
      host?: string;
32
      port?: number;
33
      user?: string;
34
      password?: string;
35
      ssl?: false | true;
36
    } & {};
37
    standby_connection?: {
38
      uri?: string;
39
      database?: string;
40
      host?: string;
41
      port?: number;
42
      user?: string;
43
      password?: string;
44
      ssl?: false | true;
45
    } & {};
46
    standby_private_connection?: {
47
      uri?: string;
48
      database?: string;
49
      host?: string;
50
      port?: number;
51
      user?: string;
52
      password?: string;
53
      ssl?: false | true;
54
    } & {};
55
  },
56
) {
57
  const url = new URL(
58
    `https://api.digitalocean.com/v2/databases/${database_cluster_uuid}/pools`,
59
  );
60

61
  const response = await fetch(url, {
62
    method: "POST",
63
    headers: {
64
      "Content-Type": "application/json",
65
      Authorization: "Bearer " + auth.token,
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