Skip to content
Snippets Groups Projects
Commit 4cd45512 authored by Dorus's avatar Dorus
Browse files

feat: added version of the script that is able to interface wth a json input

parent 4179a0cd
No related branches found
Tags v1.18.0
No related merge requests found
Pipeline #143364 passed
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment