@toolprint/hypertool-mcp
Version:
HyperTool MCP proxy server for routing requests between clients and multiple underlying MCP servers
112 lines • 3.26 kB
JavaScript
/**
* Database service using file-based implementation
*/
import { FileDatabaseService } from "./fileDatabaseService.js";
import { createChildLogger } from "../utils/logging.js";
const logger = createChildLogger({ module: "DatabaseService" });
/**
* Database service that uses file-based implementation
*/
export class CompositeDatabaseService {
implementation;
isInitialized = false;
constructor() {
this.implementation = new FileDatabaseService();
}
/**
* Get the servers repository
*/
get servers() {
if (!this.isInitialized) {
throw new Error("Database not initialized. Call init() first.");
}
return this.implementation.servers;
}
/**
* Get the groups repository
*/
get groups() {
if (!this.isInitialized) {
throw new Error("Database not initialized. Call init() first.");
}
return this.implementation.groups;
}
/**
* Get the config sources repository
*/
get configSources() {
if (!this.isInitialized) {
throw new Error("Database not initialized. Call init() first.");
}
return this.implementation.configSources;
}
/**
* Initialize the database
*/
async init() {
if (this.isInitialized) {
logger.warn("Database already initialized");
return;
}
logger.debug("Initializing database service");
// Initialize the file-based implementation
await this.implementation.init();
this.isInitialized = true;
logger.info("Database service initialized successfully");
}
/**
* Close the database
*/
async close() {
if (!this.isInitialized) {
return;
}
await this.implementation.close();
this.isInitialized = false;
logger.info("Database service closed successfully");
}
/**
* Reset the database for testing purposes
* Only available in test environment
*/
resetForTesting() {
if (process.env.NODE_ENV !== "test" &&
!process.env.NODE_ENV?.includes("test")) {
throw new Error("resetForTesting() can only be called in test environment");
}
this.isInitialized = false;
this.implementation = new FileDatabaseService();
}
/**
* Get the underlying implementation type (for debugging/testing)
*/
getImplementationType() {
return "file";
}
}
// Singleton instance
let instance = null;
/**
* Get the singleton database service instance
*/
export function getCompositeDatabaseService() {
if (!instance) {
instance = new CompositeDatabaseService();
}
return instance;
}
/**
* Reset the singleton instance for testing
* Only available in test environment
*/
export function resetCompositeDatabaseServiceForTesting() {
if (process.env.NODE_ENV !== "test" &&
!process.env.NODE_ENV?.includes("test")) {
throw new Error("resetCompositeDatabaseServiceForTesting() can only be called in test environment");
}
if (instance) {
instance.resetForTesting();
instance = null;
}
}
//# sourceMappingURL=compositeDatabaseService.js.map