Next.js Discord

Discord Forum

What is the correct way to initialize a connection to a Neo4j database?

Unanswered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
I'm currently starting out a personal project and am going to try and use Neo4j as the database.

I have a function which I call from the root layout.tsx to initialize the driver. It seems to be working fine when I build the app. i.e it's only getting called once when I use run npm start.

I'm just a bit of a newb and was wondering if there's a better approach?

Root layout.tsx:

// app/layout.tsx
import { initNeo4jDriver } from "./utils/neo4j-helper";

initNeo4jDriver();

export default function RootLayout({
  children
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body className={inter.className}>
        {children}
      </body>
    </html>
  );
} 


neo4j-helper.js:

import { initDriver, getDriver } from "./neo4j.js";
import {
  API_PREFIX,
  JWT_SECRET,
  NEO4J_PASSWORD,
  NEO4J_URI,
  NEO4J_USERNAME
} from "./constants";

const driver = getDriver();

export async function initNeo4jDriver() {
  await initDriver(NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD);
}

...

I originally had the following function in the root layout component, but was scared that this may be exposing sensitive data to the client....
initDriver(NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD);


Basically as a newb, is this an okay way to do this? or is there a better way? anything i should read up on?

Thanks

1 Reply

Cape lionOP