Introduction
Executive summary: need to export your Neo4j favourites to co-workers, friends and family? This article provides a JavaScript solution, as well as a Chrome extension, to do so.
Neo4j is a really cool, fast and powerful graph database. It allows you to write awesome Cypher queries to quickly reveal relationships and links between parts of your system that were previously just connected in an abstract, classical relational kind of way.
Even more to recommend, it’s free, and it has a quite slick front-end for navigating through your database. The front-end has a well presented, force-graph layout and functionality for saving favourite scripts.
But what about when you want to share those scripts with the rest of your team? Neo4j has a cloud persistence option, but that seems like signing up for a lot of hoops just to move a couple of strings around. The only other answer commonly found online is to copy and paste them. That works, for a couple at a time, but it’s not a great solution if you have an awesome database of them (shouldn’t these be in the database?)
There are two solutions here – how to do it from the JavaScript side, if you’d rather roll your own / have more control. If you just need the functionality, plus some additional safeguards to make sure you don’t accidentally delete all your scripts, I’ve packaged this all up into a Chrome extension which you can find here. The extension is very new, so please feel free to provide suggestions / bug reports.
Anyways – let’s see some code.
Some Code
Neo4j stores your favourite scripts / folders in your browser’s localStorage , specifically in neo4j.documents and neo4j.folders as nice legible strings of JSON objects. The folders appear to have identifying GUIDs, upon which documents (scripts) are tied to them. The scripts also have names, which either haven’t been implemented in the front-end or I’m just missing (which is entirely possible).
In any case, let’s have a look at the export side of things first. This little script will download either a raw dump of the folders and scripts, ready for re-importing directly into another Neo4j front-end, or as a slightly more formatted output showing the name of the script followed by its contents.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
var cypher = (function() { // http://stackoverflow.com/questions/13405129/javascript-create-and-save-file function download(text, name, type) { var a = document.createElement("a"); var file = new Blob([text], {type: type}); a.href = URL.createObjectURL(file); a.download = name; a.click(); } function rawExport() { download('localStorage["neo4j.documents"]=JSON.stringify('+localStorage["neo4j.documents"] + ');', "cypher-queries.txt", "txt"); download('localStorage["neo4j.folders"]=JSON.stringify('+localStorage["neo4j.folders"] + ');', "cypher-folders.txt", "txt"); } function formattedExport() { var result = ""; var objects = JSON.parse(localStorage["neo4j.documents"]); for (var i = 0; i < objects.length; i++) { result = result + objects[i].name + ":\r\n" + objects[i].content.replace("\n", "\r\n") + "\r\n\r\n"; } download(result, 'formatted-queries.txt', 'txt'); } var result = {}; result.rawExport = rawExport; result.formattedExport = formattedExport; return result; })(); |
Opening up, say, a Chrome developer window (try F12) and pasting this into the console will produce an object called cypher . This object has two functions available on it, rawExport() and formattedExport() .
Running rawExport will download two files, cypher-folders.txt and cypher-queries.txt . These files contain some JavaScript – running cypher-folders.txt will recreate your folders, and cypher-queries.txt will recreate your queries. Note that this will overwrite your existing queries.
If you need a more reliable solution that won’t trash your existing queries, then the Extension is probably what you’re after
formattedExport produces a more readable set of queries. If this is too manual for you, see the Extension.
The Extension
The extension adds buttons for exporting raw and formatted data, as well as a drag and drop zone for importing your favourites queries. It expands on the above by not simply replacing the scripts, but by examining IDs to work out which scripts should be updated, which scripts appended, and which ones should be left alone.
It can be found here.
I am getting “undefined” error. I am using Chrome. I inlcuded your extension. Thanks
Odd – I’ll have a look. Do you have any more details? Can you send me whatever is on the console? Was this on export or import?
It comes when I executed your javascript in the console window…so way before the export and import enabled
I tried this code on default browser got undefined
It’s been a while since I’ve done any Neo4j stuff – I’ll see if I have a chance to have a look.