0

Add a Database User

by
Published Dec 20, 2024

To add a new database user, send a POST request to `/v2/databases/$DATABASE_ID/users` with the desired username.

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 Database User
7
 * To add a new database user, send a POST request to `/v2/databases/$DATABASE_ID/users`
8
with the desired username.
9
 */
10
export async function main(
11
  auth: Digitalocean,
12
  database_cluster_uuid: string,
13
  body: {
14
    name: string;
15
    role?: "primary" | "normal";
16
    password?: string;
17
    access_cert?: string;
18
    access_key?: string;
19
    mysql_settings?: {
20
      auth_plugin: "mysql_native_password" | "caching_sha2_password";
21
    };
22
    settings?: {
23
      pg_allow_replication?: false | true;
24
      opensearch_acl?: {
25
        index?: string;
26
        permission?: "deny" | "admin" | "read" | "readwrite" | "write";
27
      }[];
28
      acl?: {
29
        id?: string;
30
        topic: string;
31
        permission: "admin" | "consume" | "produce" | "produceconsume";
32
      }[];
33
    };
34
  } & { readonly?: false | true },
35
) {
36
  const url = new URL(
37
    `https://api.digitalocean.com/v2/databases/${database_cluster_uuid}/users`,
38
  );
39

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