From 6238b738e454d0dce83b889469af6a8ef3de130b Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Wed, 19 Oct 2011 00:35:00 +0400 Subject: [PATCH] Update neo4j: safe callbacks, update indexes on save --- lib/adapters/neo4j.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/adapters/neo4j.js b/lib/adapters/neo4j.js index 6893c44f..fc3ab94b 100644 --- a/lib/adapters/neo4j.js +++ b/lib/adapters/neo4j.js @@ -56,7 +56,8 @@ Neo4j.prototype.mixClassMethods = function mixClassMethods(class, properties) { neo.node(from, function (err, node) { if (err) return cb(err); node._getRelationships(direction, type, function (err, rels) { - if (err) return cb(err); + if (err && cb) return cb(err); + if (err && !cb) throw err; var found = false; if (rels && rels.forEach) { rels.forEach(function (r) { @@ -65,7 +66,7 @@ Neo4j.prototype.mixClassMethods = function mixClassMethods(class, properties) { } }); } - cb(err, found); + cb && cb(err, found); }); }); }; @@ -73,12 +74,14 @@ Neo4j.prototype.mixClassMethods = function mixClassMethods(class, properties) { class.createRelationshipTo = function createRelationshipTo(id1, id2, type, data, cb) { var fromNode, toNode; neo.node(id1, function (err, node) { - if (err) return cb(err); + if (err && cb) return cb(err); + if (err && !cb) throw err; fromNode = node; ok(); }); neo.node(id2, function (err, node) { - if (err) return cb(err); + if (err && cb) return cb(err); + if (err && !cb) throw err; toNode = node; ok(); }); @@ -96,8 +99,9 @@ Neo4j.prototype.mixClassMethods = function mixClassMethods(class, properties) { // only create relationship if it is not exists class.ensureRelationshipTo = function (id1, id2, type, data, cb) { class.relationshipExists(id1, id2, type, 'outgoing', function (err, exists) { - if (err) return cb(err); - if (exists) return cb(null); + if (err && cb) return cb(err); + if (err && !cb) throw err; + if (exists) return cb && cb(null); class.createRelationshipTo(id1, id2, type, data, cb); }); } @@ -168,10 +172,17 @@ Neo4j.prototype.updateIndexes = function updateIndexes(model, node, cb) { }; Neo4j.prototype.save = function save(model, data, callback) { + var self = this; this.node(data.id, function (err, node) { if (err) return callback(err); node.data = cleanup(data); - node.save(callback); + node.save(function (err) { + if (err) return callback(err); + self.updateIndexes(model, node, function (err) { + if (err) return console.log(err); + callback(null); + }); + }); }); };