From e8cd7e1572dfc9660b2d215afddb3d39d5b8ce98 Mon Sep 17 00:00:00 2001 From: "dr.dimitru" Date: Thu, 4 Dec 2014 20:31:45 +0300 Subject: [PATCH] Add catchall callbacks functionality --- ostrio:neo4jdriver.js | 67 +++++++++++++++++++++++++++++++++++++++++-- package.js | 2 +- readme.md | 13 +++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) diff --git a/ostrio:neo4jdriver.js b/ostrio:neo4jdriver.js index a83f241..b6c0295 100644 --- a/ostrio:neo4jdriver.js +++ b/ostrio:neo4jdriver.js @@ -1,10 +1,73 @@ +/* + * + * @function + * @name Neo4j + * @description Get GraphDatabase from node-neo4j npm package + * + */ this.Neo4j = (function() { + /* + * + * @function + * @name Neo4j + * @param url {string} - url to Neo4j database + * @description Get GraphDatabase from node-neo4j npm package + * + */ function Neo4j(url) { this.url = url != null ? url : process.env['NEO4J_URL'] || process.env['GRAPHENEDB_URL'] || 'http://localhost:7474'; this.N4j = Meteor.npmRequire('neo4j'); - return new this.N4j.GraphDatabase(this.url); + _n4j = this.N4j; + + var GraphDatabase = new _n4j.GraphDatabase(this.url); + GraphDatabase.callbacks = [] + + /* + * + * @function + * @namespace N4j.GraphDatabase + * @name query + * @param query {String} - The Cypher query. NOTE: Can't be multi-line. + * @param opts {Object} - A map of parameters for the Cypher query. + * @param callback {function} - Callback function + * @description Replace standard GraphDatabase.query method + * Add functionality of callbacks which runs on every query execution + * + */ + GraphDatabase.query = function(query, opts, callback){ + + return new _n4j.GraphDatabase(this.url).query(query, opts, function(err, results){ + + _.forEach(GraphDatabase.callbacks, function(cb){ + if(cb){ + cb(query, opts); + } + }); + + if(callback){ + callback(err, results); + } + }); + } + + /* + * + * @function + * @namespace N4j.GraphDatabase + * @name listen + * param callback {function} - Callback function with: + * @param query {String} - The Cypher query. NOTE: Can't be multi-line. + * @param opts {Object} - A map of parameters for the Cypher query. + * @description Add callback function + * + */ + GraphDatabase.listen = function(callback){ + + GraphDatabase.callbacks.push(callback); + } + + return GraphDatabase; } return Neo4j; - })(); \ No newline at end of file diff --git a/package.js b/package.js index 0dda08c..de55ba3 100644 --- a/package.js +++ b/package.js @@ -1,7 +1,7 @@ Package.describe({ name: 'ostrio:neo4jdriver', summary: 'Meteor.js node-neo4j wrapper to be used with meteor applications (a.k.a. neo4j Connector)', - version: '0.1.13', + version: '0.2.1', git: 'https://github.com/VeliovGroup/ostrio-neo4jdriver.git' }); diff --git a/readme.md b/readme.md index d278f8e..2acf386 100644 --- a/readme.md +++ b/readme.md @@ -63,6 +63,19 @@ N4JDB.query 'Match (o:User {_id:"' + Meteor.userId() + '"}) ' + #handle error here ``` +```coffeescript +### +Register catch all callback +Note - you may register as meny callbacks as you need +@param query {string} - Cypher query +@param opts {object} - A map of parameters for the Cypher query +### +N4JDB = new Neo4j() +N4JDB.listen (query, opts) -> + console.log query, opts + +``` + **For more info see: [node-neo4j](https://github.com/thingdom/node-neo4j)** Code licensed under Apache v. 2.0: [node-neo4j License](https://github.com/thingdom/node-neo4j/blob/master/LICENSE)