cypher.js
v1.0.0-beta.6
Published
Utilities to parse, transform and format cypher query. Inspired by [node-cypher-parser](https://github.com/Loupi/node-cypher-parser) with 3 main differences:
Downloads
43
Readme
cypher.js
Utilities to parse, transform and format cypher query. Inspired by node-cypher-parser with 3 main differences:
- this package can handle long query (longer than 1024 characters)
- this package doesn't rename
node.type
fromlibcypher-parser
- this package will eventually include a
format
utility to print an AST back to a string.
APIs
1. parse(query)
Accepts a cypher query string and try to parse it to Abstract Syntax Tree using native libcypher-parser.
Example:
const { parse } = require('cypher.js');
const result = parse("MATCH (n) RETURN n");
> console.log(require('util').inspect(result, { depth: null }))
/*
{ nnodes: 10,
eof: false,
errors: [],
root:
{ type: 'statement',
body:
{ type: 'query',
clauses:
[ { type: 'match',
optional: false,
pattern:
{ type: 'pattern',
paths:
[ { type: 'pattern-path',
elements:
[ { type: 'node-pattern',
identifier: { type: 'identifier', name: 'n' },
labels: [] } ] } ] },
hints: [] },
{ type: 'return',
distinct: false,
includeExisting: false,
projections:
[ { type: 'projection',
expression: { type: 'identifier', name: 'n' } } ] } ],
options: [] },
options: [] } }
*/
2. format(root, transform)
Stability: experimental
Accepts a AST root and and optional transform function and returns a reformatted string.
Example:
const { parse, UNSTABLE__format } = require('cypher.js');
const result = parse('MATCH (n) RETURN n');
const reformatted = UNSTABLE__format(result.root);
// reformatted = "MATCH (n)\nRTURN n;"