@fortress-js/node
Version:
The Fortress Backend SDK
109 lines (108 loc) • 4.34 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Fortress = void 0;
const client_1 = require("./client");
const postgres_1 = require("./postgres");
class Fortress {
/**
* Create a new Fortress client
* @param orgId The organization ID
* @param apiKey The API key
*/
constructor(orgId, apiKey) {
if (!orgId) {
throw new Error("Organization ID is required");
}
if (!apiKey) {
throw new Error("API Key is required");
}
this.fortress = new client_1.Client(orgId, apiKey);
this.tenantConnectionCache = {};
}
/**
* Create a new database on the Fortress platform
* @param platform The cloud platform the database will be hosted on (aws or managed)
* @param alias Alias for the database (optional)
* @returns The ID of the created database
*/
createDatabase(platform, alias) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.fortress.createDatabase(platform, alias);
});
}
/**
* Delete a database on the Fortress platform
* @param databaseId The ID of the database to delete
*/
deleteDatabase(databaseId) {
return __awaiter(this, void 0, void 0, function* () {
yield this.fortress.deleteDatabase(databaseId);
});
}
/**
* List all databases on the Fortress platform
* @returns An array of databases
*/
listDatabases() {
return __awaiter(this, void 0, void 0, function* () {
return yield this.fortress.listDatabases();
});
}
/**
* Connect to a tenant's database
* @param tenantId The ID of the tenant
* @returns A connection to the tenant's database
*/
connectTenant(tenantId) {
return __awaiter(this, void 0, void 0, function* () {
if (this.tenantConnectionCache[tenantId]) {
return this.tenantConnectionCache[tenantId];
}
const response = yield this.fortress.getUri(tenantId, "tenant");
const connection = yield new postgres_1.PostgresClient(response.url, response.port, response.username, response.password, response.database).connect();
this.tenantConnectionCache[tenantId] = connection;
return connection;
});
}
/**
* Create a new tenant on the Fortress platform
* @param tenantId The ID of the tenant
* @param isolationLevel The isolation level for the tenant's database (shared or dedicated)
* @param platform The cloud platform the tenant's database will be hosted on (aws or managed)
* @param alias Alias for the tenant's database (optional)
* @param databaseId The ID of the database to use for the tenant (optional)
*/
createTenant(tenantId, isolationLevel, platform, alias, databaseId) {
return __awaiter(this, void 0, void 0, function* () {
yield this.fortress.createTenant(tenantId, isolationLevel, platform, alias, databaseId);
});
}
/**
* Delete a tenant on the Fortress platform
* @param tenantId The ID of the tenant to delete
*/
deleteTenant(tenantId) {
return __awaiter(this, void 0, void 0, function* () {
yield this.fortress.deleteTenant(tenantId);
});
}
/**
* List all tenants on the Fortress platform
* @returns An array of tenants
*/
listTenants() {
return __awaiter(this, void 0, void 0, function* () {
return yield this.fortress.listTenants();
});
}
}
exports.Fortress = Fortress;