Exporting Cypher queries from the Neo4j browser

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.

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

Screenshot of 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.

Tagged with: , , , , , ,
Posted in Javascript, Technology

Leave a Reply