Permalink
Browse files

Create individual relationships

  • Loading branch information...
1 parent 124e3d7 commit 4161ca2a84d1b40d691eee88e0d29278ccd05370 @safwank committed Aug 12, 2012
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -1,29 +1,33 @@
// TODO: Switch between local Redis and Redis To Go (Heroku)
var redis = require('redis'),
- redisClient = redis.createClient();
+ redisClient = redis.createClient();
var TOTAL_NODES_KEY = 'total_nodes_imported';
exports.incrementTotalNodesBy = function(value) {
- redisClient.incrby(TOTAL_NODES_KEY, value, redis.print);
+ redisClient.incrby(TOTAL_NODES_KEY, value, redis.print);
}
exports.incrementTotalNodes = function() {
- redisClient.incr(TOTAL_NODES_KEY);
+ redisClient.incr(TOTAL_NODES_KEY);
}
exports.decrementTotalNodesBy = function(value) {
- redisClient.decrby(TOTAL_NODES_KEY, value);
+ redisClient.decrby(TOTAL_NODES_KEY, value);
}
exports.decrementTotalNodes = function() {
- redisClient.decr(TOTAL_NODES_KEY, redis.print);
+ redisClient.decr(TOTAL_NODES_KEY, redis.print);
+}
+
+exports.resetTotalNodes = function() {
+ redisClient.set(TOTAL_NODES_KEY, 0, redis.print);
}
exports.getTotalNodes = function(callback) {
- redisClient.get('total_nodes_imported', function(err, reply) {
- if (err) return callback(err);
+ redisClient.get('total_nodes_imported', function(err, reply) {
+ if (err) return callback(err);
- callback(null, reply);
- });
+ callback(null, reply);
+ });
}
View
@@ -1,25 +1,22 @@
// Initialization
-
-var util = require('../common/util'),
- redisUtil = require('../common/redisutil'),
- csv = require('csv'),
- neo4j = require('neo4j'),
- db = new neo4j.GraphDatabase(process.env.NEO4J_URL || 'http://localhost:7474');
+var sys = require('sys'),
+ util = require('../common/util'),
+ redisUtil = require('../common/redisutil'),
+ csv = require('csv'),
+ neo4j = require('neo4j'),
+ db = new neo4j.GraphDatabase(process.env.NEO4J_URL || 'http://localhost:7474');
// Constants
-
var INDEX_NAME = 'nodes';
var INDEX_KEY = 'type';
var INDEX_VAL = 'individual';
// Private "constructors"
-
var Individual = module.exports = function Individual(_node) {
this._node = _node;
-};
+ };
// Pass-through Individual properties
-
util.proxyProperty(Individual, 'Id', true);
util.proxyProperty(Individual, 'ReportTo', true);
util.proxyProperty(Individual, 'Title', true);
@@ -62,36 +59,91 @@ function handleCreated(error, data) {
}
// Public functions
-
Individual.loadFromCSV = function(csvFilePath) {
- csv().fromPath(csvFilePath, { columns: true, trim: true })
- .on('data', loadIndividual)
- .on('end', handleSuccess)
- .on('error', handleLoadError);
+ csv().fromPath(csvFilePath, {
+ columns: true,
+ trim: true
+ }).on('data', loadIndividual).on('end', handleSuccess).on('error', handleLoadError);
};
-Individual.create = function (data, callback) {
+Individual.create = function(data, callback) {
var node = db.createNode(data);
var individual = new Individual(node);
-
- node.save(function (err) {
+
+ node.save(function(err) {
if (err) return callback(err);
-
- node.index(INDEX_NAME, INDEX_KEY, INDEX_VAL, function (err) {
+
+ node.index(INDEX_NAME, INDEX_KEY, INDEX_VAL, function(err) {
if (err) return callback(err);
callback(null, individual);
});
});
};
-Individual.getAll = function (callback) {
- db.getIndexedNodes(INDEX_NAME, INDEX_KEY, INDEX_VAL, function (err, nodes) {
+Individual.getAll = function(callback) {
+ db.getIndexedNodes(INDEX_NAME, INDEX_KEY, INDEX_VAL, function(err, nodes) {
if (err) return callback(null, []);
- var individuals = nodes.map(function (node) {
- return new Individual(node);
+ var individuals = nodes.map(function(node) {
+ return new Individual(node);
});
-
+
callback(null, individuals);
});
+};
+
+Individual.getById = function(id, callback) {
+ var query = [
+ 'START person=node:INDEX_NAME(INDEX_KEY="INDEX_VAL")',
+ 'WHERE person.Id="PERSON_ID"',
+ 'RETURN person',
+ 'LIMIT 1'
+ ].join('\n')
+ .replace('INDEX_NAME', INDEX_NAME)
+ .replace('INDEX_KEY', INDEX_KEY)
+ .replace('INDEX_VAL', INDEX_VAL)
+ .replace('PERSON_ID', id);
+
+ var params = { personId: id };
+
+ db.query(query, params, function(err, results) {
+ if (err) return callback(err);
+
+ var person = results[0] && results[0]['person'];
+ callback(null, new Individual(person));
+ });
+};
+
+Individual.relateTwoIndividuals = function(personId1, personId2, relationshipType, callback) {
+ /*
+ var query = [
+ 'START person1=node:INDEX_NAME(INDEX_KEY="INDEX_VAL"), person2=node:INDEX_NAME(INDEX_KEY="INDEX_VAL")',
+ 'RELATE person1-[r:IS_RELATED_TO {type:"{relationshipType}"}]->person2',
+ 'WHERE (person1.Id="{personId1}" AND person2.Id="{personId2}")'
+ 'RETURN relationship'
+ ].join('\n')
+ .replace('INDEX_NAME', INDEX_NAME)
+ .replace('INDEX_KEY', INDEX_KEY)
+ .replace('INDEX_VAL', INDEX_VAL);
+
+ var params = {
+ personId1: personId1,
+ personId2: personId2,
+ relationshipType: relationshipType
+ };
+
+ db.query(query, params, function(err, results) {
+ if (err) return callback(err);
+
+ var relationship = results[0] && results[0]['relationship'];
+ callback(null, relationship);
+ });*/
+
+ Individual.getById(personId1, function(error, person1) {
+ Individual.getById(personId2, function(error, person2) {
+ person1._node.createRelationshipTo(person2._node, relationshipType, {}, function (error, relationship) {
+ callback(error, relationship);
+ });
+ });
+ });
};
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 4161ca2

Please sign in to comment.