diff --git a/src/manual_mode/json_interface.ts b/src/manual_mode/json_interface.ts new file mode 100644 index 0000000000000000000000000000000000000000..48bb9d265943414d019a568e2af0fd363a7482d3 --- /dev/null +++ b/src/manual_mode/json_interface.ts @@ -0,0 +1,75 @@ +import { Neo4jConnection, type DbConnection } from "ts-common"; +import { queryService } from "../readers/queryService"; +import * as fs from "fs"; +import path from "path"; +import { Command } from "commander"; + +const program = new Command(); + +// Define CLI options +program + .name("manualMode") + .description("Run Cypher queries against a Neo4j database from a JSON input") + .option("-i, --input <string>", "Input JSON file", "query_input.json") + .option("-o, --output <string>", "Output file name", "output.json"); + +program.parse(process.argv); + +const options = program.opts(); + +interface QueryInput { + database: { + host?: string; + port?: number; + username?: string; + password?: string; + }; + query: string; +} + +async function executeQuery(inputFile: string, outputFile: string) { + // Read input JSON file + const inputPath = path.join(__dirname, inputFile); + let inputData: QueryInput; + + try { + inputData = JSON.parse(fs.readFileSync(inputPath, 'utf8')); + } catch (err) { + console.error(`Error reading input file: ${err}`); + process.exit(1); + } + + // Construct database connection configuration + const dbConfig: DbConnection = { + id: 1, + internalDatabaseName: "neo4j", + url: `://${inputData.database?.host || 'localhost'}`, + protocol: "bolt", + port: inputData.database?.port ? parseInt(inputData.database.port.toString()) : 7687, + username: inputData.database?.username || 'neo4j', + password: inputData.database?.password || 'password', + type: "neo4j", + }; + + // Execute query + try { + const result = await queryService(dbConfig, inputData.query); + + // Write output to file + const outputPath = path.join(__dirname, outputFile); + fs.writeFileSync(outputPath, JSON.stringify(result, null, 2)); + + console.log(`Query results written to ${outputPath}`); + return result; + } catch (err) { + console.error('Error executing query:', err); + process.exit(1); + } +} + +// Only run if directly executed (not imported) +if (require.main === module) { + executeQuery(options.input, options.output); +} + +export { executeQuery }; // Allow importing for testing or other uses