@agentdb/sdk
Version:
JavaScript SDK for AgentDB database service
161 lines (135 loc) โข 5.31 kB
JavaScript
import { DatabaseService } from "../index.js";
import { statSync } from "fs";
// Configuration - replace with your actual values or set your environment variables
const BASE_URL = process.env.AGENTDB_BASE_URL || "https://api.agentdb.dev";
const API_KEY = process.env.AGENTDB_API_KEY || "your-api-key-here";
const TOKEN = process.env.AGENTDB_TOKEN || "your-uuid-token-here";
const dbName = "my_custom_db_name";
const templateName = "my_custom_template_name";
/**
* AgentDB SDK Upload Flow Example
*/
/**
* Using custom names
*/
async function uploadDemoWorkflow(filePath) {
console.log("=== AgentDB SDK Upload Flow Example ===");
console.log(`Processing file: ${filePath}\n`);
const service = new DatabaseService(BASE_URL, API_KEY);
try {
// Option 1: Custom names (must be unique - will fail if they exist)
const result = await service.uploadAndCreateTemplate(
TOKEN,
filePath,
{
dbName,
templateName,
onProgress: (step, message) => {
console.log(`Step ${step}: ${message}`);
},
}
);
console.log("\n๐ Upload and template creation completed successfully!");
console.log(`Created Database: ${result.dbName}`);
console.log(`Created Template: ${result.templateName}`);
return result;
} catch (error) {
console.error("\nโ Workflow failed:", error.message);
throw error; // Let main() handle any additional error processing
}
}
/**
* Using auto-generated names
*/
async function uploadWithAutoNames(filePath) {
console.log("=== Auto-Generated Names Example ===");
console.log(`Processing file: ${filePath}\n`);
const service = new DatabaseService(BASE_URL, API_KEY);
try {
const result = await service.uploadAndCreateTemplate(
TOKEN,
filePath,
{
onProgress: (step, message) => {
console.log(`Step ${step}: ${message}`);
},
}
);
console.log("\n๐ Upload completed with auto-generated names!");
console.log(`Created Database: ${result.dbName}`);
console.log(`Created Template: ${result.templateName}`);
console.log(`Description: ${result.description?.substring(0, 100)}...`);
return result;
} catch (error) {
console.error("\nโ Workflow failed:", error.message);
throw error;
}
}
async function main() {
// Check if file path is provided as command line argument
const filePath = process.argv[2];
const useCustomNames = process.argv[3] === '--custom-names';
if (!filePath) {
console.log("Usage: node upload-a-file.js <file-path> [--custom-names]");
console.log("");
console.log("Examples:");
console.log(" node upload-a-file.js ./data/sample.csv");
console.log(" node upload-a-file.js ./data/sample.csv --custom-names");
console.log(" node upload-a-file.js ./data/database.sqlite");
console.log("");
console.log("Options:");
console.log(" --custom-names Use predefined custom names (may fail if they exist)");
console.log(" (default) Use auto-generated names with conflict resolution");
console.log("");
console.log("Make sure to set these environment variables:");
console.log(" AGENTDB_API_KEY (required)");
console.log(" AGENTDB_TOKEN (required)");
process.exit(1);
}
if (API_KEY === "your-api-key-here") {
console.error("โ Please set AGENTDB_API_KEY environment variable");
process.exit(1);
}
if (TOKEN === "your-uuid-token-here") {
console.error("โ Please set AGENTDB_TOKEN environment variable");
process.exit(1);
}
try {
// Check if file exists
statSync(filePath);
// Choose workflow based on command line option
const result = useCustomNames
? await uploadDemoWorkflow(filePath)
: await uploadWithAutoNames(filePath);
console.log("\nโ
Demo completed successfully!");
console.log("\nNext steps:");
console.log(`1. Connect: service.connect('${TOKEN}', '${result.dbName}', 'sqlite')`);
console.log(`2. Query: connection.execute(query, [], '${result.templateName}')`);
console.log(`3. Natural language: connection.naturalLanguageToSql('show me the data', null, '${result.templateName}')`);
} catch (error) {
if (error.code === "ENOENT") {
console.error(`โ File not found: ${filePath}`);
} else {
console.error(`โ Error: ${error.message}`);
// Only cleanup if it's a workflow error (not a file/validation error)
if (error.message.includes("already exists") || error.message.includes("Template") || error.message.includes("Database")) {
console.log("\n๐งน Cleaning up any partial resources...");
const service = new DatabaseService(BASE_URL, API_KEY);
try {
await service.deleteDatabase(TOKEN, dbName, "sqlite");
console.log(`Deleted database: ${dbName}`);
} catch (cleanupError) {
console.log(`Note: Could not delete database ${dbName} (may not exist)`);
}
try {
await service.deleteTemplate(templateName);
console.log(`Deleted template: ${templateName}`);
} catch (cleanupError) {
console.log(`Note: Could not delete template ${templateName} (may not exist)`);
}
}
}
process.exit(1);
}
}
main();