Git Product home page Git Product logo

neo4j-fiber's Introduction

Package Quality Join the chat at https://gitter.im/VeliovGroup/neo4j-fiber

Most advanced, well documented and efficient REST client for Neo4j database, with 100% tests coverage. Fibers allows to give a new level experience to developers, no more callback-hell and blocking operations. Speed and low resources consumption is top priority of neo4j-fiber package.

  • 100% tests coverage
  • Version for Meteor.js - https://atmospherejs.com/ostrio/neo4jdriver
  • This this library is heavily depends from Fibers, so you required to wrap all code into Fiber, see example
  • This package uses batch operations to perform queries. Batch operations lets you execute multiple API calls through a single HTTP call. This improves performance for large insert and update operations significantly
  • This package was tested and works like a charm with GrapheneDB
  • To find more about how to use Cypher read Neo4j cheat sheet

Installation

npm install --save neo4j-fiber

Demo Apps

API:

Please see full API with examples in our wiki

Basic Usage Examples

Connect to Neo4j

const Neo4jDB = require('neo4j-fiber').Neo4jDB;
const db = new Neo4jDB('http://localhost:7474', {
  username: 'neo4j',
  password: '1234'
});

Set connection URL via environment variables

Set NEO4J_URL or GRAPHENEDB_URL to as connection URL to Neo4j Database

NEO4J_URL="http://neo4j:1234@localhost:7474" node index.js

If environment variable is set, no need to pass url argument into Neo4jDB constructor

const Neo4jDB = require('neo4j-fiber').Neo4jDB;
const db = new Neo4jDB();

Run simple query

const cursor = db.query('CREATE (n:City {props}) RETURN n', {
  props: {
    title: 'Ottawa',
    lat: 45.416667,
    long: -75.683333
  }
});

console.log(cursor.fetch());
// Returns array of nodes:
// [{
//   n: {
//     long: -75.683333,
//     lat: 45.416667,
//     title: "Ottawa",
//     id: 8421,
//     labels": ["City"],
//     metadata: {
//       id: 8421,
//       labels": ["City"]
//     }
//   }
// }]

// Iterate through results as plain objects:
cursor.forEach((node) => {
  console.log(node)
  // Returns node as Object:
  // {
  //   n: {
  //     long: -75.683333,
  //     lat: 45.416667,
  //     title: "Ottawa",
  //     id: 8421,
  //     labels": ["City"],
  //     metadata: {
  //       id: 8421,
  //       labels": ["City"]
  //     }
  //   }
  // }
});

// Iterate through cursor as `Neo4jNode` instances:
cursor.each((node) => {
  console.log(node.n.get());
  // {
  //   long: -75.683333,
  //   lat: 45.416667,
  //   title: "Ottawa",
  //   id: 8421,
  //   labels": ["City"],
  //   metadata: {
  //     id: 8421,
  //     labels": ["City"]
  //   }
  // }
});

Create node

const node  = db.nodes();
const node2 = db.nodes({property: 'value', property2: ['val', 'val2', 'val3']});

Get node by id

const node = db.nodes(123);

Delete node

node.delete();

Create relationship

const n1 = db.nodes();
const relationship = db.nodes().to(n1, "KNOWS", {property: 'value'});

Delete relationship

relationship.delete();

Cities example

// Create some data:
const cities = {};
cities['Zürich'] = db.nodes({
  title: 'Zürich',
  lat: 47.27,
  long: 8.31
}).label(['City']);

cities['Tokyo'] = db.nodes({
  title: 'Tokyo',
  lat: 35.40,
  long: 139.45
}).label(['City']);

cities['Athens'] = db.nodes({
  title: 'Athens',
  lat: 37.58,
  long: 23.43
}).label(['City']);

cities['Cape Town'] = db.nodes({
  title: 'Cape Town',
  lat: 33.55,
  long: 18.22
}).label(['City']);


// Add relationship between cities
// At this example we set distance
cities['Zürich'].to(cities['Tokyo'], "DISTANCE", {m: 9576670, km: 9576.67, mi: 5950.67});
cities['Tokyo'].to(cities['Zürich'], "DISTANCE", {m: 9576670, km: 9576.67, mi: 5950.67});

// Create route 1 (Zürich -> Athens -> Cape Town -> Tokyo)
cities['Zürich'].to(cities['Athens'], "ROUTE", {m: 1617270, km: 1617.27, mi: 1004.93, price: 50});
cities['Athens'].to(cities['Cape Town'], "ROUTE", {m: 8015080, km: 8015.08, mi: 4980.34, price: 500});
cities['Cape Town'].to(cities['Tokyo'], "ROUTE", {m: 9505550, km: 9505.55, mi: 5906.48, price: 850});

// Create route 2 (Zürich -> Cape Town -> Tokyo)
cities['Zürich'].to(cities['Cape Town'], "ROUTE", {m: 1617270, km: 1617.27, mi: 1004.93, price: 550});
cities['Cape Town'].to(cities['Tokyo'], "ROUTE", {m: 9576670, km: 9576.67, mi: 5950.67, price: 850});

// Create route 3 (Zürich -> Athens -> Tokyo)
cities['Zürich'].to(cities['Athens'], "ROUTE", {m: 1617270, km: 1617.27, mi: 1004.93, price: 50});
cities['Athens'].to(cities['Tokyo'], "ROUTE", {m: 9576670, km: 9576.67, mi: 5950.67, price: 850});

// Get Shortest Route (in km) between two Cities:
const shortest  = cities['Zürich'].path(cities['Tokyo'], "ROUTE", {cost_property: 'km', algorithm: 'dijkstra'})[0];
let shortestStr = 'Shortest from Zürich to Tokyo, via: ';
shortest.nodes.forEach((id) => {
  shortestStr += db.nodes(id).property('title') + ', ';
});

shortestStr += '| Distance: ' + shortest.weight + ' km';
console.info(shortestStr); // <-- Shortest from Zürich to Tokyo, via: Zürich, Cape Town, Tokyo, | Distance: 11122.82 km

// Get Cheapest Route (in notional currency) between two Cities:
const cheapest  = cities['Zürich'].path(cities['Tokyo'], "ROUTE", {cost_property: 'price', algorithm: 'dijkstra'})[0];
let cheapestStr = 'Cheapest from Zürich to Tokyo, via: ';
cheapest.nodes.forEach((id) => {
  cheapestStr += db.nodes(id).property('title') + ', ';
});

cheapestStr += '| Price: ' + cheapest.weight + ' nc';
console.info(cheapestStr); // <-- Cheapest from Zürich to Tokyo, via: Zürich, Athens, Tokyo, | Price: 900 nc

For more complex examples and docs, please see our wiki

neo4j-fiber's People

Contributors

dr-dimitru avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

neo4j-fiber's Issues

Reactivity does not work

I use neo4j-fiber with ccorcos:any-db.
Here is my code:

SERVER:

let c = db.query({
cypher:  MATCH (node) RETURN node.id as _id, node.id, node.topic , 
reactive: true, 
});
AnyDb.publish('allNodes', ( data ) => {
return c.fetch();
});

CLIENT:

Session.set('nodes', 'loading');
let subscripiton = AnyDb.subscribe('allNodes', {data: 'AKAI'}, ( sub ) => {
    log("sub ready");
    Session.set('nodes', sub.data);
    sub.onChange( (data) => {
        log("new sub data")
        Session.set('nodes', data);
    });
}); 
export default createContainer(() => {
    return {
        nodes: Session.get('nodes'), 
    };
}, EventsListPage);

Data is fetched only once, but when i add some nodes to the DB directly - nothing updates reactivelly. Only when i refresh the page data does update.

Query does not return Arrays such as collect(), labels()

Cypher expressions such as collect(n) or labels(n) can be used within a query but when trying to return the array a java exception breaks the execution.

e.g. db.graph("MATCH n RETURN collect(n)").fetch()
(ArrayList cannot be cast to Map)
array_return_error

Returning the array with UNWIND works but the result set is not useful in many cases, e.g. one cannot mix a single row with collected results of child nodes.

Nested arrays are not parsed into Neo4jNode class

Arrays are returned raw as per neo4j REST reply.
When mixing results (i.e. with collect function) in a single row only the top level entity (node or rel) is parsed into a plain object.

This means that metadata and data are in different object keys along with unuseful data:
collect_results

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.