cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Where should I initialize a driver instance in NodeJS

A basic question for anyone who uses Neo4J + NodeJS. I want to initialize my neo4j driver instance such that I use it for every session throughout my server. Currently I create a new driver instance (re-authenticate) and then make a new session everytime I want to make a call to Neo4J.

(1) Sanity check: should I even be worrying about this? Does this slow the application down?
(2) If this is not best practice (which I'm guessing it isn't), where should I put the below code (where I create a driver instance and function that makes a new session given a query) given my file structure?

Current file structure:
2X_b_b66e3f3731acea5ef506afe57177d552bb6e267b.png

In the neo4j.ts file, I have the following code:

import neo4j, { Record } from 'neo4j-driver';
import { NEO4J_HOST, NEO4J_PASSWORD, NEO4J_PORT, NEO4J_PROTOCOL, NEO4J_USERNAME } from '../constants/secrets';

const driver = neo4j.driver(
  `${NEO4J_PROTOCOL}://${NEO4J_HOST}:${NEO4J_PORT}`,
  neo4j.auth.basic(`${NEO4J_USERNAME}`, `${NEO4J_PASSWORD}`),
);

const dbQuery = async (query: string, queryData: object = {}): Promise<Record[] | null> => {
  const session = driver.session();
  try {
    const result = await session.run(query, queryData);
    return result.records;
  } catch (error) {
    console.error(error.message);
    return null;
  } finally {
    session.close();
  }
};

export default dbQuery;

I import dbQuery from this file, neo4j.ts into different subfolders under routes. Where should I put this code?

0 REPLIES 0