From faf3b1f307d6311ee810819c7de910ad6c86ca54 Mon Sep 17 00:00:00 2001 From: David Snelling Date: Fri, 20 Jun 2025 10:58:11 -0700 Subject: [PATCH] **feat: add `import-sparse` command to CLI for importing sparse data** ### Changes: - Introduced the `import-sparse` command in `src/cli.ts`: - Allows importing sparse data from a JSON file into the database. - Includes an option (`--clear`) to clear existing data before import. - Displays counts of imported nouns and verbs upon successful completion. - Updated CLI help and autocompletion configuration to support the new command. ### Purpose: Enhanced the CLI by adding `import-sparse`, providing users with the ability to efficiently import sparse data into the database. This addition aligns with the application's functionality for managing sparse data operations. --- src/cli.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/cli.ts b/src/cli.ts index 755c80c5..803322a8 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -382,6 +382,36 @@ program } }) +program + .command('import-sparse') + .description('Import sparse data (without vectors) from a JSON file into the database') + .argument('', 'Input JSON file') + .option('-c, --clear', 'Clear existing data before importing', false) + .action(async (filename, options) => { + try { + const db = createDb() + await db.init() + + // Read the file + if (!fs.existsSync(filename)) { + console.error(`File not found: ${filename}`) + process.exit(1) + } + + const fileContent = fs.readFileSync(filename, 'utf8') + const data = JSON.parse(fileContent) + + // Import the sparse data + const result = await db.importSparseData(data, { clearExisting: options.clear }) + + console.log(`Sparse data imported successfully from ${filename}`) + console.log(`Imported ${result.nounsRestored} nouns and ${result.verbsRestored} verbs`) + } catch (error) { + console.error('Error:', (error as Error).message) + process.exit(1) + } + }) + program .command('visualize') .description('Visualize the graph structure in ASCII format') @@ -767,6 +797,7 @@ completion.tree({ 'generate-random-graph', 'backup', 'restore', + 'import-sparse', 'completion-setup', 'init', 'help', @@ -808,6 +839,9 @@ completion.tree({ restore: { _: () => ['--clear'] }, + 'import-sparse': { + _: () => ['--clear'] + }, 'generate-random-graph': { _: () => [ '--noun-count 10',