Decypher
decypher is a node.js library packing a handful of utilities to deal with cypher queries.
It includes the following:
Installation
You can install decypher from npm likewise:
npm install decypher
or from github if you need the latest development version:
npm install git+https://github.com/Yomguithereal/decypher.git
Query loader
Query files
Write one or more cypher queries per file:
File containing a single query
// Any comments...
MATCH (n)-[r]-(t)
RETURN n,r,t LIMIT 100;
File containing multiple named queries
// name: first
// Retrieve book nodes
MATCH (b:Book)
RETURN b;
// name: second
// Retrieve vocabulary nodes
MATCH (v:Vocabulary)
RETURN v;
Decyphering
Just require decypher and use it to load your queries:
var decypher = require('decypher');
decypher('./single-query.cypher');
>>> 'MATCH (n)-[r]-(t)\nRETURN n,r,t LIMIT 100;'
decypher('./multiple-queries.cypher');
>>> {
first: 'MATCH (b:Book)\nRETURN b;',
second: 'MATCH (v:Vocabulary)\nRETURN v;'
}
decypher({
single: './single-query.cypher',
multiple: './multiple-queries.cypher'
});
>>> {
single: 'MATCH (n)-[r]-(t)\nRETURN n,r,t LIMIT 100;',
multiple: {
first: 'MATCH (b:Book)\nRETURN b;',
second: 'MATCH (v:Vocabulary)\nRETURN v;'
}
}
decypher('./folder');
>>> {
single: 'MATCH (n)-[r]-(t)\nRETURN n,r,t LIMIT 100;',
multiple: {
first: 'MATCH (b:Book)\nRETURN b;',
second: 'MATCH (v:Vocabulary)\nRETURN v;'
}
}
decypher('./path-to-queries-folder', 'cql');
Query builder
Note that this query builder is widely inspired by the query-builder package by @shesek but fixed and updated to support cypher's latest evolutions.
The result object of the builder is also made to match @thingdom's node-neo4j specifications for the db.cypher
method.
var Query = require('decypher').Query;
var cypher = new Query()
.match('MATCH (n:Node)')
.where('n.title = {title}', {title: 'The best title'})
.return('n');
cypher.compile();
cypher.toString();
>>> `MATCH (n:Node)
WHERE n.title = {title}
RETURN n;`
cypher.params();
>>> {
title: 'The best title'
}
cypher.statements();
>>> [
'MATCH (n:Node)',
'WHERE n.title = {title}',
'RETURN n'
]
var {query, params, statements} = cypher.build();
cypher.orderBy('n.title');
cypher.params({whatever: 'is needed'});
cypher.create(['(a:Actor)', '(m:Movie)']);
>>> 'CREATE (a:Actor), (m:Movie)'
cypher.add('anything you want');
cypher.add('with {param}', {param: 'heart'});
var cypher = new Query(),
start = cypher.segment(),
end = cypher.segment();
end.return('a');
start.match('(a:Actor)');
cypher.compile();
>>> `MATCH (a:Actor)
RETURN a;`
Helpers
Escaping identifiers
var helpers = require('decypher').helpers;
helpers.escapeIdentifier('Complex `Identifier`');
>>> '`Complex ``Identifier```'
Escaping literal maps
var helpers = require('decypher').helpers;
helpers.escapeLiteralMap({
hello: 'world',
'complex key': 3
});
>>> '{hello: "world", `complex key`: 3}'
helpers.escapeLiteralMap({
name: 'name',
number: 2
}, ['name']);
>>> '{name: {name}, number: 2}'
Building node patterns
var helpers = require('decypher').helpers;
helpers.nodePattern();
>>> '()'
helpers.nodePattern({
identifier: 'n',
label: 'Node'
});
>>> '(n:Node)'
helpers.nodePattern({
label: 'Node',
data: {title: 'Hello'}
});
>>> '(n:Node {title: "Hello"})'
helpers.nodePattern({
identifier: 'n',
data: 'paramName'
});
>>> '(n {paramName})'
helpers.nodePattern({
label: 'Chapter',
data: {title: 'title'},
paramKeys: ['title']
});
>>> '(:Chapter {title: {title}})'
Building relationship patterns
var helpers = require('decypher').helpers;
helpers.relationshipPattern();
>>> '--'
helpers.relationshipPattern({
direction: 'out',
identifier: 'r',
predicate: 'KNOWS'
});
>>> '-[r:KNOWS]->'
helpers.relationshipPattern({
direction: 'in',
predicate: ['PLAYS_IN', 'KNOWS']
});
>>> '<-[:PLAYS_IN|:KNOWS]-'
helpers.relationshipPattern({
direction: 'in',
identifier: 'r',
data: 'paramName'
});
>>> '<-[r {paramName}]-'
helpers.relationshipPattern({
predicate: 'KNOWS',
data: {since: 1975}
});
>>> '-[:KNOWS {since: 1975}]-'
Contribution
Contributions are of course more than welcome. Be sure to add and pass any relevant unit tests before submitting any code.
git clone git@github.com:Yomguithereal/decypher.git
cd decypher
npm install
npm test
Roadmap
License
MIT