Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ AppSignal:
- `APPSIGNAL_APP_NAME` : name of this app (e.g. 'Biofactoid')
- `APPSIGNAL_APP_ENV` : used to indicate which instance is running (e.g 'master', 'production', 'unstable')

Graph Database:

- `GRAPHDB_CONN` : The connection string
- `GRAPHDB_USER` : Authentication username
- `GRAPHDB_PASS` : Authentication password

The following environment variables should always be set in production instances:

- `NODE_ENV` : set to `production`
Expand Down
47 changes: 16 additions & 31 deletions neo4j-test/connect-neo4j.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,23 @@
//import { config } from 'dotenv';
import { expect } from 'chai';
import neo4j from 'neo4j-driver';

import { closeDriver, getDriver, initDriver } from '../src/neo4j/neo4j-driver.js';

describe('01. Initiate Driver', () => {
//beforeAll(() => config());
//afterAll(() => closeDriver());

it('Should create a driver instance and connect to server', async () => {
await initDriver();
});

it('Driver has been instantiated', () => {
const driver = getDriver();
expect(driver).toBeDefined();

expect(driver.constructor.name).toEqual('Driver');
});

it('Driver can verify connectivity', () => {
const driver = getDriver();
expect(driver).toBeDefined();
expect(driver.constructor.name).toEqual('Driver');

driver.verifyConnectivity()
.then(() => {
expect(true).toEqual(true);
})
.catch(e => {
expect(e).toBeUndefined('Unable to verify connectivity');
});
});

it('Close the Driver', () => {
closeDriver();
});
it('initDriver Should initialize and return a driver', () => {
const driver = initDriver();
expect(driver).an.instanceof(neo4j.Driver);
});

it('getDriver should return the driver', () => {
initDriver();
const driver = getDriver();
expect(driver).an.instanceof(neo4j.Driver);
});

it('closeDriver should remove driver instance', async () => {
const driver = await closeDriver();
expect(driver).to.be.undefined;
});
});
7 changes: 7 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,10 @@ export const MIN_RELATED_PAPERS = env('MIN_RELATED_PAPERS', 6);
// google analytics
// google tag manager
export const GTM_ID = env('GTM_ID', 'GTM-NV468LC');

// Graph Database
export const GRAPHDB_CONN = env('GRAPHDB_CONN', 'bolt://localhost:7687');
export const GRAPHDB_USER = env('GRAPHDB_USER', undefined);
export const GRAPHDB_PASS = env('GRAPHDB_PASS', undefined);
export const GRAPHDB_DBNAME = env('GRAPHDB_DBNAME', 'factoid');

33 changes: 26 additions & 7 deletions src/neo4j/neo4j-driver.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import neo4j from 'neo4j-driver';
import { GRAPHDB_CONN, GRAPHDB_USER, GRAPHDB_PASS } from '../config';

let driver;

export function initDriver() {
driver = neo4j.driver('bolt://localhost:7687');
return driver.verifyConnectivity()
.then( () => driver);
/**
* Initialize the Neo4j driver singleton
*
* @param {object} config additional configuration
* @returns Neo4j {@link https://neo4j.com/docs/api/javascript-driver/current/class/lib6/driver.js~Driver.html Driver} instance
*/
export function initDriver( config = {} ) {
driver = neo4j.driver( GRAPHDB_CONN,
neo4j.auth.basic( GRAPHDB_USER, GRAPHDB_PASS ),
config
);
return driver;
}

/**
* Retrieve the Neo4j driver instance
*
* @returns The Neo4j Driver instance
*/
export function getDriver() {
return driver;
return driver;
}

export function closeDriver() {
return driver && driver.close();
/**
* Close the Neo4j driver instance
*
* @returns Promise<void>
*/
export async function closeDriver() {
return driver && driver.close();
}