Star

Created With

linkHelpers

The QueryBuilder class also provides some helpers for generating strings which could be used in a statement.

linkGetting normalized labels

QueryBuilder.getNormalizedLabels returns a single string to be used in a query.

1linkconst { getNormalizedLabels } = QueryBuilder;

2link

3linkconsole.log(getNormalizedLabels('Users')); // `Users`

4link

5linkconsole.log(getNormalizedLabels('Important Users')); // "`Important Users`"

6link

7linkconsole.log(getNormalizedLabels(['Users', 'Active'])); // "`Users:Active`"

8link

9linkconsole.log(getNormalizedLabels(['Users', 'Active'], 'or')); // "`Users|Active`"

10link

11linkconsole.log(getNormalizedLabels(['Users', 'Active', 'Old'])); // "`Users:Active:Old`"

linkGetting a node statement

QueryBuilder.getNodeStatement returns a string for a node's identifier, label, and inner info (like a where), to be used in a query.

Every parameter is optional.

1linkconst { getNodeStatement } = QueryBuilder;

2link

3linkconsole.log(getNodeStatement({

4link identifier: 'n',

5link label: 'MyLabel'

6link})); // (n:MyLabel)

7link

8linkconsole.log(getNodeStatement({

9link identifier: 'n',

10link})); // (n)

11link

12linkconsole.log(getNodeStatement({

13link label: 'MyLabel',

14link})); // (:MyLabel)

15link

16linkconsole.log(getNodeStatement({})); // ()

17link

18link// --> an inner statement can be given

19linkconsole.log(getNodeStatement({

20link identifier: 'n',

21link label: 'MyLabel'

22link inner: '{ id: 1 }' // --> using a literal string as inner

23link})); // (n:MyLabel { id: 1 })

24link

25link/* --> using a Where instance as inner */

26linkconst where = new Where({ id: 1 });

27linkconsole.log(getNodeStatement({

28link identifier: 'n',

29link label: 'MyLabel'

30link inner: where

31link})); // (n:MyLabel { id: $id })

32linkconsole.log(where.getBindParam().get()); // { id: 1 }

33link

34link/* --> using a BindParam and a properties object instance as inner */

35linkconst bindParam = new BindParam();

36linkconsole.log(getNodeStatement({

37link identifier: 'n',

38link label: 'MyLabel'

39link inner: {

40link properties: {

41link id: 1,

42link },

43link bindParam: bindParam

44link }

45link})); // (n:MyLabel { id: $id })

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

linkGetting a relationship statement

QueryBuilder.getRelationshipStatement returns a string for a relationship's direction, name, and inner info (like a where), to be used in a query.

1linkconst { getRelationshipStatement } = QueryBuilder;

2link

3linkconsole.log(getRelationshipStatement({

4link direction: 'out',

5link name: 'HAS',

6link identifier: 'r'

7link})); // -[r:HAS]->

8link

9linkconsole.log(getRelationshipStatement({

10link direction: 'in',

11link name: 'HAS',

12link identifier: 'r'

13link})); // <-[r:HAS]-

14link

15linkconsole.log(getRelationshipStatement({

16link direction: 'none',

17link name: 'HAS',

18link identifier: 'r'

19link})); // -[r:HAS]-

20link

21linkconsole.log(getRelationshipStatement({

22link direction: 'out',

23link name: 'HAS',

24link // --> in any of the above cases, the identifier can be skipped

25link})); // -[:HAS]->

26link

27link// --> an inner statement can be given

28linkconsole.log(getRelationshipStatement({

29link direction: 'out',

30link name: 'HAS',

31link identifier: 'r',

32link inner: '{ id: 1 }' // --> using a literal string as inner

33link})); // -[r:HAS { id: 1}]->

34link

35link/* --> using a Where instance as inner */

36linkconst where = new Where({ id: 1 });

37linkconsole.log(getRelationshipStatement({

38link direction: 'out',

39link name: 'HAS',

40link identifier: 'r',

41link inner: where

42link})); // -[r:HAS { id: $id }]->

43linkconsole.log(where.getBindParam().get()); // { id: 1 }

44link

45link/* --> using a BindParam and a properties object instance as inner */

46linkconst bindParam = new BindParam();

47linkconsole.log(getRelationshipStatement({

48link direction: 'out',

49link name: 'HAS',

50link identifier: 'r',

51link inner: {

52link properties: {

53link id: 1,

54link },

55link bindParam: bindParam

56link }

57link})); // -[r:HAS { id: $id }]->

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

linkGetting an identifier with a label

QueryBuilder.getIdentifierWithLabel returns a string to be used in a query, regardless if any of the identifier or label are null

1linkconst { getIdentifierWithLabel } = QueryBuilder;

2link

3linkconsole.log(getIdentifierWithLabel('MyIdentifier', 'MyLabel')); // "MyIdentifier:MyLabel"

4link

5linkconsole.log(getIdentifierWithLabel('MyIdentifier', 'MyLabel')); // "MyIdentifier"

6link

7linkconsole.log(getIdentifierWithLabel('MyIdentifier', 'MyLabel')); // ":MyLabel"

linkGetting parts for a SET operation

QueryBuilder.getSetParts returns the parts and the statement for a SET operation.

1linkconst { getSetParts } = QueryBuilder;

2link

3linkconst existingBindParam = new BindParam({});

4linkconst result = getSetParts({

5link /* --> the data to set */

6link data: {

7link x: 5,

8link y: 'foo'

9link },

10link /* --> BindParam instance to be used */

11link bindParam: existingBindParam, // @see [Bind Paramters](../Bind-Parameters)

12link /* --> the identifier to use */

13link identifier: 'node'

14link});

15linkconsole.log(result.parts); // ["node.x = $x", "node.y = $y"]

16linkconsole.log(result.statement); // "SET node.x = $x, node.y = $y"

17linkconsole.log(bindParam.get()); // { x: 5, y: 'foo' }

18link

19linkconst existingBindParam = new BindParam({

20link x: 'irrelevant'

21link});

22linkconst result = getSetParts({

23link data: {

24link x: 5,

25link y: 'foo'

26link },

27link bindParam: existingBindParam,

28link identifier: 'node'

29link});

30linkconsole.log(result.parts); // ["node.x = $x__aaaa", "node.y = $y"]

31linkconsole.log(result.statement); // "SET node.x = $x__aaaa, node.y = $y"

32linkconsole.log(bindParam.get()); // { x: 'irrelevant', x_aaaa: 5, y: 'foo' }

linkGetting properties with query param values

QueryBuilder exposes a getPropertiesWithParams function which returns an object in a string format to be used in queries, while replacing its values with bind params.

1link/* --> an existing BindParam instance, could have existing values */

2linkconst bindParam = new BindParam({

3link x: 4,

4link});

5linkconst result = QueryBuilder.getPropertiesWithParams(

6link /* --> the object to use */

7link {

8link x: 5,

9link y: 'foo'

10link },

11link /* --> an existing bindParam must be passed */

12link bindParam

13link);

14link

15link/* --> the result gives us the needed object, while replacing its values with the appropriate bind param */

16linkconsole.log(result); // "{ x: $x__aaaa, y: $y }"

17linkconsole.log(bindParam.get()); // { x: 4, x__aaaa: 5, y: 'foo' }

linkGetting the inner string of a variable length relationship

1linkconst onlyMinHops = QueryBuilder.getVariableLengthRelationshipString({

2link minHops: 3,

3link});

4linkconsole.log(onlyMinHops); // "*3.."

5link

6linkconst onlyMaxHops = QueryBuilder.getVariableLengthRelationshipString({

7link maxHops: 5,

8link});

9linkconsole.log(onlyMaxHops); // "*5.."

10link

11linkconst bothHops = QueryBuilder.getVariableLengthRelationshipString({

12link minHops: 3,

13link maxHops: 5,

14link});

15linkconsole.log(bothHops); // "*3..5"

16link

17linkconst equalHops = QueryBuilder.getVariableLengthRelationshipString({

18link minHops: 5,

19link maxHops: 5,

20link});

21linkconsole.log(equalHops); // "*5*"

22link

23linkconst ifiniteHops = QueryBuilder.getVariableLengthRelationshipString({

24link maxHops: Infinity,

25link});

26linkconsole.log(ifiniteHops); // "*"

HelpersGetting normalized labelsGetting a node statementGetting a relationship statementGetting an identifier with a labelGetting parts for a SET operationGetting properties with query param valuesGetting the inner string of a variable length relationship

Introduction Getting Started

Modelschevron_right

Sessions and Transactions

Query Builderchevron_right
Query Runnerchevron_right

Bind Parameters

Where Parameters