Skip to content

Instantly share code, notes, and snippets.

@pstaender
Last active December 30, 2015 08:48
Show Gist options
  • Save pstaender/7804768 to your computer and use it in GitHub Desktop.
Save pstaender/7804768 to your computer and use it in GitHub Desktop.
Export script for neo4jmapper
var Neo4jMapper = require('neo4jmapper');
var neo4j = null;
var out = function(s) { console.log(s); }
var e = function(s) { process.stderr.write(s); }
try {
neo4j = new Neo4jMapper(process.argv[2]);
} catch (e) {
console.error(e.message);
out('\nUsage: `node '+require('path').basename(__filename)+' http://localhost:7474/ > exports.json`\n');
process.exit(1);
}
var Node = neo4j.Node;
var Relationship = neo4j.Relationship;
var Graph = neo4j.Graph;
var helpers = neo4j.helpers;
var startTime = new Date().getTime();
var count = {
relationships: 0,
nodes: 0,
};
Node.prototype.toJSON = function(labels) {
if (labels)
this.setLabels(labels);
return JSON.stringify({
id: this.id,
classification: 'Node',
data: this.data,
labels: this.labels,
});
}
Relationship.prototype.toJSON = function() {
return JSON.stringify({
id: this.id,
classification: 'Relationship',
type: this.type,
properties: this.data,
from: this.from.id,
to: this.to.id,
});
}
var done = {
up: function() {
this.count++;
if (this.count === 2) {
out(' null\n]');
console.error('\nDone. Exportet '+count.nodes+' nodes and '+count.relationships+' relationships in '+Math.round((new Date().getTime()-startTime)/1000)+'[s]');
process.exit(0);
}
},
count: 0
};
out('[');
Graph.stream('START n=node(*) RETURN n, labels(n)', function(res) {
if (res) {
var node = res[0];
var labels = res[1];
out(' '+node.toJSON(labels)+', ');
e('.'); // show progress
count.nodes++;
} else {
done.up();
}
});
Graph.stream('START r=relationship(*) RETURN r', function(res) {
if (res) {
out(' '+res.toJSON()+', ');
e('.'); // show progress
count.relationships++;
} else {
done.up();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment