> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Datenbanken

> Stelle Square Cloud-Datenbanken bereit, überwache und verwalte sie, inklusive Rotation von Zugangsdaten und Snapshots.

<Info>
  Datenbanken werden über das Modul `api.databases` bereitgestellt — eine Erweiterung in v4. Sie sind in den Plänen **Standard, Pro und Enterprise** verfügbar.
</Info>

## Eine Datenbank erstellen

`api.databases.create(options)` stellt eine neue Datenbank bereit. Das `password` und das `certificate` werden **nur bei der Erstellung** zurückgegeben — speichere sie jetzt.

```typescript theme={null}
const created = await api.databases.create({
    name: "my-mongo",
    memory: 512,
    type: "mongo",
    version: "7.0",
});

console.log(created.id);
console.log(created.connection_url);
console.log(created.password);     // shown once — store securely
console.log(created.certificate);  // shown once — store securely
```

| Feld      | Typ      | Beschreibung                                     |
| --------- | -------- | ------------------------------------------------ |
| `name`    | `string` | Anzeigename                                      |
| `memory`  | `number` | Zugewiesener Speicher in MB                      |
| `type`    | `string` | Engine-Slug (z. B. `mongo`, `postgres`, `mysql`) |
| `version` | `string` | Engine-Version (z. B. `"7.0"`)                   |

## Deine Datenbanken auflisten

`api.user.get()` füllt `user.databases` mit einer `Collection` aller Datenbanken, die dir gehören:

```typescript theme={null}
const user = await api.user.get();

for (const [id, db] of user.databases) {
    console.log(`${db.name} (${id}) — ${db.type} ${db.ram}MB`);
}
```

## Eine einzelne Datenbank abrufen

```typescript theme={null}
const db = await api.databases.fetch(created.id);

console.log(db.id);
console.log(db.name);
console.log(db.owner);
console.log(db.type);
console.log(db.ram);
console.log(db.cluster);
console.log(db.port);
console.log(db.createdAt);
```

## Lebenszyklus

```typescript theme={null}
await db.start();
await db.stop();
```

## Status und Metriken

```typescript theme={null}
const status = await db.getStatus();
console.log(`${status.status} • CPU ${status.usage.cpu} • RAM ${status.usage.ram}`);

const metrics = await db.getMetrics();
console.log(`${metrics.length} metric points (up to 288 over 24h)`);
```

### Zusammenfassender Status für jede Datenbank

```typescript theme={null}
const all = await api.databases.statusAll();

for (const summary of all) {
    console.log(`${summary.databaseId} → ${summary.running ? "running" : "stopped"}`);

    // Promote to the full status with .fetch()
    if (summary.running) {
        const full = await summary.fetch();
        console.log(full.usage);
    }
}
```

## Aktualisieren

`db.update(options)` ändert den Anzeigenamen und/oder die Speicherzuweisung. Mindestens ein Feld muss angegeben werden.

```typescript theme={null}
await db.update({ name: "primary-db", ram: 1024 });
```

## Zugangsdaten

`db.credentials.certificate()` gibt das TLS-Zertifikat zurück (base64-kodiertes PEM):

```typescript theme={null}
const cert = await db.credentials.certificate();
console.log(`Certificate length: ${cert.length}`);
```

`db.credentials.reset(type)` rotiert entweder das Passwort oder das Zertifikat.

```typescript theme={null}
// Rotate the password — the new value is shown only once
const { password } = await db.credentials.reset("password");
console.log(`New password: ${password}`);

// Rotate the certificate — fetch the new one via .certificate()
await db.credentials.reset("certificate");
const newCert = await db.credentials.certificate();
```

<Warning>
  `db.credentials.certificate()` und `db.credentials.reset()` erfordern, dass die Datenbank **läuft**.
</Warning>

## Snapshots

`db.snapshots` spiegelt die Snapshots-API der Anwendungen wider.

```typescript theme={null}
import { writeFile } from "node:fs/promises";

const snapshots = await db.snapshots.list();
console.log(`${snapshots.length} snapshots stored`);

const fresh = await db.snapshots.create();
console.log(`Download URL: ${fresh.url}`);

const buffer = await db.snapshots.download();
await writeFile("./db-backup.tar.gz", buffer);

await db.snapshots.restore(
    "00000000-0000-4000-8000-000000000000_mongo",
    "v1",
);
```

<Note>
  Anders als `app.snapshots.restore({ snapshotId, versionId })` nimmt `db.snapshots.restore(snapshotId, versionId)` Positionsargumente entgegen.
</Note>

## Eine Datenbank löschen

<Warning>
  Das Löschen einer Datenbank ist unwiderruflich. Stelle sicher, dass du einen aktuellen Snapshot hast, falls du die Daten wiederherstellen musst.
</Warning>

```typescript theme={null}
await db.delete();
```
