メインコンテンツへスキップ
データベースは api.databases モジュールによって公開されます。これは v4 で追加されました。Standard、Pro、Enterprise プランで利用できます。

データベースの作成

api.databases.create(options) は新しいデータベースをプロビジョニングします。passwordcertificate作成時にのみ 返されます。今すぐ保存してください。
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);     // 一度だけ表示される — 安全に保存すること
console.log(created.certificate);  // 一度だけ表示される — 安全に保存すること
FieldTypeDescription
namestring表示名
memorynumber割り当てるメモリ (MB)
typestringエンジンスラッグ (例: mongopostgresmysql)
versionstringエンジンバージョン (例: "7.0")

データベースの一覧取得

api.user.get() は、あなたが所有するすべてのデータベースの Collectionuser.databases に設定します:
const user = await api.user.get();

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

単一のデータベースの取得

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);

ライフサイクル

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

ステータスとメトリクス

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)`);

すべてのデータベースの概要ステータス

const all = await api.databases.statusAll();

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

    // .fetch() で完全なステータスに昇格させる
    if (summary.running) {
        const full = await summary.fetch();
        console.log(full.usage);
    }
}

更新

db.update(options) は表示名やメモリ割り当てを変更します。少なくとも 1 つのフィールドを指定する必要があります。
await db.update({ name: "primary-db", ram: 1024 });

認証情報

db.credentials.certificate() は TLS 証明書(base64 エンコードされた PEM)を返します:
const cert = await db.credentials.certificate();
console.log(`Certificate length: ${cert.length}`);
db.credentials.reset(type) は、パスワードまたは証明書のいずれかをローテーションします。
// パスワードをローテーションする — 新しい値は一度だけ表示される
const { password } = await db.credentials.reset("password");
console.log(`New password: ${password}`);

// 証明書をローテーションする — 新しいものは .certificate() で取得する
await db.credentials.reset("certificate");
const newCert = await db.credentials.certificate();
db.credentials.certificate()db.credentials.reset() は、データベースが 稼働中 であることを必要とします。

Snapshots

db.snapshots は、アプリケーションの snapshot API をミラーリングします。
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",
);
app.snapshots.restore({ snapshotId, versionId }) とは異なり、db.snapshots.restore(snapshotId, versionId) は位置引数を取ります。

データベースの削除

データベースの削除は取り消せません。データを復元する必要があるかもしれない場合は、最新の snapshot があることを確認してください。
await db.delete();