All files / src index.ts

95.35% Statements 41/43
50% Branches 2/4
100% Functions 9/9
97.5% Lines 39/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 819x     9x                       9x   9x   9x         9x     9x 9x 9x 9x       9x 9x 9x     9x 9x 9x     9x       801x 801x 801x 801x     800x 800x 800x 642x 642x 663x 642x   800x 800x 800x 800x     1x 1x         9x   9x 9x 9x 9x  
import { v1 as neo4j } from "neo4j-driver";
import { Model } from "./Model";
import { ModelInstance } from "./ModelInstance";
import * as debug from "debug";
import { Neo4jResultStats } from "./Types";
 
export type Neo4jsOptions = {
  boltUri?: string;
  boltPort?: number;
  restUri?: string;
  restPort?: number;
  username?: string;
  password?: string;
};
 
export * from "./Types";
 
const d = debug("neo4js:debug");
 
export class neo4js {
  options: Neo4jsOptions;
  driver: any;
 
  init(options: Neo4jsOptions) {
    this.options = { ...options };
 
    // TODO: at least some verification...
    const uri = `bolt://${this.options.boltUri}:${this.options.boltPort}`;
    const { username, password } = this.options;
    let auth = undefined;
    Iif (username) {
      auth = neo4j.auth.basic(username, password);
    }
 
    d("Init neo4js: %O", { ...this.options, uri, username, password });
    Iif (this.driver) this.close();
    this.driver = neo4j.driver(uri, auth);
  }
 
  close = () => {
    d("Closing neo4js connection");
    this.driver.close();
  };
 
  run = (
    cmd: string,
    params?: any
  ): Promise<any[] & { _stats: Neo4jResultStats; _raw: any }> => {
    let session = this.driver.session();
    d("Cypher query: %s", cmd);
    d("Params: %O", params);
    return session
      .run(cmd, params)
      .then(raw => {
        d("Raw result: %O", raw);
        session.close();
        let result = raw.records.map(r => r.toObject()).map(r => {
          let keys = Object.keys(r);
          let o = {};
          keys.forEach(k => (o[k] = r[k].properties));
          return o;
        });
        result._stats = raw.summary.counters._stats;
        result._raw = raw;
        d("Prepared result: %O", result);
        return result;
      })
      .catch(err => {
        session.close();
        throw err;
      });
  };
}
 
export default new neo4js();
 
export * from "./Model";
export * from "./ModelInstance";
export * from "./Relation";
export * from "./Decorators";