multibridge
Version:
A multi-database connection framework with centralized configuration
60 lines (59 loc) • 2.07 kB
JavaScript
;
/**
* Base utilities for ORM adapters
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.requireTenantContext = requireTenantContext;
exports.getTenantConnection = getTenantConnection;
exports.getTenantDBConfig = getTenantDBConfig;
exports.validateORMSupport = validateORMSupport;
const connectionManager_1 = require("../connections/connectionManager");
const tenantContext_1 = require("../context/tenantContext");
const dbConfig_1 = require("../config/dbConfig");
const errors_1 = require("../utils/errors");
/**
* Get the current tenant context or throw if not available
*/
function requireTenantContext() {
const tenant = (0, tenantContext_1.getTenant)();
if (!tenant) {
throw new errors_1.TenantContextError("No tenant context available. Use runWithTenant() to set tenant context.");
}
return tenant;
}
/**
* Get connection data for the current tenant
*/
async function getTenantConnection() {
const tenant = requireTenantContext();
const connectionData = await (0, connectionManager_1.getConnection)(tenant);
return {
tenant,
connectionData,
dbType: connectionData.dbType,
};
}
/**
* Get database configuration for the current tenant
* This is useful for ORMs that need connection details
*/
async function getTenantDBConfig() {
const tenant = requireTenantContext();
const dbConfig = await (0, dbConfig_1.fetchDBConfig)(tenant.appid, tenant.orgid);
if (!dbConfig) {
throw new errors_1.ConnectionError(`No database configuration found for appid: ${tenant.appid}, orgid: ${tenant.orgid}`, { tenant });
}
return {
tenant,
dbConfig,
dbType: dbConfig.db_type,
};
}
/**
* Validate that the database type is supported for the ORM
*/
function validateORMSupport(dbType, supportedTypes) {
if (!supportedTypes.includes(dbType)) {
throw new errors_1.ConnectionError(`Database type '${dbType}' is not supported. Supported types: ${supportedTypes.join(", ")}`, { dbType, supportedTypes });
}
}