本章中你将会学到如何在node.js中使用neo4j图形数据库。node
当你想存储或者查询和数据紧密关联的数据的时候,图形数据库颇有用。git
neo4j是一个可有效存储,处理和查询你数据模型中紧密相连的元素的数据库。github
neo4j有很强大且灵活的数据模型,你可使用其来表示你真实的,易变结构的信息,且不失真。数据库
Neo4j数据库有内在的HTTP REST接口,咱们可使用其直接和Neo4j数据库交接(interface)。npm
你须要简单的使用POST向一个HTTP URL发送请求,且接受来自Neo4j的响应。json
在下面的实例中你能够看见Node.js请求模块调用了REST,在请求模块(request module)中这样作很便利。缓存
安装request模块是:dom
> npm install request
让咱们建立一个空的文件,且在文档中写下下述代码:函数
//Let’s load the request module var request = require("request");
上面咱们将使用require函数来在咱们的项目中加载request模块post
建立一个将会触发密码查询(cypher query)的方法:
下面咱们将要建立一个函数,使用密码查询(cypher query)做为输入,且使用HTTP接口在数据库中触发这个密码查询(cypher query)。咱们在manual中能够详尽看到端点协议(endpoint protocol)和格式。你可使用其作更多的事情:例如,在每一个请求中发送不少标准 或者 在众多请求中保持通信打开(keep transactions)。你可在neoe4j手册中多看看。
下面让咱们定义数据库的host
//Define your host and port. This is where your database is running. Here it is defined on localhost. var host = 'localhost', port = 7474;
定义咱们须要链接的URL,在Neo4说明文档中指明:
//This is the URL where we will POST our data to fire the cypher query. This is specified in Neo4j docs. var httpUrlForTransaction = 'http://' + host + ':' + port + '/db/data/transaction/commit';
接下来,你须要定义可将cyper做为参数的函数,用来执行密码查询。
参数能够是任何的用来密码查询的参数,且当数据库有响应的时候要触发回调函数。
//Let’s define a function which fires the cypher query. function runCypherQuery(query, params, callback) { request.post({ uri: httpUrlForTransaction, json: {statements: [{statement: query, parameters: params}]} }, function (err, res, body) { callback(err, body); }) }
触发一些查询:
如今咱们须要一个能够在neo4.js中触发查询的函数,让咱们经过保存neo4j中一个节点的方法来使用这个函数。
/** * Let’s fire some queries below. * */ runCypherQuery( 'CREATE (somebody:Person { name: {name}, from: {company}, age: {age} }) RETURN somebody', { name: 'Ghuffran', company: 'Modulus', age: 44 }, function (err, resp) { if (err) { console.log(err); } else { console.log(resp); } } );
在上面的例子中,咱们使用了一个密码查询来保存节点(node)
注意:使用上述的参数来进行查询是好想法。由于Neo4j缓存查询路径,而后结合咱们传递的不一样参数来运行这个查询路径。这增长了查询执行的速度。
如今让咱们总结上面的全部代码,咱们的文件应该看上去是:
//Let’s load the request module var request = require("request"); //Define your host and port. This is where your database is running. Here it’s on localhost. var host = 'localhost', port = 7474; //This is the url where we will POST our data to fire the cypher query. This is specified in Neo4j docs. var httpUrlForTransaction = 'http://' + host + ':' + port + '/db/data/transaction/commit'; //Let’s define a function which fires the cypher query. function runCypherQuery(query, params, callback) { request.post({ uri: httpUrlForTransaction, json: {statements: [{statement: query, parameters: params}]} }, function (err, res, body) { callback(err, body); }) } /** * Let’s fire some queries as shown below. * */ runCypherQuery( 'CREATE (somebody:Person { name: {name}, from: {company}, age: {age} }) RETURN somebody', { name: 'Ghuffran', company: 'Modulus', age: 44 }, function (err, resp) { if (err) { console.log(err); } else { console.log(resp); } } );
如今咱们只要使用内置的HTTP REST API和Neo4j 数据库进行交接就可。
你可以使用一些模块来和Node.js只的neo4j进行interface,可是node-neo4j(Thingdom)和node-neo4j(philipkueng)是使用最普遍的。
你能够根据我的喜爱进行选择使用。
下面例子中使用的是node-neo4j()philippkueng模块,由于其利于说明:
咱们能够加载这个模块:
> npm install node-neo4j
如今让咱们看看如何使用node-neo4j模块来触发密码查询,就像使用其余的对象接口同样。
//Require the Neo4J module var neo4j = require('node-neo4j'); //Create a db object. We will using this object to work on the DB. db = new neo4j('http://localhost:7474'); //Run raw cypher with params db.cypherQuery( 'CREATE (somebody:Person { name: {name}, from: {company}, age: {age} }) RETURN somebody', { name: 'Ghuffran', company: 'Modulus', age: ~~(Math.random() * 100) //generate random age }, function (err, result) { if (err) { return console.log(err); } console.log(result.data); // delivers an array of query results console.log(result.columns); // delivers an array of names of objects getting returned } );
上麦咱们使用模块提供的db.cypherQuery(query, [params|Optional], [include_stats|Optional], callback) 方法来运行咱们的密码查询。
node-neo4j模块提供了一系列的帮助方法。
让咱们看看咱们如何经过使用帮助函数来保存咱们的节点。
//Require the Neo4J module var neo4j = require('node-neo4j'); //Create a db object. We will using this object to work on the DB. db = new neo4j('http://localhost:7474'); //Let’s create a node db.insertNode({ name: 'Ghuffran', company: 'Modulus', age: ~~(Math.random() * 100) }, function (err, node) { if (err) { return console.log(err); } // Output node data. console.log(node); });
上面咱们使用 db.insertNode 来帮助咱们建立一个特殊的节点。
存在一些能够用来更新,读取和删除的方法,你可以使用这些方法和neo4j数据库进行交互,而不须要处理密码查询。你能够在API文档中查看更多。