@dataql/node
Version:
DataQL core SDK for unified data management with MongoDB and GraphQL - Production Multi-Cloud Ready
87 lines (86 loc) • 2.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IntrospectionService = void 0;
const MongoDBIntrospector_js_1 = require("./MongoDBIntrospector.js");
/**
* Main introspection service that routes to appropriate database-specific introspectors
*/
class IntrospectionService {
/**
* Create introspector for the given database connection
*/
static createIntrospector(connection, options = {}) {
const databaseType = this.detectDatabaseType(connection.url);
switch (databaseType) {
case "mongodb":
return new MongoDBIntrospector_js_1.MongoDBIntrospector(connection, options);
default:
throw new Error(`Unsupported database type: ${databaseType}`);
}
}
/**
* Convenience method to introspect a database directly
*/
static async introspect(connection, options = {}) {
const introspector = this.createIntrospector(connection, options);
return await introspector.introspect();
}
/**
* Detect database type from connection URL
*/
static detectDatabaseType(url) {
const urlLower = url.toLowerCase();
if (urlLower.startsWith("mongodb://") ||
urlLower.startsWith("mongodb+srv://")) {
return "mongodb";
}
if (urlLower.startsWith("postgresql://") ||
urlLower.startsWith("postgres://")) {
return "postgresql";
}
if (urlLower.startsWith("mysql://")) {
return "mysql";
}
if (urlLower.startsWith("sqlite://") || urlLower.startsWith("file:")) {
return "sqlite";
}
if (urlLower.includes("cloudflare.com") && urlLower.includes("/d1/")) {
return "d1";
}
throw new Error(`Unable to detect database type from URL: ${url}`);
}
/**
* Get supported database types
*/
static getSupportedDatabaseTypes() {
return ["mongodb"]; // Will expand as we add more introspectors
}
/**
* Check if a database type is supported
*/
static isDatabaseTypeSupported(type) {
return this.getSupportedDatabaseTypes().includes(type.toLowerCase());
}
/**
* Validate database connection URL
*/
static validateConnectionUrl(url) {
try {
const databaseType = this.detectDatabaseType(url);
if (!this.isDatabaseTypeSupported(databaseType)) {
return {
valid: false,
error: `Database type '${databaseType}' is not yet supported. Supported types: ${this.getSupportedDatabaseTypes().join(", ")}`,
};
}
return { valid: true };
}
catch (error) {
return {
valid: false,
error: error.message,
};
}
}
}
exports.IntrospectionService = IntrospectionService;