2011-10-03 13:37:33 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2011-10-16 17:21:08 +00:00
|
|
|
var neo4j = require('./neo4j-lib');
|
2011-10-03 13:37:33 +00:00
|
|
|
|
|
|
|
exports.initialize = function initializeSchema(schema, callback) {
|
|
|
|
schema.client = new neo4j.GraphDatabase(schema.settings.url);
|
|
|
|
schema.adapter = new Neo4j(schema.client);
|
|
|
|
};
|
|
|
|
|
|
|
|
function Neo4j(client) {
|
|
|
|
this._models = {};
|
|
|
|
this.client = client;
|
|
|
|
this.cache = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Neo4j.prototype.define = function defineModel(descr) {
|
2011-10-16 17:21:08 +00:00
|
|
|
this.mixClassMethods(descr.model, descr.properties);
|
|
|
|
this.mixInstanceMethods(descr.model.prototype, descr.properties);
|
2011-10-03 13:37:33 +00:00
|
|
|
this._models[descr.model.modelName] = descr;
|
|
|
|
};
|
|
|
|
|
2011-10-16 17:21:08 +00:00
|
|
|
Neo4j.prototype.createIndexHelper = function (class, indexName) {
|
|
|
|
var db = this.client;
|
|
|
|
var method = 'findBy' + indexName[0].toUpperCase() + indexName.substr(1);
|
|
|
|
class[method] = function (value, cb) {
|
|
|
|
db.getIndexedNode(class.modelName, indexName, value, function (err, node) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
if (node) {
|
|
|
|
node.data.id = node.id;
|
|
|
|
cb(null, new class(node.data));
|
|
|
|
} else {
|
|
|
|
cb(null, null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
Neo4j.prototype.mixClassMethods = function mixClassMethods(class, properties) {
|
|
|
|
var neo = this;
|
|
|
|
|
|
|
|
Object.keys(properties).forEach(function (name) {
|
|
|
|
if (properties[name].index) {
|
|
|
|
neo.createIndexHelper(class, name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param from - id of object to check relation from
|
|
|
|
* @param to - id of object to check relation to
|
|
|
|
* @param type - type of relation
|
|
|
|
* @param direction - all | incoming | outgoing
|
|
|
|
* @param cb - callback (err, rel || false)
|
|
|
|
*/
|
|
|
|
class.relationshipExists = function relationshipExists(from, to, type, direction, cb) {
|
|
|
|
neo.node(from, function (err, node) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
node._getRelationships(direction, type, function (err, rels) {
|
2011-10-18 20:35:00 +00:00
|
|
|
if (err && cb) return cb(err);
|
|
|
|
if (err && !cb) throw err;
|
2011-10-16 17:21:08 +00:00
|
|
|
var found = false;
|
|
|
|
if (rels && rels.forEach) {
|
|
|
|
rels.forEach(function (r) {
|
|
|
|
if (r.start.id === from && r.end.id === to) {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-10-18 20:35:00 +00:00
|
|
|
cb && cb(err, found);
|
2011-10-16 17:21:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
class.createRelationshipTo = function createRelationshipTo(id1, id2, type, data, cb) {
|
|
|
|
var fromNode, toNode;
|
|
|
|
neo.node(id1, function (err, node) {
|
2011-10-18 20:35:00 +00:00
|
|
|
if (err && cb) return cb(err);
|
|
|
|
if (err && !cb) throw err;
|
2011-10-16 17:21:08 +00:00
|
|
|
fromNode = node;
|
|
|
|
ok();
|
|
|
|
});
|
|
|
|
neo.node(id2, function (err, node) {
|
2011-10-18 20:35:00 +00:00
|
|
|
if (err && cb) return cb(err);
|
|
|
|
if (err && !cb) throw err;
|
2011-10-16 17:21:08 +00:00
|
|
|
toNode = node;
|
|
|
|
ok();
|
|
|
|
});
|
|
|
|
function ok() {
|
|
|
|
if (fromNode && toNode) {
|
|
|
|
fromNode.createRelationshipTo(toNode, type, cleanup(data), cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class.createRelationshipFrom = function createRelationshipFrom(id1, id2, type, data, cb) {
|
|
|
|
class.createRelationshipTo(id2, id1, type, data, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2011-10-18 20:35:00 +00:00
|
|
|
if (err && cb) return cb(err);
|
|
|
|
if (err && !cb) throw err;
|
|
|
|
if (exists) return cb && cb(null);
|
2011-10-16 17:21:08 +00:00
|
|
|
class.createRelationshipTo(id1, id2, type, data, cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Neo4j.prototype.mixInstanceMethods = function mixInstanceMethods(proto) {
|
|
|
|
var neo = this;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param obj - Object or id of object to check relation with
|
|
|
|
* @param type - type of relation
|
|
|
|
* @param cb - callback (err, rel || false)
|
|
|
|
*/
|
|
|
|
proto.isInRelationWith = function isInRelationWith(obj, type, direction, cb) {
|
|
|
|
this.constructor.relationshipExists(this.id, obj.id || obj, type, 'all', cb);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2011-10-03 13:37:33 +00:00
|
|
|
Neo4j.prototype.node = function find(id, callback) {
|
|
|
|
if (this.cache[id]) {
|
|
|
|
callback(null, this.cache[id]);
|
|
|
|
} else {
|
|
|
|
this.client.getNodeById(id, function (err, node) {
|
|
|
|
this.cache[id] = node;
|
|
|
|
callback(err, node);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Neo4j.prototype.create = function create(model, data, callback) {
|
|
|
|
data.nodeType = model;
|
2011-10-05 20:33:07 +00:00
|
|
|
var node = this.client.createNode();
|
|
|
|
node.data = cleanup(data);
|
2011-10-03 13:37:33 +00:00
|
|
|
node.save(function (err) {
|
|
|
|
if (err) {
|
2011-10-16 17:21:08 +00:00
|
|
|
return callback(err);
|
2011-10-03 13:37:33 +00:00
|
|
|
}
|
|
|
|
this.cache[node.id] = node;
|
|
|
|
node.index(model, 'id', node.id, function (err) {
|
2011-10-16 17:21:08 +00:00
|
|
|
if (err) return callback(err);
|
|
|
|
this.updateIndexes(model, node, function (err) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
callback(null, node.id);
|
|
|
|
});
|
|
|
|
}.bind(this));
|
2011-10-03 13:37:33 +00:00
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
2011-10-16 17:21:08 +00:00
|
|
|
Neo4j.prototype.updateIndexes = function updateIndexes(model, node, cb) {
|
|
|
|
var props = this._models[model].properties;
|
|
|
|
var wait = 1;
|
|
|
|
Object.keys(props).forEach(function (key) {
|
|
|
|
if (props[key].index) {
|
|
|
|
wait += 1;
|
|
|
|
node.index(model, key, node.data[key], done);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
var error = false;
|
|
|
|
function done(err) {
|
|
|
|
error = error || err;
|
|
|
|
if (--wait === 0) {
|
|
|
|
cb(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-10-03 13:37:33 +00:00
|
|
|
Neo4j.prototype.save = function save(model, data, callback) {
|
2011-10-18 20:35:00 +00:00
|
|
|
var self = this;
|
2011-10-03 13:37:33 +00:00
|
|
|
this.node(data.id, function (err, node) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
node.data = cleanup(data);
|
2011-10-18 20:35:00 +00:00
|
|
|
node.save(function (err) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
self.updateIndexes(model, node, function (err) {
|
|
|
|
if (err) return console.log(err);
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
});
|
2011-10-03 13:37:33 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Neo4j.prototype.exists = function exists(model, id, callback) {
|
|
|
|
delete this.cache[id];
|
|
|
|
this.node(id, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
Neo4j.prototype.find = function find(model, id, callback) {
|
|
|
|
delete this.cache[id];
|
|
|
|
this.node(id, function (err, node) {
|
|
|
|
if (node && node.data) {
|
|
|
|
node.data.id = id;
|
|
|
|
}
|
|
|
|
callback(err, this.readFromDb(model, node && node.data));
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
Neo4j.prototype.readFromDb = function readFromDb(model, data) {
|
|
|
|
if (!data) return data;
|
|
|
|
var res = {};
|
|
|
|
var props = this._models[model].properties;
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
if (props[key] && props[key].type.name === 'Date') {
|
|
|
|
res[key] = new Date(data[key]);
|
|
|
|
} else {
|
|
|
|
res[key] = data[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
|
|
|
|
Neo4j.prototype.destroy = function destroy(model, id, callback) {
|
2011-10-16 17:21:08 +00:00
|
|
|
var force = true;
|
2011-10-03 13:37:33 +00:00
|
|
|
this.node(id, function (err, node) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
node.delete(function (err) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
delete this.cache[id];
|
2011-10-16 17:21:08 +00:00
|
|
|
}.bind(this), force);
|
2011-10-03 13:37:33 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Neo4j.prototype.all = function all(model, filter, callback) {
|
|
|
|
this.client.queryNodeIndex(model, 'id:*', function (err, nodes) {
|
|
|
|
callback(err, filter && nodes ? nodes.filter(applyFilter(filter)) : nodes);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function applyFilter(filter) {
|
|
|
|
if (typeof filter === 'function') {
|
|
|
|
return filter;
|
|
|
|
}
|
|
|
|
var keys = Object.keys(filter);
|
|
|
|
return function (obj) {
|
|
|
|
var pass = true;
|
|
|
|
keys.forEach(function (key) {
|
|
|
|
if (!test(filter[key], obj.data[key])) {
|
|
|
|
pass = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
function test(example, value) {
|
|
|
|
if (typeof value === 'string' && example && example.constructor.name === 'RegExp') {
|
|
|
|
return value.match(example);
|
|
|
|
}
|
|
|
|
// not strict equality
|
|
|
|
return example == value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Neo4j.prototype.destroyAll = function destroyAll(model, callback) {
|
|
|
|
var wait, error = null;
|
|
|
|
this.all(model, null, function (err, collection) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
wait = collection.length;
|
|
|
|
collection && collection.forEach && collection.forEach(function (node) {
|
|
|
|
node.delete(done, true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function done(err) {
|
|
|
|
error = error || err;
|
|
|
|
if (--wait === 0) {
|
|
|
|
callback(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Neo4j.prototype.count = function count(model, callback) {
|
|
|
|
this.all(model, null, function (err, collection) {
|
|
|
|
callback(err, collection ? collection.length : 0);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Neo4j.prototype.updateAttributes = function updateAttributes(model, id, data, cb) {
|
|
|
|
data.id = id;
|
|
|
|
this.node(id, function (err, node) {
|
|
|
|
this.save(model, merge(node.data, data), cb);
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
function cleanup(data) {
|
2011-10-16 17:21:08 +00:00
|
|
|
if (!data) return null;
|
2011-10-03 13:37:33 +00:00
|
|
|
var res = {};
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
var v = data[key];
|
|
|
|
if (v === null) {
|
|
|
|
// skip
|
|
|
|
// console.log('skip null', key);
|
2011-10-08 17:11:26 +00:00
|
|
|
} else if (v && v.constructor.name === 'Array' && v.length === 0) {
|
2011-10-03 13:37:33 +00:00
|
|
|
// skip
|
|
|
|
// console.log('skip blank array', key);
|
2011-10-08 17:11:26 +00:00
|
|
|
} else if (typeof v !== 'undefined') {
|
2011-10-03 13:37:33 +00:00
|
|
|
res[key] = v;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function merge(base, update) {
|
|
|
|
Object.keys(update).forEach(function (key) {
|
|
|
|
base[key] = update[key];
|
|
|
|
});
|
|
|
|
return base;
|
|
|
|
}
|