From 4cd45512039c3f59971108a2f216d439e0cabb4a Mon Sep 17 00:00:00 2001
From: Dorus <doruskeijzer@gmail.com>
Date: Tue, 17 Dec 2024 16:58:31 +0100
Subject: [PATCH] feat: added version of the script that is able to interface
 wth a json input

---
 src/manual_mode/json_interface.ts | 75 +++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
 create mode 100644 src/manual_mode/json_interface.ts

diff --git a/src/manual_mode/json_interface.ts b/src/manual_mode/json_interface.ts
new file mode 100644
index 0000000..48bb9d2
--- /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
-- 
GitLab