Star

Created With

linkThe Query Builder

The QueryBuilder class can be used to easily generate cypher using objects. It supports all Neo4j clauses, and automatically uses a Bind Parameter for the given values.

linkCreating a QueryBuilder instance

After a QueryBuilder instance is created, parameters can be added to it by using the parameter methods. More info about them can be found in Clauses

1linkconst queryBuilder = new QueryBuilder()

2link .match({

3link identifier: 'p1'

4link })

5link .return('p1');

6link

7link/* --> additional parameters can be added at any point */

8linkqueryBuilder.limit(1);

linkAdding parameters by using an array of parameter objects

Additional parameters can be added using the addParams method of the QueryBuilder instance. The parameters are the same as in the constructor.

1link// --> create the QueryBuilder instance using some initial parameters

2linkconst queryBuilder = new QueryBuilder()

3link .match({

4link identifier: 'p1'

5link });

6link

7link// --> add more parameters (using an array)

8linkqueryBuilder.addParams([

9link {

10link match: '(n)'

11link }

12link]);

13link

14link// --> add more parameters (using comma-separated objects)

15linkqueryBuilder.addParams(

16link {

17link limit: 2

18link },

19link {

20link return: 'p1'

21link }

22link);

linkGetting the statement from a QueryBuilder instance

The cypher statement of a QueryBuilder instance can be taken by using the getStatement method.

1linkconst queryBuilder = new QueryBuilder().match({

2link identifier: 'p1'

3link});

4link

5linkconsole.log(queryBuilder.getStatement()); // MATCH (p1)

The BindParam of a QueryBuilder instance can be taken by using the getBindParam method.

1linkconst queryBuilder = new QueryBuilder().match({

2link identifier: 'p1',

3link where: {

4link id: '1'

5link }

6link});

7link

8linkconsole.log(queryBuilder.getStatement()); // MATCH (p1 { id: $id })

9linkconst bindParam = queryBuilder.getBindParam();

10linkconsole.log(bindParam.get()); // { id: '1' }

linkUsing an existing BindParam instance

In case a BindParam instance is used on the constructor, it will be used.

1linkconst existingBindParam = new BindParam({

2link id: false,

3link});

4link

5linkconst queryBuilder = new QueryBuilder(existingBindParam)

6link .match({

7link identifier: 'p1',

8link where: {

9link id: '1'

10link }

11link });

12link

13linkconsole.log(queryBuilder.getStatement()); // MATCH (p1 { id: $id__aaaa })

14linkconst bindParam = queryBuilder.getBindParam();

15linkconsole.log(bindParam === existingBindParam); // true

16linkconsole.log(bindParam.get()); // { id: false, id__aaaa: '1' }

linkRunning a QueryBuilder instance

If a Neogma instance is defined, a QueryBuilder instance can be ran straight away.

1link await new QueryBuilder()

2link .raw('match n return n')

3link .run();

An existing session can be given

1link await new QueryBuilder()

2link .raw('match n return n')

3link .run(session);

Defining a Neogma instance also sets the QueryBuilder.queryRunner object to the one it uses, so no QueryRunner object needs to be passed.

In case a different QueryRunner instance needs to be passed, it can either happen on an instance level:

1link/** let 'queryRunner' be a QueryRunner instance */

2linkconst result = await new QueryBuilder()

3link .match('n')

4link .return('n')

5link .run(queryRunner);

6link

7linkconsole.log(result.records.map(r => r.get('n')));

An existing session can be given

1link /** let 'queryRunner' be a QueryRunner instance and 'session' be a Session/Transaction */

2link await new QueryBuilder()

3link .raw('match n return n')

4link .run(queryRunner, session);

Or on a global level:

1link QueryBuilder.queryRunner = new QueryRunner(...);

linkUsing a literal string

A Literal String can be used at Where conditions, when the typing supports it.

The Query BuilderCreating a QueryBuilder instanceAdding parameters by using an array of parameter objectsGetting the statement from a QueryBuilder instanceUsing an existing BindParam instanceRunning a QueryBuilder instanceUsing a literal string

Introduction Getting Started

Modelschevron_right

Sessions and Transactions

Query Builderchevron_right
Query Runnerchevron_right

Bind Parameters

Where Parameters