Update neo4j: safe callbacks, update indexes on save

This commit is contained in:
Anatoliy Chakkaev 2011-10-19 00:35:00 +04:00
parent f19bacd681
commit 6238b738e4
1 changed files with 18 additions and 7 deletions

View File

@ -56,7 +56,8 @@ Neo4j.prototype.mixClassMethods = function mixClassMethods(class, properties) {
neo.node(from, function (err, node) { neo.node(from, function (err, node) {
if (err) return cb(err); if (err) return cb(err);
node._getRelationships(direction, type, function (err, rels) { 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; var found = false;
if (rels && rels.forEach) { if (rels && rels.forEach) {
rels.forEach(function (r) { 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) { class.createRelationshipTo = function createRelationshipTo(id1, id2, type, data, cb) {
var fromNode, toNode; var fromNode, toNode;
neo.node(id1, function (err, node) { neo.node(id1, function (err, node) {
if (err) return cb(err); if (err && cb) return cb(err);
if (err && !cb) throw err;
fromNode = node; fromNode = node;
ok(); ok();
}); });
neo.node(id2, function (err, node) { neo.node(id2, function (err, node) {
if (err) return cb(err); if (err && cb) return cb(err);
if (err && !cb) throw err;
toNode = node; toNode = node;
ok(); ok();
}); });
@ -96,8 +99,9 @@ Neo4j.prototype.mixClassMethods = function mixClassMethods(class, properties) {
// only create relationship if it is not exists // only create relationship if it is not exists
class.ensureRelationshipTo = function (id1, id2, type, data, cb) { class.ensureRelationshipTo = function (id1, id2, type, data, cb) {
class.relationshipExists(id1, id2, type, 'outgoing', function (err, exists) { class.relationshipExists(id1, id2, type, 'outgoing', function (err, exists) {
if (err) return cb(err); if (err && cb) return cb(err);
if (exists) return cb(null); if (err && !cb) throw err;
if (exists) return cb && cb(null);
class.createRelationshipTo(id1, id2, type, data, cb); 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) { Neo4j.prototype.save = function save(model, data, callback) {
var self = this;
this.node(data.id, function (err, node) { this.node(data.id, function (err, node) {
if (err) return callback(err); if (err) return callback(err);
node.data = cleanup(data); 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);
});
});
}); });
}; };