**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.
This commit is contained in:
parent
243a5f65f4
commit
faf3b1f307
1 changed files with 34 additions and 0 deletions
34
src/cli.ts
34
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('<filename>', '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',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue