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) {
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);
});
});
});
};