1 | module.exports = function(cb){
|
2 |
|
3 | var env = this;
|
4 | var config = env.config;
|
5 | if(!config.neo4j) return cb();
|
6 |
|
7 | var neo4j = require('neo4j-io')("http://"+(config.neo4j.host || "localhost")+":"+(config.neo4j.port || 7474));
|
8 | var cypher = 'MATCH (n:NODE) RETURN n LIMIT 1';
|
9 | neo4j
|
10 | .query(cypher)
|
11 | .then(function(){
|
12 | env.engines.neo4j = neo4j;
|
13 | env.i.do("log.sys", "ne04j", "Connected to Neo4J on "+(config.neo4j.host || "localhost")+":"+(config.neo4j.port||5432));
|
14 | cb();
|
15 | })
|
16 | .catch(function(err){
|
17 | cb(err);
|
18 | });
|
19 |
|
20 | neo4j.format = function(query, data, cb){
|
21 | if (!data) return query;
|
22 | if(data.options) query = query.replace(/(@\w+)/g, function (txt, key){
|
23 | key = key.slice(1);
|
24 | if(data.options.hasOwnProperty(key)) {
|
25 | if(typeof data.options[key] === "function") return data.options[key].call(data.context, data.values, data.options);
|
26 | else return data.options[key];
|
27 | }
|
28 | return txt;
|
29 | });
|
30 |
|
31 | if(data.values) query = query.replace(/([#]\w+)/g, function (txt, key){
|
32 | key = key.slice(1);
|
33 | if(data.values.hasOwnProperty(key)) return escape(data.values[key]);
|
34 | return txt;
|
35 | });
|
36 |
|
37 | return query;
|
38 | }
|
39 |
|
40 | neo4j.escape = escape;
|
41 |
|
42 | function escape(val){
|
43 | if(Array.isArray(val)){
|
44 | var self = this;
|
45 | return val.map(function(v, index, val){return escape(val[index]);}).join(",");
|
46 | }
|
47 | else{
|
48 | if(typeof val === "number") return val.toString();
|
49 | else return "\""+val.toString().replace(/\\/g, "\\\\").replace(/"/g, "\\\"")+"\"";
|
50 | }
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 | } |
\ | No newline at end of file |