From 775b6b8b1c310f506602ad7b8fec135b0d746ba9 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Sun, 23 Oct 2011 23:45:44 +0400 Subject: [PATCH] Neo4j cypher query support --- lib/adapters/neo4j.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/adapters/neo4j.js b/lib/adapters/neo4j.js index fc3ab94b..ab920882 100644 --- a/lib/adapters/neo4j.js +++ b/lib/adapters/neo4j.js @@ -45,6 +45,27 @@ Neo4j.prototype.mixClassMethods = function mixClassMethods(class, properties) { } }); + class.setupCypherQuery = function (name, queryStr, rowHandler) { + class[name] = function cypherQuery(params, cb) { + if (typeof params === 'function') { + cb = params; + params = []; + } else if (params.constructor.name !== 'Array') { + params = [params]; + } + + var i = 0; + var q = queryStr.replace(/\?/g, function () { + return params[i++]; + }); + + neo.client.query(function (err, result) { + if (err) return cb(err, []); + cb(null, result.map(rowHandler)); + }, q); + }; + }; + /** * @param from - id of object to check relation from * @param to - id of object to check relation to @@ -228,6 +249,9 @@ Neo4j.prototype.destroy = function destroy(model, id, callback) { Neo4j.prototype.all = function all(model, filter, callback) { this.client.queryNodeIndex(model, 'id:*', function (err, nodes) { + if (nodes) { + nodes = nodes.map(function (s) { return s.data }); + } callback(err, filter && nodes ? nodes.filter(applyFilter(filter)) : nodes); }); };