Zum Hauptinhalt springen
Datenbanken werden über das Modul api.databases bereitgestellt — eine Erweiterung in v4. Sie sind in den Plänen Standard, Pro und Enterprise verfügbar.

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.
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
FeldTypBeschreibung
namestringAnzeigename
memorynumberZugewiesener Speicher in MB
typestringEngine-Slug (z. B. mongo, postgres, mysql)
versionstringEngine-Version (z. B. "7.0")

Deine Datenbanken auflisten

api.user.get() füllt user.databases mit einer Collection aller Datenbanken, die dir gehören:
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

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

await db.start();
await db.stop();

Status und Metriken

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

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.
await db.update({ name: "primary-db", ram: 1024 });

Zugangsdaten

db.credentials.certificate() gibt das TLS-Zertifikat zurück (base64-kodiertes PEM):
const cert = await db.credentials.certificate();
console.log(`Certificate length: ${cert.length}`);
db.credentials.reset(type) rotiert entweder das Passwort oder das Zertifikat.
// 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();
db.credentials.certificate() und db.credentials.reset() erfordern, dass die Datenbank läuft.

Snapshots

db.snapshots spiegelt die Snapshots-API der Anwendungen wider.
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",
);
Anders als app.snapshots.restore({ snapshotId, versionId }) nimmt db.snapshots.restore(snapshotId, versionId) Positionsargumente entgegen.

Eine Datenbank löschen

Das Löschen einer Datenbank ist unwiderruflich. Stelle sicher, dass du einen aktuellen Snapshot hast, falls du die Daten wiederherstellen musst.
await db.delete();