From d93f6b329e508250ff3b7571049040ce19d3fe4f Mon Sep 17 00:00:00 2001 From: Denis Betsi Date: Wed, 4 Feb 2015 21:33:37 -0400 Subject: [PATCH] - added support for delete All - added support for NON Numeric IDs - fixed a non functional match ALL which required IN type query - added support for updating attributes --- lib/neo4j.js | 87 +++++++++++++++++++++++++++++++++++++++------------- package.json | 30 ++++++++++++++++-- 2 files changed, 93 insertions(+), 24 deletions(-) diff --git a/lib/neo4j.js b/lib/neo4j.js index 721f390..3103e55 100644 --- a/lib/neo4j.js +++ b/lib/neo4j.js @@ -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); @@ -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); @@ -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(); }; diff --git a/package.json b/package.json index 5ac9051..53674a5 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,9 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "author": "haio", + "author": { + "name": "haio" + }, "license": "MIT", "dependencies": { "neo4j": "~1.1.0" @@ -14,5 +16,29 @@ "repository": { "type": "git", "url": "git@github.com:IndustrialCloudSolutions/loopback-connector-neo4j.git" - } + }, + "bugs": { + "url": "https://github.com/IndustrialCloudSolutions/loopback-connector-neo4j/issues" + }, + "homepage": "https://github.com/IndustrialCloudSolutions/loopback-connector-neo4j", + "_id": "loopback-connector-neo4j@0.1.3", + "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": "mockingror@gmail.com" + }, + "maintainers": [ + { + "name": "haio", + "email": "mockingror@gmail.com" + } + ], + "directories": {}, + "_shasum": "ae0fa1f2b46e36c5a73bf6caf5f9624c3e366d13", + "_resolved": "https://registry.npmjs.org/loopback-connector-neo4j/-/loopback-connector-neo4j-0.1.3.tgz" }