本章中你將會學到如何在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文檔中查看更多。