-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Extend api and simplify by using the cypher MERGE approach. Use this as a guidline:
Entity.merge = function (label, data, callback) {
Object.getOwnPropertyNames(data).forEach(function(key) {
//var val = data[key]);
if (!Entity.prototype.hasOwnProperty(key)) {
Object.defineProperty(Entity.prototype, key, {
get: function () {
return this._node.data[key];
},
set: function (v) {
this._node.data[key] = v;
}
})
}
});
//convert json to cypher literals
var cypherLiterals = JSON.stringify(data).replace(/"([^"]+)":/g,"$1:").replace(/\uFFFF/g,"\"");
var query = [
'MERGE (entity:' + label + ' ' + cypherLiterals + ')',
'RETURN entity',
].join('\n');
db.query(query, {}, function (err, results) {
if (err) return callback(err);
var entity = new Entity(results[0]['entity']);
entity.label = label;
callback(null, entity);
});
};