Git Product home page Git Product logo

ogmneo's Introduction

No Maintenance Intended

⚠️ Deprecated ⚠️

This library is deprecated and will no longer be updated.

OGMNeo

Abstract some trivial operations on the Neo4j driver for Nodejs and make the use simpler. That's why we created OGMNeo.

npm version npm MIT Travis Codecov

Installation

You can find ogmneo in npm here and install using the follow command

 npm install ogmneo

Usage

Connecting to neo4j database

const ogmneo = require('ogmneo');
ogmneo.Connection.connect('neo4j', 'databasepass', 'localhost');
// Or if you want to add some neo4j driver configuration options 
ogmneo.Connection.connect('neo4j', 'databasepass', 'localhost', { maxTransactionRetryTime: 30000, encrypted: false });
// See more about the config options you can add on: http://neo4j.com/docs/api/javascript-driver/current/function/index.html#static-function-driver

OGMNeo connects using the neo4j bolt protocol.

Log generated cypher on console

You can see the generated Cypher on your console by setting Connection.logCypherEnabled property true.

const ogmneo = require('ogmneo');
ogmneo.Connection.logCypherEnabled = true;

Create node example

  const ogmneo = require('ogmneo');
  
  ogmneo.Node.create({ name: 'name', tes: 3 }, 'test')
  .then((node) => {
       //Created returned object => {id: 1, name: 'name', tes: 3}
  }).catch((error) => {
       //Handle error
  });

Find Nodes

  const ogmneo = require('ogmneo');
  
  let query = ogmneo.Query.create('test')
                             .where(new ogmneo.Where('name', { $eq: 'name1' }));

  ogmneo.Node.find(query)
  .then((nodes) => {
      //Found nodes.
  }).catch((error) => {
      //Handle error.
  });

Create relations

You can create relations between nodes.

  const ogmneo = require('ogmneo');
  ogmneo.Relation.relate(node1.id, 'relatedto', node2.id, {property: 'a'})
  .then((rels) => {
        // Created relation node {id: 2, type: 'relatedto', property: 'a'}
  }).catch((error) => {
        //Handle error
  });

Find Relations

You can find the relation nodes.

  const ogmneo = require('ogmneo');
  
  let query = ogmneo.RelationQuery.create('relatedto')
                                 .startNode(node1.id)
                                 .endNode(node2.id)
                                 .relationWhere(ogmneo.Where.create('property', { $eq: 'c' }))
                                 .ascOrderBy('property')
                                 .limit(3);
  ogmneo.Relation.find(query)
  .then((nodes) => {
        //Found relation nodes.
  }).catch((error) => {
        //Handle error.
  });
  
  //OR
  
  ogmneo.Relation.findPopulated(query)
  .then((nodes) => {
        //Found relation nodes with start and end nodes populated.
  }).catch((error) => {
        //Handle error.
  });
  

Executing Cypher

You can execute Cypher using the direct Neo4j Driver session object. Or you can use OGMNeoCypher.

  const ogmneo = require('ogmneo');

  ogmneo.Cypher.transactionalRead(cypherStatement)
  .then((result) => {
     console.log(result);
  }).catch((error) => {
     reject(error);
  });
  
  //OR
   ogmneo.Cypher.transactionalWrite(cypherStatement)
  .then((result) => {
     console.log(result);
  }).catch((error) => {
     reject(error);
  });
  

Creating and dropping indexes

You can create and drop indexes in properties.

  const ogmneo = require('ogmneo');
  //Creating
  ogmneo.Index.create('label', ['property'])
  .then((result) => {
     //Handle creation
  });
  //Dropping
  ogmneo.Index.drop('label', ['property'])
  .then((result) => {
     //Handle drop
  });

Operation API

Almost every method of ogmneo.Node and ogmneo.Relation have now the Operation API, that instead of executing the function on database returning a promise, it creates an ogmneo.Operation object that can be executed after by the ogmneo.OperationExecuter. Exemple:

  const ogmneo = require('ogmneo');
  
  let operation = ogmneo.Node.createOperation({ name: 'name', tes: 3 }, 'test');
  ogmneo.OperationExecuter.execute(operation)
  .then((node) => {
       //Created returned object => {id: 1, name: 'name', tes: 3}
  }).catch((error) => {
       //Handle error
  });

Transactional API

With the Operation API we can now execute as many READ or WRITE operations on the same transaction. For example, you want to create nodes and then relate those two. But if the relationship operation fails you want to rollback all the operations.

  const ogmneo = require('ogmneo');
  
  let createDriver = ogmneo.Node.createOperation({name: 'Ayrton Senna', carNumber: 12 }, 'Driver');
  ogmneo.OperationExecuter.write((transaction) => {
        return ogmneo.OperationExecuter.execute(createDriver, transaction)
                               .then((driver) => {
                                    let createCar = ogmneo.Node.createOperation({name: 'MP4/4'}, 'Car');
                                    return ogmneo.OperationExecuter.execute(createCar, transaction).then((car) => {
                                       let relate = ogmneo.Relation.relateOperation(driver.id, 'DRIVES', car.id, {year: 1988});
                                       return ogmneo.OperationExecuter.execute(relate, transaction);
                                    });
                               });
    }).then((result) => {
       //Result here
    });

All of those operations will be executed on the same transaction and you can rollback anytime you want. The transaction is the neo4j driver transaction object and you can see more about it on their docs here.

Batching operation in a single transaction

You can also batch many operation READ or WRITE operations in a single transaction.

    const ogmneo = require('ogmneo');
  
    let createUser1 = OGMNeoNode.createOperation({name: 'Ayrton Senna'}, 'Person');
    let createUser2 = OGMNeoNode.createOperation({name: 'Alain Prost'}, 'Person');

    ogmneo.OperationExecuter.batchWriteOperations([createUser1, createUser2]).then((result) => {
        let created1 = result[0];
        let created2 = result[1];
        console.log(created1.name); // 'Ayrton Senna'
        console.log(created2.name); // 'Alain Prost'
    });

If one of those fails, all other operations on the transaction will be rolledback automatically.

Documentation

See the full API documentation at docs. All docs was generated by JSDoc.

Exemple

See a demo sample on the ogmneo-demo repository.

Tests

Most of this library functions are covered by unit tests. See the code coverage on codecov.io.

Licence

OGMNeo is released under the MIT License.

ogmneo's People

Contributors

dependabot[bot] avatar hariseldon78 avatar lucianopalmeida avatar mostafa avatar peterclemenko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar

ogmneo's Issues

Too many "ECONNRESET" error

We are getting a lot of "ECONNRESET" errors. So I wanted to create this issue so that we can get someone can help. I am not sure whether this is neo4j itself or this ORM library.

Neo4jError: read ECONNRESET\n\n at captureStacktrace (/var/neo4j-sample-app/node_modules/neo4j-driver/lib/v1/result.js:200:15)\n at new Result (/var/neo4j-sample-app/node_modules/neo4j-driver/lib/v1/result.js:73:19)\n at _newRunResult (/var/neo4j-sample-app/node_modules/neo4j-driver/lib/v1/transaction.js:348:10)\n at Object.run (/var/neo4j-sample-app/node_modules/neo4j-driver/lib/v1/transaction.js:259:14)\n at Transaction.run (/var/neo4j-sample-app/node_modules/neo4j-driver/lib/v1/transaction.js:103:26)\n at Function.runInTransaction (/var/neo4j-sample-app/node_modules/ogmneo/lib/ogmneo-operation-executer.js:154:32)\n at session.readTransaction (/var/neo4j-sample-app/node_modules/ogmneo/lib/ogmneo-operation-executer.js:129:40)\n at TransactionExecutor._executeTransactionInsidePromise (/var/neo4j-sample-app/node_modules/neo4j-driver/lib/v1/internal/transaction-executor.js:115:37)\n at /var/neo4j-sample-app/node_modules/neo4j-driver/lib/v1/internal/transaction-executor.js:67:15\n at new Promise ()\n at new F (/var/neo4j-sample-app/node_modules/core-js/library/modules/_export.js:36:28)\n at TransactionExecutor.execute (/var/neo4j-sample-app/node_modules/neo4j-driver/lib/v1/internal/transaction-executor.js:66:14)\n at Session._runTransaction (/var/neo4j-sample-app/node_modules/neo4j-driver/lib/v1/session.js:214:40)\n at Session.readTransaction (/var/neo4j-sample-app/node_modules/neo4j-driver/lib/v1/session.js:187:19)\n at Promise (/var/neo4j-sample-app/node_modules/ogmneo/lib/ogmneo-operation-executer.js:128:25)\n at new Promise ()\n at Function._executeRead (/var/neo4j-sample-app/node_modules/ogmneo/lib/ogmneo-operation-executer.js:122:16)\n at Function.execute (/var/neo4j-sample-app/node_modules/ogmneo/lib/ogmneo-operation-executer.js:110:29)\n at OGMNeoModel.findOne (/var/neo4j-sample-app/src/api/ogm-neo/ogmneo-model.js:399:44)\n at OAuthAccessTokenManager.findOne (/var/neo4j-sample-app/src/api/managers/BaseManager.js:242:27)\n at onJwtBearerStrategyAuth (/var/neo4j-sample-app/src/config/passport.js:102:52)\n at /var/neo4j-sample-app/node_modules/passport-http-jwt-bearer/lib/strategy.js:99:11

This is running on the environment below:
NodeJS v8.9.3
Neo4J v3.4.0
neo4j-driver v1.6.2
The app is running on a Docker container with Centos 7

There are some issues have been raised in the neo4j driver but we still not figure out the root cause and resolution
philippkueng/node-neo4j#43
neo4j/neo4j-javascript-driver#126

Delete node cascade

The ogmneo.Node.delete deletes only the nodes that don't have relations. The proposal is add a delete that optionally also deletes the relations in a cascade way :)

merge nodes and relations

Having a merge function for nodes and relationships would help for carrying out creating unique nodes/relations.

Improve Documentation

Maybe we have some english grammar to fix on the comments Docs that can be improved.

docs not hosted

Hi, the docs link just goes to a folder of HTML files, is it possible to host that for free somehow?

Compability probem with Neo4j 4.0

Hi!

I'm using this great library to manage a neo4j database in version 4.0.

When I make a create (node) and pass a javascript object, it always gives a syntax error. I understand that it is not adapted to this version, because I am using the latest version (1.0.6). Is it like that or is it a bug in the library?

ogmneo.Node.create(entityToDB, 'Entity') .then((node) => node ).catch((error) => { //Handle error console.error(error) });

Neo4jError: The old parameter syntax {param}is no longer supported. Please use$param instead (line 1, column 28 (offset: 27)) "CREATE (n:Entity { name : {name}, description : {description}, type : {type }) RETURN n"

Thank you so much!!

socket error

hello,
I use OGMNeo in koa server, But I got an socket error like this:

error

i cannot catch this error in koa middleWare
i catch it in process.on

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.