Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions docs/modules/ROOT/pages/clauses/create.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

This page describes how to add a link:https://neo4j.com/docs/cypher-manual/current/clauses/create/[`CREATE`] clause with the `Cypher.Create` class.

To make a `CREATE` clause, first create a valid pattern using the xref:/patterns.adoc[Pattern] class:

To add the clause, first create a valid pattern using the xref:/patterns.adoc[Pattern] class:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This page relates to CREATE clause, but I feel it may still be confusing to just refer to it as "the clause". In many places we need to either use multiple clauses or we may reference other clauses, so even at the risk of it being a bit verbose, I'd prefer for the create to be explicitly referenced


[source, javascript]
----
Expand Down Expand Up @@ -39,8 +38,8 @@ This generates the following `CREATE` clause:
CREATE (this:Movie {title: "The Matrix"})
----

== Relationships

== Relationships

Relationships can be used in a match clause by creating the relevant xref:/patterns.adoc[Pattern]:

Expand All @@ -62,9 +61,9 @@ CREATE (this1:`Person`)-[this0:ACTED_IN]->(this2:`Movie`)

== Update properties

The clauses `SET` and `REMOVE` can be added with the methods `.set` and `.remove` respectively.
Add the `SET` and `REMOVE` clauses with the methods `.set` and `.remove` respectively.

`.set` take tuples of 2 elements, the first being the property to update and the second the value:
`.set` takes a tuple of two elements - a property to update and a new value:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be hard to explain, but the method take multiple tuples, so the proposed change is not accurate

So, for example:

.set(
[movie.property("title"), new Cypher.Param("The Matrix")], // Tuple 1
[movie.property("description"), new Cypher.Param("A description")] //Tuple 2
)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rephrased


[source, javascript]
----
Expand All @@ -82,7 +81,6 @@ SET this0.title = $param0

Multiple properties can be updated by passing multiple tuples.


`.remove` takes the properties to remove:

[source, javascript]
Expand Down
3 changes: 2 additions & 1 deletion docs/modules/ROOT/pages/compatibility.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ This page outlines the compatibility requirements for `@neo4j/cypher-builder` ve

[NOTE]
====
`@neo4j/cypher-builder` version 1 is no longer supported. Version 2 is a full replacement and maintains the same compatibility with Cypher and Node.js.
`@neo4j/cypher-builder` version 1 is no longer supported.
Version 2 is a full replacement and maintains the same compatibility with Cypher and Node.js.
====
23 changes: 13 additions & 10 deletions docs/modules/ROOT/pages/getting-started/connecting-to-neo4j.adoc
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[[connecting-to-neo4j]]
:description: "This tutorial demonstrates how to execute your Cypher Builder queries in a Neo4j instance using the Neo4j Javascript driver.
:description: This page demonstrates how to execute your Cypher Builder queries in a Neo4j instance using the Neo4j Javascript driver.
= Connecting to Neo4j

The Cypher Builder library helps you compose Cypher queries, but sending these to Neo4j must be done separately through a link:https://www.npmjs.com/package/neo4j-driver[Neo4j driver].
This tutorial demonstrates how you can do that.
This page demonstrates how you can do that.


== Setup the project

. Initialize your project by following the instructions in the xref:/getting-started/installation.adoc[Installation] page.
. Initialize your project by following the instructions in xref:/getting-started/installation.adoc[Installation].

. To connect to Neo4j, make sure you have both `@neo4j/cypher-builder` and `neo4j-driver` installed in your NodeJS project:
+
Expand All @@ -17,17 +17,18 @@ This tutorial demonstrates how you can do that.
npm install @neo4j/cypher-builder neo4j-driver
----
+
For this example, the https://neo4j.com/docs/getting-started/appendix/example-data[Movies dataset] is used.
Alternatively, you can create entries with the following Cypher examples:
For this example, the https://neo4j.com/docs/getting-started/appendix/example-data[movies data set] is used.
Alternatively, you can create entries with the following Cypher:
+
[source, cypher]
----
CREATE(:Movie {title: "The Matrix"})
CREATE(:Movie {title: "The Terminal"})
----


== Initialize the driver
Add the following lines to the JavaScript file created in the xref:/getting-started/installation.adoc[Installation] step to initialize the driver:
Add the following lines to the xref:/getting-started/installation.adoc[installation] JavaScript file to initialize the driver:

[source, javascript]
----
Expand All @@ -37,10 +38,11 @@ import neo4j from "neo4j-driver";
const driver = neo4j.driver("neo4j://localhost", neo4j.auth.basic("neo4j", "password"));
----


== Construct your Cypher query

You can compose any query by using the Cypher object.
For instance, this query:
For example:

[source, javascript]
----
Expand All @@ -51,14 +53,15 @@ const query = new Cypher.Match(new Cypher.Pattern(movie, { labels: ["Movie"] }))
]);
----

This will translate to:
This creates the following query:

[source, cypher]
----
MATCH (this0:Movie)
RETURN this0.title AS title
----


== Execute the query

Use `.build` in the query object to generate the Cypher query and the necessary parameters to pass to `neo4j-driver`:
Expand Down Expand Up @@ -89,7 +92,7 @@ for (const record of records) {
}
----

With the xref:getting-started/connecting-to-neo4j.adoc#_initialize_the_driver[previous example data], this should output:
For the example data from xref:getting-started/connecting-to-neo4j.adoc#_setup_the_project[], this yields:

[source]
----
Expand All @@ -100,7 +103,7 @@ The Terminal

== Conclusion

After following the steps here described, your script should look like this:
Your script should look like this:


[source, javascript]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[[filters-and-projections]]
:description: This tutorial shows how to use filters and projections in Cypher Builder.
:description: This page shows you how to use filters and projections in Cypher Builder.
= Filters and projections

After learning how to write a basic `MATCH` and `RETURN` Cypher query in the xref:getting-started/querying.adoc[Querying] tutorial, you can now add filters and change the projection to only return some fields, as well as work with parameters.
After learning how to write a xref:getting-started/querying.adoc[basic Cypher query] using `MATCH` and `RETURN`, you can add filters and change the projection to only return some fields, as well as work with parameters.

This tutorial shows how to achieve the resulting query when using the link:https://neo4j.com/docs/getting-started/appendix/example-data/#built-in-examples[Movies Dataset]:
This page shows you how to achieve the resulting query when using the link:https://neo4j.com/docs/getting-started/appendix/example-data/#built-in-examples[movies data set]:

[source, cypher]
----
Expand All @@ -13,8 +13,9 @@ WHERE (m.title = "The Matrix" AND m.released < 2000)
RETURN m.title, m.tagline, m.released
----

This query should return all movies with the title `"The Matrix"` released before the year of `2000`.
It should return only the movie "The Matrix", released in 1999.
This query returns all movies with the title `"The Matrix"` released before the year of `2000`.
It only returns the movie "The Matrix", released in 1999.


== Filtering

Expand All @@ -37,7 +38,7 @@ const clause = new Cypher.Match(new Cypher.Pattern(movieNode, { labels: ["Movie"
m.title = "The Matrix"
----

.. Use the function `Cypher.eq()` to add an **eq**uality operator (`=`) in where:
.. Use the function `Cypher.eq()` to add an equality operator (`=`) to the where statement:
+
[source, javascript]
----
Expand All @@ -51,14 +52,14 @@ m.title = "The Matrix"
movieNode.property("title")
----

.. The second argument is the value to compare (`"The Matrix"`). Pass it as a link:https://neo4j.com/docs/cypher-manual/current/syntax/parameters/[Parameter] with `new Cypher.Param`
.. The second argument is the value to compare (`"The Matrix"`). Pass it as a link:https://neo4j.com/docs/cypher-manual/current/syntax/parameters/[parameter] with `new Cypher.Param`:
+
[source, javascript]
----
new Cypher.Param("The Matrix")
----
+
`Cypher.Param` will replace the string with a suitable `$param`, to avoid injecting values directly into the generated Cypher.
`Cypher.Param` replaces the string with a suitable `$param`, to avoid injecting values directly into the generated Cypher.

. Your `MATCH` clause should now look like this:
+
Expand All @@ -70,7 +71,7 @@ const clause = new Cypher.Match(new (movieNode, { labels: ["Movie"] }))
----

. Run the script again (`node main.js`).
You should now get the following Cypher:
This results in the following Cypher:
+
[source, cypher]
----
Expand All @@ -79,8 +80,8 @@ WHERE this0.title = $param0
RETURN this0
----
+
That means `Movie` is now filtered by its title to a param, but the param itself is missing from the output.
To fix that, you need to modify the build process at the end of the script:
It filters `Movie` nodes by their title when they match a parameter, but the parameter itself is missing from the output.
To fix that, modify the build process at the end of the script:
+
[source, javascript]
----
Expand All @@ -103,6 +104,7 @@ RETURN this0
{ param0: 'The Matrix' }
----


=== `AND` filtering

Here is how to add an `AND` filter to your query:
Expand All @@ -117,9 +119,10 @@ Here is how to add an `AND` filter to your query:
+
This takes the same parameters as `.where` and ensures the proper concatenation of the filters with `AND`.
+
In this case, the function `Cypher.lt` will add the operator `<` (**l**ower **t**han) and, like before, the comparison happens between a property of `movieNode` and a param.
In this case, the function `Cypher.lt` adds the operator `<` (less than).
Like before, a property of `movieNode` is compared to a parameter.

. Run the script again. You should get these results:
. Run the script again to get these results:
+
[source, cypher]
----
Expand All @@ -137,12 +140,13 @@ RETURN this0
====
The `AND` operation automatically adds parentheses to ensure operation precedence.
This is an important feature when dealing with complex and nested filters.
See more information about xref:getting-started/relationships-and-advanced-filtering.adoc[Advanced filtering].
For more information see xref:getting-started/relationships-and-advanced-filtering.adoc[Advanced filtering].
====


== Projection

Returning the full node is usually not necessary, but you can do that by explicitly setting a projection in `RETURN`.
Returning the full node is not necessary, but you can do that by explicitly setting a projection in `RETURN`.
For example:

[source, cypher]
Expand All @@ -157,7 +161,7 @@ To define these columns, you can pass the variables into the `.return` method:
.return(movieNode.property("title"), movieNode.property("tagline"), movieNode.property("year"));
----

The returned Cypher now should have the explicit projection:
The returned Cypher has the explicit projection:

[source, cypher]
----
Expand All @@ -166,12 +170,13 @@ WHERE (this0.title = $param0 AND this0.released < $param1)
RETURN this0.title, this0.tagline, this0.released
----


=== Reusing variables

The current query is filtering and returning the properties `id` and `title` of the `Movie` nodes.
However, relying on the name of the properties might make maintenance unecessarily more difficult.
The query so far filters and returns the properties `id` and `title` of the `Movie` nodes.
However, relying on the name of the properties might make maintenance unecessarily difficult.

To avoid that scenario, make sure both filters and projections use the same property variable (in this case, `movieNode.`):
To avoid that, make sure both filters and projections use the same property variable (in this case, `movieNode.`):

[source, javascript]
----
Expand All @@ -185,18 +190,19 @@ const clause = new Cypher.Match(movieNode)
.return(titleProp, taglineProp, yearProp);
----

That should keep different parts of the query in sync and also make the clause itself shorter.
This should keep different parts of the query in sync and also make the clause itself shorter.

[NOTE]
====
Params can also be assigned to a variable and be reused.
Parameters can also be assigned to a variable and be reused.
This can be particularly useful when having multiple filters over the same parameter.
See an example in xref:getting-started/relationships-and-advanced-filtering.adoc#_boolean_operations[Relationships and advanced filtering].
====


== Conclusion

After going through all the steps previously described, your script should look like this:
Your script should look like this:

[source, javascript]
----
Expand All @@ -220,7 +226,7 @@ console.log(cypher);
console.log(params);
----

And its execution should show the following query:
It results in the following query:

[source, cypher]
----
Expand All @@ -234,6 +240,6 @@ RETURN this0.title, this0.tagline, this0.released
{ param0: 'The Matrix', param1: 2000 }
----

With this, you already have the tools to write simple queries and to add parameters to it.
You can now add parameters to simple queries.

Refer to xref:/getting-started/relationships-and-advanced-filtering.adoc[Relationships and advanced filtering] to learn how to add relationships and more advanced filters to this query.
8 changes: 5 additions & 3 deletions docs/modules/ROOT/pages/getting-started/installation.adoc
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
[[installation]]
:description: This guide shows how to start using Cypher Builder.
:description: This page shows you how to install Cypher Builder.
= Installation

This guide shows how to start using Cypher Builder by setting up a Node.js project with `@neo4j/cypher-builder`.
This page shows you how to set up a Node.js project with `@neo4j/cypher-builder`.


== Requirements

* link:https://nodejs.org/[Node.js] 16.0.0 or greater
* link:https://docs.npmjs.com/downloading-and-installing-node-js-and-npm[npm]
* **[Optional]** A link:https://neo4j.com/cloud/platform/aura-graph-database/?ref=nav-get-started-cta[Neo4j] database to try Cypher queries.
* **[Optional]** A link:https://neo4j.com/cloud/platform/aura-graph-database/?ref=nav-get-started-cta[Neo4j database] where you can try Cypher queries


== Instructions

Expand Down
Loading
Loading