Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- added support for delete All, update Attributes, Non numeric ID attribute of node and match all that supports IN type query #2

Merged
merged 1 commit into from
Feb 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 65 additions & 22 deletions lib/neo4j.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,46 @@ neo4j.create = function (model, data, cb) {
}

neo4j.save = function (model, data, cb) {
console.log('save...');
cb();
}

neo4j.all = function (model, filter, cb) {
var keys = Object.keys(filter);

var keyToSymbol = function (key) {
return key + ':{filter}.' + key;
}
var f = keys
.reduce(function (out, k, i) {
var symbol = keyToSymbol(k);
if (keys.length === 0) {
out = '';
} else if (keys.length === 1) {
out = '{ ' + symbol + '}';
} else if (i === 0) {
out = '{ ' + symbol;
} else if (i === keys.length-1) {
out = out + ',' + symbol;
} else {
out = out + ',' + symbol + '}';

// Build filter which supports IN query
var newfilter = "";
if(filter){
var item;
if(filter.where)
item = filter.where;
else
item = filter;

// Build new filter
for (var property in item) {
if (item.hasOwnProperty(property)) {
if(newfilter.length>0) newfilter +=" AND "
else newfilter = "where "
if(item[property].inq){
newfilter += 'n.' + property + ' IN ' + JSON.stringify(item[property].inq);
}else{
newfilter += 'n.' + property + ' = ' + JSON.stringify(item[property]);
}
}
return out;
}, '');
}
}


var query = ('match (n:%node% ' + f + ') return n')
.replace('%node%', model);
var params = { filter: filter };
var query = ('match (n:%node%) %filter% return n')
.replace('%node%', model)
query = query.replace('%filter%', newfilter);

// Passing filter in the query itself without parameters
var params = {};

this.db.query(query, params, function (err, results) {
if (err) return cb(err);
Expand All @@ -70,6 +81,7 @@ neo4j.all = function (model, filter, cb) {
});
}


neo4j.find = function (model, id, cb) {
this.db.getNodeById(id, function (err, node) {
if (err) return cb(err);
Expand All @@ -83,18 +95,49 @@ neo4j.exists = function (model, id, cb) {
}

neo4j.destroy = function (model, id, cb) {
var query = 'match (n:%node%) where ID(n) = {id} delete n'
console.log('in delete');
var query = 'match (n:%node%) where (n.id) = {id} delete n'
.replace('%node%', model);
var params = { id: Number(id) };
this.db.query(query, params, cb);
}

neo4j.destroyAll = function(model, where, cb) {
console.log('in deleteAll');
var query = 'match (n:%node%) where (n.id) = {id} delete n'
.replace('%node%', model);
var params = where;
this.db.query(query, params, cb);
}

neo4j.count = function (model, cb, where) {
console.log('count', where);
cb();
};

neo4j.updateAttributes = function (model, id, data, cb) {
console.log('updateAttributes', id, data)
cb();
console.log('updateAttributes', id, model);
var payload = '';

for (var property in data) {
if (data.hasOwnProperty(property)) {
if(payload.length>0)
payload +=" , ";
else
payload = "{";

payload += property + ' : ' + JSON.stringify(data[property]);
}
}
payload += "}";


var query = 'match (n:%node%) where (n.id) = {id} set n=' + payload ;
query = query.replace('%node%', model);
console.log('Finaly query is : ' + query);

var params = { id: data.id };
this.db.query(query, params, cb);

// cb();
};
30 changes: 28 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,39 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "haio",
"author": {
"name": "haio"
},
"license": "MIT",
"dependencies": {
"neo4j": "~1.1.0"
},
"repository": {
"type": "git",
"url": "[email protected]:IndustrialCloudSolutions/loopback-connector-neo4j.git"
}
},
"bugs": {
"url": "https://github.com/IndustrialCloudSolutions/loopback-connector-neo4j/issues"
},
"homepage": "https://github.com/IndustrialCloudSolutions/loopback-connector-neo4j",
"_id": "[email protected]",
"dist": {
"shasum": "ae0fa1f2b46e36c5a73bf6caf5f9624c3e366d13",
"tarball": "http://registry.npmjs.org/loopback-connector-neo4j/-/loopback-connector-neo4j-0.1.3.tgz"
},
"_from": "loopback-connector-neo4j@*",
"_npmVersion": "1.3.24",
"_npmUser": {
"name": "haio",
"email": "[email protected]"
},
"maintainers": [
{
"name": "haio",
"email": "[email protected]"
}
],
"directories": {},
"_shasum": "ae0fa1f2b46e36c5a73bf6caf5f9624c3e366d13",
"_resolved": "https://registry.npmjs.org/loopback-connector-neo4j/-/loopback-connector-neo4j-0.1.3.tgz"
}