Coder Social home page Coder Social logo

cypher.js's Introduction

cypher.js

Utilities to parse, transform and format cypher query. Inspired by node-cypher-parser with 3 main differences:

  1. this package can handle long query (longer than 1024 characters)
  2. this package doesn't rename node.type from libcypher-parser
  3. 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;"

cypher.js's People

Contributors

tungv avatar dependabot[bot] avatar

Stargazers

Roman avatar  avatar Vinh Nguyen avatar  avatar Daniel Nguyen avatar

Watchers

James Cloos avatar

Forkers

jthuraisamy

cypher.js's Issues

Unstable__format bad handling of lists

When using an array of maps the rebuilt query miss the [ ]

Query

UNWIND [{a:1},{b:2}] AS props

Result

UNWIND { a: 1 }{ b: 2 } AS props;

AST

{
  nnodes: 11,
  eof: true,
  errors: [],
  root: {
    type: 'statement',
    body: {
      type: 'query',
      clauses: [
        {
          type: 'unwind',
          expression: {
            type: 'collection',
            elements: [
              {
                type: 'map',
                entries: { a: { type: 'integer', value: 1 } }
              },
              {
                type: 'map',
                entries: { b: { type: 'integer', value: 2 } }
              }
            ]
          },
          alias: { type: 'identifier', name: 'props' }
        }
      ],
      options: []
    },
    options: []
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.