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

feat: made sure database connection gets closed upon exiting

parent f659a522
No related branches found
No related tags found
No related merge requests found
import neo4j from 'neo4j-driver';
import neo4j, {Session} from 'neo4j-driver';
import { Neo4jConnection } from 'ts-common';
import { queryService } from '../queryService';
import * as fs from 'fs';
......@@ -6,56 +6,64 @@ import path from 'path';
import argv from "process"
import { createInterface } from 'readline';
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
function manualQuery(cypherQuery: string, outputFile: string) {
return `${cypherQuery} ${outputFile}`
}
function connectToDataBase(uri: string, username: string, password: string){
return `${uri} ${username} ${password}`
function connectToDataBase(uri: string, username: string, password: string): neo4j.Session | null {
try {
console.log(`Connecting to: ${uri}`);
const driver = neo4j.driver(uri, neo4j.auth.basic(username, password));
const session = driver.session(); // Create session here
console.log(`Connected successfully`);
return session;
} catch (error) {
console.error(`Error connecting to Neo4j: ${error}`);
return null;
}
}
if (require.main === module) {
// Command-line args:
// 1st uri,
// 2nd username
// 3rd password
// 4th output file name
const uri: string = process.argv[2] || "bolt://localhost:7687"; // default neo4j port
const username: string = process.argv[3] || "neo4j";
const password: string = process.argv[4] || "password";
const outputFile: string = process.argv[5] || 'output.json'; // Default output file if none provided
const session: Session = connectToDataBase(uri, username, password)
// connect to neo4j
try
{
console.log(`Connecting to: ${uri}`);
const driver = neo4j.driver(uri, neo4j.auth.basic(username, password));
const session = driver.session();
}
catch(error)
{
console.log(`Error connecting to neo4j: ${error}` )
}
console.log(`Connected succesfully`);
console.log("Type a cypher query to query or type 'exit' in order to exit")
// reccursive function that executes queries
const promptQuery = () => {
rl.question('query: ', async (cypherQuery) => {
if(cypherQuery == "exit")
{
// todo: close database connections
if (session) {
await session.close();
console.log("Connection closed. Exiting...")
}
rl.close();
process.exit()
}
try {
// todo: Perform query
console.log(`Query ${cypherQuery} written to ${outputFile}`);
} catch (err) {
console.error('Error executing query:', err);
......
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