@bhagat-surya-dev/dashchat-database-manager
Version:
AI-powered database schema analysis and management library
103 lines โข 4.75 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const DatabaseManager_1 = __importDefault(require("./DatabaseManager"));
const dotenv = __importStar(require("dotenv"));
const fs = __importStar(require("fs/promises"));
dotenv.config();
/**
* Main function to run the database analysis process.
*/
async function main() {
// --- 1. Configuration ---
// It's recommended to load these from environment variables (.env file)
const cerebrasApiKey = "csk-j54r63jt99m6rw43evwtv9ek3wr2pwtcndhh9h585fettw4m";
const databaseUrl = 'mongodb://admin:password123@localhost:27017/ecommerce_startup?authSource=admin';
if (!cerebrasApiKey) {
console.error('โ Cerebras API key not found. Please set it in your .env file or directly in the script.');
return;
}
// Initialize the DatabaseManager with your configuration
const dbManager = new DatabaseManager_1.default({
cerebrasApiKey: cerebrasApiKey,
databaseUrl: databaseUrl,
model: "qwen-3-235b-a22b-instruct-2507" // Specify the model you want to use
});
// --- 2. Test Database Connection ---
console.log('๐งช Testing database connection...');
try {
const isConnected = await dbManager.testConnection();
if (isConnected) {
console.log('โ
Connection successful!');
}
else {
// The testConnection method will log detailed errors internally
console.error('โ Connection failed. Please check your database URL, credentials, and network access.');
}
}
catch (error) {
console.error('โ Error during connection test:', error);
}
// --- 3. Perform AI Schema Analysis and Save to File ---
console.log('\n๐ค Performing AI-powered schema analysis...');
try {
// A unique ID for your database, used by the manager for caching or logging
const databaseId = 'ecommerce_startup_db';
// Call the analysis function to get AI-generated descriptions for your schema
const analyzedSchema = await dbManager.analyzeAndCacheSchema(databaseUrl, databaseId);
// Check if the analysis returned any data before proceeding
if (!analyzedSchema || analyzedSchema.tables.length === 0) {
console.warn('โ ๏ธ AI analysis did not return any table data. This could be due to an empty database or permissions issues. Halting.');
return;
}
// Convert the schema object into a nicely formatted JSON string
const schemaJson = JSON.stringify(analyzedSchema, null, 2);
// Create a timestamp string (e.g., "2025-08-14T19-30-00") to make the filename unique
const timestamp = new Date().toISOString().replace(/:/g, '-').slice(0, 19);
const outputFilename = `schema_analysis_${timestamp}.json`;
// Asynchronously write the JSON string to the new timestamped file
await fs.writeFile(outputFilename, schemaJson);
console.log(`โ
AI-analyzed schema successfully saved to ${outputFilename}`);
}
catch (error) {
console.error('โ An error occurred during AI analysis or file saving:', error);
}
}
// Run the main function
main();
//# sourceMappingURL=example.js.map