Skip to Content

Database

Manage PostgreSQL, Redis, and MongoDB instances with connection helpers.

import { EspaceTech } from "@espace-tech/sdk"; const client = new EspaceTech({ apiToken: process.env.ESPACE_TECH_TOKEN! });

Get Connection

The easiest way to connect — returns a ready-to-use connection string.

const conn = await client.database.getConnection("db-id"); console.log(conn.url); // Full connection string

PostgreSQL

import { Pool } from "pg"; const conn = await client.database.getConnection("db-id"); const pool = new Pool({ connectionString: conn.url }); const result = await pool.query("SELECT NOW()");

With Prisma — set DATABASE_URL in your .env:

.env
DATABASE_URL=postgresql://u_mydb:pass@host:5432/db_mydb

MongoDB

import mongoose from "mongoose"; const conn = await client.database.getConnection("db-id"); await mongoose.connect(conn.url);

Redis

import Redis from "ioredis"; const conn = await client.database.getConnection("db-id"); const redis = new Redis(conn.url); await redis.set("key", "value");

Returns: ConnectionConfig

PropertyTypeDescription
urlstringFull connection string
hoststringDatabase host
portnumberDatabase port
usernamestringUsername
passwordstringPassword
databasestringDatabase name (PG/Mongo only)

Get Credentials

Get raw credentials without a connection string.

const creds = await client.database.getCredentials("db-id"); console.log(creds.host, creds.port, creds.username, creds.password);

Create Database

const db = await client.database.create({ name: "my-postgres", project_id: "project-id", engine: "postgresql", // "postgresql" | "redis" | "mongodb" });

List Databases

const databases = await client.database.list(); for (const db of databases) { console.log(db.name, db.engine, db.status); }

Get Database

const db = await client.database.get("db-id");

Stop / Start

await client.database.stop("db-id"); await client.database.start("db-id");

Expose / Unexpose

Enable or disable external access:

// Enable external access (accessible at *.db.espace-tech.com) await client.database.expose("db-id"); // Disable external access await client.database.unexpose("db-id");

Link a database to a project to auto-inject connection environment variables:

await client.database.link("db-id", "project-id"); await client.database.unlink("db-id", "project-id");

Rotate Credentials

Generate a new password. Linked projects are updated automatically.

await client.database.rotate("db-id");

Metrics

const metrics = await client.database.getMetrics("db-id"); console.log(metrics.connections); // Active connections console.log(metrics.size); // Storage used

Backups

Create Backup

const backup = await client.database.createBackup("db-id"); console.log(backup.id, backup.size, backup.created_at);

List Backups

const backups = await client.database.listBackups("db-id");

Restore from Backup

await client.database.restoreBackup("db-id", "backup-id");

Restoring a backup overwrites the current database contents.

Delete Backup

await client.database.deleteBackup("db-id", "backup-id");

Delete Database

await client.database.delete("db-id");

This permanently deletes the database and all data. Create a backup first.